usart.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /* This header file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
  2. /*This file is prepared for Doxygen automatic documentation generation.*/
  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. * - Compiler: IAR EWAVR32 and GNU GCC for AVR32
  11. * - Supported devices: All AVR32 devices with a USART module can be used.
  12. * - AppNote:
  13. *
  14. * \author Atmel Corporation: http://www.atmel.com \n
  15. * Support and FAQ: http://support.atmel.no/
  16. *
  17. ******************************************************************************/
  18. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright notice, this
  24. * list of conditions and the following disclaimer.
  25. *
  26. * 2. Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. *
  30. * 3. The name of Atmel may not be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * 4. This software may only be redistributed and used in connection with an Atmel
  34. * AVR product.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  38. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  39. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  40. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  41. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  45. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  46. *
  47. */
  48. #ifndef _USART_H_
  49. #define _USART_H_
  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 reponse.
  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 addititional 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. #if (defined __GNUC__)
  350. __attribute__((__always_inline__))
  351. #endif
  352. extern __inline__ void usart_reset_status(volatile avr32_usart_t *usart)
  353. {
  354. usart->cr = AVR32_USART_CR_RSTSTA_MASK;
  355. }
  356. /*! \brief Checks if a parity error has occurred since last status reset.
  357. *
  358. * \param usart Base address of the USART instance.
  359. *
  360. * \return \c 1 if a parity error has been detected, otherwise \c 0.
  361. */
  362. #if (defined __GNUC__)
  363. __attribute__((__always_inline__))
  364. #endif
  365. extern __inline__ int usart_parity_error(volatile avr32_usart_t *usart)
  366. {
  367. return (usart->csr & AVR32_USART_CSR_PARE_MASK) != 0;
  368. }
  369. /*! \brief Checks if a framing error has occurred since last status reset.
  370. *
  371. * \param usart Base address of the USART instance.
  372. *
  373. * \return \c 1 if a framing error has been detected, otherwise \c 0.
  374. */
  375. #if (defined __GNUC__)
  376. __attribute__((__always_inline__))
  377. #endif
  378. extern __inline__ int usart_framing_error(volatile avr32_usart_t *usart)
  379. {
  380. return (usart->csr & AVR32_USART_CSR_FRAME_MASK) != 0;
  381. }
  382. /*! \brief Checks if an overrun error has occurred since last status reset.
  383. *
  384. * \param usart Base address of the USART instance.
  385. *
  386. * \return \c 1 if a overrun error has been detected, otherwise \c 0.
  387. */
  388. #if (defined __GNUC__)
  389. __attribute__((__always_inline__))
  390. #endif
  391. extern __inline__ int usart_overrun_error(volatile avr32_usart_t *usart)
  392. {
  393. return (usart->csr & AVR32_USART_CSR_OVRE_MASK) != 0;
  394. }
  395. #if defined(AVR32_USART_400_H_INCLUDED) || \
  396. defined(AVR32_USART_410_H_INCLUDED) || \
  397. defined(AVR32_USART_420_H_INCLUDED) || \
  398. defined(AVR32_USART_440_H_INCLUDED) || \
  399. defined(AVR32_USART_602_H_INCLUDED)
  400. /*! \brief Get LIN Error Status
  401. *
  402. * \param usart Base address of the USART instance.
  403. *
  404. * \retval The binary value of the error field.
  405. */
  406. #if (defined __GNUC__)
  407. __attribute__((__always_inline__))
  408. #endif
  409. extern __inline__ int usart_lin_get_error(volatile avr32_usart_t *usart)
  410. {
  411. return (usart->csr & (AVR32_USART_CSR_LINSNRE_MASK |
  412. AVR32_USART_CSR_LINCE_MASK |
  413. AVR32_USART_CSR_LINIPE_MASK |
  414. AVR32_USART_CSR_LINISFE_MASK |
  415. AVR32_USART_CSR_LINBE_MASK)) >> AVR32_USART_CSR_LINBE_OFFSET;
  416. }
  417. #endif // USART rev. >= 4.0.0
  418. //! @}
  419. //------------------------------------------------------------------------------
  420. /*! \name ISO7816 Control Functions
  421. */
  422. //! @{
  423. /*! \brief Enables the ISO7816 receiver.
  424. *
  425. * The ISO7816 transmitter is disabled.
  426. *
  427. * \param usart Base address of the USART instance.
  428. */
  429. #if (defined __GNUC__)
  430. __attribute__((__always_inline__))
  431. #endif
  432. extern __inline__ void usart_iso7816_enable_receiver(volatile avr32_usart_t *usart)
  433. {
  434. usart->cr = AVR32_USART_CR_TXDIS_MASK | AVR32_USART_CR_RXEN_MASK;
  435. }
  436. /*! \brief Enables the ISO7816 transmitter.
  437. *
  438. * The ISO7816 receiver is disabled.
  439. *
  440. * \param usart Base address of the USART instance.
  441. */
  442. #if (defined __GNUC__)
  443. __attribute__((__always_inline__))
  444. #endif
  445. extern __inline__ void usart_iso7816_enable_transmitter(volatile avr32_usart_t *usart)
  446. {
  447. usart->cr = AVR32_USART_CR_RXDIS_MASK | AVR32_USART_CR_TXEN_MASK;
  448. }
  449. //! @}
  450. //------------------------------------------------------------------------------
  451. #if defined(AVR32_USART_400_H_INCLUDED) || \
  452. defined(AVR32_USART_410_H_INCLUDED) || \
  453. defined(AVR32_USART_420_H_INCLUDED) || \
  454. defined(AVR32_USART_440_H_INCLUDED) || \
  455. defined(AVR32_USART_602_H_INCLUDED)
  456. /*! \name LIN Control Functions
  457. */
  458. //! @{
  459. /*! \brief Sets the node action.
  460. *
  461. * \param usart Base address of the USART instance.
  462. * \param action The node action: \ref USART_LIN_PUBLISH_ACTION,
  463. * \ref USART_LIN_SUBSCRIBE_ACTION or
  464. * \ref USART_LIN_IGNORE_ACTION.
  465. */
  466. #if (defined __GNUC__)
  467. __attribute__((__always_inline__))
  468. #endif
  469. extern __inline__ void usart_lin_set_node_action(volatile avr32_usart_t *usart, unsigned char action)
  470. {
  471. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_NACT_MASK) |
  472. action << AVR32_USART_LINMR_NACT_OFFSET;
  473. }
  474. /*! \brief Enables or disables the Identifier parity.
  475. *
  476. * \param usart Base address of the USART instance.
  477. * \param parity Whether to enable the Identifier parity: \c TRUE or \c FALSE.
  478. */
  479. #if (defined __GNUC__)
  480. __attribute__((__always_inline__))
  481. #endif
  482. extern __inline__ void usart_lin_enable_parity(volatile avr32_usart_t *usart, unsigned char parity)
  483. {
  484. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_PARDIS_MASK) |
  485. !parity << AVR32_USART_LINMR_PARDIS_OFFSET;
  486. }
  487. /*! \brief Enables or disables the checksum.
  488. *
  489. * \param usart Base address of the USART instance.
  490. * \param parity Whether to enable the checksum: \c TRUE or \c FALSE.
  491. */
  492. #if (defined __GNUC__)
  493. __attribute__((__always_inline__))
  494. #endif
  495. extern __inline__ void usart_lin_enable_checksum(volatile avr32_usart_t *usart, unsigned char checksum)
  496. {
  497. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKDIS_MASK) |
  498. !checksum << AVR32_USART_LINMR_CHKDIS_OFFSET;
  499. }
  500. /*! \brief Sets the checksum type.
  501. *
  502. * \param usart Base address of the USART instance.
  503. * \param chktyp The checksum type: \ref USART_LIN_ENHANCED_CHEKSUM or
  504. * \ref USART_LIN_CLASSIC_CHECKSUM.
  505. */
  506. #if (defined __GNUC__)
  507. __attribute__((__always_inline__))
  508. #endif
  509. extern __inline__ void usart_lin_set_checksum(volatile avr32_usart_t *usart, unsigned char chktyp)
  510. {
  511. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_CHKTYP_MASK) |
  512. chktyp << AVR32_USART_LINMR_CHKTYP_OFFSET;
  513. }
  514. /*! \brief Gets the response data length.
  515. *
  516. * \param usart Base address of the USART instance.
  517. *
  518. * \return The response data length.
  519. */
  520. #if (defined __GNUC__)
  521. __attribute__((__always_inline__))
  522. #endif
  523. extern __inline__ unsigned char usart_lin_get_data_length(volatile avr32_usart_t *usart)
  524. {
  525. if (usart->linmr & AVR32_USART_LINMR_DLM_MASK)
  526. {
  527. unsigned char data_length = 1 << ((usart->linir >> (AVR32_USART_LINIR_IDCHR_OFFSET + 4)) & 0x03);
  528. if (data_length == 1)
  529. data_length = 2;
  530. return data_length;
  531. }
  532. else
  533. return ((usart->linmr & AVR32_USART_LINMR_DLC_MASK) >> AVR32_USART_LINMR_DLC_OFFSET) + 1;
  534. }
  535. /*! \brief Sets the response data length for LIN 1.x.
  536. *
  537. * \param usart Base address of the USART instance.
  538. */
  539. #if (defined __GNUC__)
  540. __attribute__((__always_inline__))
  541. #endif
  542. extern __inline__ void usart_lin_set_data_length_lin1x(volatile avr32_usart_t *usart)
  543. {
  544. usart->linmr |= AVR32_USART_LINMR_DLM_MASK;
  545. }
  546. /*! \brief Sets the response data length for LIN 2.x.
  547. *
  548. * \param usart Base address of the USART instance.
  549. * \param data_length The response data length.
  550. */
  551. #if (defined __GNUC__)
  552. __attribute__((__always_inline__))
  553. #endif
  554. extern __inline__ void usart_lin_set_data_length_lin2x(volatile avr32_usart_t *usart, unsigned char data_length)
  555. {
  556. usart->linmr = (usart->linmr & ~(AVR32_USART_LINMR_DLC_MASK |
  557. AVR32_USART_LINMR_DLM_MASK)) |
  558. (data_length - 1) << AVR32_USART_LINMR_DLC_OFFSET;
  559. }
  560. /*! \brief Enables or disables the frame slot mode.
  561. *
  562. * \param usart Base address of the USART instance.
  563. * \param frameslot Whether to enable the frame slot mode: \c TRUE or
  564. * \c FALSE.
  565. */
  566. #if (defined __GNUC__)
  567. __attribute__((__always_inline__))
  568. #endif
  569. extern __inline__ void usart_lin_enable_frameslot(volatile avr32_usart_t *usart, unsigned char frameslot)
  570. {
  571. usart->linmr = (usart->linmr & ~AVR32_USART_LINMR_FSDIS_MASK) |
  572. !frameslot << AVR32_USART_LINMR_FSDIS_OFFSET;
  573. }
  574. /*! \brief Gets the Identifier character.
  575. *
  576. * \param usart Base address of the USART instance.
  577. *
  578. * \return The Identifier character.
  579. */
  580. #if (defined __GNUC__)
  581. __attribute__((__always_inline__))
  582. #endif
  583. extern __inline__ unsigned char usart_lin_get_id_char(volatile avr32_usart_t *usart)
  584. {
  585. return (usart->linir & AVR32_USART_LINIR_IDCHR_MASK) >> AVR32_USART_LINIR_IDCHR_OFFSET;
  586. }
  587. /*! \brief Sets the Identifier character.
  588. *
  589. * \param usart Base address of the USART instance.
  590. * \param id_char The Identifier character.
  591. */
  592. #if (defined __GNUC__)
  593. __attribute__((__always_inline__))
  594. #endif
  595. extern __inline__ void usart_lin_set_id_char(volatile avr32_usart_t *usart, unsigned char id_char)
  596. {
  597. usart->linir = (usart->linir & ~AVR32_USART_LINIR_IDCHR_MASK) |
  598. id_char << AVR32_USART_LINIR_IDCHR_OFFSET;
  599. }
  600. //! @}
  601. #endif // USART rev. >= 4.0.0
  602. //------------------------------------------------------------------------------
  603. #if defined(AVR32_USART_400_H_INCLUDED) || \
  604. defined(AVR32_USART_410_H_INCLUDED) || \
  605. defined(AVR32_USART_420_H_INCLUDED) || \
  606. defined(AVR32_USART_440_H_INCLUDED) || \
  607. defined(AVR32_USART_602_H_INCLUDED)
  608. /*! \name SPI Control Functions
  609. */
  610. //! @{
  611. /*! \brief Selects SPI slave chip.
  612. *
  613. * \param usart Base address of the USART instance.
  614. *
  615. * \retval USART_SUCCESS Success.
  616. */
  617. extern int usart_spi_selectChip(volatile avr32_usart_t *usart);
  618. /*! \brief Unselects SPI slave chip.
  619. *
  620. * \param usart Base address of the USART instance.
  621. *
  622. * \retval USART_SUCCESS Success.
  623. * \retval USART_FAILURE Time-out.
  624. */
  625. extern int usart_spi_unselectChip(volatile avr32_usart_t *usart);
  626. //! @}
  627. #endif // USART rev. >= 4.0.0
  628. //------------------------------------------------------------------------------
  629. /*! \name Transmit/Receive Functions
  630. */
  631. //! @{
  632. /*! \brief Addresses a receiver.
  633. *
  634. * While in RS485 mode, receivers only accept data addressed to them.
  635. * A packet/char with the address tag set has to precede any data.
  636. * This function is used to address a receiver. This receiver should read
  637. * all the following data, until an address packet addresses another receiver.
  638. *
  639. * \param usart Base address of the USART instance.
  640. * \param address Address of the target device.
  641. *
  642. * \retval USART_SUCCESS Address successfully sent (if current mode is RS485).
  643. * \retval USART_MODE_FAULT Wrong operating mode.
  644. */
  645. extern int usart_send_address(volatile avr32_usart_t *usart, int address);
  646. /*! \brief Tests if the USART is ready to transmit a character.
  647. *
  648. * \param usart Base address of the USART instance.
  649. *
  650. * \return \c 1 if the USART Transmit Holding Register is free, otherwise \c 0.
  651. */
  652. #if (defined __GNUC__)
  653. __attribute__((__always_inline__))
  654. #endif
  655. extern __inline__ int usart_tx_ready(volatile avr32_usart_t *usart)
  656. {
  657. return (usart->csr & AVR32_USART_CSR_TXRDY_MASK) != 0;
  658. }
  659. /*! \brief Writes the given character to the TX buffer if the transmitter is ready.
  660. *
  661. * \param usart Base address of the USART instance.
  662. * \param c The character (up to 9 bits) to transmit.
  663. *
  664. * \retval USART_SUCCESS The transmitter was ready.
  665. * \retval USART_TX_BUSY The transmitter was busy.
  666. */
  667. extern int usart_write_char(volatile avr32_usart_t *usart, int c);
  668. /*! \brief An active wait writing a character to the USART.
  669. *
  670. * \param usart Base address of the USART instance.
  671. * \param c The character (up to 9 bits) to transmit.
  672. */
  673. #if (defined __GNUC__)
  674. __attribute__((__always_inline__))
  675. #endif
  676. extern __inline__ void usart_bw_write_char(volatile avr32_usart_t *usart, int c)
  677. {
  678. while (usart_write_char(usart, c) != USART_SUCCESS);
  679. }
  680. /*! \brief Sends a character with the USART.
  681. *
  682. * \param usart Base address of the USART instance.
  683. * \param c Character to write.
  684. *
  685. * \retval USART_SUCCESS The character was written.
  686. * \retval USART_FAILURE The function timed out before the USART transmitter became ready to send.
  687. */
  688. extern int usart_putchar(volatile avr32_usart_t *usart, int c);
  689. /*! \brief Tests if all requested USART transmissions are over.
  690. *
  691. * \param usart Base address of the USART instance.
  692. *
  693. * \return \c 1 if the USART Transmit Shift Register and the USART Transmit
  694. * Holding Register are free, otherwise \c 0.
  695. */
  696. #if (defined __GNUC__)
  697. __attribute__((__always_inline__))
  698. #endif
  699. extern __inline__ int usart_tx_empty(volatile avr32_usart_t *usart)
  700. {
  701. return (usart->csr & AVR32_USART_CSR_TXEMPTY_MASK) != 0;
  702. }
  703. /*! \brief Tests if the USART contains a received character.
  704. *
  705. * \param usart Base address of the USART instance.
  706. *
  707. * \return \c 1 if the USART Receive Holding Register is full, otherwise \c 0.
  708. */
  709. #if (defined __GNUC__)
  710. __attribute__((__always_inline__))
  711. #endif
  712. extern __inline__ int usart_test_hit(volatile avr32_usart_t *usart)
  713. {
  714. return (usart->csr & AVR32_USART_CSR_RXRDY_MASK) != 0;
  715. }
  716. /*! \brief Checks the RX buffer for a received character, and stores it at the
  717. * given memory location.
  718. *
  719. * \param usart Base address of the USART instance.
  720. * \param c Pointer to the where the read character should be stored
  721. * (must be at least short in order to accept 9-bit characters).
  722. *
  723. * \retval USART_SUCCESS The character was read successfully.
  724. * \retval USART_RX_EMPTY The RX buffer was empty.
  725. * \retval USART_RX_ERROR An error was deteceted.
  726. */
  727. extern int usart_read_char(volatile avr32_usart_t *usart, int *c);
  728. /*! \brief Waits until a character is received, and returns it.
  729. *
  730. * \param usart Base address of the USART instance.
  731. *
  732. * \return The received character, or \ref USART_FAILURE upon error.
  733. */
  734. extern int usart_getchar(volatile avr32_usart_t *usart);
  735. /*! \brief Writes one character string to the USART.
  736. *
  737. * \param usart Base address of the USART instance.
  738. * \param string String to be written.
  739. */
  740. extern void usart_write_line(volatile avr32_usart_t *usart, const char *string);
  741. /*! \brief Gets and echoes characters until end of line.
  742. *
  743. * \param usart Base address of the USART instance.
  744. *
  745. * \retval USART_SUCCESS Success.
  746. * \retval USART_FAILURE Low-level error detected or ETX character received.
  747. */
  748. extern int usart_get_echo_line(volatile avr32_usart_t *usart);
  749. #if defined(AVR32_USART_400_H_INCLUDED) || \
  750. defined(AVR32_USART_410_H_INCLUDED) || \
  751. defined(AVR32_USART_420_H_INCLUDED) || \
  752. defined(AVR32_USART_440_H_INCLUDED) || \
  753. defined(AVR32_USART_602_H_INCLUDED)
  754. /*! \brief Abort LIN transmission.
  755. *
  756. * \param usart Base address of the USART instance.
  757. */
  758. #if (defined __GNUC__)
  759. __attribute__((__always_inline__))
  760. #endif
  761. extern __inline__ void usart_lin_abort(volatile avr32_usart_t *usart)
  762. {
  763. usart->cr = AVR32_USART_LINABT_MASK;
  764. }
  765. /*! \brief Tests if a LIN transfer has been completed.
  766. *
  767. * \param usart Base address of the USART instance.
  768. *
  769. * \return \c 1 if a LIN transfer has been completed, otherwise \c 0.
  770. */
  771. #if (defined __GNUC__)
  772. __attribute__((__always_inline__))
  773. #endif
  774. extern __inline__ int usart_lin_transfer_completed(volatile avr32_usart_t *usart)
  775. {
  776. return (usart->csr & AVR32_USART_CSR_LINTC_MASK) != 0;
  777. }
  778. #endif // USART rev. >= 4.0.0
  779. //! @}
  780. #endif // _USART_H_