Naze32 clone with Frysky receiver
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SerialBase.h 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MBED_SERIALBASE_H
  17. #define MBED_SERIALBASE_H
  18. #include "platform.h"
  19. #if DEVICE_SERIAL
  20. #include "Stream.h"
  21. #include "FunctionPointer.h"
  22. #include "serial_api.h"
  23. #if DEVICE_SERIAL_ASYNCH
  24. #include "CThunk.h"
  25. #include "dma_api.h"
  26. #endif
  27. namespace mbed {
  28. /** A base class for serial port implementations
  29. * Can't be instantiated directly (use Serial or RawSerial)
  30. */
  31. class SerialBase {
  32. public:
  33. /** Set the baud rate of the serial port
  34. *
  35. * @param baudrate The baudrate of the serial port (default = 9600).
  36. */
  37. void baud(int baudrate);
  38. enum Parity {
  39. None = 0,
  40. Odd,
  41. Even,
  42. Forced1,
  43. Forced0
  44. };
  45. enum IrqType {
  46. RxIrq = 0,
  47. TxIrq
  48. };
  49. enum Flow {
  50. Disabled = 0,
  51. RTS,
  52. CTS,
  53. RTSCTS
  54. };
  55. /** Set the transmission format used by the serial port
  56. *
  57. * @param bits The number of bits in a word (5-8; default = 8)
  58. * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
  59. * @param stop The number of stop bits (1 or 2; default = 1)
  60. */
  61. void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
  62. /** Determine if there is a character available to read
  63. *
  64. * @returns
  65. * 1 if there is a character available to read,
  66. * 0 otherwise
  67. */
  68. int readable();
  69. /** Determine if there is space available to write a character
  70. *
  71. * @returns
  72. * 1 if there is space to write a character,
  73. * 0 otherwise
  74. */
  75. int writeable();
  76. /** Attach a function to call whenever a serial interrupt is generated
  77. *
  78. * @param fptr A pointer to a void function, or 0 to set as none
  79. * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
  80. */
  81. void attach(void (*fptr)(void), IrqType type=RxIrq);
  82. /** Attach a member function to call whenever a serial interrupt is generated
  83. *
  84. * @param tptr pointer to the object to call the member function on
  85. * @param mptr pointer to the member function to be called
  86. * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty)
  87. */
  88. template<typename T>
  89. void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
  90. if((mptr != NULL) && (tptr != NULL)) {
  91. _irq[type].attach(tptr, mptr);
  92. serial_irq_set(&_serial, (SerialIrq)type, 1);
  93. } else {
  94. serial_irq_set(&_serial, (SerialIrq)type, 0);
  95. }
  96. }
  97. /** Generate a break condition on the serial line
  98. */
  99. void send_break();
  100. #if DEVICE_SERIAL_FC
  101. /** Set the flow control type on the serial port
  102. *
  103. * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
  104. * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
  105. * @param flow2 the second flow control pin (CTS for RTSCTS)
  106. */
  107. void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
  108. #endif
  109. static void _irq_handler(uint32_t id, SerialIrq irq_type);
  110. #if DEVICE_SERIAL_ASYNCH
  111. /** Begin asynchronous write using 8bit buffer. The completition invokes registered TX event callback
  112. *
  113. * @param buffer The buffer where received data will be stored
  114. * @param length The buffer length in bytes
  115. * @param callback The event callback function
  116. * @param event The logical OR of TX events
  117. */
  118. int write(const uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
  119. /** Begin asynchronous write using 16bit buffer. The completition invokes registered TX event callback
  120. *
  121. * @param buffer The buffer where received data will be stored
  122. * @param length The buffer length in bytes
  123. * @param callback The event callback function
  124. * @param event The logical OR of TX events
  125. */
  126. int write(const uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
  127. /** Abort the on-going write transfer
  128. */
  129. void abort_write();
  130. /** Begin asynchronous reading using 8bit buffer. The completition invokes registred RX event callback.
  131. *
  132. * @param buffer The buffer where received data will be stored
  133. * @param length The buffer length in bytes
  134. * @param callback The event callback function
  135. * @param event The logical OR of RX events
  136. * @param char_match The matching character
  137. */
  138. int read(uint8_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
  139. /** Begin asynchronous reading using 16bit buffer. The completition invokes registred RX event callback.
  140. *
  141. * @param buffer The buffer where received data will be stored
  142. * @param length The buffer length in bytes
  143. * @param callback The event callback function
  144. * @param event The logical OR of RX events
  145. * @param char_match The matching character
  146. */
  147. int read(uint16_t *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
  148. /** Abort the on-going read transfer
  149. */
  150. void abort_read();
  151. /** Configure DMA usage suggestion for non-blocking TX transfers
  152. *
  153. * @param usage The usage DMA hint for peripheral
  154. * @return Zero if the usage was set, -1 if a transaction is on-going
  155. */
  156. int set_dma_usage_tx(DMAUsage usage);
  157. /** Configure DMA usage suggestion for non-blocking RX transfers
  158. *
  159. * @param usage The usage DMA hint for peripheral
  160. * @return Zero if the usage was set, -1 if a transaction is on-going
  161. */
  162. int set_dma_usage_rx(DMAUsage usage);
  163. protected:
  164. void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
  165. void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t& callback, int event);
  166. void interrupt_handler_asynch(void);
  167. #endif
  168. protected:
  169. SerialBase(PinName tx, PinName rx);
  170. virtual ~SerialBase() {
  171. }
  172. int _base_getc();
  173. int _base_putc(int c);
  174. #if DEVICE_SERIAL_ASYNCH
  175. CThunk<SerialBase> _thunk_irq;
  176. event_callback_t _tx_callback;
  177. event_callback_t _rx_callback;
  178. DMAUsage _tx_usage;
  179. DMAUsage _rx_usage;
  180. #endif
  181. serial_t _serial;
  182. FunctionPointer _irq[2];
  183. int _baud;
  184. };
  185. } // namespace mbed
  186. #endif
  187. #endif