serial.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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. tx_dma->activated = RT_FALSE;
  255. rt_data_queue_init(&(tx_dma->data_queue), 8, 4, RT_NULL);
  256. serial->serial_tx = tx_dma;
  257. dev->open_flag |= RT_DEVICE_FLAG_DMA_TX;
  258. }
  259. else if (oflag & RT_DEVICE_FLAG_INT_TX)
  260. {
  261. struct rt_serial_tx_fifo *tx_fifo;
  262. tx_fifo = (struct rt_serial_tx_fifo*) rt_malloc(sizeof(struct rt_serial_tx_fifo));
  263. RT_ASSERT(tx_fifo != RT_NULL);
  264. rt_completion_init(&(tx_fifo->completion));
  265. serial->serial_tx = tx_fifo;
  266. dev->open_flag |= RT_DEVICE_FLAG_INT_TX;
  267. /* configure low level device */
  268. serial->ops->control(serial, RT_DEVICE_CTRL_SET_INT, (void *)RT_DEVICE_FLAG_INT_TX);
  269. }
  270. else
  271. {
  272. serial->serial_tx = RT_NULL;
  273. }
  274. }
  275. return RT_EOK;
  276. }
  277. static rt_err_t rt_serial_close(struct rt_device *dev)
  278. {
  279. struct rt_serial_device *serial;
  280. RT_ASSERT(dev != RT_NULL);
  281. serial = (struct rt_serial_device *)dev;
  282. /* this device has more reference count */
  283. if (dev->ref_count > 1) return RT_EOK;
  284. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  285. {
  286. struct rt_serial_rx_fifo* rx_fifo;
  287. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  288. RT_ASSERT(rx_fifo != RT_NULL);
  289. rt_free(rx_fifo);
  290. serial->serial_rx = RT_NULL;
  291. dev->open_flag &= ~RT_DEVICE_FLAG_INT_RX;
  292. /* configure low level device */
  293. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  294. }
  295. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  296. {
  297. struct rt_serial_rx_dma* rx_dma;
  298. rx_dma = (struct rt_serial_rx_dma*)serial->serial_tx;
  299. RT_ASSERT(rx_dma != RT_NULL);
  300. rt_free(rx_dma);
  301. serial->serial_rx = RT_NULL;
  302. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_RX;
  303. }
  304. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  305. {
  306. struct rt_serial_tx_fifo* tx_fifo;
  307. tx_fifo = (struct rt_serial_tx_fifo*)serial->serial_rx;
  308. RT_ASSERT(tx_fifo != RT_NULL);
  309. rt_free(tx_fifo);
  310. serial->serial_tx = RT_NULL;
  311. dev->open_flag &= ~RT_DEVICE_FLAG_INT_TX;
  312. /* configure low level device */
  313. serial->ops->control(serial, RT_DEVICE_CTRL_CLR_INT, (void*)RT_DEVICE_FLAG_INT_TX);
  314. }
  315. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  316. {
  317. struct rt_serial_tx_dma* tx_dma;
  318. tx_dma = (struct rt_serial_tx_dma*)serial->serial_tx;
  319. RT_ASSERT(tx_dma != RT_NULL);
  320. rt_free(tx_dma);
  321. serial->serial_tx = RT_NULL;
  322. dev->open_flag &= ~RT_DEVICE_FLAG_DMA_TX;
  323. }
  324. return RT_EOK;
  325. }
  326. static rt_size_t rt_serial_read(struct rt_device *dev,
  327. rt_off_t pos,
  328. void *buffer,
  329. rt_size_t size)
  330. {
  331. struct rt_serial_device *serial;
  332. RT_ASSERT(dev != RT_NULL);
  333. if (size == 0) return 0;
  334. serial = (struct rt_serial_device *)dev;
  335. if (dev->open_flag & RT_DEVICE_FLAG_INT_RX)
  336. {
  337. return _serial_int_rx(serial, buffer, size);
  338. }
  339. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_RX)
  340. {
  341. return _serial_dma_rx(serial, buffer, size);
  342. }
  343. return _serial_poll_rx(serial, buffer, size);
  344. }
  345. static rt_size_t rt_serial_write(struct rt_device *dev,
  346. rt_off_t pos,
  347. const void *buffer,
  348. rt_size_t size)
  349. {
  350. struct rt_serial_device *serial;
  351. RT_ASSERT(dev != RT_NULL);
  352. if (size == 0) return 0;
  353. serial = (struct rt_serial_device *)dev;
  354. if (dev->open_flag & RT_DEVICE_FLAG_INT_TX)
  355. {
  356. return _serial_int_tx(serial, buffer, size);
  357. }
  358. else if (dev->open_flag & RT_DEVICE_FLAG_DMA_TX)
  359. {
  360. return _serial_dma_tx(serial, buffer, size);
  361. }
  362. else
  363. {
  364. return _serial_poll_tx(serial, buffer, size);
  365. }
  366. }
  367. static rt_err_t rt_serial_control(struct rt_device *dev,
  368. rt_uint8_t cmd,
  369. void *args)
  370. {
  371. struct rt_serial_device *serial;
  372. RT_ASSERT(dev != RT_NULL);
  373. serial = (struct rt_serial_device *)dev;
  374. switch (cmd)
  375. {
  376. case RT_DEVICE_CTRL_SUSPEND:
  377. /* suspend device */
  378. dev->flag |= RT_DEVICE_FLAG_SUSPENDED;
  379. break;
  380. case RT_DEVICE_CTRL_RESUME:
  381. /* resume device */
  382. dev->flag &= ~RT_DEVICE_FLAG_SUSPENDED;
  383. break;
  384. case RT_DEVICE_CTRL_CONFIG:
  385. /* configure device */
  386. serial->ops->configure(serial, (struct serial_configure *)args);
  387. break;
  388. default :
  389. /* control device */
  390. serial->ops->control(serial, cmd, args);
  391. break;
  392. }
  393. return RT_EOK;
  394. }
  395. /*
  396. * serial register
  397. */
  398. rt_err_t rt_hw_serial_register(struct rt_serial_device *serial,
  399. const char *name,
  400. rt_uint32_t flag,
  401. void *data)
  402. {
  403. struct rt_device *device;
  404. RT_ASSERT(serial != RT_NULL);
  405. device = &(serial->parent);
  406. device->type = RT_Device_Class_Char;
  407. device->rx_indicate = RT_NULL;
  408. device->tx_complete = RT_NULL;
  409. device->init = rt_serial_init;
  410. device->open = rt_serial_open;
  411. device->close = rt_serial_close;
  412. device->read = rt_serial_read;
  413. device->write = rt_serial_write;
  414. device->control = rt_serial_control;
  415. device->user_data = data;
  416. /* register a character device */
  417. return rt_device_register(device, name, flag);
  418. }
  419. /* ISR for serial interrupt */
  420. void rt_hw_serial_isr(struct rt_serial_device *serial, int event)
  421. {
  422. switch (event & 0xff)
  423. {
  424. case RT_SERIAL_EVENT_RX_IND:
  425. {
  426. int ch = -1;
  427. rt_base_t level;
  428. struct rt_serial_rx_fifo* rx_fifo;
  429. /* interrupt mode receive */
  430. rx_fifo = (struct rt_serial_rx_fifo*)serial->serial_rx;
  431. RT_ASSERT(rx_fifo != RT_NULL);
  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. }