serial.c 11 KB

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