Driver_USART.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* -----------------------------------------------------------------------------
  2. * Copyright (c) 2013-2014 ARM Ltd.
  3. *
  4. * This software is provided 'as-is', without any express or implied warranty.
  5. * In no event will the authors be held liable for any damages arising from
  6. * the use of this software. Permission is granted to anyone to use this
  7. * software for any purpose, including commercial applications, and to alter
  8. * it and redistribute it freely, subject to the following restrictions:
  9. *
  10. * 1. The origin of this software must not be misrepresented; you must not
  11. * claim that you wrote the original software. If you use this software in
  12. * a product, an acknowledgment in the product documentation would be
  13. * appreciated but is not required.
  14. *
  15. * 2. Altered source versions must be plainly marked as such, and must not be
  16. * misrepresented as being the original software.
  17. *
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. *
  20. *
  21. * $Date: 24. Nov 2014
  22. * $Revision: V2.02
  23. *
  24. * Project: USART (Universal Synchronous Asynchronous Receiver Transmitter)
  25. * Driver definitions
  26. * -------------------------------------------------------------------------- */
  27. /* History:
  28. * Version 2.02
  29. * Corrected ARM_USART_CPOL_Pos and ARM_USART_CPHA_Pos definitions
  30. * Version 2.01
  31. * Removed optional argument parameter from Signal Event
  32. * Version 2.00
  33. * New simplified driver:
  34. * complexity moved to upper layer (especially data handling)
  35. * more unified API for different communication interfaces
  36. * renamed driver UART -> USART (Asynchronous & Synchronous)
  37. * Added modes:
  38. * Synchronous
  39. * Single-wire
  40. * IrDA
  41. * Smart Card
  42. * Changed prefix ARM_DRV -> ARM_DRIVER
  43. * Version 1.10
  44. * Namespace prefix ARM_ added
  45. * Version 1.01
  46. * Added events:
  47. * ARM_UART_EVENT_TX_EMPTY, ARM_UART_EVENT_RX_TIMEOUT
  48. * ARM_UART_EVENT_TX_THRESHOLD, ARM_UART_EVENT_RX_THRESHOLD
  49. * Added functions: SetTxThreshold, SetRxThreshold
  50. * Added "rx_timeout_event" to capabilities
  51. * Version 1.00
  52. * Initial release
  53. */
  54. #ifndef __DRIVER_USART_H
  55. #define __DRIVER_USART_H
  56. #include "Driver_Common.h"
  57. #define ARM_USART_API_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2,02) /* API version */
  58. /****** USART Control Codes *****/
  59. #define ARM_USART_CONTROL_Pos 0
  60. #define ARM_USART_CONTROL_Msk (0xFFUL << ARM_USART_CONTROL_Pos)
  61. /*----- USART Control Codes: Mode -----*/
  62. #define ARM_USART_MODE_ASYNCHRONOUS (0x01UL << ARM_USART_CONTROL_Pos) ///< UART (Asynchronous); arg = Baudrate
  63. #define ARM_USART_MODE_SYNCHRONOUS_MASTER (0x02UL << ARM_USART_CONTROL_Pos) ///< Synchronous Master (generates clock signal); arg = Baudrate
  64. #define ARM_USART_MODE_SYNCHRONOUS_SLAVE (0x03UL << ARM_USART_CONTROL_Pos) ///< Synchronous Slave (external clock signal)
  65. #define ARM_USART_MODE_SINGLE_WIRE (0x04UL << ARM_USART_CONTROL_Pos) ///< UART Single-wire (half-duplex); arg = Baudrate
  66. #define ARM_USART_MODE_IRDA (0x05UL << ARM_USART_CONTROL_Pos) ///< UART IrDA; arg = Baudrate
  67. #define ARM_USART_MODE_SMART_CARD (0x06UL << ARM_USART_CONTROL_Pos) ///< UART Smart Card; arg = Baudrate
  68. /*----- USART Control Codes: Mode Parameters: Data Bits -----*/
  69. #define ARM_USART_DATA_BITS_Pos 8
  70. #define ARM_USART_DATA_BITS_Msk (7UL << ARM_USART_DATA_BITS_Pos)
  71. #define ARM_USART_DATA_BITS_5 (5UL << ARM_USART_DATA_BITS_Pos) ///< 5 Data bits
  72. #define ARM_USART_DATA_BITS_6 (6UL << ARM_USART_DATA_BITS_Pos) ///< 6 Data bit
  73. #define ARM_USART_DATA_BITS_7 (7UL << ARM_USART_DATA_BITS_Pos) ///< 7 Data bits
  74. #define ARM_USART_DATA_BITS_8 (0UL << ARM_USART_DATA_BITS_Pos) ///< 8 Data bits (default)
  75. #define ARM_USART_DATA_BITS_9 (1UL << ARM_USART_DATA_BITS_Pos) ///< 9 Data bits
  76. /*----- USART Control Codes: Mode Parameters: Parity -----*/
  77. #define ARM_USART_PARITY_Pos 12
  78. #define ARM_USART_PARITY_Msk (3UL << ARM_USART_PARITY_Pos)
  79. #define ARM_USART_PARITY_NONE (0UL << ARM_USART_PARITY_Pos) ///< No Parity (default)
  80. #define ARM_USART_PARITY_EVEN (1UL << ARM_USART_PARITY_Pos) ///< Even Parity
  81. #define ARM_USART_PARITY_ODD (2UL << ARM_USART_PARITY_Pos) ///< Odd Parity
  82. /*----- USART Control Codes: Mode Parameters: Stop Bits -----*/
  83. #define ARM_USART_STOP_BITS_Pos 14
  84. #define ARM_USART_STOP_BITS_Msk (3UL << ARM_USART_STOP_BITS_Pos)
  85. #define ARM_USART_STOP_BITS_1 (0UL << ARM_USART_STOP_BITS_Pos) ///< 1 Stop bit (default)
  86. #define ARM_USART_STOP_BITS_2 (1UL << ARM_USART_STOP_BITS_Pos) ///< 2 Stop bits
  87. #define ARM_USART_STOP_BITS_1_5 (2UL << ARM_USART_STOP_BITS_Pos) ///< 1.5 Stop bits
  88. #define ARM_USART_STOP_BITS_0_5 (3UL << ARM_USART_STOP_BITS_Pos) ///< 0.5 Stop bits
  89. /*----- USART Control Codes: Mode Parameters: Flow Control -----*/
  90. #define ARM_USART_FLOW_CONTROL_Pos 16
  91. #define ARM_USART_FLOW_CONTROL_Msk (3UL << ARM_USART_FLOW_CONTROL_Pos)
  92. #define ARM_USART_FLOW_CONTROL_NONE (0UL << ARM_USART_FLOW_CONTROL_Pos) ///< No Flow Control (default)
  93. #define ARM_USART_FLOW_CONTROL_RTS (1UL << ARM_USART_FLOW_CONTROL_Pos) ///< RTS Flow Control
  94. #define ARM_USART_FLOW_CONTROL_CTS (2UL << ARM_USART_FLOW_CONTROL_Pos) ///< CTS Flow Control
  95. #define ARM_USART_FLOW_CONTROL_RTS_CTS (3UL << ARM_USART_FLOW_CONTROL_Pos) ///< RTS/CTS Flow Control
  96. /*----- USART Control Codes: Mode Parameters: Clock Polarity (Synchronous mode) -----*/
  97. #define ARM_USART_CPOL_Pos 18
  98. #define ARM_USART_CPOL_Msk (1UL << ARM_USART_CPOL_Pos)
  99. #define ARM_USART_CPOL0 (0UL << ARM_USART_CPOL_Pos) ///< CPOL = 0 (default)
  100. #define ARM_USART_CPOL1 (1UL << ARM_USART_CPOL_Pos) ///< CPOL = 1
  101. /*----- USART Control Codes: Mode Parameters: Clock Phase (Synchronous mode) -----*/
  102. #define ARM_USART_CPHA_Pos 19
  103. #define ARM_USART_CPHA_Msk (1UL << ARM_USART_CPHA_Pos)
  104. #define ARM_USART_CPHA0 (0UL << ARM_USART_CPHA_Pos) ///< CPHA = 0 (default)
  105. #define ARM_USART_CPHA1 (1UL << ARM_USART_CPHA_Pos) ///< CPHA = 1
  106. /*----- USART Control Codes: Miscellaneous Controls -----*/
  107. #define ARM_USART_SET_DEFAULT_TX_VALUE (0x10UL << ARM_USART_CONTROL_Pos) ///< Set default Transmit value (Synchronous Receive only); arg = value
  108. #define ARM_USART_SET_IRDA_PULSE (0x11UL << ARM_USART_CONTROL_Pos) ///< Set IrDA Pulse in ns; arg: 0=3/16 of bit period
  109. #define ARM_USART_SET_SMART_CARD_GUARD_TIME (0x12UL << ARM_USART_CONTROL_Pos) ///< Set Smart Card Guard Time; arg = number of bit periods
  110. #define ARM_USART_SET_SMART_CARD_CLOCK (0x13UL << ARM_USART_CONTROL_Pos) ///< Set Smart Card Clock in Hz; arg: 0=Clock not generated
  111. #define ARM_USART_CONTROL_SMART_CARD_NACK (0x14UL << ARM_USART_CONTROL_Pos) ///< Smart Card NACK generation; arg: 0=disabled, 1=enabled
  112. #define ARM_USART_CONTROL_TX (0x15UL << ARM_USART_CONTROL_Pos) ///< Transmitter; arg: 0=disabled, 1=enabled
  113. #define ARM_USART_CONTROL_RX (0x16UL << ARM_USART_CONTROL_Pos) ///< Receiver; arg: 0=disabled, 1=enabled
  114. #define ARM_USART_CONTROL_BREAK (0x17UL << ARM_USART_CONTROL_Pos) ///< Continuous Break transmission; arg: 0=disabled, 1=enabled
  115. #define ARM_USART_ABORT_SEND (0x18UL << ARM_USART_CONTROL_Pos) ///< Abort \ref ARM_USART_Send
  116. #define ARM_USART_ABORT_RECEIVE (0x19UL << ARM_USART_CONTROL_Pos) ///< Abort \ref ARM_USART_Receive
  117. #define ARM_USART_ABORT_TRANSFER (0x1AUL << ARM_USART_CONTROL_Pos) ///< Abort \ref ARM_USART_Transfer
  118. /****** USART specific error codes *****/
  119. #define ARM_USART_ERROR_MODE (ARM_DRIVER_ERROR_SPECIFIC - 1) ///< Specified Mode not supported
  120. #define ARM_USART_ERROR_BAUDRATE (ARM_DRIVER_ERROR_SPECIFIC - 2) ///< Specified baudrate not supported
  121. #define ARM_USART_ERROR_DATA_BITS (ARM_DRIVER_ERROR_SPECIFIC - 3) ///< Specified number of Data bits not supported
  122. #define ARM_USART_ERROR_PARITY (ARM_DRIVER_ERROR_SPECIFIC - 4) ///< Specified Parity not supported
  123. #define ARM_USART_ERROR_STOP_BITS (ARM_DRIVER_ERROR_SPECIFIC - 5) ///< Specified number of Stop bits not supported
  124. #define ARM_USART_ERROR_FLOW_CONTROL (ARM_DRIVER_ERROR_SPECIFIC - 6) ///< Specified Flow Control not supported
  125. #define ARM_USART_ERROR_CPOL (ARM_DRIVER_ERROR_SPECIFIC - 7) ///< Specified Clock Polarity not supported
  126. #define ARM_USART_ERROR_CPHA (ARM_DRIVER_ERROR_SPECIFIC - 8) ///< Specified Clock Phase not supported
  127. /**
  128. \brief USART Status
  129. */
  130. typedef struct _ARM_USART_STATUS {
  131. uint32_t tx_busy : 1; ///< Transmitter busy flag
  132. uint32_t rx_busy : 1; ///< Receiver busy flag
  133. uint32_t tx_underflow : 1; ///< Transmit data underflow detected (cleared on start of next send operation)
  134. uint32_t rx_overflow : 1; ///< Receive data overflow detected (cleared on start of next receive operation)
  135. uint32_t rx_break : 1; ///< Break detected on receive (cleared on start of next receive operation)
  136. uint32_t rx_framing_error : 1; ///< Framing error detected on receive (cleared on start of next receive operation)
  137. uint32_t rx_parity_error : 1; ///< Parity error detected on receive (cleared on start of next receive operation)
  138. } ARM_USART_STATUS;
  139. /**
  140. \brief USART Modem Control
  141. */
  142. typedef enum _ARM_USART_MODEM_CONTROL {
  143. ARM_USART_RTS_CLEAR, ///< Deactivate RTS
  144. ARM_USART_RTS_SET, ///< Activate RTS
  145. ARM_USART_DTR_CLEAR, ///< Deactivate DTR
  146. ARM_USART_DTR_SET ///< Activate DTR
  147. } ARM_USART_MODEM_CONTROL;
  148. /**
  149. \brief USART Modem Status
  150. */
  151. typedef struct _ARM_USART_MODEM_STATUS {
  152. uint32_t cts : 1; ///< CTS state: 1=Active, 0=Inactive
  153. uint32_t dsr : 1; ///< DSR state: 1=Active, 0=Inactive
  154. uint32_t dcd : 1; ///< DCD state: 1=Active, 0=Inactive
  155. uint32_t ri : 1; ///< RI state: 1=Active, 0=Inactive
  156. } ARM_USART_MODEM_STATUS;
  157. /****** USART Event *****/
  158. #define ARM_USART_EVENT_SEND_COMPLETE (1UL << 0) ///< Send completed; however USART may still transmit data
  159. #define ARM_USART_EVENT_RECEIVE_COMPLETE (1UL << 1) ///< Receive completed
  160. #define ARM_USART_EVENT_TRANSFER_COMPLETE (1UL << 2) ///< Transfer completed
  161. #define ARM_USART_EVENT_TX_COMPLETE (1UL << 3) ///< Transmit completed (optional)
  162. #define ARM_USART_EVENT_TX_UNDERFLOW (1UL << 4) ///< Transmit data not available (Synchronous Slave)
  163. #define ARM_USART_EVENT_RX_OVERFLOW (1UL << 5) ///< Receive data overflow
  164. #define ARM_USART_EVENT_RX_TIMEOUT (1UL << 6) ///< Receive character timeout (optional)
  165. #define ARM_USART_EVENT_RX_BREAK (1UL << 7) ///< Break detected on receive
  166. #define ARM_USART_EVENT_RX_FRAMING_ERROR (1UL << 8) ///< Framing error detected on receive
  167. #define ARM_USART_EVENT_RX_PARITY_ERROR (1UL << 9) ///< Parity error detected on receive
  168. #define ARM_USART_EVENT_CTS (1UL << 10) ///< CTS state changed (optional)
  169. #define ARM_USART_EVENT_DSR (1UL << 11) ///< DSR state changed (optional)
  170. #define ARM_USART_EVENT_DCD (1UL << 12) ///< DCD state changed (optional)
  171. #define ARM_USART_EVENT_RI (1UL << 13) ///< RI state changed (optional)
  172. // Function documentation
  173. /**
  174. \fn ARM_DRIVER_VERSION ARM_USART_GetVersion (void)
  175. \brief Get driver version.
  176. \return \ref ARM_DRIVER_VERSION
  177. \fn ARM_USART_CAPABILITIES ARM_USART_GetCapabilities (void)
  178. \brief Get driver capabilities
  179. \return \ref ARM_USART_CAPABILITIES
  180. \fn int32_t ARM_USART_Initialize (ARM_USART_SignalEvent_t cb_event)
  181. \brief Initialize USART Interface.
  182. \param[in] cb_event Pointer to \ref ARM_USART_SignalEvent
  183. \return \ref execution_status
  184. \fn int32_t ARM_USART_Uninitialize (void)
  185. \brief De-initialize USART Interface.
  186. \return \ref execution_status
  187. \fn int32_t ARM_USART_PowerControl (ARM_POWER_STATE state)
  188. \brief Control USART Interface Power.
  189. \param[in] state Power state
  190. \return \ref execution_status
  191. \fn int32_t ARM_USART_Send (const void *data, uint32_t num)
  192. \brief Start sending data to USART transmitter.
  193. \param[in] data Pointer to buffer with data to send to USART transmitter
  194. \param[in] num Number of data items to send
  195. \return \ref execution_status
  196. \fn int32_t ARM_USART_Receive (void *data, uint32_t num)
  197. \brief Start receiving data from USART receiver.
  198. \param[out] data Pointer to buffer for data to receive from USART receiver
  199. \param[in] num Number of data items to receive
  200. \return \ref execution_status
  201. \fn int32_t ARM_USART_Transfer (const void *data_out,
  202. void *data_in,
  203. uint32_t num)
  204. \brief Start sending/receiving data to/from USART transmitter/receiver.
  205. \param[in] data_out Pointer to buffer with data to send to USART transmitter
  206. \param[out] data_in Pointer to buffer for data to receive from USART receiver
  207. \param[in] num Number of data items to transfer
  208. \return \ref execution_status
  209. \fn uint32_t ARM_USART_GetTxCount (void)
  210. \brief Get transmitted data count.
  211. \return number of data items transmitted
  212. \fn uint32_t ARM_USART_GetRxCount (void)
  213. \brief Get received data count.
  214. \return number of data items received
  215. \fn int32_t ARM_USART_Control (uint32_t control, uint32_t arg)
  216. \brief Control USART Interface.
  217. \param[in] control Operation
  218. \param[in] arg Argument of operation (optional)
  219. \return common \ref execution_status and driver specific \ref usart_execution_status
  220. \fn ARM_USART_STATUS ARM_USART_GetStatus (void)
  221. \brief Get USART status.
  222. \return USART status \ref ARM_USART_STATUS
  223. \fn int32_t ARM_USART_SetModemControl (ARM_USART_MODEM_CONTROL control)
  224. \brief Set USART Modem Control line state.
  225. \param[in] control \ref ARM_USART_MODEM_CONTROL
  226. \return \ref execution_status
  227. \fn ARM_USART_MODEM_STATUS ARM_USART_GetModemStatus (void)
  228. \brief Get USART Modem Status lines state.
  229. \return modem status \ref ARM_USART_MODEM_STATUS
  230. \fn void ARM_USART_SignalEvent (uint32_t event)
  231. \brief Signal USART Events.
  232. \param[in] event \ref USART_events notification mask
  233. \return none
  234. */
  235. typedef void (*ARM_USART_SignalEvent_t) (uint32_t event); ///< Pointer to \ref ARM_USART_SignalEvent : Signal USART Event.
  236. /**
  237. \brief USART Device Driver Capabilities.
  238. */
  239. typedef struct _ARM_USART_CAPABILITIES {
  240. uint32_t asynchronous : 1; ///< supports UART (Asynchronous) mode
  241. uint32_t synchronous_master : 1; ///< supports Synchronous Master mode
  242. uint32_t synchronous_slave : 1; ///< supports Synchronous Slave mode
  243. uint32_t single_wire : 1; ///< supports UART Single-wire mode
  244. uint32_t irda : 1; ///< supports UART IrDA mode
  245. uint32_t smart_card : 1; ///< supports UART Smart Card mode
  246. uint32_t smart_card_clock : 1; ///< Smart Card Clock generator available
  247. uint32_t flow_control_rts : 1; ///< RTS Flow Control available
  248. uint32_t flow_control_cts : 1; ///< CTS Flow Control available
  249. uint32_t event_tx_complete : 1; ///< Transmit completed event: \ref ARM_USART_EVENT_TX_COMPLETE
  250. uint32_t event_rx_timeout : 1; ///< Signal receive character timeout event: \ref ARM_USART_EVENT_RX_TIMEOUT
  251. uint32_t rts : 1; ///< RTS Line: 0=not available, 1=available
  252. uint32_t cts : 1; ///< CTS Line: 0=not available, 1=available
  253. uint32_t dtr : 1; ///< DTR Line: 0=not available, 1=available
  254. uint32_t dsr : 1; ///< DSR Line: 0=not available, 1=available
  255. uint32_t dcd : 1; ///< DCD Line: 0=not available, 1=available
  256. uint32_t ri : 1; ///< RI Line: 0=not available, 1=available
  257. uint32_t event_cts : 1; ///< Signal CTS change event: \ref ARM_USART_EVENT_CTS
  258. uint32_t event_dsr : 1; ///< Signal DSR change event: \ref ARM_USART_EVENT_DSR
  259. uint32_t event_dcd : 1; ///< Signal DCD change event: \ref ARM_USART_EVENT_DCD
  260. uint32_t event_ri : 1; ///< Signal RI change event: \ref ARM_USART_EVENT_RI
  261. } ARM_USART_CAPABILITIES;
  262. /**
  263. \brief Access structure of the USART Driver.
  264. */
  265. typedef struct _ARM_DRIVER_USART {
  266. ARM_DRIVER_VERSION (*GetVersion) (void); ///< Pointer to \ref ARM_USART_GetVersion : Get driver version.
  267. ARM_USART_CAPABILITIES (*GetCapabilities) (void); ///< Pointer to \ref ARM_USART_GetCapabilities : Get driver capabilities.
  268. int32_t (*Initialize) (ARM_USART_SignalEvent_t cb_event); ///< Pointer to \ref ARM_USART_Initialize : Initialize USART Interface.
  269. int32_t (*Uninitialize) (void); ///< Pointer to \ref ARM_USART_Uninitialize : De-initialize USART Interface.
  270. int32_t (*PowerControl) (ARM_POWER_STATE state); ///< Pointer to \ref ARM_USART_PowerControl : Control USART Interface Power.
  271. int32_t (*Send) (const void *data, uint32_t num); ///< Pointer to \ref ARM_USART_Send : Start sending data to USART transmitter.
  272. int32_t (*Receive) ( void *data, uint32_t num); ///< Pointer to \ref ARM_USART_Receive : Start receiving data from USART receiver.
  273. int32_t (*Transfer) (const void *data_out,
  274. void *data_in,
  275. uint32_t num); ///< Pointer to \ref ARM_USART_Transfer : Start sending/receiving data to/from USART.
  276. uint32_t (*GetTxCount) (void); ///< Pointer to \ref ARM_USART_GetTxCount : Get transmitted data count.
  277. uint32_t (*GetRxCount) (void); ///< Pointer to \ref ARM_USART_GetRxCount : Get received data count.
  278. int32_t (*Control) (uint32_t control, uint32_t arg); ///< Pointer to \ref ARM_USART_Control : Control USART Interface.
  279. ARM_USART_STATUS (*GetStatus) (void); ///< Pointer to \ref ARM_USART_GetStatus : Get USART status.
  280. int32_t (*SetModemControl) (ARM_USART_MODEM_CONTROL control); ///< Pointer to \ref ARM_USART_SetModemControl : Set USART Modem Control line state.
  281. ARM_USART_MODEM_STATUS (*GetModemStatus) (void); ///< Pointer to \ref ARM_USART_GetModemStatus : Get USART Modem Status lines state.
  282. } const ARM_DRIVER_USART;
  283. #endif /* __DRIVER_USART_H */