usart.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. /* This source 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. #include "compiler.h"
  49. #include "usart.h"
  50. //------------------------------------------------------------------------------
  51. /*! \name Private Functions
  52. */
  53. //! @{
  54. /*! \brief Checks if the USART is in multidrop mode.
  55. *
  56. * \param usart Base address of the USART instance.
  57. *
  58. * \return \c 1 if the USART is in multidrop mode, otherwise \c 0.
  59. */
  60. #if (defined __GNUC__)
  61. __attribute__((__always_inline__))
  62. #endif
  63. static __inline__ int usart_mode_is_multidrop(volatile avr32_usart_t *usart)
  64. {
  65. return ((usart->mr >> AVR32_USART_MR_PAR_OFFSET) & AVR32_USART_MR_PAR_MULTI) == AVR32_USART_MR_PAR_MULTI;
  66. }
  67. /*! \brief Calculates a clock divider (\e CD) and a fractional part (\e FP) for
  68. * the USART asynchronous modes to generate a baud rate as close as
  69. * possible to the baud rate set point.
  70. *
  71. * Baud rate calculation:
  72. * \f$ Baudrate = \frac{SelectedClock}{Over \times (CD + \frac{FP}{8})} \f$, \e Over being 16 or 8.
  73. * The maximal oversampling is selected if it allows to generate a baud rate close to the set point.
  74. *
  75. * \param usart Base address of the USART instance.
  76. * \param baudrate Baud rate set point.
  77. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  78. *
  79. * \retval USART_SUCCESS Baud rate successfully initialized.
  80. * \retval USART_INVALID_INPUT Baud rate set point is out of range for the given input clock frequency.
  81. */
  82. static int usart_set_async_baudrate(volatile avr32_usart_t *usart, unsigned int baudrate, unsigned long pba_hz)
  83. {
  84. unsigned int over = (pba_hz >= 16 * baudrate) ? 16 : 8;
  85. unsigned int cd_fp = ((1 << AVR32_USART_BRGR_FP_SIZE) * pba_hz + (over * baudrate) / 2) / (over * baudrate);
  86. unsigned int cd = cd_fp >> AVR32_USART_BRGR_FP_SIZE;
  87. unsigned int fp = cd_fp & ((1 << AVR32_USART_BRGR_FP_SIZE) - 1);
  88. if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
  89. return USART_INVALID_INPUT;
  90. usart->mr = (usart->mr & ~(AVR32_USART_MR_USCLKS_MASK |
  91. AVR32_USART_MR_SYNC_MASK |
  92. AVR32_USART_MR_OVER_MASK)) |
  93. AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
  94. ((over == 16) ? AVR32_USART_MR_OVER_X16 : AVR32_USART_MR_OVER_X8) << AVR32_USART_MR_OVER_OFFSET;
  95. usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET |
  96. fp << AVR32_USART_BRGR_FP_OFFSET;
  97. return USART_SUCCESS;
  98. }
  99. /*! \brief Calculates a clock divider (\e CD) for the USART synchronous master
  100. * modes to generate a baud rate as close as possible to the baud rate
  101. * set point.
  102. *
  103. * Baud rate calculation:
  104. * \f$ Baudrate = \frac{SelectedClock}{CD} \f$.
  105. *
  106. * \param usart Base address of the USART instance.
  107. * \param baudrate Baud rate set point.
  108. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  109. *
  110. * \retval USART_SUCCESS Baud rate successfully initialized.
  111. * \retval USART_INVALID_INPUT Baud rate set point is out of range for the given input clock frequency.
  112. */
  113. static int usart_set_sync_master_baudrate(volatile avr32_usart_t *usart, unsigned int baudrate, unsigned long pba_hz)
  114. {
  115. unsigned int cd = (pba_hz + baudrate / 2) / baudrate;
  116. if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
  117. return USART_INVALID_INPUT;
  118. usart->mr = (usart->mr & ~AVR32_USART_MR_USCLKS_MASK) |
  119. AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
  120. AVR32_USART_MR_SYNC_MASK;
  121. usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET;
  122. return USART_SUCCESS;
  123. }
  124. /*! \brief Selects the SCK pin as the source of baud rate for the USART
  125. * synchronous slave modes.
  126. *
  127. * \param usart Base address of the USART instance.
  128. *
  129. * \retval USART_SUCCESS Baud rate successfully initialized.
  130. */
  131. static int usart_set_sync_slave_baudrate(volatile avr32_usart_t *usart)
  132. {
  133. usart->mr = (usart->mr & ~AVR32_USART_MR_USCLKS_MASK) |
  134. AVR32_USART_MR_USCLKS_SCK << AVR32_USART_MR_USCLKS_OFFSET |
  135. AVR32_USART_MR_SYNC_MASK;
  136. return USART_SUCCESS;
  137. }
  138. /*! \brief Calculates a clock divider (\e CD) for the USART ISO7816 mode to
  139. * generate an ISO7816 clock as close as possible to the clock set point.
  140. *
  141. * ISO7816 clock calculation:
  142. * \f$ Clock = \frac{SelectedClock}{CD} \f$.
  143. *
  144. * \param usart Base address of the USART instance.
  145. * \param clock ISO7816 clock set point.
  146. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  147. *
  148. * \retval USART_SUCCESS ISO7816 clock successfully initialized.
  149. * \retval USART_INVALID_INPUT ISO7816 clock set point is out of range for the given input clock frequency.
  150. */
  151. static int usart_set_iso7816_clock(volatile avr32_usart_t *usart, unsigned int clock, unsigned long pba_hz)
  152. {
  153. unsigned int cd = (pba_hz + clock / 2) / clock;
  154. if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
  155. return USART_INVALID_INPUT;
  156. usart->mr = (usart->mr & ~(AVR32_USART_MR_USCLKS_MASK |
  157. AVR32_USART_MR_SYNC_MASK |
  158. AVR32_USART_MR_OVER_MASK)) |
  159. AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
  160. AVR32_USART_MR_OVER_X16 << AVR32_USART_MR_OVER_OFFSET;
  161. usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET;
  162. return USART_SUCCESS;
  163. }
  164. #if defined(AVR32_USART_400_H_INCLUDED) || \
  165. defined(AVR32_USART_410_H_INCLUDED) || \
  166. defined(AVR32_USART_420_H_INCLUDED) || \
  167. defined(AVR32_USART_440_H_INCLUDED) || \
  168. defined(AVR32_USART_602_H_INCLUDED)
  169. /*! \brief Calculates a clock divider (\e CD) for the USART SPI master mode to
  170. * generate a baud rate as close as possible to the baud rate set point.
  171. *
  172. * Baud rate calculation:
  173. * \f$ Baudrate = \frac{SelectedClock}{CD} \f$.
  174. *
  175. * \param usart Base address of the USART instance.
  176. * \param baudrate Baud rate set point.
  177. * \param pba_hz USART module input clock frequency (PBA clock, Hz).
  178. *
  179. * \retval USART_SUCCESS Baud rate successfully initialized.
  180. * \retval USART_INVALID_INPUT Baud rate set point is out of range for the given input clock frequency.
  181. */
  182. static int usart_set_spi_master_baudrate(volatile avr32_usart_t *usart, unsigned int baudrate, unsigned long pba_hz)
  183. {
  184. unsigned int cd = (pba_hz + baudrate / 2) / baudrate;
  185. if (cd < 4 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
  186. return USART_INVALID_INPUT;
  187. usart->mr = (usart->mr & ~AVR32_USART_MR_USCLKS_MASK) |
  188. AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET;
  189. usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET;
  190. return USART_SUCCESS;
  191. }
  192. /*! \brief Selects the SCK pin as the source of baud rate for the USART SPI
  193. * slave mode.
  194. *
  195. * \param usart Base address of the USART instance.
  196. *
  197. * \retval USART_SUCCESS Baud rate successfully initialized.
  198. */
  199. static int usart_set_spi_slave_baudrate(volatile avr32_usart_t *usart)
  200. {
  201. usart->mr = (usart->mr & ~AVR32_USART_MR_USCLKS_MASK) |
  202. AVR32_USART_MR_USCLKS_SCK << AVR32_USART_MR_USCLKS_OFFSET;
  203. return USART_SUCCESS;
  204. }
  205. #endif // USART rev. >= 4.0.0
  206. //! @}
  207. //------------------------------------------------------------------------------
  208. /*! \name Initialization Functions
  209. */
  210. //! @{
  211. void usart_reset(volatile avr32_usart_t *usart)
  212. {
  213. Bool global_interrupt_enabled = Is_global_interrupt_enabled();
  214. // Disable all USART interrupts.
  215. // Interrupts needed should be set explicitly on every reset.
  216. if (global_interrupt_enabled) Disable_global_interrupt();
  217. usart->idr = 0xFFFFFFFF;
  218. usart->csr;
  219. if (global_interrupt_enabled) Enable_global_interrupt();
  220. // Reset mode and other registers that could cause unpredictable behavior after reset.
  221. usart->mr = 0;
  222. usart->rtor = 0;
  223. usart->ttgr = 0;
  224. // Shutdown TX and RX (will be re-enabled when setup has successfully completed),
  225. // reset status bits and turn off DTR and RTS.
  226. usart->cr = AVR32_USART_CR_RSTRX_MASK |
  227. AVR32_USART_CR_RSTTX_MASK |
  228. AVR32_USART_CR_RSTSTA_MASK |
  229. AVR32_USART_CR_RSTIT_MASK |
  230. AVR32_USART_CR_RSTNACK_MASK |
  231. #ifndef AVR32_USART_440_H_INCLUDED
  232. // Note: Modem Signal Management DTR-DSR-DCD-RI are not included in USART rev.440.
  233. AVR32_USART_CR_DTRDIS_MASK |
  234. #endif
  235. AVR32_USART_CR_RTSDIS_MASK;
  236. }
  237. int usart_init_rs232(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  238. {
  239. // Reset the USART and shutdown TX and RX.
  240. usart_reset(usart);
  241. // Check input values.
  242. if (!opt || // Null pointer.
  243. opt->charlength < 5 || opt->charlength > 9 ||
  244. opt->paritytype > 7 ||
  245. opt->stopbits > 2 + 255 ||
  246. opt->channelmode > 3 ||
  247. usart_set_async_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
  248. return USART_INVALID_INPUT;
  249. if (opt->charlength == 9)
  250. {
  251. // Character length set to 9 bits. MODE9 dominates CHRL.
  252. usart->mr |= AVR32_USART_MR_MODE9_MASK;
  253. }
  254. else
  255. {
  256. // CHRL gives the character length (- 5) when MODE9 = 0.
  257. usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
  258. }
  259. usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
  260. opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
  261. if (opt->stopbits > USART_2_STOPBITS)
  262. {
  263. // Set two stop bits
  264. usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
  265. // and a timeguard period gives the rest.
  266. usart->ttgr = opt->stopbits - USART_2_STOPBITS;
  267. }
  268. else
  269. // Insert 1, 1.5 or 2 stop bits.
  270. usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
  271. // Set normal mode.
  272. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  273. AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
  274. // Setup complete; enable communication.
  275. // Enable input and output.
  276. usart->cr = AVR32_USART_CR_RXEN_MASK |
  277. AVR32_USART_CR_TXEN_MASK;
  278. return USART_SUCCESS;
  279. }
  280. int usart_init_rs232_tx_only(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  281. {
  282. // Reset the USART and shutdown TX and RX.
  283. usart_reset(usart);
  284. // Check input values.
  285. if (!opt || // Null pointer.
  286. opt->charlength < 5 || opt->charlength > 9 ||
  287. opt->paritytype > 7 ||
  288. opt->stopbits == 1 || opt->stopbits > 2 + 255 ||
  289. opt->channelmode > 3 ||
  290. usart_set_sync_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
  291. return USART_INVALID_INPUT;
  292. if (opt->charlength == 9)
  293. {
  294. // Character length set to 9 bits. MODE9 dominates CHRL.
  295. usart->mr |= AVR32_USART_MR_MODE9_MASK;
  296. }
  297. else
  298. {
  299. // CHRL gives the character length (- 5) when MODE9 = 0.
  300. usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
  301. }
  302. usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
  303. opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
  304. if (opt->stopbits > USART_2_STOPBITS)
  305. {
  306. // Set two stop bits
  307. usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
  308. // and a timeguard period gives the rest.
  309. usart->ttgr = opt->stopbits - USART_2_STOPBITS;
  310. }
  311. else
  312. // Insert 1 or 2 stop bits.
  313. usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
  314. // Set normal mode.
  315. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  316. AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
  317. // Setup complete; enable communication.
  318. // Enable only output as input is not possible in synchronous mode without
  319. // transferring clock.
  320. usart->cr = AVR32_USART_CR_TXEN_MASK;
  321. return USART_SUCCESS;
  322. }
  323. int usart_init_hw_handshaking(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  324. {
  325. // First: Setup standard RS232.
  326. if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
  327. return USART_INVALID_INPUT;
  328. // Set hardware handshaking mode.
  329. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  330. AVR32_USART_MR_MODE_HARDWARE << AVR32_USART_MR_MODE_OFFSET;
  331. return USART_SUCCESS;
  332. }
  333. int usart_init_modem(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  334. {
  335. // First: Setup standard RS232.
  336. if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
  337. return USART_INVALID_INPUT;
  338. // Set modem mode.
  339. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  340. AVR32_USART_MR_MODE_MODEM << AVR32_USART_MR_MODE_OFFSET;
  341. return USART_SUCCESS;
  342. }
  343. int usart_init_sync_master(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  344. {
  345. // Reset the USART and shutdown TX and RX.
  346. usart_reset(usart);
  347. // Check input values.
  348. if (!opt || // Null pointer.
  349. opt->charlength < 5 || opt->charlength > 9 ||
  350. opt->paritytype > 7 ||
  351. opt->stopbits == 1 || opt->stopbits > 2 + 255 ||
  352. opt->channelmode > 3 ||
  353. usart_set_sync_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
  354. return USART_INVALID_INPUT;
  355. if (opt->charlength == 9)
  356. {
  357. // Character length set to 9 bits. MODE9 dominates CHRL.
  358. usart->mr |= AVR32_USART_MR_MODE9_MASK;
  359. }
  360. else
  361. {
  362. // CHRL gives the character length (- 5) when MODE9 = 0.
  363. usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
  364. }
  365. usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
  366. opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
  367. if (opt->stopbits > USART_2_STOPBITS)
  368. {
  369. // Set two stop bits
  370. usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
  371. // and a timeguard period gives the rest.
  372. usart->ttgr = opt->stopbits - USART_2_STOPBITS;
  373. }
  374. else
  375. // Insert 1 or 2 stop bits.
  376. usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
  377. // Set normal mode.
  378. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  379. AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET |
  380. AVR32_USART_MR_CLKO_MASK;
  381. // Setup complete; enable communication.
  382. // Enable input and output.
  383. usart->cr = AVR32_USART_CR_RXEN_MASK |
  384. AVR32_USART_CR_TXEN_MASK;
  385. return USART_SUCCESS;
  386. }
  387. int usart_init_sync_slave(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  388. {
  389. // Reset the USART and shutdown TX and RX.
  390. usart_reset(usart);
  391. // Check input values.
  392. if (!opt || // Null pointer.
  393. opt->charlength < 5 || opt->charlength > 9 ||
  394. opt->paritytype > 7 ||
  395. opt->stopbits == 1 || opt->stopbits > 2 + 255 ||
  396. opt->channelmode > 3 ||
  397. usart_set_sync_slave_baudrate(usart) == USART_INVALID_INPUT)
  398. return USART_INVALID_INPUT;
  399. if (opt->charlength == 9)
  400. {
  401. // Character length set to 9 bits. MODE9 dominates CHRL.
  402. usart->mr |= AVR32_USART_MR_MODE9_MASK;
  403. }
  404. else
  405. {
  406. // CHRL gives the character length (- 5) when MODE9 = 0.
  407. usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
  408. }
  409. usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
  410. opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET;
  411. if (opt->stopbits > USART_2_STOPBITS)
  412. {
  413. // Set two stop bits
  414. usart->mr |= AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET;
  415. // and a timeguard period gives the rest.
  416. usart->ttgr = opt->stopbits - USART_2_STOPBITS;
  417. }
  418. else
  419. // Insert 1 or 2 stop bits.
  420. usart->mr |= opt->stopbits << AVR32_USART_MR_NBSTOP_OFFSET;
  421. // Set normal mode.
  422. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  423. AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
  424. // Setup complete; enable communication.
  425. // Enable input and output.
  426. usart->cr = AVR32_USART_CR_RXEN_MASK |
  427. AVR32_USART_CR_TXEN_MASK;
  428. return USART_SUCCESS;
  429. }
  430. int usart_init_rs485(volatile avr32_usart_t *usart, const usart_options_t *opt, long pba_hz)
  431. {
  432. // First: Setup standard RS232.
  433. if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
  434. return USART_INVALID_INPUT;
  435. // Set RS485 mode.
  436. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  437. AVR32_USART_MR_MODE_RS485 << AVR32_USART_MR_MODE_OFFSET;
  438. return USART_SUCCESS;
  439. }
  440. int usart_init_IrDA(volatile avr32_usart_t *usart, const usart_options_t *opt,
  441. long pba_hz, unsigned char irda_filter)
  442. {
  443. // First: Setup standard RS232.
  444. if (usart_init_rs232(usart, opt, pba_hz) == USART_INVALID_INPUT)
  445. return USART_INVALID_INPUT;
  446. // Set IrDA filter.
  447. usart->ifr = irda_filter;
  448. // Set IrDA mode and activate filtering of input.
  449. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) |
  450. AVR32_USART_MODE_IRDA << AVR32_USART_MR_MODE_OFFSET |
  451. AVR32_USART_MR_FILTER_MASK;
  452. return USART_SUCCESS;
  453. }
  454. int usart_init_iso7816(volatile avr32_usart_t *usart, const usart_iso7816_options_t *opt, int t, long pba_hz)
  455. {
  456. // Reset the USART and shutdown TX and RX.
  457. usart_reset(usart);
  458. // Check input values.
  459. if (!opt || // Null pointer.
  460. opt->paritytype > 1)
  461. return USART_INVALID_INPUT;
  462. if (t == 0)
  463. {
  464. // Set USART mode to ISO7816, T=0.
  465. // The T=0 protocol always uses 2 stop bits.
  466. usart->mr = AVR32_USART_MR_MODE_ISO7816_T0 << AVR32_USART_MR_MODE_OFFSET |
  467. AVR32_USART_MR_NBSTOP_2 << AVR32_USART_MR_NBSTOP_OFFSET |
  468. opt->bit_order << AVR32_USART_MR_MSBF_OFFSET; // Allow MSBF in T=0.
  469. }
  470. else if (t == 1)
  471. {
  472. // Only LSB first in the T=1 protocol.
  473. // max_iterations field is only used in T=0 mode.
  474. if (opt->bit_order != 0 ||
  475. opt->max_iterations != 0)
  476. return USART_INVALID_INPUT;
  477. // Set USART mode to ISO7816, T=1.
  478. // The T=1 protocol always uses 1 stop bit.
  479. usart->mr = AVR32_USART_MR_MODE_ISO7816_T1 << AVR32_USART_MR_MODE_OFFSET |
  480. AVR32_USART_MR_NBSTOP_1 << AVR32_USART_MR_NBSTOP_OFFSET;
  481. }
  482. else
  483. return USART_INVALID_INPUT;
  484. if (usart_set_iso7816_clock(usart, opt->iso7816_hz, pba_hz) == USART_INVALID_INPUT)
  485. return USART_INVALID_INPUT;
  486. // Set FIDI register: bit rate = selected clock/FI_DI_ratio/16.
  487. usart->fidi = opt->fidi_ratio;
  488. // Set ISO7816 spesific options in the MODE register.
  489. usart->mr |= opt->paritytype << AVR32_USART_MR_PAR_OFFSET |
  490. AVR32_USART_MR_CLKO_MASK | // Enable clock output.
  491. opt->inhibit_nack << AVR32_USART_MR_INACK_OFFSET |
  492. opt->dis_suc_nack << AVR32_USART_MR_DSNACK_OFFSET |
  493. opt->max_iterations << AVR32_USART_MR_MAX_ITERATION_OFFSET;
  494. // Setup complete; enable the receiver by default.
  495. usart_iso7816_enable_receiver(usart);
  496. return USART_SUCCESS;
  497. }
  498. #if defined(AVR32_USART_400_H_INCLUDED) || \
  499. defined(AVR32_USART_410_H_INCLUDED) || \
  500. defined(AVR32_USART_420_H_INCLUDED) || \
  501. defined(AVR32_USART_440_H_INCLUDED) || \
  502. defined(AVR32_USART_602_H_INCLUDED)
  503. int usart_init_lin_master(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz)
  504. {
  505. // Reset the USART and shutdown TX and RX.
  506. usart_reset(usart);
  507. // Check input values.
  508. if (usart_set_async_baudrate(usart, baudrate, pba_hz) == USART_INVALID_INPUT)
  509. return USART_INVALID_INPUT;
  510. usart->mr |= AVR32_USART_MR_MODE_LIN_MASTER << AVR32_USART_MR_MODE_OFFSET; // LIN master mode.
  511. // Setup complete; enable communication.
  512. // Enable input and output.
  513. usart->cr = AVR32_USART_CR_RXEN_MASK |
  514. AVR32_USART_CR_TXEN_MASK;
  515. return USART_SUCCESS;
  516. }
  517. int usart_init_lin_slave(volatile avr32_usart_t *usart, unsigned long baudrate, long pba_hz)
  518. {
  519. // Reset the USART and shutdown TX and RX.
  520. usart_reset(usart);
  521. // Check input values.
  522. if (usart_set_async_baudrate(usart, baudrate, pba_hz) == USART_INVALID_INPUT)
  523. return USART_INVALID_INPUT;
  524. usart->mr |= AVR32_USART_MR_MODE_LIN_SLAVE << AVR32_USART_MR_MODE_OFFSET; // LIN slave mode.
  525. // Setup complete; enable communication.
  526. // Enable input and output.
  527. usart->cr = AVR32_USART_CR_RXEN_MASK |
  528. AVR32_USART_CR_TXEN_MASK;
  529. return USART_SUCCESS;
  530. }
  531. int usart_init_spi_master(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz)
  532. {
  533. // Reset the USART and shutdown TX and RX.
  534. usart_reset(usart);
  535. // Check input values.
  536. if (!opt || // Null pointer.
  537. opt->charlength < 5 || opt->charlength > 9 ||
  538. opt->spimode > 3 ||
  539. opt->channelmode > 3 ||
  540. usart_set_spi_master_baudrate(usart, opt->baudrate, pba_hz) == USART_INVALID_INPUT)
  541. return USART_INVALID_INPUT;
  542. if (opt->charlength == 9)
  543. {
  544. // Character length set to 9 bits. MODE9 dominates CHRL.
  545. usart->mr |= AVR32_USART_MR_MODE9_MASK;
  546. }
  547. else
  548. {
  549. // CHRL gives the character length (- 5) when MODE9 = 0.
  550. usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
  551. }
  552. usart->mr |= AVR32_USART_MR_MODE_SPI_MASTER << AVR32_USART_MR_MODE_OFFSET | // SPI master mode.
  553. ((opt->spimode & 0x1) ^ 0x1) << AVR32_USART_MR_SYNC_OFFSET | // SPI clock phase.
  554. opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET | // Channel mode.
  555. (opt->spimode >> 1) << AVR32_USART_MR_MSBF_OFFSET | // SPI clock polarity.
  556. AVR32_USART_MR_CLKO_MASK; // Drive SCK pin.
  557. // Setup complete; enable communication.
  558. // Enable input and output.
  559. usart->cr = AVR32_USART_CR_RXEN_MASK |
  560. AVR32_USART_CR_TXEN_MASK;
  561. return USART_SUCCESS;
  562. }
  563. int usart_init_spi_slave(volatile avr32_usart_t *usart, const usart_spi_options_t *opt, long pba_hz)
  564. {
  565. // Reset the USART and shutdown TX and RX.
  566. usart_reset(usart);
  567. // Check input values.
  568. if (!opt || // Null pointer.
  569. opt->charlength < 5 || opt->charlength > 9 ||
  570. opt->spimode > 3 ||
  571. opt->channelmode > 3 ||
  572. usart_set_spi_slave_baudrate(usart) == USART_INVALID_INPUT)
  573. return USART_INVALID_INPUT;
  574. if (opt->charlength == 9)
  575. {
  576. // Character length set to 9 bits. MODE9 dominates CHRL.
  577. usart->mr |= AVR32_USART_MR_MODE9_MASK;
  578. }
  579. else
  580. {
  581. // CHRL gives the character length (- 5) when MODE9 = 0.
  582. usart->mr |= (opt->charlength - 5) << AVR32_USART_MR_CHRL_OFFSET;
  583. }
  584. usart->mr |= AVR32_USART_MR_MODE_SPI_SLAVE << AVR32_USART_MR_MODE_OFFSET | // SPI slave mode.
  585. ((opt->spimode & 0x1) ^ 0x1) << AVR32_USART_MR_SYNC_OFFSET | // SPI clock phase.
  586. opt->channelmode << AVR32_USART_MR_CHMODE_OFFSET | // Channel mode.
  587. (opt->spimode >> 1) << AVR32_USART_MR_MSBF_OFFSET; // SPI clock polarity.
  588. // Setup complete; enable communication.
  589. // Enable input and output.
  590. usart->cr = AVR32_USART_CR_RXEN_MASK |
  591. AVR32_USART_CR_TXEN_MASK;
  592. return USART_SUCCESS;
  593. }
  594. #endif // USART rev. >= 4.0.0
  595. //! @}
  596. //------------------------------------------------------------------------------
  597. #if defined(AVR32_USART_400_H_INCLUDED) || \
  598. defined(AVR32_USART_410_H_INCLUDED) || \
  599. defined(AVR32_USART_420_H_INCLUDED) || \
  600. defined(AVR32_USART_440_H_INCLUDED) || \
  601. defined(AVR32_USART_602_H_INCLUDED)
  602. /*! \name SPI Control Functions
  603. */
  604. //! @{
  605. int usart_spi_selectChip(volatile avr32_usart_t *usart)
  606. {
  607. // Force the SPI chip select.
  608. usart->cr = AVR32_USART_CR_RTSEN_MASK;
  609. return USART_SUCCESS;
  610. }
  611. int usart_spi_unselectChip(volatile avr32_usart_t *usart)
  612. {
  613. int timeout = USART_DEFAULT_TIMEOUT;
  614. do
  615. {
  616. if (!timeout--) return USART_FAILURE;
  617. } while (!usart_tx_empty(usart));
  618. // Release the SPI chip select.
  619. usart->cr = AVR32_USART_CR_RTSDIS_MASK;
  620. return USART_SUCCESS;
  621. }
  622. //! @}
  623. #endif // USART rev. >= 4.0.0
  624. //------------------------------------------------------------------------------
  625. /*! \name Transmit/Receive Functions
  626. */
  627. //! @{
  628. int usart_send_address(volatile avr32_usart_t *usart, int address)
  629. {
  630. // Check if USART is in multidrop / RS485 mode.
  631. if (!usart_mode_is_multidrop(usart)) return USART_MODE_FAULT;
  632. // Prepare to send an address.
  633. usart->cr = AVR32_USART_CR_SENDA_MASK;
  634. // Write the address to TX.
  635. usart_bw_write_char(usart, address);
  636. return USART_SUCCESS;
  637. }
  638. int usart_write_char(volatile avr32_usart_t *usart, int c)
  639. {
  640. if (usart_tx_ready(usart))
  641. {
  642. usart->thr = (c << AVR32_USART_THR_TXCHR_OFFSET) & AVR32_USART_THR_TXCHR_MASK;
  643. return USART_SUCCESS;
  644. }
  645. else
  646. return USART_TX_BUSY;
  647. }
  648. int usart_putchar(volatile avr32_usart_t *usart, int c)
  649. {
  650. int timeout = USART_DEFAULT_TIMEOUT;
  651. if (c == '\n')
  652. {
  653. do
  654. {
  655. if (!timeout--) return USART_FAILURE;
  656. } while (usart_write_char(usart, '\r') != USART_SUCCESS);
  657. timeout = USART_DEFAULT_TIMEOUT;
  658. }
  659. do
  660. {
  661. if (!timeout--) return USART_FAILURE;
  662. } while (usart_write_char(usart, c) != USART_SUCCESS);
  663. return USART_SUCCESS;
  664. }
  665. int usart_read_char(volatile avr32_usart_t *usart, int *c)
  666. {
  667. // Check for errors: frame, parity and overrun. In RS485 mode, a parity error
  668. // would mean that an address char has been received.
  669. if (usart->csr & (AVR32_USART_CSR_OVRE_MASK |
  670. AVR32_USART_CSR_FRAME_MASK |
  671. AVR32_USART_CSR_PARE_MASK))
  672. return USART_RX_ERROR;
  673. // No error; if we really did receive a char, read it and return SUCCESS.
  674. if (usart_test_hit(usart))
  675. {
  676. *c = (usart->rhr & AVR32_USART_RHR_RXCHR_MASK) >> AVR32_USART_RHR_RXCHR_OFFSET;
  677. return USART_SUCCESS;
  678. }
  679. else
  680. return USART_RX_EMPTY;
  681. }
  682. int usart_getchar(volatile avr32_usart_t *usart)
  683. {
  684. int c, ret;
  685. while ((ret = usart_read_char(usart, &c)) == USART_RX_EMPTY);
  686. if (ret == USART_RX_ERROR)
  687. return USART_FAILURE;
  688. return c;
  689. }
  690. void usart_write_line(volatile avr32_usart_t *usart, const char *string)
  691. {
  692. while (*string != '\0')
  693. usart_putchar(usart, *string++);
  694. }
  695. int usart_get_echo_line(volatile avr32_usart_t *usart)
  696. {
  697. int rx_char;
  698. int retval = USART_SUCCESS;
  699. while (1)
  700. {
  701. rx_char = usart_getchar(usart);
  702. if (rx_char == USART_FAILURE)
  703. {
  704. usart_write_line(usart, "Error!!!\n");
  705. retval = USART_FAILURE;
  706. break;
  707. }
  708. if (rx_char == '\x03')
  709. {
  710. retval = USART_FAILURE;
  711. break;
  712. }
  713. usart_putchar(usart, rx_char);
  714. if (rx_char == '\r')
  715. {
  716. usart_putchar(usart, '\n');
  717. break;
  718. }
  719. }
  720. return retval;
  721. }
  722. //! @}