usart.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*****************************************************************************
  2. *
  3. * \file
  4. *
  5. * \brief USART driver for AVR32 UC3.
  6. *
  7. * This file contains basic functions for the AVR32 USART, with support for all
  8. * modes, settings and clock speeds.
  9. *
  10. * Copyright (c) 2009-2018 Microchip Technology Inc. and its subsidiaries.
  11. *
  12. * \asf_license_start
  13. *
  14. * \page License
  15. *
  16. * Subject to your compliance with these terms, you may use Microchip
  17. * software and any derivatives exclusively with Microchip products.
  18. * It is your responsibility to comply with third party license terms applicable
  19. * to your use of third party software (including open source software) that
  20. * may accompany Microchip software.
  21. *
  22. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  23. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  24. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  25. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  26. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  27. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  28. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  29. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  30. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  31. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  32. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  33. *
  34. * \asf_license_stop
  35. *
  36. ******************************************************************************/
  37. /*
  38. * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
  39. */
  40. #ifndef _USART_H_
  41. #define _USART_H_
  42. /**
  43. * \defgroup group_avr32_drivers_usart USART - Univ. Sync/Async Serial Rec/Trans
  44. *
  45. * Driver for the USART (Universal Synchronous Asynchronous Receiver Transmitter).
  46. * The driver supports the following modes: RS232, RS485, SPI, LIN and ISO7816.
  47. *
  48. * \{
  49. */
  50. #include <avr32/io.h>
  51. #include "compiler.h"
  52. /*! \name Return Values
  53. */
  54. //! @{
  55. #define USART_SUCCESS 0 //!< Successful completion.
  56. #define USART_FAILURE -1 //!< Failure because of some unspecified reason.
  57. #define USART_INVALID_INPUT 1 //!< Input value out of range.
  58. #define USART_INVALID_ARGUMENT -1 //!< Argument value out of range.
  59. #define USART_TX_BUSY 2 //!< Transmitter was busy.
  60. #define USART_RX_EMPTY 3 //!< Nothing was received.
  61. #define USART_RX_ERROR 4 //!< Transmission error occurred.
  62. #define USART_MODE_FAULT 5 //!< USART not in the appropriate mode.
  63. //! @}
  64. //! Default time-out value (number of attempts).
  65. #define USART_DEFAULT_TIMEOUT 10000
  66. /*! \name Parity Settings
  67. */
  68. //! @{
  69. #define USART_EVEN_PARITY AVR32_USART_MR_PAR_EVEN //!< Use even parity on character transmission.
  70. #define USART_ODD_PARITY AVR32_USART_MR_PAR_ODD //!< Use odd parity on character transmission.
  71. #define USART_SPACE_PARITY AVR32_USART_MR_PAR_SPACE //!< Use a space as parity bit.
  72. #define USART_MARK_PARITY AVR32_USART_MR_PAR_MARK //!< Use a mark as parity bit.
  73. #define USART_NO_PARITY AVR32_USART_MR_PAR_NONE //!< Don't use a parity bit.
  74. #define USART_MULTIDROP_PARITY AVR32_USART_MR_PAR_MULTI //!< Parity bit is used to flag address characters.
  75. //! @}
  76. /*! \name Stop Bits Settings
  77. */
  78. //! @{
  79. #define USART_1_STOPBIT AVR32_USART_MR_NBSTOP_1 //!< Use 1 stop bit.
  80. #define USART_1_5_STOPBITS AVR32_USART_MR_NBSTOP_1_5 //!< Use 1.5 stop bits.
  81. #define USART_2_STOPBITS AVR32_USART_MR_NBSTOP_2 //!< Use 2 stop bits (for more, just give the number of bits).
  82. //! @}
  83. /*! \name Channel Modes
  84. */
  85. //! @{
  86. #define USART_NORMAL_CHMODE AVR32_USART_MR_CHMODE_NORMAL //!< Normal communication.
  87. #define USART_AUTO_ECHO AVR32_USART_MR_CHMODE_ECHO //!< Echo data.
  88. #define USART_LOCAL_LOOPBACK AVR32_USART_MR_CHMODE_LOCAL_LOOP //!< Local loopback.
  89. #define USART_REMOTE_LOOPBACK AVR32_USART_MR_CHMODE_REMOTE_LOOP //!< Remote loopback.
  90. //! @}
  91. #if defined(AVR32_USART_400_H_INCLUDED) || \
  92. defined(AVR32_USART_410_H_INCLUDED) || \
  93. defined(AVR32_USART_420_H_INCLUDED) || \
  94. defined(AVR32_USART_440_H_INCLUDED) || \
  95. defined(AVR32_USART_602_H_INCLUDED)
  96. /*! \name LIN Node Actions
  97. */
  98. //! @{
  99. #define USART_LIN_PUBLISH_ACTION AVR32_USART_LINMR_NACT_PUBLISH //!< The USART transmits the response.
  100. #define USART_LIN_SUBSCRIBE_ACTION AVR32_USART_LINMR_NACT_SUBSCRIBE //!< The USART receives the response.
  101. #define USART_LIN_IGNORE_ACTION AVR32_USART_LINMR_NACT_IGNORE //!< The USART does not transmit and does not receive the response.
  102. //! @}
  103. /*! \name LIN Checksum Types
  104. */
  105. //! @{
  106. #define USART_LIN_ENHANCED_CHECKSUM 0 //!< LIN 2.0 "enhanced" checksum.
  107. #define USART_LIN_CLASSIC_CHECKSUM 1 //!< LIN 1.3 "classic" checksum.
  108. //! @}
  109. #endif // USART rev. >= 4.0.0
  110. //! Input parameters when initializing RS232 and similar modes.
  111. typedef struct
  112. {
  113. //! Set baud rate of the USART (unused in slave modes).
  114. unsigned long baudrate;
  115. //! Number of bits to transmit as a character (5 to 9).
  116. unsigned char charlength;
  117. //! How to calculate the parity bit: \ref USART_EVEN_PARITY, \ref USART_ODD_PARITY,
  118. //! \ref USART_SPACE_PARITY, \ref USART_MARK_PARITY, \ref USART_NO_PARITY or
  119. //! \ref USART_MULTIDROP_PARITY.
  120. unsigned char paritytype;
  121. //! Number of stop bits between two characters: \ref USART_1_STOPBIT,
  122. //! \ref USART_1_5_STOPBITS, \ref USART_2_STOPBITS or any number from 3 to 257
  123. //! which will result in a time guard period of that length between characters.
  124. //! \note \ref USART_1_5_STOPBITS is supported in asynchronous modes only.
  125. unsigned short stopbits;
  126. //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,
  127. //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.
  128. unsigned char channelmode;
  129. } usart_options_t;
  130. //! Input parameters when initializing ISO7816 mode.
  131. typedef struct
  132. {
  133. //! Set the frequency of the ISO7816 clock.
  134. unsigned long iso7816_hz;
  135. //! The number of ISO7816 clock ticks in every bit period (1 to 2047, 0 = disable clock).
  136. //! Bit rate = \ref iso7816_hz / \ref fidi_ratio.
  137. unsigned short fidi_ratio;
  138. //! How to calculate the parity bit: \ref USART_EVEN_PARITY for normal mode or
  139. //! \ref USART_ODD_PARITY for inverse mode.
  140. unsigned char paritytype;
  141. //! Inhibit Non Acknowledge:\n
  142. //! - 0: the NACK is generated;\n
  143. //! - 1: the NACK is not generated.
  144. //!
  145. //! \note This bit will be used only in ISO7816 mode, protocol T = 0 receiver.
  146. int inhibit_nack;
  147. //! Disable successive NACKs.
  148. //! Successive parity errors are counted up to the value in the \ref max_iterations field.
  149. //! These parity errors generate a NACK on the ISO line. As soon as this value is reached,
  150. //! no additional NACK is sent on the ISO line. The ITERATION flag is asserted.
  151. int dis_suc_nack;
  152. //! Max number of repetitions (0 to 7).
  153. unsigned char max_iterations;
  154. //! Bit order in transmitted characters:\n
  155. //! - 0: LSB first;\n
  156. //! - 1: MSB first.
  157. int bit_order;
  158. } usart_iso7816_options_t;
  159. #if defined(AVR32_USART_400_H_INCLUDED) || \
  160. defined(AVR32_USART_410_H_INCLUDED) || \
  161. defined(AVR32_USART_420_H_INCLUDED) || \
  162. defined(AVR32_USART_440_H_INCLUDED) || \
  163. defined(AVR32_USART_602_H_INCLUDED)
  164. //! Input parameters when initializing SPI mode.
  165. typedef struct
  166. {
  167. //! Set the frequency of the SPI clock (unused in slave mode).
  168. unsigned long baudrate;
  169. //! Number of bits to transmit as a character (5 to 9).
  170. unsigned char charlength;
  171. //! Which SPI mode to use.
  172. unsigned char spimode;
  173. //! Run the channel in testmode: \ref USART_NORMAL_CHMODE, \ref USART_AUTO_ECHO,
  174. //! \ref USART_LOCAL_LOOPBACK or \ref USART_REMOTE_LOOPBACK.
  175. unsigned char channelmode;
  176. } usart_spi_options_t;
  177. #endif // USART rev. >= 4.0.0
  178. //------------------------------------------------------------------------------
  179. /*! \name Initialization Functions
  180. */
  181. //! @{
  182. /*! \brief Resets the USART and disables TX and RX.
  183. *
  184. * \param usart Base address of the USART instance.
  185. */
  186. extern void usart_reset(volatile avr32_usart_t *usart);
  187. /*! \brief Sets up the USART to use the standard RS232 protocol.
  188. *
  189. * \param usart Base address of the USART instance.
  190. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  191. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  192. *
  193. * \retval USART_SUCCESS Mode successfully initialized.
  194. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  195. */
  196. extern int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  197. /*! \brief Sets up the USART to use the standard RS232 protocol in TX-only mode.
  198. *
  199. * Compared to \ref usart_init_rs232, this function allows very high baud rates
  200. * (up to \a pba_hz instead of \a pba_hz / \c 8) at the expense of full duplex.
  201. *
  202. * \param usart Base address of the USART instance.
  203. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  204. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  205. *
  206. * \retval USART_SUCCESS Mode successfully initialized.
  207. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  208. *
  209. * \note The \c 1.5 stop bit is not supported in this mode.
  210. */
  211. extern int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  212. /*! \brief Sets up the USART to use hardware handshaking.
  213. *
  214. * \param usart Base address of the USART instance.
  215. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  216. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  217. *
  218. * \retval USART_SUCCESS Mode successfully initialized.
  219. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  220. *
  221. * \note \ref usart_init_rs232 does not need to be invoked before this function.
  222. */
  223. extern int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  224. /*! \brief Sets up the USART to use the modem protocol, activating dedicated inputs/outputs.
  225. *
  226. * \param usart Base address of the USART instance.
  227. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  228. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  229. *
  230. * \retval USART_SUCCESS Mode successfully initialized.
  231. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  232. */
  233. extern int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  234. /*! \brief Sets up the USART to use a synchronous RS232-like protocol in master mode.
  235. *
  236. * \param usart Base address of the USART instance.
  237. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  238. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  239. *
  240. * \retval USART_SUCCESS Mode successfully initialized.
  241. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  242. */
  243. extern int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  244. /*! \brief Sets up the USART to use a synchronous RS232-like protocol in slave mode.
  245. *
  246. * \param usart Base address of the USART instance.
  247. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  248. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  249. *
  250. * \retval USART_SUCCESS Mode successfully initialized.
  251. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  252. */
  253. extern int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  254. /*! \brief Sets up the USART to use the RS485 protocol.
  255. *
  256. * \param usart Base address of the USART instance.
  257. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  258. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  259. *
  260. * \retval USART_SUCCESS Mode successfully initialized.
  261. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  262. */
  263. extern int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz);
  264. /*! \brief Sets up the USART to use the IrDA protocol.
  265. *
  266. * \param usart Base address of the USART instance.
  267. * \param opt Options needed to set up RS232 communication (see \ref usart_options_t).
  268. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  269. * \param irda_filter Counter used to distinguish received ones from zeros.
  270. *
  271. * \retval USART_SUCCESS Mode successfully initialized.
  272. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  273. */
  274. extern int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,
  275. long pba_hz, unsigned char irda_filter);
  276. /*! \brief Sets up the USART to use the ISO7816 T=0 or T=1 smartcard protocols.
  277. *
  278. * The receiver is enabled by default. \ref usart_iso7816_enable_receiver and
  279. * \ref usart_iso7816_enable_transmitter can be called to change the half-duplex
  280. * communication direction.
  281. *
  282. * \param usart Base address of the USART instance.
  283. * \param opt Options needed to set up ISO7816 communication (see \ref usart_iso7816_options_t).
  284. * \param t ISO7816 mode to use (T=0 or T=1).
  285. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  286. *
  287. * \retval USART_SUCCESS Mode successfully initialized.
  288. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  289. */
  290. extern int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz);
  291. #if defined(AVR32_USART_400_H_INCLUDED) || \
  292. defined(AVR32_USART_410_H_INCLUDED) || \
  293. defined(AVR32_USART_420_H_INCLUDED) || \
  294. defined(AVR32_USART_440_H_INCLUDED) || \
  295. defined(AVR32_USART_602_H_INCLUDED)
  296. /*! \brief Sets up the USART to use the LIN master mode.
  297. *
  298. * \param usart Base address of the USART instance.
  299. * \param baudrate Baud rate.
  300. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  301. *
  302. */
  303. extern int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
  304. /*! \brief Sets up the USART to use the LIN slave mode.
  305. *
  306. * \param usart Base address of the USART instance.
  307. * \param baudrate Baud rate.
  308. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  309. *
  310. */
  311. extern int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz);
  312. /*! \brief Sets up the USART to use the SPI master mode.
  313. *
  314. * \ref usart_spi_selectChip and \ref usart_spi_unselectChip can be called to
  315. * select or unselect the SPI slave chip.
  316. *
  317. * \param usart Base address of the USART instance.
  318. * \param opt Options needed to set up SPI mode (see \ref usart_spi_options_t).
  319. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  320. *
  321. * \retval USART_SUCCESS Mode successfully initialized.
  322. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  323. */
  324. extern int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
  325. /*! \brief Sets up the USART to use the SPI slave mode.
  326. *
  327. * \param usart Base address of the USART instance.
  328. * \param opt Options needed to set up SPI mode (see \ref usart_spi_options_t).
  329. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  330. *
  331. * \retval USART_SUCCESS Mode successfully initialized.
  332. * \retval USART_INVALID_INPUT One or more of the arguments is out of valid range.
  333. */
  334. extern int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz);
  335. #endif // USART rev. >= 4.0.0
  336. //! @}
  337. //------------------------------------------------------------------------------
  338. /*! \name Read and Reset Error Status Bits
  339. */
  340. //! @{
  341. /*! \brief Resets the error status.
  342. *
  343. * This function resets the status bits indicating that a parity error,
  344. * framing error or overrun has occurred. The RXBRK bit, indicating
  345. * a start/end of break condition on the RX line, is also reset.
  346. *
  347. * \param usart Base address of the USART instance.
  348. */
  349. __always_inline static void usart_reset_status(volatile avr32_usart_t *usart)
  350. {
  351. usart->cr = AVR32_USART_CR_RSTSTA_MASK;
  352. }
  353. /*! \brief Checks if a parity error has occurred since last status reset.
  354. *
  355. * \param usart Base address of the USART instance.
  356. *
  357. * \return \c 1 if a parity error has been detected, otherwise \c 0.
  358. */
  359. __always_inline static int usart_parity_error(volatile avr32_usart_t *usart)
  360. {
  361. return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
  362. }
  363. /*! \brief Checks if a framing error has occurred since last status reset.
  364. *
  365. * \param usart Base address of the USART instance.
  366. *
  367. * \return \c 1 if a framing error has been detected, otherwise \c 0.
  368. */
  369. __always_inline static int usart_framing_error(volatile avr32_usart_t *usart)
  370. {
  371. return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
  372. }
  373. /*! \brief Checks if an overrun error has occurred since last status reset.
  374. *
  375. * \param usart Base address of the USART instance.
  376. *
  377. * \return \c 1 if a overrun error has been detected, otherwise \c 0.
  378. */
  379. __always_inline static int usart_overrun_error(volatile avr32_usart_t *usart)
  380. {
  381. return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
  382. }
  383. #if defined(AVR32_USART_400_H_INCLUDED) || \
  384. defined(AVR32_USART_410_H_INCLUDED) || \
  385. defined(AVR32_USART_420_H_INCLUDED) || \
  386. defined(AVR32_USART_440_H_INCLUDED) || \
  387. defined(AVR32_USART_602_H_INCLUDED)
  388. /*! \brief Get LIN Error Status
  389. *
  390. * \param usart Base address of the USART instance.
  391. *
  392. * \retval The binary value of the error field.
  393. */
  394. __always_inline static int usart_lin_get_error(volatile avr32_usart_t *usart)
  395. {
  396. return (usart->csr & (AVR32_USART_CSR_LINSNRE_MASK |
  397. AVR32_USART_CSR_LINCE_MASK |
  398. AVR32_USART_CSR_LINIPE_MASK |
  399. AVR32_USART_CSR_LINISFE_MASK |
  400. AVR32_USART_CSR_LINBE_MASK)) >> AVR32_USART_CSR_LINBE_OFFSET;
  401. }
  402. #endif // USART rev. >= 4.0.0
  403. //! @}
  404. //------------------------------------------------------------------------------
  405. /*! \name ISO7816 Control Functions
  406. */
  407. //! @{
  408. /*! \brief Enables the ISO7816 receiver.
  409. *
  410. * The ISO7816 transmitter is disabled.
  411. *
  412. * \param usart Base address of the USART instance.
  413. */
  414. __always_inline static void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)
  415. {
  416. usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;
  417. }
  418. /*! \brief Enables the ISO7816 transmitter.
  419. *
  420. * The ISO7816 receiver is disabled.
  421. *
  422. * \param usart Base address of the USART instance.
  423. */
  424. __always_inline static void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)
  425. {
  426. usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;
  427. }
  428. //! @}
  429. //------------------------------------------------------------------------------
  430. #if defined(AVR32_USART_400_H_INCLUDED) || \
  431. defined(AVR32_USART_410_H_INCLUDED) || \
  432. defined(AVR32_USART_420_H_INCLUDED) || \
  433. defined(AVR32_USART_440_H_INCLUDED) || \
  434. defined(AVR32_USART_602_H_INCLUDED)
  435. /*! \name LIN Control Functions
  436. */
  437. //! @{
  438. /*! \brief Sets the node action.
  439. *
  440. * \param usart Base address of the USART instance.
  441. * \param action The node action: \ref USART_LIN_PUBLISH_ACTION,
  442. * \ref USART_LIN_SUBSCRIBE_ACTION or
  443. * \ref USART_LIN_IGNORE_ACTION.
  444. */
  445. __always_inline static void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)
  446. {
  447. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |
  448. action << AVR32_USART_LINMR_NACT_OFFSET;
  449. }
  450. /*! \brief Enables or disables the Identifier parity.
  451. *
  452. * \param usart Base address of the USART instance.
  453. * \param parity Whether to enable the Identifier parity: \c true or \c false.
  454. */
  455. __always_inline static void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)
  456. {
  457. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |
  458. !parity << AVR32_USART_LINMR_PARDIS_OFFSET;
  459. }
  460. /*! \brief Enables or disables the checksum.
  461. *
  462. * \param usart Base address of the USART instance.
  463. * \param parity Whether to enable the checksum: \c true or \c false.
  464. */
  465. __always_inline static void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)
  466. {
  467. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |
  468. !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;
  469. }
  470. /*! \brief Sets the checksum type.
  471. *
  472. * \param usart Base address of the USART instance.
  473. * \param chktyp The checksum type: \ref USART_LIN_ENHANCED_CHEKSUM or
  474. * \ref USART_LIN_CLASSIC_CHECKSUM.
  475. */
  476. __always_inline static void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)
  477. {
  478. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |
  479. chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;
  480. }
  481. /*! \brief Gets the response data length.
  482. *
  483. * \param usart Base address of the USART instance.
  484. *
  485. * \return The response data length.
  486. */
  487. __always_inline static unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)
  488. {
  489. if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)
  490. {
  491. unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);
  492. if (data_length == 1)
  493. data_length = 2;
  494. return data_length;
  495. }
  496. else
  497. return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;
  498. }
  499. /*! \brief Sets the response data length for LIN 1.x.
  500. *
  501. * \param usart Base address of the USART instance.
  502. */
  503. __always_inline static void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)
  504. {
  505. usart->linmr |= AVR32_USART_LINMR_DLM_MASK;
  506. }
  507. /*! \brief Sets the response data length for LIN 2.x.
  508. *
  509. * \param usart Base address of the USART instance.
  510. * \param data_length The response data length.
  511. */
  512. __always_inline static void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)
  513. {
  514. usart->linmr = (usart->linmr & ~(AVR32_USART_LINMR_DLC_MASK |
  515. AVR32_USART_LINMR_DLM_MASK)) |
  516. (data_length - 1) << AVR32_USART_LINMR_DLC_OFFSET;
  517. }
  518. /*! \brief Enables or disables the frame slot mode.
  519. *
  520. * \param usart Base address of the USART instance.
  521. * \param frameslot Whether to enable the frame slot mode: \c true or
  522. * \c false.
  523. */
  524. __always_inline static void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)
  525. {
  526. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |
  527. !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;
  528. }
  529. /*! \brief Gets the Identifier character.
  530. *
  531. * \param usart Base address of the USART instance.
  532. *
  533. * \return The Identifier character.
  534. */
  535. __always_inline static unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)
  536. {
  537. return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;
  538. }
  539. /*! \brief Sets the Identifier character.
  540. *
  541. * \param usart Base address of the USART instance.
  542. * \param id_char The Identifier character.
  543. */
  544. __always_inline static void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)
  545. {
  546. usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |
  547. id_char << AVR32_USART_LINIR_IDCHR_OFFSET;
  548. }
  549. //! @}
  550. #endif // USART rev. >= 4.0.0
  551. //------------------------------------------------------------------------------
  552. #if defined(AVR32_USART_400_H_INCLUDED) || \
  553. defined(AVR32_USART_410_H_INCLUDED) || \
  554. defined(AVR32_USART_420_H_INCLUDED) || \
  555. defined(AVR32_USART_440_H_INCLUDED) || \
  556. defined(AVR32_USART_602_H_INCLUDED)
  557. /*! \name SPI Control Functions
  558. */
  559. //! @{
  560. /*! \brief Selects SPI slave chip.
  561. *
  562. * \param usart Base address of the USART instance.
  563. *
  564. * \retval USART_SUCCESS Success.
  565. */
  566. extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
  567. /*! \brief Unselects SPI slave chip.
  568. *
  569. * \param usart Base address of the USART instance.
  570. *
  571. * \retval USART_SUCCESS Success.
  572. * \retval USART_FAILURE Time-out.
  573. */
  574. extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
  575. //! @}
  576. #endif // USART rev. >= 4.0.0
  577. //------------------------------------------------------------------------------
  578. /*! \name Transmit/Receive Functions
  579. */
  580. //! @{
  581. /*! \brief Addresses a receiver.
  582. *
  583. * While in RS485 mode, receivers only accept data addressed to them.
  584. * A packet/char with the address tag set has to precede any data.
  585. * This function is used to address a receiver. This receiver should read
  586. * all the following data, until an address packet addresses another receiver.
  587. *
  588. * \param usart Base address of the USART instance.
  589. * \param address Address of the target device.
  590. *
  591. * \retval USART_SUCCESS Address successfully sent (if current mode is RS485).
  592. * \retval USART_MODE_FAULT Wrong operating mode.
  593. */
  594. extern int usart_send_address(volatile avr32_usart_t *usart, int address);
  595. /*! \brief Tests if the USART is ready to transmit a character.
  596. *
  597. * \param usart Base address of the USART instance.
  598. *
  599. * \return \c 1 if the USART Transmit Holding Register is free, otherwise \c 0.
  600. */
  601. __always_inline static int usart_tx_ready(volatile avr32_usart_t *usart)
  602. {
  603. return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;
  604. }
  605. /*! \brief Writes the given character to the TX buffer if the transmitter is ready.
  606. *
  607. * \param usart Base address of the USART instance.
  608. * \param c The character (up to 9 bits) to transmit.
  609. *
  610. * \retval USART_SUCCESS The transmitter was ready.
  611. * \retval USART_TX_BUSY The transmitter was busy.
  612. */
  613. extern int usart_write_char(volatile avr32_usart_t *usart, int c);
  614. /*! \brief An active wait writing a character to the USART.
  615. *
  616. * \param usart Base address of the USART instance.
  617. * \param c The character (up to 9 bits) to transmit.
  618. */
  619. __always_inline static void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
  620. {
  621. while (usart_write_char(usart, c) != USART_SUCCESS);
  622. }
  623. /*! \brief Sends a character with the USART.
  624. *
  625. * \param usart Base address of the USART instance.
  626. * \param c Character to write.
  627. *
  628. * \retval USART_SUCCESS The character was written.
  629. * \retval USART_FAILURE The function timed out before the USART transmitter became ready to send.
  630. */
  631. extern int usart_putchar(volatile avr32_usart_t *usart, int c);
  632. /*! \brief Tests if all requested USART transmissions are over.
  633. *
  634. * \param usart Base address of the USART instance.
  635. *
  636. * \return \c 1 if the USART Transmit Shift Register and the USART Transmit
  637. * Holding Register are free, otherwise \c 0.
  638. */
  639. __always_inline static int usart_tx_empty(volatile avr32_usart_t *usart)
  640. {
  641. return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;
  642. }
  643. /*! \brief Tests if the USART contains a received character.
  644. *
  645. * \param usart Base address of the USART instance.
  646. *
  647. * \return \c 1 if the USART Receive Holding Register is full, otherwise \c 0.
  648. */
  649. __always_inline static int usart_test_hit(volatile avr32_usart_t *usart)
  650. {
  651. return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;
  652. }
  653. /*! \brief Checks the RX buffer for a received character, and stores it at the
  654. * given memory location.
  655. *
  656. * \param usart Base address of the USART instance.
  657. * \param c Pointer to the where the read character should be stored
  658. * (must be at least short in order to accept 9-bit characters).
  659. *
  660. * \retval USART_SUCCESS The character was read successfully.
  661. * \retval USART_RX_EMPTY The RX buffer was empty.
  662. * \retval USART_RX_ERROR An error was detected.
  663. */
  664. extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
  665. /*! \brief Waits until a character is received, and returns it.
  666. *
  667. * \param usart Base address of the USART instance.
  668. *
  669. * \return The received character, or \ref USART_FAILURE upon error.
  670. */
  671. extern int usart_getchar(volatile avr32_usart_t *usart);
  672. /*! \brief Writes one character string to the USART.
  673. *
  674. * \param usart Base address of the USART instance.
  675. * \param string String to be written.
  676. */
  677. extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
  678. /*! \brief Gets and echoes characters until end of line (detected by a CR character).
  679. *
  680. * \param usart Base address of the USART instance.
  681. *
  682. * \retval USART_SUCCESS Success.
  683. * \retval USART_FAILURE Low-level error detected or ETX character received.
  684. */
  685. extern int usart_get_echo_line(volatile avr32_usart_t *usart);
  686. #if defined(AVR32_USART_400_H_INCLUDED) || \
  687. defined(AVR32_USART_410_H_INCLUDED) || \
  688. defined(AVR32_USART_420_H_INCLUDED) || \
  689. defined(AVR32_USART_440_H_INCLUDED) || \
  690. defined(AVR32_USART_602_H_INCLUDED)
  691. /*! \brief Abort LIN transmission.
  692. *
  693. * \param usart Base address of the USART instance.
  694. */
  695. __always_inline static void usart_lin_abort(volatile avr32_usart_t *usart)
  696. {
  697. usart->cr = AVR32_USART_LINABT_MASK;
  698. }
  699. /*! \brief Tests if a LIN transfer has been completed.
  700. *
  701. * \param usart Base address of the USART instance.
  702. *
  703. * \return \c 1 if a LIN transfer has been completed, otherwise \c 0.
  704. */
  705. __always_inline static int usart_lin_transfer_completed(volatile avr32_usart_t *usart)
  706. {
  707. return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;
  708. }
  709. #endif // USART rev. >= 4.0.0
  710. //! @}
  711. /**
  712. * \}
  713. */
  714. #endif // _USART_H_