drv_uart.c 6.0 KB

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