serial.c 6.4 KB

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