serial.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2009-02-05 Bernard first version
  9. * 2009-10-25 Bernard fix rt_serial_read bug when there is no data
  10. * in the buffer.
  11. * 2010-03-29 Bernard cleanup code.
  12. */
  13. #include "serial.h"
  14. #include <stm32f2xx_dma.h>
  15. #include <stm32f2xx_usart.h>
  16. static void rt_serial_enable_dma(DMA_Stream_TypeDef* dma_channel,
  17. rt_uint32_t address, rt_uint32_t size);
  18. /**
  19. * @addtogroup STM32
  20. */
  21. /*@{*/
  22. /* RT-Thread Device Interface */
  23. static rt_err_t rt_serial_init (rt_device_t dev)
  24. {
  25. struct stm32_serial_device* uart = (struct stm32_serial_device*) dev->user_data;
  26. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  27. {
  28. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  29. {
  30. rt_memset(uart->int_rx->rx_buffer, 0,
  31. sizeof(uart->int_rx->rx_buffer));
  32. uart->int_rx->read_index = 0;
  33. uart->int_rx->save_index = 0;
  34. }
  35. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  36. {
  37. RT_ASSERT(uart->dma_tx->dma_channel != RT_NULL);
  38. uart->dma_tx->list_head = uart->dma_tx->list_tail = RT_NULL;
  39. /* init data node memory pool */
  40. rt_mp_init(&(uart->dma_tx->data_node_mp), "dn",
  41. uart->dma_tx->data_node_mem_pool,
  42. sizeof(uart->dma_tx->data_node_mem_pool),
  43. sizeof(struct stm32_serial_data_node));
  44. }
  45. /* Enable USART */
  46. USART_Cmd(uart->uart_device, ENABLE);
  47. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  48. }
  49. return RT_EOK;
  50. }
  51. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  52. {
  53. return RT_EOK;
  54. }
  55. static rt_err_t rt_serial_close(rt_device_t dev)
  56. {
  57. return RT_EOK;
  58. }
  59. static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  60. {
  61. rt_uint8_t* ptr;
  62. rt_err_t err_code;
  63. struct stm32_serial_device* uart;
  64. ptr = buffer;
  65. err_code = RT_EOK;
  66. uart = (struct stm32_serial_device*)dev->user_data;
  67. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  68. {
  69. /* interrupt mode Rx */
  70. while (size)
  71. {
  72. rt_base_t level;
  73. /* disable interrupt */
  74. level = rt_hw_interrupt_disable();
  75. if (uart->int_rx->read_index != uart->int_rx->save_index)
  76. {
  77. /* read a character */
  78. *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index];
  79. size--;
  80. /* move to next position */
  81. uart->int_rx->read_index ++;
  82. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  83. uart->int_rx->read_index = 0;
  84. }
  85. else
  86. {
  87. /* set error code */
  88. err_code = -RT_EEMPTY;
  89. /* enable interrupt */
  90. rt_hw_interrupt_enable(level);
  91. break;
  92. }
  93. /* enable interrupt */
  94. rt_hw_interrupt_enable(level);
  95. }
  96. }
  97. else
  98. {
  99. /* polling mode */
  100. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  101. {
  102. while (uart->uart_device->SR & USART_FLAG_RXNE)
  103. {
  104. *ptr = uart->uart_device->DR & 0xff;
  105. ptr ++;
  106. }
  107. }
  108. }
  109. /* set error code */
  110. rt_set_errno(err_code);
  111. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  112. }
  113. static void rt_serial_enable_dma(DMA_Stream_TypeDef* dma_channel,
  114. rt_uint32_t address, rt_uint32_t size)
  115. {
  116. RT_ASSERT(dma_channel != RT_NULL);
  117. /* disable DMA */
  118. DMA_Cmd(dma_channel, DISABLE);
  119. /* set buffer address */
  120. dma_channel->M0AR = address;
  121. /* set size */
  122. dma_channel->NDTR = size;
  123. /* enable DMA */
  124. DMA_Cmd(dma_channel, ENABLE);
  125. }
  126. static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  127. {
  128. rt_uint8_t* ptr;
  129. rt_err_t err_code;
  130. struct stm32_serial_device* uart;
  131. err_code = RT_EOK;
  132. ptr = (rt_uint8_t*)buffer;
  133. uart = (struct stm32_serial_device*)dev->user_data;
  134. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  135. {
  136. /* interrupt mode Tx, does not support */
  137. RT_ASSERT(0);
  138. }
  139. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  140. {
  141. /* DMA mode Tx */
  142. /* allocate a data node */
  143. struct stm32_serial_data_node* data_node = (struct stm32_serial_data_node*)
  144. rt_mp_alloc (&(uart->dma_tx->data_node_mp), RT_WAITING_FOREVER);
  145. if (data_node == RT_NULL)
  146. {
  147. /* set error code */
  148. err_code = -RT_ENOMEM;
  149. }
  150. else
  151. {
  152. rt_uint32_t level;
  153. /* fill data node */
  154. data_node->data_ptr = ptr;
  155. data_node->data_size = size;
  156. /* insert to data link */
  157. data_node->next = RT_NULL;
  158. /* disable interrupt */
  159. level = rt_hw_interrupt_disable();
  160. data_node->prev = uart->dma_tx->list_tail;
  161. if (uart->dma_tx->list_tail != RT_NULL)
  162. uart->dma_tx->list_tail->next = data_node;
  163. uart->dma_tx->list_tail = data_node;
  164. if (uart->dma_tx->list_head == RT_NULL)
  165. {
  166. /* start DMA to transmit data */
  167. uart->dma_tx->list_head = data_node;
  168. /* Enable DMA Channel */
  169. rt_serial_enable_dma(uart->dma_tx->dma_channel,
  170. (rt_uint32_t)uart->dma_tx->list_head->data_ptr,
  171. uart->dma_tx->list_head->data_size);
  172. }
  173. /* enable interrupt */
  174. rt_hw_interrupt_enable(level);
  175. }
  176. }
  177. else
  178. {
  179. /* polling mode */
  180. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  181. {
  182. /* stream mode */
  183. while (size)
  184. {
  185. if (*ptr == '\n')
  186. {
  187. while (!(uart->uart_device->SR & USART_FLAG_TXE));
  188. uart->uart_device->DR = '\r';
  189. }
  190. while (!(uart->uart_device->SR & USART_FLAG_TXE));
  191. uart->uart_device->DR = (*ptr & 0x1FF);
  192. ++ptr; --size;
  193. }
  194. }
  195. else
  196. {
  197. /* write data directly */
  198. while (size)
  199. {
  200. while (!(uart->uart_device->SR & USART_FLAG_TXE));
  201. uart->uart_device->DR = (*ptr & 0x1FF);
  202. ++ptr; --size;
  203. }
  204. }
  205. }
  206. /* set error code */
  207. rt_set_errno(err_code);
  208. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  209. }
  210. static rt_err_t rt_serial_control (rt_device_t dev, int cmd, void *args)
  211. {
  212. struct stm32_serial_device* uart;
  213. RT_ASSERT(dev != RT_NULL);
  214. uart = (struct stm32_serial_device*)dev->user_data;
  215. switch (cmd)
  216. {
  217. case RT_DEVICE_CTRL_SUSPEND:
  218. /* suspend device */
  219. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  220. USART_Cmd(uart->uart_device, DISABLE);
  221. break;
  222. case RT_DEVICE_CTRL_RESUME:
  223. /* resume device */
  224. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  225. USART_Cmd(uart->uart_device, ENABLE);
  226. break;
  227. }
  228. return RT_EOK;
  229. }
  230. /*
  231. * serial register for STM32
  232. * support STM32F103VB and STM32F103ZE
  233. */
  234. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct stm32_serial_device *serial)
  235. {
  236. RT_ASSERT(device != RT_NULL);
  237. if ((flag & RT_DEVICE_FLAG_DMA_RX) ||
  238. (flag & RT_DEVICE_FLAG_INT_TX))
  239. {
  240. RT_ASSERT(0);
  241. }
  242. device->type = RT_Device_Class_Char;
  243. device->rx_indicate = RT_NULL;
  244. device->tx_complete = RT_NULL;
  245. device->init = rt_serial_init;
  246. device->open = rt_serial_open;
  247. device->close = rt_serial_close;
  248. device->read = rt_serial_read;
  249. device->write = rt_serial_write;
  250. device->control = rt_serial_control;
  251. device->user_data = serial;
  252. /* register a character device */
  253. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  254. }
  255. /* ISR for serial interrupt */
  256. void rt_hw_serial_isr(rt_device_t device)
  257. {
  258. struct stm32_serial_device* uart = (struct stm32_serial_device*) device->user_data;
  259. if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET)
  260. {
  261. /* interrupt mode receive */
  262. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  263. /* save on rx buffer */
  264. while (uart->uart_device->SR & USART_FLAG_RXNE)
  265. {
  266. rt_base_t level;
  267. /* disable interrupt */
  268. level = rt_hw_interrupt_disable();
  269. /* save character */
  270. uart->int_rx->rx_buffer[uart->int_rx->save_index] = uart->uart_device->DR & 0xff;
  271. uart->int_rx->save_index ++;
  272. if (uart->int_rx->save_index >= UART_RX_BUFFER_SIZE)
  273. uart->int_rx->save_index = 0;
  274. /* if the next position is read index, discard this 'read char' */
  275. if (uart->int_rx->save_index == uart->int_rx->read_index)
  276. {
  277. uart->int_rx->read_index ++;
  278. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  279. uart->int_rx->read_index = 0;
  280. }
  281. /* enable interrupt */
  282. rt_hw_interrupt_enable(level);
  283. }
  284. /* clear interrupt */
  285. USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE);
  286. /* invoke callback */
  287. if (device->rx_indicate != RT_NULL)
  288. {
  289. rt_size_t rx_length;
  290. /* get rx length */
  291. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  292. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  293. uart->int_rx->save_index - uart->int_rx->read_index;
  294. device->rx_indicate(device, rx_length);
  295. }
  296. }
  297. if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET)
  298. {
  299. /* clear interrupt */
  300. USART_ClearITPendingBit(uart->uart_device, USART_IT_TC);
  301. }
  302. }
  303. /*
  304. * ISR for DMA mode Tx
  305. */
  306. void rt_hw_serial_dma_tx_isr(rt_device_t device)
  307. {
  308. rt_uint32_t level;
  309. struct stm32_serial_data_node* data_node;
  310. struct stm32_serial_device* uart = (struct stm32_serial_device*) device->user_data;
  311. /* DMA mode receive */
  312. RT_ASSERT(device->flag & RT_DEVICE_FLAG_DMA_TX);
  313. /* get the first data node */
  314. data_node = uart->dma_tx->list_head;
  315. RT_ASSERT(data_node != RT_NULL);
  316. /* invoke call to notify tx complete */
  317. if (device->tx_complete != RT_NULL)
  318. device->tx_complete(device, data_node->data_ptr);
  319. /* disable interrupt */
  320. level = rt_hw_interrupt_disable();
  321. /* remove list head */
  322. uart->dma_tx->list_head = data_node->next;
  323. if (uart->dma_tx->list_head == RT_NULL) /* data link empty */
  324. uart->dma_tx->list_tail = RT_NULL;
  325. /* enable interrupt */
  326. rt_hw_interrupt_enable(level);
  327. /* release data node memory */
  328. rt_mp_free(data_node);
  329. if (uart->dma_tx->list_head != RT_NULL)
  330. {
  331. /* transmit next data node */
  332. rt_serial_enable_dma(uart->dma_tx->dma_channel,
  333. (rt_uint32_t)uart->dma_tx->list_head->data_ptr,
  334. uart->dma_tx->list_head->data_size);
  335. }
  336. else
  337. {
  338. /* no data to be transmitted, disable DMA */
  339. DMA_Cmd(uart->dma_tx->dma_channel, DISABLE);
  340. }
  341. }
  342. /*@}*/