serial.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. * 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. * 2010-03-30 Kyle Ported from STM32 to AVR32.
  13. */
  14. #include "serial.h"
  15. #include "compiler.h"
  16. #include "usart.h"
  17. struct rt_device _rt_usart_device;
  18. struct avr32_serial_int_rx _rt_usart_rx;
  19. struct avr32_serial_device uart =
  20. {
  21. .uart_device = (avr32_usart_t *) &AVR32_USART1,
  22. .int_rx = &_rt_usart_rx
  23. };
  24. /**
  25. * @addtogroup AVR32UC3
  26. */
  27. /*@{*/
  28. /* RT-Thread Device Interface */
  29. static rt_err_t rt_serial_init (rt_device_t dev)
  30. {
  31. struct avr32_serial_device* uart = (struct avr32_serial_device*) dev->user_data;
  32. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  33. {
  34. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  35. {
  36. rt_memset(uart->int_rx->rx_buffer, 0, sizeof(uart->int_rx->rx_buffer));
  37. uart->int_rx->read_index = 0;
  38. uart->int_rx->save_index = 0;
  39. }
  40. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  41. }
  42. return RT_EOK;
  43. }
  44. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  45. {
  46. return RT_EOK;
  47. }
  48. static rt_err_t rt_serial_close(rt_device_t dev)
  49. {
  50. return RT_EOK;
  51. }
  52. static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  53. {
  54. rt_uint8_t* ptr;
  55. rt_err_t err_code;
  56. struct avr32_serial_device* uart;
  57. ptr = buffer;
  58. err_code = RT_EOK;
  59. uart = (struct avr32_serial_device*)dev->user_data;
  60. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  61. {
  62. /* interrupt mode Rx */
  63. while (size)
  64. {
  65. rt_base_t level;
  66. /* disable interrupt */
  67. level = rt_hw_interrupt_disable();
  68. if (uart->int_rx->read_index != uart->int_rx->save_index)
  69. {
  70. /* read a character */
  71. *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index];
  72. size--;
  73. /* move to next position */
  74. uart->int_rx->read_index ++;
  75. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  76. uart->int_rx->read_index = 0;
  77. }
  78. else
  79. {
  80. /* set error code */
  81. err_code = -RT_EEMPTY;
  82. /* enable interrupt */
  83. rt_hw_interrupt_enable(level);
  84. break;
  85. }
  86. /* enable interrupt */
  87. rt_hw_interrupt_enable(level);
  88. }
  89. }
  90. else
  91. {
  92. /* polling mode */
  93. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  94. {
  95. while (usart_test_hit(uart->uart_device))
  96. {
  97. *ptr = uart->uart_device->rhr & 0xff;
  98. ptr ++;
  99. }
  100. }
  101. }
  102. /* set error code */
  103. rt_set_errno(err_code);
  104. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  105. }
  106. static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  107. {
  108. rt_uint8_t* ptr;
  109. rt_err_t err_code;
  110. struct avr32_serial_device* uart;
  111. err_code = RT_EOK;
  112. ptr = (rt_uint8_t*)buffer;
  113. uart = (struct avr32_serial_device*)dev->user_data;
  114. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  115. {
  116. /* interrupt mode Tx, does not support */
  117. RT_ASSERT(0);
  118. }
  119. else
  120. {
  121. /* polling mode */
  122. if (dev->flag & RT_DEVICE_FLAG_STREAM)
  123. {
  124. /* stream mode */
  125. while (size)
  126. {
  127. usart_putchar(uart->uart_device, (int) *ptr);
  128. ++ptr; --size;
  129. }
  130. }
  131. else
  132. {
  133. /* write data directly */
  134. while (size)
  135. {
  136. usart_bw_write_char(uart->uart_device, (int) *ptr);
  137. ++ptr; --size;
  138. }
  139. }
  140. }
  141. /* set error code */
  142. rt_set_errno(err_code);
  143. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  144. }
  145. static rt_err_t rt_serial_control (rt_device_t dev, int cmd, void *args)
  146. {
  147. struct avr32_serial_device* uart;
  148. RT_ASSERT(dev != RT_NULL);
  149. uart = (struct avr32_serial_device*)dev->user_data;
  150. switch (cmd)
  151. {
  152. case RT_DEVICE_CTRL_SUSPEND:
  153. /* suspend device */
  154. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  155. break;
  156. case RT_DEVICE_CTRL_RESUME:
  157. /* resume device */
  158. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  159. break;
  160. }
  161. return RT_EOK;
  162. }
  163. /*
  164. * serial register for STM32
  165. * support STM32F103VB and STM32F103ZE
  166. */
  167. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name, rt_uint32_t flag, struct avr32_serial_device *serial)
  168. {
  169. RT_ASSERT(device != RT_NULL);
  170. if ((flag & RT_DEVICE_FLAG_DMA_RX) ||
  171. (flag & RT_DEVICE_FLAG_INT_TX))
  172. {
  173. RT_ASSERT(0);
  174. }
  175. device->type = RT_Device_Class_Char;
  176. device->rx_indicate = RT_NULL;
  177. device->tx_complete = RT_NULL;
  178. device->init = rt_serial_init;
  179. device->open = rt_serial_open;
  180. device->close = rt_serial_close;
  181. device->read = rt_serial_read;
  182. device->write = rt_serial_write;
  183. device->control = rt_serial_control;
  184. device->user_data = serial;
  185. /* register a character device */
  186. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  187. }
  188. /* ISR for serial interrupt */
  189. void rt_hw_serial_isr(void)
  190. {
  191. struct avr32_serial_device* uart = (struct avr32_serial_device*) _rt_usart_device.user_data;
  192. rt_base_t level;
  193. if (usart_test_hit(uart->uart_device))
  194. {
  195. /* interrupt mode receive */
  196. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  197. /* disable interrupt */
  198. level = rt_hw_interrupt_disable();
  199. /* save character */
  200. uart->int_rx->rx_buffer[uart->int_rx->save_index] = uart->uart_device->rhr & 0xff;
  201. uart->int_rx->save_index ++;
  202. if (uart->int_rx->save_index >= UART_RX_BUFFER_SIZE)
  203. uart->int_rx->save_index = 0;
  204. /* if the next position is read index, discard this 'read char' */
  205. if (uart->int_rx->save_index == uart->int_rx->read_index)
  206. {
  207. uart->int_rx->read_index ++;
  208. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  209. uart->int_rx->read_index = 0;
  210. }
  211. /* enable interrupt */
  212. rt_hw_interrupt_enable(level);
  213. /* invoke callback */
  214. if (_rt_usart_device.rx_indicate != RT_NULL)
  215. {
  216. rt_size_t rx_length;
  217. /* get rx length */
  218. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  219. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  220. uart->int_rx->save_index - uart->int_rx->read_index;
  221. _rt_usart_device.rx_indicate(&_rt_usart_device, rx_length);
  222. }
  223. }
  224. else
  225. {
  226. usart_reset_status(uart->uart_device);
  227. }
  228. }
  229. /*@}*/