usart.c 27 KB

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