uart.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /**
  2. * \file
  3. *
  4. * \brief SAM UART Driver for SAMB11
  5. *
  6. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include "uart.h"
  47. /**
  48. * \internal
  49. * Internal driver device instance struct.
  50. */
  51. struct uart_module *_uart_instances[UART_INST_NUM];
  52. /**
  53. * \internal
  54. * Writes a character from the TX buffer to the Data register.
  55. *
  56. * \param[in,out] module Pointer to UART software instance struct
  57. */
  58. static void _uart_write(struct uart_module *const module)
  59. {
  60. /* Pointer to the hardware module instance */
  61. Uart *const uart_hw = module->hw;
  62. /* Write value will be at least 8-bits long */
  63. uint8_t data_to_send = *(module->tx_buffer_ptr);
  64. /* Increment 8-bit pointer */
  65. (module->tx_buffer_ptr)++;
  66. /* Write the data to send*/
  67. uart_hw->TRANSMIT_DATA.reg = data_to_send & UART_TRANSMIT_DATA_MASK;
  68. /* Decrement remaining buffer length */
  69. (module->remaining_tx_buffer_length)--;
  70. }
  71. /**
  72. * \internal
  73. * Reads a character from the Data register to the RX buffer.
  74. *
  75. * \param[in,out] module Pointer to UART software instance struct
  76. */
  77. static void _uart_read(
  78. struct uart_module *const module)
  79. {
  80. /* Pointer to the hardware module instance */
  81. Uart *const uart_hw = module->hw;
  82. uint16_t received_data = (uart_hw->RECEIVE_DATA.reg & UART_RECEIVE_DATA_MASK);
  83. /* Read value will be at least 8-bits long */
  84. *(module->rx_buffer_ptr) = received_data;
  85. /* Increment 8-bit pointer */
  86. module->rx_buffer_ptr += 1;
  87. /* Decrement length of the remaining buffer */
  88. module->remaining_rx_buffer_length--;
  89. }
  90. static void uart_rx0_isr_handler(void)
  91. {
  92. struct uart_module *module = _uart_instances[0];
  93. /* get interrupt flags and mask out enabled callbacks */
  94. uint32_t flags = module->hw->RECEIVE_STATUS.reg;
  95. if (flags & UART_RECEIVE_STATUS_FIFO_OVERRUN) {
  96. /* Store the error code */
  97. module->status = STATUS_ERR_OVERFLOW;
  98. /* Disable interrupt */
  99. module->hw->RX_INTERRUPT_MASK.reg &=
  100. ~(UART_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK |
  101. SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  102. if ((module->callback_enable_mask & (1 << UART_RX_FIFO_OVERRUN)) &&
  103. (module->callback_reg_mask & (1 << UART_RX_FIFO_OVERRUN))) {
  104. (module->callback[UART_RX_FIFO_OVERRUN])(module);
  105. }
  106. /* Flush */
  107. uint8_t flush = module->hw->RECEIVE_DATA.reg;
  108. UNUSED(flush);
  109. }
  110. if (flags & UART_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY) {
  111. _uart_read(module);
  112. if (module->remaining_rx_buffer_length == 0) {
  113. if ((module->callback_enable_mask & (1 << UART_RX_COMPLETE)) &&
  114. (module->callback_reg_mask & (1 << UART_RX_COMPLETE))) {
  115. module->status = STATUS_OK;
  116. module->hw->RX_INTERRUPT_MASK.reg &=
  117. ~(UART_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  118. (module->callback[UART_RX_COMPLETE])(module);
  119. }
  120. }
  121. }
  122. }
  123. static void uart_tx0_isr_handler(void)
  124. {
  125. struct uart_module *module = _uart_instances[0];
  126. /* get interrupt flags and mask out enabled callbacks */
  127. uint32_t flags = module->hw->TRANSMIT_STATUS.reg;
  128. if (flags & UART_TRANSMIT_STATUS_TX_FIFO_NOT_FULL) {
  129. _uart_write(module);
  130. if (module->remaining_tx_buffer_length == 0) {
  131. module->hw->TX_INTERRUPT_MASK.reg &=
  132. ~UART_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  133. module->hw->TX_INTERRUPT_MASK.reg |=
  134. UART_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  135. }
  136. }
  137. if (flags & UART_TRANSMIT_STATUS_TX_FIFO_EMPTY) {
  138. if ((module->callback_enable_mask & (1 << UART_TX_COMPLETE)) &&
  139. (module->callback_reg_mask & (1 << UART_TX_COMPLETE))) {
  140. module->status = STATUS_OK;
  141. /* Disable interrupt */
  142. module->hw->TX_INTERRUPT_MASK.reg &=
  143. ~UART_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  144. (module->callback[UART_TX_COMPLETE])(module);
  145. }
  146. }
  147. if (flags & UART_TRANSMIT_STATUS_CTS_ACTIVE) {
  148. if ((module->callback_enable_mask & (1 << UART_CTS_ACTIVE)) &&
  149. (module->callback_reg_mask & (1 << UART_CTS_ACTIVE))) {
  150. (module->callback[UART_CTS_ACTIVE])(module);
  151. }
  152. }
  153. }
  154. static void uart_rx1_isr_handler(void)
  155. {
  156. struct uart_module *module = _uart_instances[1];
  157. /* get interrupt flags and mask out enabled callbacks */
  158. uint32_t flags = module->hw->RECEIVE_STATUS.reg;
  159. if (flags & UART_RECEIVE_STATUS_FIFO_OVERRUN) {
  160. /* Store the error code */
  161. module->status = STATUS_ERR_OVERFLOW;
  162. /* Disable interrupt */
  163. module->hw->RX_INTERRUPT_MASK.reg &=
  164. ~(UART_RX_INTERRUPT_MASK_FIFO_OVERRUN_MASK |
  165. SPI_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  166. if ((module->callback_enable_mask & (1 << UART_RX_FIFO_OVERRUN)) &&
  167. (module->callback_reg_mask & (1 << UART_RX_FIFO_OVERRUN))) {
  168. (module->callback[UART_RX_FIFO_OVERRUN])(module);
  169. }
  170. /* Flush */
  171. uint8_t flush = module->hw->RECEIVE_DATA.reg;
  172. UNUSED(flush);
  173. }
  174. if (flags & UART_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY) {
  175. _uart_read(module);
  176. if (module->remaining_rx_buffer_length == 0) {
  177. if ((module->callback_enable_mask & (1 << UART_RX_COMPLETE)) &&
  178. (module->callback_reg_mask & (1 << UART_RX_COMPLETE))) {
  179. module->status = STATUS_OK;
  180. module->hw->RX_INTERRUPT_MASK.reg &=
  181. ~(UART_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK);
  182. (module->callback[UART_RX_COMPLETE])(module);
  183. }
  184. }
  185. }
  186. }
  187. static void uart_tx1_isr_handler(void)
  188. {
  189. struct uart_module *module = _uart_instances[1];
  190. /* get interrupt flags and mask out enabled callbacks */
  191. uint32_t flags = module->hw->TRANSMIT_STATUS.reg;
  192. if (flags & UART_TRANSMIT_STATUS_TX_FIFO_NOT_FULL) {
  193. _uart_write(module);
  194. if (module->remaining_tx_buffer_length == 0) {
  195. module->hw->TX_INTERRUPT_MASK.reg &=
  196. ~UART_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  197. module->hw->TX_INTERRUPT_MASK.reg |=
  198. UART_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  199. }
  200. }
  201. if (flags & UART_TRANSMIT_STATUS_TX_FIFO_EMPTY) {
  202. if ((module->callback_enable_mask & (1 << UART_TX_COMPLETE)) &&
  203. (module->callback_reg_mask & (1 << UART_TX_COMPLETE))) {
  204. module->status = STATUS_OK;
  205. /* Disable interrupt */
  206. module->hw->TX_INTERRUPT_MASK.reg &=
  207. ~UART_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  208. (module->callback[UART_TX_COMPLETE])(module);
  209. }
  210. }
  211. if (flags & UART_TRANSMIT_STATUS_CTS_ACTIVE) {
  212. if ((module->callback_enable_mask & (1 << UART_CTS_ACTIVE)) &&
  213. (module->callback_reg_mask & (1 << UART_CTS_ACTIVE))) {
  214. (module->callback[UART_CTS_ACTIVE])(module);
  215. }
  216. }
  217. }
  218. static void uart_set_baudrate(struct uart_module *const module,
  219. const uint32_t baud_rate)
  220. {
  221. uint32_t clock;
  222. uint16_t integerpart = 0;
  223. uint8_t fractionalpart = 0;
  224. uint32_t diff;
  225. uint8_t i = 0;
  226. clock = system_clock_get_value();
  227. integerpart = clock / baud_rate;
  228. diff = clock - (baud_rate * integerpart);
  229. i = 0;
  230. while(diff > (baud_rate / 16)) {
  231. i++;
  232. diff -= (baud_rate / 16);
  233. }
  234. fractionalpart = (i + 1) / 2;
  235. module->hw->UART_CLOCK_SOURCE.reg = UART_CLOCK_SOURCE_CLOCK_SELECT_0;
  236. module->hw->UART_BAUD_RATE.reg =
  237. UART_BAUD_RATE_INTEGER_DIVISION(integerpart) |
  238. UART_BAUD_RATE_FRACTIONAL_DIVISION(fractionalpart);
  239. }
  240. /**
  241. * \brief Gets the UART default configurations
  242. *
  243. * Use to initialize the configuration structure to known default values.
  244. *
  245. * The default configuration is as follows:
  246. * - Baudrate 115200
  247. * - parity UART_NO_PARITY
  248. * - flow_control 0 - No Flow control
  249. * - stop_bits 1 - 1 stop bit
  250. * - pinmux_pad[] - Pinmux default are UART0.
  251. *
  252. * \param[out] config Pointer to configuration structure to be initiated
  253. */
  254. void uart_get_config_defaults(
  255. struct uart_config *const config)
  256. {
  257. config->baud_rate = 115200;
  258. config->data_bits = UART_8_BITS;
  259. config->stop_bits = UART_1_STOP_BIT;
  260. config->parity = UART_NO_PARITY;
  261. config->flow_control = false;
  262. config->pin_number_pad[0] = PIN_LP_GPIO_2;
  263. config->pin_number_pad[1] = PIN_LP_GPIO_3;
  264. config->pin_number_pad[2] = PIN_LP_GPIO_4;
  265. config->pin_number_pad[3] = PIN_LP_GPIO_5;
  266. config->pinmux_sel_pad[0] = MUX_LP_GPIO_2_UART0_RXD;
  267. config->pinmux_sel_pad[1] = MUX_LP_GPIO_3_UART0_TXD;
  268. config->pinmux_sel_pad[2] = MUX_LP_GPIO_4_UART0_CTS;
  269. config->pinmux_sel_pad[3] = MUX_LP_GPIO_5_UART0_RTS;
  270. }
  271. /**
  272. * \brief Initializes the device
  273. *
  274. * Initializes the UART device based on the setting specified in the
  275. * configuration struct.
  276. *
  277. * \param[in] module enumeration UART hw module
  278. * \param[in] hw Pointer to USART hardware instance
  279. * \param[in] config Pointer to configuration struct
  280. *
  281. * \return Status of the initialization.
  282. *
  283. * \retval STATUS_OK The initialization was successful
  284. */
  285. enum status_code uart_init(struct uart_module *const module, Uart * const hw,
  286. const struct uart_config *const config)
  287. {
  288. /* Sanity check arguments */
  289. Assert(module);
  290. Assert(hw);
  291. Assert(config);
  292. uint8_t config_temp = 0;
  293. uint8_t i,index;
  294. /* Assign module pointer to software instance struct */
  295. module->hw = hw;
  296. for (i = 0; i < UART_CALLBACK_N; i++) {
  297. module->callback[i] = NULL;
  298. }
  299. module->rx_buffer_ptr = NULL;
  300. module->tx_buffer_ptr = NULL;
  301. module->remaining_rx_buffer_length = 0;
  302. module->remaining_tx_buffer_length = 0;
  303. module->callback_reg_mask = 0;
  304. module->callback_enable_mask = 0;
  305. module->status = STATUS_OK;
  306. if (hw == UART0) {
  307. system_peripheral_reset(PERIPHERAL_UART0_CORE);
  308. system_peripheral_reset(PERIPHERAL_UART0_IF);
  309. system_clock_peripheral_enable(PERIPHERAL_UART0_CORE);
  310. system_clock_peripheral_enable(PERIPHERAL_UART0_IF);
  311. _uart_instances[0] = module;
  312. system_register_isr(RAM_ISR_TABLE_UARTRX0_INDEX, (uint32_t)uart_rx0_isr_handler);
  313. system_register_isr(RAM_ISR_TABLE_UARTTX0_INDEX, (uint32_t)uart_tx0_isr_handler);
  314. NVIC_EnableIRQ(UART0_RX_IRQn);
  315. NVIC_EnableIRQ(UART0_TX_IRQn);
  316. } else if (hw == UART1) {
  317. system_peripheral_reset(PERIPHERAL_UART1_CORE);
  318. system_peripheral_reset(PERIPHERAL_UART1_IF);
  319. system_clock_peripheral_enable(PERIPHERAL_UART1_CORE);
  320. system_clock_peripheral_enable(PERIPHERAL_UART1_IF);
  321. _uart_instances[1] = module;
  322. system_register_isr(RAM_ISR_TABLE_UARTRX1_INDEX, (uint32_t)uart_rx1_isr_handler);
  323. system_register_isr(RAM_ISR_TABLE_UARTTX1_INDEX, (uint32_t)uart_tx1_isr_handler);
  324. NVIC_EnableIRQ(UART1_RX_IRQn);
  325. NVIC_EnableIRQ(UART1_TX_IRQn);
  326. }
  327. /* Set the pinmux for this UART module. */
  328. if(config->flow_control) {
  329. index = 4;
  330. } else {
  331. index = 2;
  332. }
  333. #if (BTLC1000)
  334. index = 2; /* BTLC1000 has no flow control function. */
  335. #endif
  336. for(i = 0; i < index; i++) {
  337. gpio_pinmux_cofiguration(config->pin_number_pad[i], \
  338. (uint16_t)(config->pinmux_sel_pad[i]));
  339. }
  340. /* empty UART FIFO */
  341. while (module->hw->RECEIVE_STATUS.reg & UART_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY) {
  342. i = module->hw->RECEIVE_DATA.reg;
  343. }
  344. /* reset configuration register */
  345. module->hw->UART_CONFIGURATION.reg = 0;
  346. /* program the uart configuration. */
  347. if(config->flow_control) {
  348. config_temp |= UART_CONFIGURATION_CTS_ENABLE_1;
  349. }
  350. config_temp |= config->data_bits;
  351. config_temp |= config->stop_bits;
  352. switch(config->parity) {
  353. case UART_NO_PARITY:
  354. config_temp |= UART_CONFIGURATION_PARITY_ENABLE_0;
  355. break;
  356. case UART_EVEN_PARITY:
  357. config_temp |= UART_CONFIGURATION_PARITY_ENABLE_1;
  358. config_temp |= UART_CONFIGURATION_PARITY_MODE_0;
  359. break;
  360. case UART_ODD_PARITY:
  361. config_temp |= UART_CONFIGURATION_PARITY_ENABLE_1;
  362. config_temp |= UART_CONFIGURATION_PARITY_MODE_1;
  363. break;
  364. case UART_SPACE_PARITY:
  365. config_temp |= UART_CONFIGURATION_PARITY_ENABLE_1;
  366. config_temp |= UART_CONFIGURATION_PARITY_MODE_2;
  367. break;
  368. case UART_MARK_PARITY:
  369. config_temp |= UART_CONFIGURATION_PARITY_ENABLE_1;
  370. config_temp |= UART_CONFIGURATION_PARITY_MODE_3;
  371. break;
  372. default:
  373. break;
  374. }
  375. module->hw->UART_CONFIGURATION.reg = config_temp;
  376. /* Calculate the baud rate. */
  377. uart_set_baudrate(module, config->baud_rate);
  378. module->hw->RX_INTERRUPT_MASK.reg = 0; // disable int at initialization, enable it at read time
  379. module->hw->TX_INTERRUPT_MASK.reg = 0; // disable int at initialization, enable it at write time
  380. return STATUS_OK;
  381. }
  382. /**
  383. * \brief Transmit a character via the UART
  384. *
  385. * This blocking function will transmit a single character via the
  386. * UART.
  387. *
  388. * \param[in] module enumeration UART hw module
  389. * \param[in] tx_data Data to transfer
  390. *
  391. * \return Status of the operation.
  392. * \retval STATUS_OK If the operation was completed
  393. */
  394. enum status_code uart_write_wait(struct uart_module *const module,
  395. const uint8_t tx_data)
  396. {
  397. while (!(module->hw->TRANSMIT_STATUS.reg & UART_TRANSMIT_STATUS_TX_FIFO_NOT_FULL));
  398. module->hw->TRANSMIT_DATA.reg = tx_data;
  399. return STATUS_OK;
  400. }
  401. /**
  402. * \brief Receive a character via the UART
  403. *
  404. * This blocking function will receive a character via the UART.
  405. *
  406. * \param[in] module enumeration UART hw module
  407. * \param[out] rx_data Pointer to received data
  408. *
  409. * \return Status of the operation.
  410. * \retval STATUS_OK If the operation was completed
  411. */
  412. enum status_code uart_read_wait(struct uart_module *const module,
  413. uint8_t *const rx_data)
  414. {
  415. while (!(module->hw->RECEIVE_STATUS.reg & UART_RECEIVE_STATUS_RX_FIFO_NOT_EMPTY));
  416. *rx_data = module->hw->RECEIVE_DATA.reg;
  417. return STATUS_OK;
  418. }
  419. /**
  420. * \brief Transmit a buffer of characters via the UART
  421. *
  422. * This blocking function will transmit a block of \c length characters
  423. * via the UART.
  424. *
  425. * \note Using this function in combination with the interrupt (\c _job) functions is
  426. * not recommended as it has no functionality to check if there is an
  427. * ongoing interrupt driven operation running or not.
  428. *
  429. * \param[in] module enumeration UART hw module
  430. * \param[in] tx_data Pointer to data to transmit
  431. * \param[in] length Number of characters to transmit
  432. *
  433. * \return Status of the operation.
  434. * \retval STATUS_OK If operation was completed
  435. */
  436. enum status_code uart_write_buffer_wait(struct uart_module *const module,
  437. const uint8_t *tx_data, uint32_t length)
  438. {
  439. while(length--)
  440. uart_write_wait(module, *tx_data++);
  441. return STATUS_OK;
  442. }
  443. /**
  444. * \brief Receive a buffer of \c length characters via the UART
  445. *
  446. * This blocking function will receive a block of \c length characters
  447. * via the UART.
  448. *
  449. * \note Using this function in combination with the interrupt (\c *_job)
  450. * functions is not recommended as it has no functionality to check if
  451. * there is an ongoing interrupt driven operation running or not.
  452. *
  453. * \param[in] module enumeration UART hw module
  454. * \param[out] rx_data Pointer to receive buffer
  455. * \param[in] length Number of characters to receive
  456. *
  457. * \return Status of the operation.
  458. * \retval STATUS_OK If operation was completed
  459. */
  460. enum status_code uart_read_buffer_wait(struct uart_module *const module,
  461. uint8_t *rx_data, uint16_t length)
  462. {
  463. while(length--)
  464. uart_read_wait(module, rx_data++);
  465. return STATUS_OK;
  466. }
  467. /**
  468. * \internal
  469. * Starts write of a buffer with a given length
  470. *
  471. * \param[in] module Pointer to UART software instance struct
  472. * \param[in] tx_data Pointer to data to be transmitted
  473. * \param[in] length Length of data buffer
  474. *
  475. */
  476. static void _uart_write_buffer(
  477. struct uart_module *const module,
  478. uint8_t *tx_data,
  479. uint16_t length)
  480. {
  481. Assert(module);
  482. Assert(tx_data);
  483. /* Write parameters to the device instance */
  484. module->remaining_tx_buffer_length = length;
  485. module->tx_buffer_ptr = tx_data;
  486. module->status = STATUS_BUSY;
  487. module->hw->TX_INTERRUPT_MASK.reg = UART_TX_INTERRUPT_MASK_TX_FIFO_NOT_FULL_MASK;
  488. }
  489. /**
  490. * \internal
  491. * Setup UART to read a buffer with a given length
  492. *
  493. * \param[in] module Pointer to UART software instance struct
  494. * \param[in] rx_data Pointer to data to be received
  495. * \param[in] length Length of data buffer
  496. *
  497. */
  498. static void _uart_read_buffer(
  499. struct uart_module *const module,
  500. uint8_t *rx_data,
  501. uint16_t length)
  502. {
  503. Assert(module);
  504. Assert(rx_data);
  505. /* Set length for the buffer and the pointer, and let
  506. * the interrupt handler do the rest */
  507. module->remaining_rx_buffer_length = length;
  508. module->rx_buffer_ptr = rx_data;
  509. module->status = STATUS_BUSY;
  510. module->hw->RX_INTERRUPT_MASK.reg = UART_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK;
  511. }
  512. /**
  513. * \brief Asynchronous buffer write
  514. *
  515. * Sets up the driver to write to the UART from a given buffer. If registered
  516. * and enabled, a callback function will be called when the write is finished.
  517. *
  518. * \param[in] module Pointer to UART software instance struct
  519. * \param[out] tx_data Pointer to data buffer to receive
  520. * \param[in] length Data buffer length
  521. *
  522. * \returns Status of the write request operation.
  523. * \retval STATUS_OK If the operation completed successfully
  524. * \retval STATUS_ERR_BUSY If the UART was already busy with a write
  525. * operation
  526. * \retval STATUS_ERR_INVALID_ARG If requested write length was zero
  527. */
  528. enum status_code uart_write_buffer_job(struct uart_module *const module,
  529. uint8_t *tx_data, uint32_t length)
  530. {
  531. Assert(module);
  532. Assert(tx_data);
  533. if (length == 0) {
  534. return STATUS_ERR_INVALID_ARG;
  535. }
  536. /* Check if the UART is busy transmitting or slave waiting for TXC*/
  537. if (module->status == STATUS_BUSY) {
  538. return STATUS_BUSY;
  539. }
  540. /* Issue internal write */
  541. _uart_write_buffer(module, tx_data, length);
  542. return STATUS_OK;
  543. }
  544. /**
  545. * \brief Asynchronous buffer read
  546. *
  547. * Sets up the driver to read from the UART to a given buffer. If registered
  548. * and enabled, a callback function will be called when the read is finished.
  549. *
  550. * \note If address matching is enabled for the slave, the first character
  551. * received and placed in the RX buffer will be the address.
  552. *
  553. * \param[in] module Pointer to UART software instance struct
  554. * \param[out] rx_data Pointer to data buffer to receive
  555. * \param[in] length Data buffer length
  556. * \param[in] dummy Dummy character to send when reading in master mode
  557. *
  558. * \returns Status of the operation.
  559. * \retval STATUS_OK If the operation completed successfully
  560. * \retval STATUS_ERR_BUSY If the UART was already busy with a read
  561. * operation
  562. * \retval STATUS_ERR_DENIED If the receiver is not enabled
  563. * \retval STATUS_ERR_INVALID_ARG If requested read length was zero
  564. */
  565. enum status_code uart_read_buffer_job(struct uart_module *const module,
  566. uint8_t *rx_data, uint16_t length)
  567. {
  568. /* Sanity check arguments */
  569. Assert(module);
  570. Assert(rx_data);
  571. if (length == 0) {
  572. return STATUS_ERR_INVALID_ARG;
  573. }
  574. /* Check if the UART is busy transmitting or slave waiting for TXC*/
  575. if (module->status == STATUS_BUSY) {
  576. return STATUS_BUSY;
  577. }
  578. /* Issue internal read */
  579. _uart_read_buffer(module, rx_data, length);
  580. return STATUS_OK;
  581. }
  582. /**
  583. * \brief Registers a callback
  584. *
  585. * Registers a callback function which is implemented by the user.
  586. *
  587. * \note The callback must be enabled by \ref uart_enable_callback,
  588. * in order for the interrupt handler to call it when the conditions for
  589. * the callback type are met.
  590. *
  591. * \param[in] module Pointer to UART software instance struct
  592. * \param[in] callback_func Pointer to callback function
  593. * \param[in] callback_type Callback type given by an enum
  594. *
  595. */
  596. void uart_register_callback(struct uart_module *const module,
  597. uart_callback_t callback_func,
  598. enum uart_callback callback_type)
  599. {
  600. /* Sanity check arguments */
  601. Assert(module);
  602. Assert(callback_func);
  603. /* Register callback function */
  604. module->callback[callback_type] = callback_func;
  605. /* Set the bit corresponding to the callback_type */
  606. module->callback_reg_mask |= (1 << callback_type);
  607. }
  608. /**
  609. * \brief Unregisters a callback
  610. *
  611. * Unregisters a callback function which is implemented by the user.
  612. *
  613. * \param[in,out] module Pointer to UART software instance struct
  614. * \param[in] callback_type Callback type given by an enum
  615. *
  616. */
  617. void uart_unregister_callback(struct uart_module *module,
  618. enum uart_callback callback_type)
  619. {
  620. /* Sanity check arguments */
  621. Assert(module);
  622. /* Unregister callback function */
  623. module->callback[callback_type] = NULL;
  624. /* Clear the bit corresponding to the callback_type */
  625. module->callback_reg_mask &= ~(1 << callback_type);
  626. }
  627. /**
  628. * \brief Enables callback
  629. *
  630. * Enables the callback function registered by the \ref usart_register_callback.
  631. * The callback function will be called from the interrupt handler when the
  632. * conditions for the callback type are met.
  633. *
  634. * \param[in] module Pointer to UART software instance struct
  635. * \param[in] callback_type Callback type given by an enum
  636. */
  637. void uart_enable_callback(struct uart_module *const module,
  638. enum uart_callback callback_type)
  639. {
  640. /* Sanity check arguments */
  641. Assert(module);
  642. /* Enable callback */
  643. module->callback_enable_mask |= (1 << callback_type);
  644. if (callback_type == UART_CTS_ACTIVE) {
  645. module->hw->TX_INTERRUPT_MASK.reg |= UART_TX_INTERRUPT_MASK_CTS_ACTIVE_MASK;
  646. }
  647. }
  648. /**
  649. * \brief Disable callback
  650. *
  651. * Disables the callback function registered by the \ref usart_register_callback,
  652. * and the callback will not be called from the interrupt routine.
  653. *
  654. * \param[in] module Pointer to UART software instance struct
  655. * \param[in] callback_type Callback type given by an enum
  656. */
  657. void uart_disable_callback(struct uart_module *const module,
  658. enum uart_callback callback_type)
  659. {
  660. /* Sanity check arguments */
  661. Assert(module);
  662. /* Disable callback */
  663. module->callback_enable_mask &= ~(1 << callback_type);
  664. if (callback_type == UART_CTS_ACTIVE) {
  665. module->hw->TX_INTERRUPT_MASK.reg &= ~UART_TX_INTERRUPT_MASK_CTS_ACTIVE_MASK;
  666. }
  667. }
  668. /**
  669. * \brief Enables UART transmit DMA
  670. *
  671. * \param[in] module Pointer to UART software instance struct
  672. */
  673. void uart_enable_transmit_dma(struct uart_module *const module)
  674. {
  675. /* Sanity check arguments */
  676. Assert(module);
  677. /* DMA need the interrupt signal to trigger */
  678. module->hw->TX_INTERRUPT_MASK.reg |= UART_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  679. /* Disable NVIC to avoid trigger the CPU interrupt */
  680. if (module->hw == UART0) {
  681. NVIC_DisableIRQ(UART0_TX_IRQn);
  682. } else if (module->hw == UART1) {
  683. NVIC_DisableIRQ(UART1_TX_IRQn);
  684. }
  685. }
  686. /**
  687. * \brief Disables UART transmit DMA
  688. *
  689. * \param[in] module Pointer to UART software instance struct
  690. */
  691. void uart_disable_transmit_dma(struct uart_module *const module)
  692. {
  693. /* Sanity check arguments */
  694. Assert(module);
  695. module->hw->TX_INTERRUPT_MASK.reg &= ~UART_TX_INTERRUPT_MASK_TX_FIFO_EMPTY_MASK;
  696. /* Enable NVIC to restore the callback functions */
  697. if (module->hw == UART0) {
  698. NVIC_EnableIRQ(UART0_TX_IRQn);
  699. } else if (module->hw == UART1) {
  700. NVIC_EnableIRQ(UART1_TX_IRQn);
  701. }
  702. }
  703. /**
  704. * \brief Enables UART receive DMA
  705. *
  706. * \param[in] module Pointer to UART software instance struct
  707. */
  708. void uart_enable_receive_dma(struct uart_module *const module)
  709. {
  710. /* Sanity check arguments */
  711. Assert(module);
  712. /* DMA need the interrupt signal to trigger */
  713. module->hw->RX_INTERRUPT_MASK.reg |= UART_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK;
  714. /* Disable NVIC to avoid trigger the CPU interrupt */
  715. if (module->hw == UART0) {
  716. NVIC_DisableIRQ(UART0_TX_IRQn);
  717. } else if (module->hw == UART1) {
  718. NVIC_DisableIRQ(UART1_TX_IRQn);
  719. }
  720. }
  721. /**
  722. * \brief Disables UART receive DMA
  723. *
  724. * \param[in] module Pointer to UART software instance struct
  725. */
  726. void uart_disable_receive_dma(struct uart_module *const module)
  727. {
  728. /* Sanity check arguments */
  729. Assert(module);
  730. module->hw->RX_INTERRUPT_MASK.reg &= ~UART_RX_INTERRUPT_MASK_RX_FIFO_NOT_EMPTY_MASK;
  731. /* Enable NVIC to restore the callback functions */
  732. if (module->hw == UART0) {
  733. NVIC_EnableIRQ(UART0_TX_IRQn);
  734. } else if (module->hw == UART1) {
  735. NVIC_EnableIRQ(UART1_TX_IRQn);
  736. }
  737. }