serial.c 9.6 KB

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