serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 MB9B610
  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;
  185. --size;
  186. }
  187. }
  188. /* set error code */
  189. rt_set_errno(err_code);
  190. return (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  191. }
  192. static rt_err_t rt_serial_control (rt_device_t dev, int cmd, void *args)
  193. {
  194. RT_ASSERT(dev != RT_NULL);
  195. switch (cmd)
  196. {
  197. case RT_DEVICE_CTRL_SUSPEND:
  198. /* suspend device */
  199. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  200. break;
  201. case RT_DEVICE_CTRL_RESUME:
  202. /* resume device */
  203. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  204. break;
  205. }
  206. return RT_EOK;
  207. }
  208. /*
  209. * serial register
  210. */
  211. rt_err_t rt_hw_serial_register(rt_device_t device, const char* name,
  212. rt_uint32_t flag, struct serial_device *serial)
  213. {
  214. RT_ASSERT(device != RT_NULL);
  215. device->type = RT_Device_Class_Char;
  216. device->rx_indicate = RT_NULL;
  217. device->tx_complete = RT_NULL;
  218. device->init = rt_serial_init;
  219. device->open = rt_serial_open;
  220. device->close = rt_serial_close;
  221. device->read = rt_serial_read;
  222. device->write = rt_serial_write;
  223. device->control = rt_serial_control;
  224. device->user_data = serial;
  225. /* register a character device */
  226. return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR | flag);
  227. }
  228. /* ISR for serial interrupt */
  229. void rt_hw_serial_isr(rt_device_t device)
  230. {
  231. struct serial_device* uart = (struct serial_device*) device->user_data;
  232. /* interrupt mode receive */
  233. RT_ASSERT(device->flag & RT_DEVICE_FLAG_INT_RX);
  234. /* save on rx buffer */
  235. while (uart->uart_device->SSR & SSR_RDRF)
  236. {
  237. rt_serial_savechar(uart, uart->uart_device->RDR & 0xff);
  238. }
  239. /* invoke callback */
  240. if (device->rx_indicate != RT_NULL)
  241. {
  242. rt_size_t rx_length;
  243. /* get rx length */
  244. rx_length = uart->int_rx->read_index > uart->int_rx->save_index ?
  245. UART_RX_BUFFER_SIZE - uart->int_rx->read_index + uart->int_rx->save_index :
  246. uart->int_rx->save_index - uart->int_rx->read_index;
  247. device->rx_indicate(device, rx_length);
  248. }
  249. }
  250. #ifdef RT_USING_UART0
  251. /* UART0 device driver structure */
  252. #define UART0 FM3_MFS0_UART
  253. struct serial_int_rx uart0_int_rx;
  254. struct serial_device uart0 =
  255. {
  256. UART0,
  257. MFS0RX_IRQn,
  258. MFS0TX_IRQn,
  259. &uart0_int_rx,
  260. RT_NULL
  261. };
  262. struct rt_device uart0_device;
  263. void MFS0RX_IRQHandler(void)
  264. {
  265. /* enter interrupt */
  266. rt_interrupt_enter();
  267. rt_hw_serial_isr(&uart0_device);
  268. /* leave interrupt */
  269. rt_interrupt_leave();
  270. }
  271. #endif /**< #ifdef RT_USING_UART0 */
  272. #ifdef RT_USING_UART2
  273. /* UART2 device driver structure */
  274. #define UART2 FM3_MFS2_UART
  275. struct serial_int_rx uart2_int_rx;
  276. struct serial_device uart2 =
  277. {
  278. UART2,
  279. MFS2RX_IRQn,
  280. MFS2TX_IRQn,
  281. &uart2_int_rx,
  282. RT_NULL
  283. };
  284. struct rt_device uart2_device;
  285. void MFS2RX_IRQHandler(void)
  286. {
  287. /* enter interrupt */
  288. rt_interrupt_enter();
  289. rt_hw_serial_isr(&uart2_device);
  290. /* leave interrupt */
  291. rt_interrupt_leave();
  292. }
  293. #endif /**< #ifdef RT_USING_UART2 */
  294. #ifdef RT_USING_UART4
  295. /* UART4 device driver structure */
  296. #define UART4 FM3_MFS4_UART
  297. struct serial_int_rx uart4_int_rx;
  298. struct serial_device uart4 =
  299. {
  300. (FM3_MFS03_UART_TypeDef*)UART4,
  301. MFS4RX_IRQn,
  302. MFS4TX_IRQn,
  303. &uart4_int_rx,
  304. RT_NULL
  305. };
  306. struct rt_device uart4_device;
  307. void MFS4RX_IRQHandler(void)
  308. {
  309. /* enter interrupt */
  310. rt_interrupt_enter();
  311. rt_hw_serial_isr(&uart4_device);
  312. /* leave interrupt */
  313. rt_interrupt_leave();
  314. }
  315. #endif /**< #ifdef RT_USING_UART4 */
  316. void rt_hw_serial_init(void)
  317. {
  318. uint32_t APB2_clock = (SystemCoreClock >> (APBC2_PSR_Val & 0x03));
  319. #ifdef RT_USING_UART0
  320. /* initialize UART0 */
  321. /* Set Uart Ch0 Port, SIN0_0:P21, SOT0_0:P22 */
  322. FM3_GPIO->ADE &= ~(1UL<<31); /* disable P22 AN31 function */
  323. FM3_GPIO->PFR2 = FM3_GPIO->PFR2 | (1<<1) | (1<<2);
  324. FM3_GPIO->EPFR07 = FM3_GPIO->EPFR07 & ~(3<<4 | 3<<6) | (1<<4) | (1<<6);
  325. uart0.uart_device->SMR = SMR_MD_UART | SMR_SOE;;
  326. uart0.uart_device->BGR = (APB2_clock + (BPS/2))/BPS - 1; /* round */
  327. uart0.uart_device->ESCR = ESCR_DATABITS_8;
  328. uart0.uart_device->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
  329. /* register UART0 device */
  330. rt_hw_serial_register(&uart0_device,
  331. "uart0",
  332. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  333. &uart0);
  334. #endif /**< #ifdef RT_USING_UART0 */
  335. #ifdef RT_USING_UART2
  336. /* initialize UART2 */
  337. /* Set Uart Ch2 Port, SIN2_2:P17, SOT2_2:P18 */
  338. FM3_GPIO->ADE &= ~(1<<7 | 1<<8); /* disable P17 AN07 and P18 AN08 function */
  339. FM3_GPIO->PFR1 = FM3_GPIO->PFR1 | (1<<7) | (1<<8) ;
  340. FM3_GPIO->EPFR07 = FM3_GPIO->EPFR07 | (3<<16) | (3<<18) ;
  341. uart2.uart_device->SMR = SMR_MD_UART | SMR_SOE;;
  342. uart2.uart_device->BGR = (APB2_clock + (BPS/2))/BPS - 1; /* round */
  343. uart2.uart_device->ESCR = ESCR_DATABITS_8;
  344. uart2.uart_device->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
  345. /* register UART2 device */
  346. rt_hw_serial_register(&uart2_device,
  347. "uart2",
  348. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  349. &uart2);
  350. #endif /**< #ifdef RT_USING_UART2 */
  351. #ifdef RT_USING_UART4
  352. /* initialize UART4 */
  353. /* Set Uart Ch4 Port, SIN4_2:P05, SOT4_2:P06 */
  354. FM3_GPIO->PFR0 = FM3_GPIO->PFR0 | (1<<5) | (1<<6);
  355. FM3_GPIO->EPFR08 = FM3_GPIO->EPFR08 | (3<<4) | (3<<6);
  356. uart4.uart_device->SMR = SMR_MD_UART | SMR_SOE;;
  357. uart4.uart_device->BGR = (APB2_clock + (BPS/2))/BPS - 1; /* round */
  358. uart4.uart_device->ESCR = ESCR_DATABITS_8;
  359. uart4.uart_device->SCR = SCR_RXE | SCR_TXE | SCR_RIE;
  360. /* register UART4 device */
  361. rt_hw_serial_register(&uart4_device,
  362. "uart4",
  363. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
  364. &uart4);
  365. #endif /**< #ifdef RT_USING_UART4 */
  366. }
  367. /*@}*/