Naze32 clone with Frysky receiver
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

MODSERIAL.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. Copyright (c) 2010 Andy Kirkham
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. @file MODSERIAL.h
  19. @purpose Extends Serial to provide fully buffered IO
  20. @version 1.6
  21. @date Nov 2010
  22. @author Andy Kirkham
  23. */
  24. #include "MODSERIAL.h"
  25. #include "MACROS.h"
  26. namespace AjK {
  27. MODSERIAL::MODSERIAL( PinName tx, PinName rx, const char* name ) : Serial( tx, rx, name )
  28. {
  29. init( MODSERIAL_DEFAULT_TX_BUFFER_SIZE, MODSERIAL_DEFAULT_RX_BUFFER_SIZE );
  30. }
  31. MODSERIAL::MODSERIAL( PinName tx, PinName rx, int bufferSize, const char* name ) : Serial( tx, rx, name )
  32. {
  33. init( bufferSize, bufferSize );
  34. }
  35. MODSERIAL::MODSERIAL( PinName tx, PinName rx, int txSize, int rxSize, const char* name ) : Serial( tx, rx, name )
  36. {
  37. init( txSize, rxSize );
  38. }
  39. MODSERIAL::~MODSERIAL()
  40. {
  41. disableIrq();
  42. if ( buffer[0] != NULL) free((char *)buffer[0] );
  43. if ( buffer[1] != NULL) free((char *)buffer[1] );
  44. }
  45. bool
  46. MODSERIAL::txBufferFull( void )
  47. {
  48. return MODSERIAL_TX_BUFFER_FULL;
  49. }
  50. bool
  51. MODSERIAL::rxBufferFull( void )
  52. {
  53. return MODSERIAL_RX_BUFFER_FULL;
  54. }
  55. bool
  56. MODSERIAL::txBufferEmpty( void )
  57. {
  58. return MODSERIAL_TX_BUFFER_EMPTY;
  59. }
  60. bool
  61. MODSERIAL::rxBufferEmpty( void )
  62. {
  63. return MODSERIAL_RX_BUFFER_EMPTY;
  64. }
  65. bool
  66. MODSERIAL::txIsBusy( void )
  67. {
  68. return ( ( _LSR & ( 3UL << 5 ) ) == 0 ) ? true : false;
  69. }
  70. void
  71. MODSERIAL::disableIrq( void )
  72. {
  73. #ifdef __LPC11UXX_H__
  74. NVIC_DisableIRQ( UART_IRQn );
  75. #else
  76. switch( _serial.index ) {
  77. case 0: NVIC_DisableIRQ( UART0_IRQn ); break;
  78. case 1: NVIC_DisableIRQ( UART1_IRQn ); break;
  79. case 2: NVIC_DisableIRQ( UART2_IRQn ); break;
  80. case 3: NVIC_DisableIRQ( UART3_IRQn ); break;
  81. }
  82. #endif
  83. }
  84. void
  85. MODSERIAL::enableIrq(void)
  86. {
  87. #ifdef __LPC11UXX_H__
  88. NVIC_EnableIRQ( UART_IRQn );
  89. #else
  90. switch( _serial.index ) {
  91. case 0: NVIC_EnableIRQ( UART0_IRQn ); break;
  92. case 1: NVIC_EnableIRQ( UART1_IRQn ); break;
  93. case 2: NVIC_EnableIRQ( UART2_IRQn ); break;
  94. case 3: NVIC_EnableIRQ( UART3_IRQn ); break;
  95. }
  96. #endif
  97. }
  98. int
  99. MODSERIAL::rxDiscardLastChar( void )
  100. {
  101. // This function can only be called indirectly from
  102. // an rxCallback function. Therefore, we know we
  103. // just placed a char into the buffer.
  104. char c = buffer[RxIrq][buffer_in[RxIrq]];
  105. if (buffer_count[RxIrq]) {
  106. buffer_count[RxIrq]--;
  107. buffer_in[RxIrq]--;
  108. if (buffer_in[RxIrq] < 0) {
  109. buffer_in[RxIrq] = buffer_size[RxIrq] - 1;
  110. }
  111. }
  112. return (int)c;
  113. }
  114. } // namespace AjK ends