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