serial.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006, 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. * 2006-03-13 Bernard first version
  13. * 2011-05-15 lgnq modified according bernard's implementaion.
  14. */
  15. #include <rtthread.h>
  16. #include "serial.h"
  17. /**
  18. * @addtogroup FM3 MB9B500
  19. */
  20. /*@{*/
  21. /* RT-Thread Device Interface */
  22. /**
  23. * This function initializes serial
  24. */
  25. static rt_err_t rt_serial_init (rt_device_t dev)
  26. {
  27. struct serial_device* uart = (struct serial_device*) dev->user_data;
  28. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  29. {
  30. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  31. {
  32. rt_memset(uart->int_rx->rx_buffer, 0,
  33. sizeof(uart->int_rx->rx_buffer));
  34. uart->int_rx->read_index = uart->int_rx->save_index = 0;
  35. }
  36. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  37. {
  38. rt_memset(uart->int_tx->tx_buffer, 0,
  39. sizeof(uart->int_tx->tx_buffer));
  40. uart->int_tx->write_index = uart->int_tx->save_index = 0;
  41. }
  42. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  43. }
  44. return RT_EOK;
  45. }
  46. /* save a char to serial buffer */
  47. static void rt_serial_savechar(struct serial_device* uart, char ch)
  48. {
  49. rt_base_t level;
  50. /* disable interrupt */
  51. level = rt_hw_interrupt_disable();
  52. uart->int_rx->rx_buffer[uart->int_rx->save_index] = ch;
  53. uart->int_rx->save_index ++;
  54. if (uart->int_rx->save_index >= UART_RX_BUFFER_SIZE)
  55. uart->int_rx->save_index = 0;
  56. /* if the next position is read index, discard this 'read char' */
  57. if (uart->int_rx->save_index == uart->int_rx->read_index)
  58. {
  59. uart->int_rx->read_index ++;
  60. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  61. uart->int_rx->read_index = 0;
  62. }
  63. /* enable interrupt */
  64. rt_hw_interrupt_enable(level);
  65. }
  66. static rt_err_t rt_serial_open(rt_device_t dev, rt_uint16_t oflag)
  67. {
  68. struct serial_device* uart;
  69. RT_ASSERT(dev != RT_NULL);
  70. uart = (struct serial_device*) dev->user_data;
  71. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  72. {
  73. /* enable interrupt */
  74. UART_ENABLE_IRQ(uart->rx_irq);
  75. }
  76. return RT_EOK;
  77. }
  78. static rt_err_t rt_serial_close(rt_device_t dev)
  79. {
  80. struct serial_device* uart;
  81. RT_ASSERT(dev != RT_NULL);
  82. uart = (struct serial_device*) dev->user_data;
  83. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  84. {
  85. /* disable interrupt */
  86. UART_DISABLE_IRQ(uart->rx_irq);
  87. }
  88. return RT_EOK;
  89. }
  90. static rt_size_t rt_serial_read (rt_device_t dev, rt_off_t pos, void* buffer,
  91. rt_size_t size)
  92. {
  93. rt_uint8_t* ptr;
  94. rt_err_t err_code;
  95. struct serial_device* uart;
  96. ptr = buffer;
  97. err_code = RT_EOK;
  98. uart = (struct serial_device*)dev->user_data;
  99. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  100. {
  101. rt_base_t level;
  102. /* interrupt mode Rx */
  103. while (size)
  104. {
  105. if (uart->int_rx->read_index != uart->int_rx->save_index)
  106. {
  107. *ptr++ = uart->int_rx->rx_buffer[uart->int_rx->read_index];
  108. size --;
  109. /* disable interrupt */
  110. level = rt_hw_interrupt_disable();
  111. uart->int_rx->read_index ++;
  112. if (uart->int_rx->read_index >= UART_RX_BUFFER_SIZE)
  113. uart->int_rx->read_index = 0;
  114. /* enable interrupt */
  115. rt_hw_interrupt_enable(level);
  116. }
  117. else
  118. {
  119. /* set error code */
  120. err_code = -RT_EEMPTY;
  121. break;
  122. }
  123. }
  124. }
  125. else
  126. {
  127. /* polling mode */
  128. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  129. {
  130. while (uart->uart_device->SSR & SSR_RDRF)
  131. {
  132. *ptr = uart->uart_device->RDR & 0xff;
  133. ptr ++;
  134. }
  135. }
  136. }
  137. /* set error code */
  138. rt_set_errno(err_code);
  139. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  140. }
  141. static rt_size_t rt_serial_write (rt_device_t dev, rt_off_t pos,
  142. const void* buffer, rt_size_t size)
  143. {
  144. rt_uint8_t* ptr;
  145. rt_err_t err_code;
  146. struct serial_device* uart;
  147. err_code = RT_EOK;
  148. ptr = (rt_uint8_t*)buffer;
  149. uart = (struct serial_device*)dev->user_data;
  150. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  151. {
  152. /* interrupt mode Tx */
  153. while (uart->int_tx->save_index != uart->int_tx->write_index)
  154. {
  155. /* save on tx buffer */
  156. uart->int_tx->tx_buffer[uart->int_tx->save_index] = *ptr++;
  157. -- size;
  158. /* move to next position */
  159. uart->int_tx->save_index ++;
  160. /* wrap save index */
  161. if (uart->int_tx->save_index >= UART_TX_BUFFER_SIZE)
  162. uart->int_tx->save_index = 0;
  163. }
  164. /* set error code */
  165. if (size > 0)
  166. err_code = -RT_EFULL;
  167. }
  168. else
  169. {
  170. /* polling mode */
  171. while (size)
  172. {
  173. /*
  174. * to be polite with serial console add a line feed
  175. * to the carriage return character
  176. */
  177. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  178. {
  179. while (!(uart->uart_device->SSR & SSR_TDRE));
  180. uart->uart_device->TDR = '\r';
  181. }
  182. while (!(uart->uart_device->SSR & SSR_TDRE));
  183. uart->uart_device->TDR = (*ptr & 0x1FF);
  184. ++ptr; --size;
  185. }
  186. }
  187. /* set error code */
  188. rt_set_errno(err_code);
  189. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  190. }
  191. static rt_err_t rt_serial_control (rt_device_t dev, rt_uint8_t cmd, void *args)
  192. {
  193. RT_ASSERT(dev != RT_NULL);
  194. switch (cmd)
  195. {
  196. case RT_DEVICE_CTRL_SUSPEND:
  197. /* suspend device */
  198. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  199. break;
  200. case RT_DEVICE_CTRL_RESUME:
  201. /* resume device */
  202. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  203. break;
  204. }
  205. return RT_EOK;
  206. }
  207. /*
  208. * serial register
  209. */
  210. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name,
  211. rt_uint32_t flag, struct serial_device *serial)
  212. {
  213. RT_ASSERT(device != RT_NULL);
  214. device->type = RT_Device_Class_Char;
  215. device->rx_indicate = RT_NULL;
  216. device->tx_complete = RT_NULL;
  217. device->init = rt_serial_init;
  218. device->open = rt_serial_open;
  219. device->close = rt_serial_close;
  220. device->read = rt_serial_read;
  221. device->write = rt_serial_write;
  222. device->control = rt_serial_control;
  223. device->user_data = serial;
  224. /* register a character device */
  225. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  226. }
  227. /* ISR for serial interrupt */
  228. void rt_hw_serial_isr(rt_device_t device)
  229. {
  230. struct serial_device* uart = (struct serial_device*) device->user_data;
  231. /* interrupt mode receive */
  232. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  233. /* save on rx buffer */
  234. while (uart->uart_device->SSR & SSR_RDRF)
  235. {
  236. rt_serial_savechar(uart, uart->uart_device->RDR & 0xff);
  237. }
  238. /* invoke callback */
  239. if (device->rx_indicate != RT_NULL)
  240. {
  241. rt_size_t rx_length;
  242. /* get rx length */
  243. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  244. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  245. uart->int_rx->save_index - uart->int_rx->read_index;
  246. device->rx_indicate(device, rx_length);
  247. }
  248. }
  249. #ifdef RT_USING_UART2
  250. /* UART2 device driver structure */
  251. #define UART2 FM3_MFS2_UART
  252. struct serial_int_rx uart2_int_rx;
  253. struct serial_device uart2 =
  254. {
  255. UART2,
  256. MFS2RX_IRQn,
  257. MFS2TX_IRQn,
  258. &uart2_int_rx,
  259. RT_NULL
  260. };
  261. struct rt_device uart2_device;
  262. void MFS2RX_IRQHandler(void)
  263. {
  264. /* enter interrupt */
  265. rt_interrupt_enter();
  266. rt_hw_serial_isr(&uart2_device);
  267. /* leave interrupt */
  268. rt_interrupt_leave();
  269. }
  270. #endif
  271. void rt_hw_serial_init(void)
  272. {
  273. #ifdef RT_USING_UART2
  274. /* initialize UART2 */
  275. /* Set Uart Ch2 Port, SIN2_1, SOT2_1 */
  276. FM3_GPIO->PFR2 = FM3_GPIO->PFR2 | 0x0030;
  277. FM3_GPIO->EPFR07 = FM3_GPIO->EPFR07 | 0x000a0000;
  278. uart2.uart_device->SMR = SMR_MD_UART | SMR_SOE;;
  279. uart2.uart_device->BGR = (40000000UL + (BPS/2))/BPS - 1;
  280. uart2.uart_device->ESCR = ESCR_DATABITS_8;
  281. uart2.uart_device->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
  282. /* register UART2 device */
  283. rt_hw_serial_register(&uart2_device,
  284. "uart2",
  285. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  286. &uart2);
  287. #endif
  288. }
  289. /*@}*/