serial.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. * File : serial.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, 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. * 2012-05-15 lgnq modified according bernard's implementation.
  14. * 2012-05-28 bernard code cleanup
  15. */
  16. #include <rthw.h>
  17. #include <rtthread.h>
  18. #include <rtdevice.h>
  19. rt_inline void serial_ringbuffer_init(struct serial_ringbuffer *rbuffer)
  20. {
  21. rt_memset(rbuffer->buffer, 0, sizeof(rbuffer->buffer));
  22. rbuffer->put_index = 0;
  23. rbuffer->get_index = 0;
  24. }
  25. rt_inline void serial_ringbuffer_putc(struct serial_ringbuffer *rbuffer,
  26. char ch)
  27. {
  28. rt_base_t level;
  29. /* disable interrupt */
  30. level = rt_hw_interrupt_disable();
  31. rbuffer->buffer[rbuffer->put_index] = ch;
  32. rbuffer->put_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  33. /* if the next position is read index, discard this 'read char' */
  34. if (rbuffer->put_index == rbuffer->get_index)
  35. {
  36. rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  37. }
  38. /* enable interrupt */
  39. rt_hw_interrupt_enable(level);
  40. }
  41. rt_inline int serial_ringbuffer_putchar(struct serial_ringbuffer *rbuffer,
  42. char ch)
  43. {
  44. rt_base_t level;
  45. rt_uint16_t next_index;
  46. /* disable interrupt */
  47. level = rt_hw_interrupt_disable();
  48. next_index = (rbuffer->put_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  49. if (next_index != rbuffer->get_index)
  50. {
  51. rbuffer->buffer[rbuffer->put_index] = ch;
  52. rbuffer->put_index = next_index;
  53. }
  54. else
  55. {
  56. /* enable interrupt */
  57. rt_hw_interrupt_enable(level);
  58. return -1;
  59. }
  60. /* enable interrupt */
  61. rt_hw_interrupt_enable(level);
  62. return 1;
  63. }
  64. rt_inline int serial_ringbuffer_getc(struct serial_ringbuffer *rbuffer)
  65. {
  66. int ch;
  67. rt_base_t level;
  68. ch = -1;
  69. /* disable interrupt */
  70. level = rt_hw_interrupt_disable();
  71. if (rbuffer->get_index != rbuffer->put_index)
  72. {
  73. ch = rbuffer->buffer[rbuffer->get_index];
  74. rbuffer->get_index = (rbuffer->get_index + 1) & (SERIAL_RBUFFER_SIZE - 1);
  75. }
  76. /* enable interrupt */
  77. rt_hw_interrupt_enable(level);
  78. return ch;
  79. }
  80. rt_inline rt_uint32_t serial_ringbuffer_size(struct serial_ringbuffer *rbuffer)
  81. {
  82. rt_uint32_t size;
  83. rt_base_t level;
  84. level = rt_hw_interrupt_disable();
  85. size = (rbuffer->put_index - rbuffer->get_index) & (SERIAL_RBUFFER_SIZE - 1);
  86. rt_hw_interrupt_enable(level);
  87. return size;
  88. }
  89. /* RT-Thread Device Interface */
  90. /*
  91. * This function initializes serial
  92. */
  93. static rt_err_t rt_serial_init(struct rt_device *dev)
  94. {
  95. rt_err_t result = RT_EOK;
  96. struct rt_serial_device *serial;
  97. RT_ASSERT(dev != RT_NULL);
  98. serial = (struct rt_serial_device *)dev;
  99. if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
  100. {
  101. /* apply configuration */
  102. if (serial->ops->configure)
  103. result = serial->ops->configure(serial, &serial->config);
  104. if (result != RT_EOK)
  105. return result;
  106. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  107. serial_ringbuffer_init(serial->int_rx);
  108. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  109. serial_ringbuffer_init(serial->int_tx);
  110. if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  111. {
  112. serial->dma_flag = RT_FALSE;
  113. /* init data queue */
  114. rt_data_queue_init(&(serial->tx_dq), RT_SERIAL_TX_DATAQUEUE_SIZE,
  115. RT_SERIAL_TX_DATAQUEUE_LWM, RT_NULL);
  116. }
  117. /* set activated */
  118. dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
  119. }
  120. return result;
  121. }
  122. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  123. {
  124. struct rt_serial_device *serial;
  125. rt_uint32_t int_flags = 0;
  126. RT_ASSERT(dev != RT_NULL);
  127. serial = (struct rt_serial_device *)dev;
  128. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  129. int_flags = RT_SERIAL_RX_INT;
  130. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  131. int_flags |= RT_SERIAL_TX_INT;
  132. if (int_flags)
  133. {
  134. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)int_flags);
  135. }
  136. return RT_EOK;
  137. }
  138. static rt_err_t rt_serial_close(struct rt_device *dev)
  139. {
  140. struct rt_serial_device *serial;
  141. rt_uint32_t int_flags = 0;
  142. RT_ASSERT(dev != RT_NULL);
  143. serial = (struct rt_serial_device *)dev;
  144. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  145. int_flags = RT_SERIAL_RX_INT;
  146. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  147. int_flags |= RT_SERIAL_TX_INT;
  148. if (int_flags)
  149. {
  150. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void *)int_flags);
  151. }
  152. return RT_EOK;
  153. }
  154. static rt_size_t rt_serial_read(struct rt_device *dev,
  155. rt_off_t pos,
  156. void *buffer,
  157. rt_size_t size)
  158. {
  159. rt_uint8_t *ptr;
  160. rt_uint32_t read_nbytes;
  161. struct rt_serial_device *serial;
  162. RT_ASSERT(dev != RT_NULL);
  163. serial = (struct rt_serial_device *)dev;
  164. ptr = (rt_uint8_t *)buffer;
  165. if (dev->flag & RT_DEVICE_FLAG_INT_RX)
  166. {
  167. /* interrupt mode Rx */
  168. while (size)
  169. {
  170. int ch;
  171. ch = serial_ringbuffer_getc(serial->int_rx);
  172. if (ch == -1)
  173. break;
  174. *ptr = ch & 0xff;
  175. ptr ++;
  176. size --;
  177. }
  178. }
  179. else
  180. {
  181. /* polling mode */
  182. while ((rt_uint32_t)ptr - (rt_uint32_t)buffer < size)
  183. {
  184. *ptr = serial->ops->getc(serial);
  185. ptr ++;
  186. }
  187. }
  188. read_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  189. /* set error code */
  190. if (read_nbytes == 0)
  191. {
  192. rt_set_errno(-RT_EEMPTY);
  193. }
  194. return read_nbytes;
  195. }
  196. static rt_size_t rt_serial_write(struct rt_device *dev,
  197. rt_off_t pos,
  198. const void *buffer,
  199. rt_size_t size)
  200. {
  201. rt_uint8_t *ptr;
  202. rt_size_t write_nbytes = 0;
  203. struct rt_serial_device *serial;
  204. RT_ASSERT(dev != RT_NULL);
  205. serial = (struct rt_serial_device *)dev;
  206. ptr = (rt_uint8_t*)buffer;
  207. if (dev->flag & RT_DEVICE_FLAG_INT_TX)
  208. {
  209. /* warning: data will be discarded if buffer is full */
  210. while (size)
  211. {
  212. if (serial_ringbuffer_putchar(serial->int_tx, *ptr) != -1)
  213. {
  214. ptr ++;
  215. size --;
  216. }
  217. else
  218. break;
  219. }
  220. }
  221. else if (dev->flag & RT_DEVICE_FLAG_DMA_TX)
  222. {
  223. const void *data_ptr = RT_NULL;
  224. rt_size_t data_size = 0;
  225. rt_base_t level;
  226. rt_err_t result;
  227. RT_ASSERT(0 == (dev->flag & RT_DEVICE_FLAG_STREAM));
  228. result = rt_data_queue_push(&(serial->tx_dq), buffer, size, 20);
  229. if (result == RT_EOK)
  230. {
  231. level = rt_hw_interrupt_disable();
  232. if (serial->dma_flag == RT_FALSE)
  233. {
  234. serial->dma_flag = RT_TRUE;
  235. rt_hw_interrupt_enable(level);
  236. if (RT_EOK == rt_data_queue_pop(&(serial->tx_dq), &data_ptr, &data_size, 0))
  237. {
  238. serial->ops->dma_transmit(serial, data_ptr, data_size);
  239. }
  240. }
  241. else
  242. rt_hw_interrupt_enable(level);
  243. return size;
  244. }
  245. else
  246. {
  247. rt_set_errno(result);
  248. return 0;
  249. }
  250. }
  251. else
  252. {
  253. /* polling mode */
  254. while (size)
  255. {
  256. /*
  257. * to be polite with serial console add a line feed
  258. * to the carriage return character
  259. */
  260. if (*ptr == '\n' && (dev->flag & RT_DEVICE_FLAG_STREAM))
  261. {
  262. serial->ops->putc(serial, '\r');
  263. }
  264. serial->ops->putc(serial, *ptr);
  265. ++ ptr;
  266. -- size;
  267. }
  268. }
  269. write_nbytes = (rt_uint32_t)ptr - (rt_uint32_t)buffer;
  270. if (write_nbytes == 0)
  271. {
  272. rt_set_errno(-RT_EFULL);
  273. }
  274. return write_nbytes;
  275. }
  276. static rt_err_t rt_serial_control(struct rt_device *dev,
  277. rt_uint8_t cmd,
  278. void *args)
  279. {
  280. struct rt_serial_device *serial;
  281. RT_ASSERT(dev != RT_NULL);
  282. serial = (struct rt_serial_device *)dev;
  283. switch (cmd)
  284. {
  285. case RT_DEVICE_CTRL_SUSPEND:
  286. /* suspend device */
  287. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  288. break;
  289. case RT_DEVICE_CTRL_RESUME:
  290. /* resume device */
  291. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  292. break;
  293. case RT_DEVICE_CTRL_CONFIG:
  294. /* configure device */
  295. serial->ops->configure(serial, (struct serial_configure *)args);
  296. break;
  297. }
  298. return RT_EOK;
  299. }
  300. /*
  301. * serial register
  302. */
  303. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  304. const char *name,
  305. rt_uint32_t flag,
  306. void *data)
  307. {
  308. struct rt_device *device;
  309. RT_ASSERT(serial != RT_NULL);
  310. device = &(serial->parent);
  311. device->type = RT_Device_Class_Char;
  312. device->rx_indicate = RT_NULL;
  313. device->tx_complete = RT_NULL;
  314. device->init = rt_serial_init;
  315. device->open = rt_serial_open;
  316. device->close = rt_serial_close;
  317. device->read = rt_serial_read;
  318. device->write = rt_serial_write;
  319. device->control = rt_serial_control;
  320. device->user_data = data;
  321. /* register a character device */
  322. return rt_device_register(device, name, flag);
  323. }
  324. /* ISR for serial interrupt */
  325. void rt_hw_serial_isr(struct rt_serial_device *serial)
  326. {
  327. int ch = -1;
  328. /* interrupt mode receive */
  329. RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
  330. while (1)
  331. {
  332. ch = serial->ops->getc(serial);
  333. if (ch == -1)
  334. break;
  335. serial_ringbuffer_putc(serial->int_rx, ch);
  336. }
  337. /* invoke callback */
  338. if (serial->parent.rx_indicate != RT_NULL)
  339. {
  340. rt_size_t rx_length;
  341. /* get rx length */
  342. rx_length = serial_ringbuffer_size(serial->int_rx);
  343. serial->parent.rx_indicate(&serial->parent, rx_length);
  344. }
  345. }
  346. /*
  347. * ISR for DMA mode Tx
  348. */
  349. void rt_hw_serial_dma_tx_isr(struct rt_serial_device *serial)
  350. {
  351. void *data_ptr;
  352. rt_size_t data_size;
  353. if (RT_EOK == rt_data_queue_pop(&(serial->tx_dq), &data_ptr, &data_size, 0))
  354. {
  355. /* transmit next data node */
  356. serial->ops->dma_transmit(serial, data_ptr, data_size);
  357. }
  358. else
  359. {
  360. serial->dma_flag = RT_FALSE;
  361. }
  362. }