drv_uart.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-04-28 xckhmf Modify for <nrfx>
  9. * 2021-06-26 supperthomas fix rt_hw_uart_init
  10. *
  11. */
  12. #include <rtdevice.h>
  13. #include <nrfx_uart.h>
  14. #include "drv_uart.h"
  15. #ifdef BSP_USING_UART
  16. typedef struct
  17. {
  18. struct rt_serial_device *serial;
  19. nrfx_uart_t uart;
  20. uint8_t rx_byte;
  21. uint16_t rx_length;
  22. uint32_t rx_pin;
  23. uint32_t tx_pin;
  24. nrfx_uart_event_handler_t event_handler;
  25. } drv_uart_cfg_t;
  26. #ifdef BSP_USING_UART0
  27. static struct rt_serial_device _serial_0;
  28. static void uart0_event_hander(nrfx_uart_event_t const *p_event,void *p_context);
  29. drv_uart_cfg_t m_uart0_cfg = {
  30. .uart = NRFX_UART_INSTANCE(0),
  31. .rx_byte = 0,
  32. .rx_length = 0,
  33. .rx_pin = BSP_UART0_RX_PIN,
  34. .tx_pin = BSP_UART0_TX_PIN,
  35. .event_handler = uart0_event_hander
  36. };
  37. #endif /* BSP_USING_UART0 */
  38. #ifdef BSP_USING_UART1
  39. #error <nrfx_uart> not support UART1. Use UART0 instead.
  40. #endif /* BSP_USING_UART1 */
  41. #ifdef BSP_USING_UART0
  42. static void uart0_event_hander(nrfx_uart_event_t const *p_event,void *p_context)
  43. {
  44. if (p_event->type == NRFX_UART_EVT_RX_DONE)
  45. {
  46. if(p_event->data.rxtx.bytes == 1)
  47. {
  48. m_uart0_cfg.rx_length = p_event->data.rxtx.bytes;
  49. /* rx_byte equal p_data */
  50. //m_uart0_cfg.rx_byte = *(p_event->data.rxtx.p_data);
  51. rt_hw_serial_isr(m_uart0_cfg.serial, RT_SERIAL_EVENT_RX_IND);
  52. }
  53. nrfx_uart_rx(&(m_uart0_cfg.uart),&m_uart0_cfg.rx_byte,1);
  54. }
  55. if (p_event->type == NRFX_UART_EVT_TX_DONE)
  56. {
  57. /* @TODO:[RT_DEVICE_FLAG_INT_TX]*/
  58. }
  59. }
  60. #endif /* BSP_USING_UART0 */
  61. static rt_err_t _uart_cfg(struct rt_serial_device *serial, struct serial_configure *cfg)
  62. {
  63. nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG(BSP_UART0_TX_PIN,BSP_UART0_RX_PIN);
  64. drv_uart_cfg_t *instance = RT_NULL;
  65. RT_ASSERT(serial != RT_NULL);
  66. RT_ASSERT(cfg != RT_NULL);
  67. if (serial->parent.user_data == RT_NULL)
  68. {
  69. return -RT_ERROR;
  70. }
  71. instance = (drv_uart_cfg_t*)serial->parent.user_data;
  72. nrfx_uart_uninit(&(instance->uart));
  73. switch (cfg->baud_rate)
  74. {
  75. case 115200:
  76. config.baudrate = NRF_UART_BAUDRATE_115200;
  77. break;
  78. case 9600:
  79. config.baudrate = NRF_UART_BAUDRATE_9600;
  80. break;
  81. default:
  82. config.baudrate = NRF_UART_BAUDRATE_115200;
  83. break;
  84. }
  85. if (cfg->parity == PARITY_NONE)
  86. {
  87. config.hal_cfg.parity = NRF_UART_PARITY_EXCLUDED;
  88. }
  89. else
  90. {
  91. config.hal_cfg.parity = NRF_UART_PARITY_INCLUDED;
  92. }
  93. config.hal_cfg.hwfc = NRF_UART_HWFC_DISABLED;
  94. config.pselrxd = instance->rx_pin;
  95. config.pseltxd = instance->tx_pin;
  96. nrfx_uart_init(&(instance->uart), &config, instance->event_handler);
  97. nrfx_uart_rx(&(instance->uart),&(instance->rx_byte),1);
  98. nrf_uart_int_disable(instance->uart.p_reg, NRF_UART_INT_MASK_TXDRDY);
  99. return RT_EOK;
  100. }
  101. static rt_err_t _uart_ctrl(struct rt_serial_device *serial, int cmd, void *arg)
  102. {
  103. drv_uart_cfg_t *instance = NULL;
  104. RT_ASSERT(serial != RT_NULL);
  105. if (serial->parent.user_data == RT_NULL)
  106. {
  107. return -RT_ERROR;
  108. }
  109. instance = (drv_uart_cfg_t*)serial->parent.user_data;
  110. switch (cmd)
  111. {
  112. /* disable interrupt */
  113. case RT_DEVICE_CTRL_CLR_INT:
  114. break;
  115. /* enable interrupt */
  116. case RT_DEVICE_CTRL_SET_INT:
  117. break;
  118. case RT_DEVICE_CTRL_CUSTOM:
  119. if ((rt_uint32_t)(arg) == UART_CONFIG_BAUD_RATE_9600)
  120. {
  121. instance->serial->config.baud_rate = 9600;
  122. }
  123. else if ((rt_uint32_t)(arg) == UART_CONFIG_BAUD_RATE_115200)
  124. {
  125. instance->serial->config.baud_rate = 115200;
  126. }
  127. _uart_cfg(instance->serial, &(instance->serial->config));
  128. break;
  129. case RT_DEVICE_CTRL_PIN:
  130. _uart_cfg(instance->serial, &(instance->serial->config));
  131. break;
  132. case RT_DEVICE_POWERSAVE:
  133. nrfx_uart_uninit(&(instance->uart));
  134. break;
  135. case RT_DEVICE_WAKEUP:
  136. _uart_cfg(instance->serial, &(instance->serial->config));
  137. break;
  138. default:
  139. return RT_ERROR;
  140. }
  141. return RT_EOK;
  142. }
  143. rt_weak int uart_putc_hook(rt_uint8_t *ch)
  144. {
  145. return -1;
  146. }
  147. static int _uart_putc(struct rt_serial_device *serial, char c)
  148. {
  149. drv_uart_cfg_t *instance = NULL;
  150. int rtn = 1;
  151. RT_ASSERT(serial != RT_NULL);
  152. if (serial->parent.user_data != RT_NULL)
  153. {
  154. instance = (drv_uart_cfg_t*)serial->parent.user_data;
  155. }
  156. nrf_uart_event_clear(instance->uart.p_reg, NRF_UART_EVENT_TXDRDY);
  157. nrf_uart_task_trigger(instance->uart.p_reg, NRF_UART_TASK_STARTTX);
  158. nrf_uart_txd_set(instance->uart.p_reg, (uint8_t)c);
  159. uart_putc_hook((rt_uint8_t *)&c);
  160. while (!nrf_uart_event_check(instance->uart.p_reg, NRF_UART_EVENT_TXDRDY))
  161. {
  162. //wait for TXD send
  163. }
  164. return rtn;
  165. }
  166. rt_weak int uart_getc_hook(rt_uint8_t *ch)
  167. {
  168. return -1;
  169. };
  170. static int _uart_getc(struct rt_serial_device *serial)
  171. {
  172. int ch = -1;
  173. drv_uart_cfg_t *instance = NULL;
  174. RT_ASSERT(serial != RT_NULL);
  175. if (serial->parent.user_data != RT_NULL)
  176. {
  177. instance = (drv_uart_cfg_t*)serial->parent.user_data;
  178. }
  179. if(instance->rx_length)
  180. {
  181. ch = instance->rx_byte;
  182. instance->rx_length--;
  183. }
  184. if (-1 != ch)
  185. {
  186. return ch;
  187. }
  188. else
  189. {
  190. if (-1 == uart_getc_hook((rt_uint8_t *)&ch))
  191. {
  192. return -1;
  193. }
  194. else
  195. {
  196. return ch;
  197. }
  198. }
  199. }
  200. static struct rt_uart_ops _uart_ops = {
  201. _uart_cfg,
  202. _uart_ctrl,
  203. _uart_putc,
  204. _uart_getc
  205. };
  206. int rt_hw_uart_init(void)
  207. {
  208. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  209. rt_err_t result = RT_EOK;
  210. #ifdef BSP_USING_UART0
  211. _serial_0.config = config;
  212. _serial_0.ops = &_uart_ops;
  213. m_uart0_cfg.serial = &_serial_0;
  214. result = rt_hw_serial_register(&_serial_0, "uart0", \
  215. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, &m_uart0_cfg);
  216. #endif /* BSP_USING_UART0 */
  217. return result;
  218. }
  219. #endif /* BSP_USING_UART */