serial.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-13 bernard first version
  23. * 2012-05-15 lgnq modified according bernard's implementation.
  24. * 2012-05-28 bernard code cleanup
  25. * 2012-11-23 bernard fix compiler warning.
  26. * 2013-02-20 bernard use RT_SERIAL_RB_BUFSZ to define
  27. * the size of ring buffer.
  28. * 2014-07-10 bernard rewrite serial framework
  29. * 2014-12-31 bernard use open_flag for poll_tx stream mode.
  30. */
  31. #include <rthw.h>
  32. #include <rtthread.h>
  33. #include <rtdevice.h>
  34. /*
  35. * Serial poll routines
  36. */
  37. rt_inline int _serial_poll_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  38. {
  39. int ch;
  40. int size;
  41. RT_ASSERT(serial != RT_NULL);
  42. size = length;
  43. while (length)
  44. {
  45. ch = serial->ops->getc(serial);
  46. *data = ch;
  47. data ++; length --;
  48. if (ch == '\n') break;
  49. }
  50. return size - length;
  51. }
  52. rt_inline int _serial_poll_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  53. {
  54. int size;
  55. RT_ASSERT(serial != RT_NULL);
  56. size = length;
  57. while (length)
  58. {
  59. /*
  60. * to be polite with serial console add a line feed
  61. * to the carriage return character
  62. */
  63. if (*data == '\n' && (serial->parent.open_flag & RT_DEVICE_FLAG_STREAM))
  64. {
  65. serial->ops->putc(serial, '\r');
  66. }
  67. serial->ops->putc(serial, *data);
  68. ++ data;
  69. -- length;
  70. }
  71. return size - length;
  72. }
  73. /*
  74. * Serial interrupt routines
  75. */
  76. rt_inline int _serial_int_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  77. {
  78. int size;
  79. struct rt_serial_rx_fifo* rx_fifo;
  80. RT_ASSERT(serial != RT_NULL);
  81. size = length;
  82. rx_fifo = (struct rt_serial_rx_fifo*) serial->serial_rx;
  83. RT_ASSERT(rx_fifo != RT_NULL);
  84. /* read from software FIFO */
  85. while (length)
  86. {
  87. int ch;
  88. rt_base_t level;
  89. /* disable interrupt */
  90. level = rt_hw_interrupt_disable();
  91. if (rx_fifo->get_index != rx_fifo->put_index)
  92. {
  93. ch = rx_fifo->buffer[rx_fifo->get_index];
  94. rx_fifo->get_index += 1;
  95. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  96. }
  97. else
  98. {
  99. /* no data, enable interrupt and break out */
  100. rt_hw_interrupt_enable(level);
  101. break;
  102. }
  103. /* enable interrupt */
  104. rt_hw_interrupt_enable(level);
  105. *data = ch & 0xff;
  106. data ++; length --;
  107. }
  108. return size - length;
  109. }
  110. rt_inline int _serial_int_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  111. {
  112. int size;
  113. struct rt_serial_tx_fifo *tx;
  114. RT_ASSERT(serial != RT_NULL);
  115. size = length;
  116. tx = (struct rt_serial_tx_fifo*) serial->serial_tx;
  117. RT_ASSERT(tx != RT_NULL);
  118. while (length)
  119. {
  120. if (serial->ops->putc(serial, *(char*)data) == -1)
  121. {
  122. rt_completion_wait(&(tx->completion), RT_WAITING_FOREVER);
  123. continue;
  124. }
  125. data ++; length --;
  126. }
  127. return size - length;
  128. }
  129. /*
  130. * Serial DMA routines
  131. */
  132. rt_inline int _serial_dma_rx(struct rt_serial_device *serial, rt_uint8_t *data, int length)
  133. {
  134. rt_base_t level;
  135. int result = RT_EOK;
  136. struct rt_serial_rx_dma *rx_dma;
  137. RT_ASSERT((serial != RT_NULL) && (data != RT_NULL));
  138. rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
  139. RT_ASSERT(rx_dma != RT_NULL);
  140. level = rt_hw_interrupt_disable();
  141. if (rx_dma->activated != RT_TRUE)
  142. {
  143. rx_dma->activated = RT_TRUE;
  144. serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_RX);
  145. }
  146. else result = -RT_EBUSY;
  147. rt_hw_interrupt_enable(level);
  148. if (result == RT_EOK) return length;
  149. rt_set_errno(result);
  150. return 0;
  151. }
  152. rt_inline int _serial_dma_tx(struct rt_serial_device *serial, const rt_uint8_t *data, int length)
  153. {
  154. rt_base_t level;
  155. rt_err_t result;
  156. struct rt_serial_tx_dma *tx_dma;
  157. tx_dma = (struct rt_serial_tx_dma*)(serial->serial_tx);
  158. result = rt_data_queue_push(&(tx_dma->data_queue), data, length, RT_WAITING_FOREVER);
  159. if (result == RT_EOK)
  160. {
  161. level = rt_hw_interrupt_disable();
  162. if (tx_dma->activated != RT_TRUE)
  163. {
  164. tx_dma->activated = RT_TRUE;
  165. rt_hw_interrupt_enable(level);
  166. /* make a DMA transfer */
  167. serial->ops->dma_transmit(serial, data, length, RT_SERIAL_DMA_TX);
  168. }
  169. else
  170. {
  171. rt_hw_interrupt_enable(level);
  172. }
  173. return length;
  174. }
  175. else
  176. {
  177. rt_set_errno(result);
  178. return 0;
  179. }
  180. }
  181. /* RT-Thread Device Interface */
  182. /*
  183. * This function initializes serial device.
  184. */
  185. static rt_err_t rt_serial_init(struct rt_device *dev)
  186. {
  187. rt_err_t result = RT_EOK;
  188. struct rt_serial_device *serial;
  189. RT_ASSERT(dev != RT_NULL);
  190. serial = (struct rt_serial_device *)dev;
  191. /* initialize rx/tx */
  192. serial->serial_rx = RT_NULL;
  193. serial->serial_tx = RT_NULL;
  194. /* apply configuration */
  195. if (serial->ops->configure)
  196. result = serial->ops->configure(serial, &serial->config);
  197. return result;
  198. }
  199. static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
  200. {
  201. struct rt_serial_device *serial;
  202. RT_ASSERT(dev != RT_NULL);
  203. serial = (struct rt_serial_device *)dev;
  204. /* check device flag with the open flag */
  205. if ((oflag & RT_DEVICE_FLAG_DMA_RX) && !(dev->flag & RT_DEVICE_FLAG_DMA_RX))
  206. return -RT_EIO;
  207. if ((oflag & RT_DEVICE_FLAG_DMA_TX) && !(dev->flag & RT_DEVICE_FLAG_DMA_TX))
  208. return -RT_EIO;
  209. if ((oflag & RT_DEVICE_FLAG_INT_RX) && !(dev->flag & RT_DEVICE_FLAG_INT_RX))
  210. return -RT_EIO;
  211. if ((oflag & RT_DEVICE_FLAG_INT_TX) && !(dev->flag & RT_DEVICE_FLAG_INT_TX))
  212. return -RT_EIO;
  213. /* get open flags */
  214. dev->open_flag = oflag & 0xff;
  215. /* initialize the Rx/Tx structure according to open flag */
  216. if (serial->serial_rx == RT_NULL)
  217. {
  218. if (oflag & RT_DEVICE_FLAG_DMA_RX)
  219. {
  220. struct rt_serial_rx_dma* rx_dma;
  221. rx_dma = (struct rt_serial_rx_dma*) rt_malloc (sizeof(struct rt_serial_rx_dma));
  222. RT_ASSERT(rx_dma != RT_NULL);
  223. rx_dma->activated = RT_FALSE;
  224. serial->serial_rx = rx_dma;
  225. dev->open_flag |= RT_DEVICE_FLAG_DMA_RX;
  226. }
  227. else if (oflag & RT_DEVICE_FLAG_INT_RX)
  228. {
  229. struct rt_serial_rx_fifo* rx_fifo;
  230. rx_fifo = (struct rt_serial_rx_fifo*) rt_malloc (sizeof(struct rt_serial_rx_fifo) +
  231. serial->config.bufsz);
  232. RT_ASSERT(rx_fifo != RT_NULL);
  233. rx_fifo->buffer = (rt_uint8_t*) (rx_fifo + 1);
  234. rt_memset(rx_fifo->buffer, 0, serial->config.bufsz);
  235. rx_fifo->put_index = 0;
  236. rx_fifo->get_index = 0;
  237. serial->serial_rx = rx_fifo;
  238. dev->open_flag |= RT_DEVICE_FLAG_INT_RX;
  239. /* configure low level device */
  240. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_RX);
  241. }
  242. else
  243. {
  244. serial->serial_rx = RT_NULL;
  245. }
  246. }
  247. if (serial->serial_tx == RT_NULL)
  248. {
  249. if (oflag & RT_DEVICE_FLAG_DMA_TX)
  250. {
  251. struct rt_serial_tx_dma* tx_dma;
  252. tx_dma = (struct rt_serial_tx_dma*) rt_malloc (sizeof(struct rt_serial_tx_dma));
  253. RT_ASSERT(tx_dma != RT_NULL);
  254. rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
  255. serial->serial_tx = tx_dma;
  256. dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
  257. }
  258. else if (oflag & RT_DEVICE_FLAG_INT_TX)
  259. {
  260. struct rt_serial_tx_fifo *tx_fifo;
  261. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc(sizeof(struct rt_serial_tx_fifo));
  262. RT_ASSERT(tx_fifo != RT_NULL);
  263. rt_completion_init(&(tx_fifo->completion));
  264. serial->serial_tx = tx_fifo;
  265. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  266. /* configure low level device */
  267. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  268. }
  269. else
  270. {
  271. serial->serial_tx = RT_NULL;
  272. }
  273. }
  274. return RT_EOK;
  275. }
  276. static rt_err_t rt_serial_close(struct rt_device *dev)
  277. {
  278. struct rt_serial_device *serial;
  279. RT_ASSERT(dev != RT_NULL);
  280. serial = (struct rt_serial_device *)dev;
  281. /* this device has more reference count */
  282. if (dev->ref_count > 1) return RT_EOK;
  283. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  284. {
  285. struct rt_serial_rx_fifo* rx_fifo;
  286. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  287. RT_ASSERT(rx_fifo != RT_NULL);
  288. rt_free(rx_fifo);
  289. serial->serial_rx = RT_NULL;
  290. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  291. /* configure low level device */
  292. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  293. }
  294. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  295. {
  296. struct rt_serial_rx_dma* rx_dma;
  297. rx_dma = (struct rt_serial_rx_dma*)serial->serial_tx;
  298. RT_ASSERT(rx_dma != RT_NULL);
  299. rt_free(rx_dma);
  300. serial->serial_rx = RT_NULL;
  301. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
  302. }
  303. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  304. {
  305. struct rt_serial_tx_fifo* tx_fifo;
  306. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_rx;
  307. RT_ASSERT(tx_fifo != RT_NULL);
  308. rt_free(tx_fifo);
  309. serial->serial_tx = RT_NULL;
  310. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  311. /* configure low level device */
  312. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  313. }
  314. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  315. {
  316. struct rt_serial_tx_dma* tx_dma;
  317. tx_dma = (struct rt_serial_tx_dma*)serial->serial_tx;
  318. RT_ASSERT(tx_dma != RT_NULL);
  319. rt_free(tx_dma);
  320. serial->serial_tx = RT_NULL;
  321. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
  322. }
  323. return RT_EOK;
  324. }
  325. static rt_size_t rt_serial_read(struct rt_device *dev,
  326. rt_off_t pos,
  327. void *buffer,
  328. rt_size_t size)
  329. {
  330. struct rt_serial_device *serial;
  331. RT_ASSERT(dev != RT_NULL);
  332. if (size == 0) return 0;
  333. serial = (struct rt_serial_device *)dev;
  334. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  335. {
  336. return _serial_int_rx(serial, buffer, size);
  337. }
  338. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  339. {
  340. return _serial_dma_rx(serial, buffer, size);
  341. }
  342. return _serial_poll_rx(serial, buffer, size);
  343. }
  344. static rt_size_t rt_serial_write(struct rt_device *dev,
  345. rt_off_t pos,
  346. const void *buffer,
  347. rt_size_t size)
  348. {
  349. struct rt_serial_device *serial;
  350. RT_ASSERT(dev != RT_NULL);
  351. if (size == 0) return 0;
  352. serial = (struct rt_serial_device *)dev;
  353. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  354. {
  355. return _serial_int_tx(serial, buffer, size);
  356. }
  357. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  358. {
  359. return _serial_dma_tx(serial, buffer, size);
  360. }
  361. else
  362. {
  363. return _serial_poll_tx(serial, buffer, size);
  364. }
  365. }
  366. static rt_err_t rt_serial_control(struct rt_device *dev,
  367. rt_uint8_t cmd,
  368. void *args)
  369. {
  370. struct rt_serial_device *serial;
  371. RT_ASSERT(dev != RT_NULL);
  372. serial = (struct rt_serial_device *)dev;
  373. switch (cmd)
  374. {
  375. case RT_DEVICE_CTRL_SUSPEND:
  376. /* suspend device */
  377. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  378. break;
  379. case RT_DEVICE_CTRL_RESUME:
  380. /* resume device */
  381. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  382. break;
  383. case RT_DEVICE_CTRL_CONFIG:
  384. /* configure device */
  385. serial->ops->configure(serial, (struct serial_configure *)args);
  386. break;
  387. default :
  388. /* control device */
  389. serial->ops->control(serial, cmd, args);
  390. break;
  391. }
  392. return RT_EOK;
  393. }
  394. /*
  395. * serial register
  396. */
  397. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  398. const char *name,
  399. rt_uint32_t flag,
  400. void *data)
  401. {
  402. struct rt_device *device;
  403. RT_ASSERT(serial != RT_NULL);
  404. device = &(serial->parent);
  405. device->type = RT_Device_Class_Char;
  406. device->rx_indicate = RT_NULL;
  407. device->tx_complete = RT_NULL;
  408. device->init = rt_serial_init;
  409. device->open = rt_serial_open;
  410. device->close = rt_serial_close;
  411. device->read = rt_serial_read;
  412. device->write = rt_serial_write;
  413. device->control = rt_serial_control;
  414. device->user_data = data;
  415. /* register a character device */
  416. return rt_device_register(device, name, flag);
  417. }
  418. /* ISR for serial interrupt */
  419. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  420. {
  421. switch (event & 0xff)
  422. {
  423. case RT_SERIAL_EVENT_RX_IND:
  424. {
  425. int ch = -1;
  426. rt_base_t level;
  427. struct rt_serial_rx_fifo* rx_fifo;
  428. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  429. RT_ASSERT(rx_fifo != RT_NULL);
  430. /* interrupt mode receive */
  431. RT_ASSERT(serial->parent.open_flag & RT_DEVICE_FLAG_INT_RX);
  432. while (1)
  433. {
  434. ch = serial->ops->getc(serial);
  435. if (ch == -1) break;
  436. /* disable interrupt */
  437. level = rt_hw_interrupt_disable();
  438. rx_fifo->buffer[rx_fifo->put_index] = ch;
  439. rx_fifo->put_index += 1;
  440. if (rx_fifo->put_index >= serial->config.bufsz) rx_fifo->put_index = 0;
  441. /* if the next position is read index, discard this 'read char' */
  442. if (rx_fifo->put_index == rx_fifo->get_index)
  443. {
  444. rx_fifo->get_index += 1;
  445. if (rx_fifo->get_index >= serial->config.bufsz) rx_fifo->get_index = 0;
  446. }
  447. /* enable interrupt */
  448. rt_hw_interrupt_enable(level);
  449. }
  450. /* invoke callback */
  451. if (serial->parent.rx_indicate != RT_NULL)
  452. {
  453. rt_size_t rx_length;
  454. /* get rx length */
  455. level = rt_hw_interrupt_disable();
  456. rx_length = (rx_fifo->put_index >= rx_fifo->get_index)? (rx_fifo->put_index - rx_fifo->get_index):
  457. (serial->config.bufsz - (rx_fifo->get_index - rx_fifo->put_index));
  458. rt_hw_interrupt_enable(level);
  459. serial->parent.rx_indicate(&serial->parent, rx_length);
  460. }
  461. break;
  462. }
  463. case RT_SERIAL_EVENT_TX_DONE:
  464. {
  465. struct rt_serial_tx_fifo* tx_fifo;
  466. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_tx;
  467. rt_completion_done(&(tx_fifo->completion));
  468. break;
  469. }
  470. case RT_SERIAL_EVENT_TX_DMADONE:
  471. {
  472. const void *data_ptr;
  473. rt_size_t data_size;
  474. const void *last_data_ptr;
  475. struct rt_serial_tx_dma* tx_dma;
  476. tx_dma = (struct rt_serial_tx_dma*) serial->serial_tx;
  477. rt_data_queue_pop(&(tx_dma->data_queue), &last_data_ptr, &data_size, 0);
  478. if (rt_data_queue_peak(&(tx_dma->data_queue), &data_ptr, &data_size) == RT_EOK)
  479. {
  480. /* transmit next data node */
  481. tx_dma->activated = RT_TRUE;
  482. serial->ops->dma_transmit(serial, data_ptr, data_size, RT_SERIAL_DMA_TX);
  483. }
  484. else
  485. {
  486. tx_dma->activated = RT_FALSE;
  487. }
  488. /* invoke callback */
  489. if (serial->parent.tx_complete != RT_NULL)
  490. {
  491. serial->parent.tx_complete(&serial->parent, (void*)last_data_ptr);
  492. }
  493. break;
  494. }
  495. case RT_SERIAL_EVENT_RX_DMADONE:
  496. {
  497. int length;
  498. struct rt_serial_rx_dma* rx_dma;
  499. rx_dma = (struct rt_serial_rx_dma*)serial->serial_rx;
  500. /* get DMA rx length */
  501. length = (event & (~0xff)) >> 8;
  502. serial->parent.rx_indicate(&(serial->parent), length);
  503. rx_dma->activated = RT_FALSE;
  504. break;
  505. }
  506. }
  507. }