uartavr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. /*
  2. * Copyright (C) 2001-2003 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file arch/avr/dev/uartavr.c
  35. * \brief AVR fast UART driver.
  36. *
  37. * \verbatim
  38. * $Id: uartavr.c 5472 2013-12-06 00:16:28Z olereinhardt $
  39. * \endverbatim
  40. */
  41. #include <string.h>
  42. #include <sys/atom.h>
  43. #include <sys/heap.h>
  44. #include <sys/event.h>
  45. #include <sys/timer.h>
  46. #include <sys/device.h>
  47. #include <dev/irqreg.h>
  48. #include <dev/uartavr.h>
  49. #include <fcntl.h>
  50. /*
  51. * Not nice because stdio already defined them. But in order to save memory,
  52. * we do the whole buffering and including stdio here would be more weird.
  53. */
  54. #ifndef _IOFBF
  55. #define _IOFBF 0x00
  56. #define _IOLBF 0x01
  57. #define _IONBF 0x02
  58. #endif
  59. /*!
  60. * \addtogroup xgUartAvr
  61. */
  62. /*@{*/
  63. /*
  64. * Handle AVR UART transmit complete.
  65. */
  66. static void TxComplete(void *arg)
  67. {
  68. NUTDEVICE *dev = (NUTDEVICE *) arg;
  69. IFSTREAM *ifs = dev->dev_icb;
  70. UARTDCB *dcb;
  71. if (ifs->if_tx_idx != ifs->if_wr_idx) {
  72. #ifdef UDR1
  73. if (dev->dev_base)
  74. UDR1 = ifs->if_tx_buf[ifs->if_tx_idx];
  75. else
  76. #endif
  77. outb(UDR, ifs->if_tx_buf[ifs->if_tx_idx]);
  78. ifs->if_tx_idx++;
  79. } else {
  80. ifs->if_tx_act = 0;
  81. dcb = dev->dev_dcb;
  82. NutEventPostFromIrq(&dcb->dcb_tx_rdy);
  83. }
  84. }
  85. /*
  86. * Handle AVR UART receive complete.
  87. *
  88. * Note, that this function modifies the receive index in
  89. * interrupt context. This requires, that any non atomic
  90. * access of this index requires interrupts being disabled.
  91. * Thanks to Mike Cornelius, who pointed this out.
  92. */
  93. static void RxComplete(void *arg)
  94. {
  95. NUTDEVICE *dev = (NUTDEVICE *) arg;
  96. IFSTREAM *ifs = dev->dev_icb;
  97. UARTDCB *dcb;
  98. #ifdef UDR1
  99. if (dev->dev_base)
  100. ifs->if_rx_buf[ifs->if_rx_idx] = inb(UDR1);
  101. else
  102. #endif
  103. ifs->if_rx_buf[ifs->if_rx_idx] = inb(UDR);
  104. if (ifs->if_rd_idx == ifs->if_rx_idx) {
  105. dcb = dev->dev_dcb;
  106. NutEventPostFromIrq(&dcb->dcb_rx_rdy);
  107. }
  108. /* Late increment fixes ICCAVR bug on volatile variables. */
  109. ifs->if_rx_idx++;
  110. }
  111. /*!
  112. * \brief Wait for input.
  113. *
  114. * This function checks the input buffer for any data. If
  115. * the buffer is empty, the calling \ref xrThread "thread"
  116. * will be blocked until at least one new character is
  117. * received or a timeout occurs.
  118. *
  119. * \param dev Indicates the UART device.
  120. *
  121. * \return 0 on success, -1 on timeout.
  122. */
  123. int UartAvrInput(NUTDEVICE * dev)
  124. {
  125. int rc = 0;
  126. int empty;
  127. IFSTREAM *ifs = dev->dev_icb;
  128. UARTDCB *dcb = dev->dev_dcb;
  129. NutEnterCritical();
  130. empty = ifs->if_rd_idx == ifs->if_rx_idx;
  131. NutExitCritical();
  132. if (empty) {
  133. /*
  134. * Changing if into a while loop fixes a serious bug:
  135. * Previous receiver events without any waiting thread
  136. * set the event handle to the signaled state. So the
  137. * wait returns immediately. Unfortunately the calling
  138. * routines rely on a filled buffer when we return 0.
  139. * Thanks to Mike Cornelius, who found this bug.
  140. *
  141. * MR (2005-07-14): Reduced critical sections
  142. */
  143. do {
  144. rc = NutEventWait(&dcb->dcb_rx_rdy, dcb->dcb_rtimeout);
  145. NutEnterCritical();
  146. empty = ifs->if_rd_idx == ifs->if_rx_idx;
  147. NutExitCritical();
  148. } while (rc == 0 && empty);
  149. }
  150. return rc;
  151. }
  152. /*!
  153. * \brief Initiate output.
  154. *
  155. * This function checks the output buffer for any data. If
  156. * the buffer contains at least one character, the transmitter
  157. * is started, if not already running. The function returns
  158. * immediately, without waiting for the character being
  159. * completely transmitted. Any remaining characters in the
  160. * output buffer are transmitted in the background.
  161. *
  162. * \param dev Indicates the UART device.
  163. *
  164. * \return 0 on success, -1 otherwise.
  165. */
  166. int UartAvrOutput(NUTDEVICE * dev)
  167. {
  168. IFSTREAM *ifs = dev->dev_icb;
  169. if (ifs->if_tx_act == 0 && ifs->if_tx_idx != ifs->if_wr_idx) {
  170. ifs->if_tx_act = 1;
  171. #ifdef UDR1
  172. if (dev->dev_base)
  173. UDR1 = ifs->if_tx_buf[ifs->if_tx_idx];
  174. else
  175. #endif
  176. outb(UDR, ifs->if_tx_buf[ifs->if_tx_idx]);
  177. ifs->if_tx_idx++;
  178. }
  179. return 0;
  180. }
  181. /*!
  182. * \brief Wait for output buffer empty.
  183. *
  184. * If the output buffer contains any data, the calling
  185. * thread is suspended until all data has been transmitted.
  186. *
  187. * \param dev Indicates the UART device.
  188. *
  189. * \return 0 on success, -1 otherwise.
  190. */
  191. int UartAvrFlush(NUTDEVICE * dev)
  192. {
  193. IFSTREAM *ifs = dev->dev_icb;
  194. UARTDCB *dcb = dev->dev_dcb;
  195. /*
  196. * Start any pending output.
  197. */
  198. if (UartAvrOutput(dev))
  199. return -1;
  200. /*
  201. * Wait until output buffer empty.
  202. */
  203. NutEnterCritical();
  204. while (ifs->if_tx_idx != ifs->if_wr_idx)
  205. NutEventWait(&dcb->dcb_tx_rdy, 100);
  206. NutExitCritical();
  207. return 0;
  208. }
  209. /*
  210. *
  211. * \param dev Indicates the UART device.
  212. *
  213. * \return 0 on success, -1 otherwise.
  214. */
  215. static int UartAvrGetStatus(NUTDEVICE * dev, uint32_t * status)
  216. {
  217. IFSTREAM *ifs = dev->dev_icb;
  218. uint8_t us;
  219. *status = 0;
  220. #ifdef UDR1
  221. if (dev->dev_base)
  222. us = inb(UCSR1A);
  223. else
  224. #endif
  225. us = inb(USR);
  226. if (us & FE)
  227. *status |= UART_FRAMINGERROR;
  228. if (us & DOR)
  229. *status |= UART_OVERRUNERROR;
  230. if (ifs->if_tx_idx == ifs->if_wr_idx)
  231. *status |= UART_TXBUFFEREMPTY;
  232. if (ifs->if_rd_idx == ifs->if_rx_idx)
  233. *status |= UART_RXBUFFEREMPTY;
  234. return 0;
  235. }
  236. /*
  237. * Carefully enable UART functions.
  238. */
  239. static void UartAvrEnable(uint16_t base)
  240. {
  241. NutEnterCritical();
  242. #ifdef UCSR1B
  243. if (base)
  244. UCSR1B = BV(RXCIE) | BV(TXCIE) | BV(RXEN) | BV(TXEN);
  245. else
  246. #endif
  247. UCR = BV(RXCIE) | BV(TXCIE) | BV(RXEN) | BV(TXEN);
  248. NutExitCritical();
  249. }
  250. /*
  251. * Carefully disable UART functions.
  252. */
  253. static void UartAvrDisable(uint16_t base)
  254. {
  255. /*
  256. * Disable UART interrupts.
  257. */
  258. NutEnterCritical();
  259. #ifdef UCSR1B
  260. if (base)
  261. UCSR1B &= ~(BV(RXCIE) | BV(TXCIE));
  262. else
  263. #endif
  264. UCR &= ~(BV(RXCIE) | BV(TXCIE));
  265. NutExitCritical();
  266. /*
  267. * Allow incoming or outgoing character to finish.
  268. */
  269. NutDelay(10);
  270. /*
  271. * Now disable UART functions.
  272. */
  273. #ifdef UCSR1B
  274. if (base)
  275. UCSR1B &= ~(BV(RXEN) | BV(TXEN));
  276. else
  277. #endif
  278. UCR &= ~(BV(RXEN) | BV(TXEN));
  279. }
  280. /*!
  281. * \brief Perform on-chip UART control functions.
  282. *
  283. * \param dev Identifies the device that receives the device-control
  284. * function.
  285. * \param req Requested control function. May be set to one of the
  286. * following constants:
  287. * - UART_SETSPEED, conf points to an uint32_t value containing the baudrate.
  288. * - UART_GETSPEED, conf points to an uint32_t value receiving the current baudrate.
  289. * - UART_SETDATABITS, conf points to an uint32_t value containing the number of data bits, 5, 6, 7 or 8.
  290. * - UART_GETDATABITS, conf points to an uint32_t value receiving the number of data bits, 5, 6, 7 or 8.
  291. * - UART_SETPARITY, conf points to an uint32_t value containing the parity, 0 (no), 1 (odd) or 2 (even).
  292. * - UART_GETPARITY, conf points to an uint32_t value receiving the parity, 0 (no), 1 (odd) or 2 (even).
  293. * - UART_SETSTOPBITS, conf points to an uint32_t value containing the number of stop bits 1 or 2.
  294. * - UART_GETSTOPBITS, conf points to an uint32_t value receiving the number of stop bits 1 or 2.
  295. * - UART_SETSTATUS
  296. * - UART_GETSTATUS
  297. * - UART_SETREADTIMEOUT, conf points to an uint32_t value containing the read timeout.
  298. * - UART_GETREADTIMEOUT, conf points to an uint32_t value receiving the read timeout.
  299. * - UART_SETWRITETIMEOUT, conf points to an uint32_t value containing the write timeout.
  300. * - UART_GETWRITETIMEOUT, conf points to an uint32_t value receiving the write timeout.
  301. * - UART_SETLOCALECHO, conf points to an uint32_t value containing 0 (off) or 1 (on).
  302. * - UART_GETLOCALECHO, conf points to an uint32_t value receiving 0 (off) or 1 (on).
  303. * - UART_SETFLOWCONTROL, conf points to an uint32_t value containing combined UART_FCTL_ values.
  304. * - UART_GETFLOWCONTROL, conf points to an uint32_t value containing receiving UART_FCTL_ values.
  305. * - UART_SETCOOKEDMODE, conf points to an uint32_t value containing 0 (off) or 1 (on).
  306. * - UART_GETCOOKEDMODE, conf points to an uint32_t value receiving 0 (off) or 1 (on).
  307. *
  308. * \param conf Points to a buffer that contains any data required for
  309. * the given control function or receives data from that
  310. * function.
  311. * \return 0 on success, -1 otherwise.
  312. *
  313. * \warning Timeout values are given in milliseconds and are limited to
  314. * the granularity of the system timer. To disable timeout,
  315. * set the parameter to NUT_WAIT_INFINITE.
  316. *
  317. * \note For ATmega103, only 8 data bits, 1 stop bit and no parity are allowed.
  318. *
  319. */
  320. int UartAvrIOCtl(NUTDEVICE * dev, int req, void *conf)
  321. {
  322. int rc = 0;
  323. UARTDCB *dcb;
  324. uint32_t *lvp = (uint32_t *) conf;
  325. uint32_t lv = *lvp;
  326. uint8_t bv = (uint8_t) lv;
  327. uint16_t sv = 0;
  328. uint8_t devnum;
  329. if (dev == 0)
  330. dev = &devUart0;
  331. devnum = dev->dev_base;
  332. dcb = dev->dev_dcb;
  333. switch (req) {
  334. case UART_SETSPEED:
  335. UartAvrDisable(devnum);
  336. #ifdef __AVR_ENHANCED__
  337. if (devnum) {
  338. if (bit_is_set(UCSR1A, U2X1)) {
  339. lv <<= 2;
  340. } else {
  341. lv <<= 3;
  342. }
  343. #if !defined(MCU_AT90USB1287)
  344. } else {
  345. if (bit_is_set(UCSR0A, U2X0)) {
  346. lv <<= 2;
  347. } else {
  348. lv <<= 3;
  349. }
  350. #endif
  351. }
  352. #else
  353. lv <<= 3;
  354. #endif
  355. sv = (uint16_t) ((NutGetCpuClock() / lv + 1UL) / 2UL) - 1;
  356. #ifdef UBRR1H
  357. if (devnum) {
  358. UBRR1L = (uint8_t) sv;
  359. UBRR1H = (uint8_t) (sv >> 8);
  360. #if !defined(MCU_AT90USB1287)
  361. } else {
  362. UBRR0L = (uint8_t) sv;
  363. UBRR0H = (uint8_t) (sv >> 8);
  364. #endif
  365. }
  366. #else
  367. UBRR = (uint8_t) sv;
  368. #endif
  369. UartAvrEnable(devnum);
  370. break;
  371. case UART_GETSPEED:
  372. #ifdef UBRR1H
  373. if (devnum) {
  374. if (bit_is_set(UCSR1A, U2X1))
  375. lv = 8UL;
  376. else
  377. lv = 16UL;
  378. sv = (uint16_t) (UBRR1H) << 8 | UBRR1L;
  379. }
  380. #if !defined(MCU_AT90USB1287)
  381. else
  382. {
  383. if (bit_is_set(UCSR0A, U2X0))
  384. lv = 8UL;
  385. else
  386. lv = 16UL;
  387. sv = (uint16_t) (UBRR0H) << 8 | UBRR0L;
  388. }
  389. #endif
  390. #else
  391. sv = UBRR;
  392. lv = 16UL;
  393. #endif
  394. *lvp = NutGetCpuClock() / (lv * (uint32_t) (sv + 1));
  395. break;
  396. case UART_SETDATABITS:
  397. UartAvrDisable(devnum);
  398. #ifdef UCSR1C
  399. if (bv >= 5 && bv <= 8) {
  400. bv = (bv - 5) << 1;
  401. if (devnum) {
  402. UCSR1C = (UCSR1C & 0xF9) | bv;
  403. UCSR1B &= 0xFB;
  404. #if !defined(MCU_AT90USB1287)
  405. } else {
  406. UCSR0C = (UCSR0C & 0xF9) | bv;
  407. UCSR0B &= 0xFB;
  408. #endif
  409. }
  410. } else
  411. rc = -1;
  412. #else
  413. if (bv != 8)
  414. rc = -1;
  415. #endif
  416. UartAvrEnable(devnum);
  417. break;
  418. case UART_GETDATABITS:
  419. #ifdef UCSR1C
  420. if (devnum)
  421. *lvp = ((UCSR1C & 0x06) >> 1) + 5;
  422. #if !defined(MCU_AT90USB1287)
  423. else
  424. *lvp = ((UCSR0C & 0x06) >> 1) + 5;
  425. #endif
  426. #else
  427. *lvp = 8;
  428. #endif
  429. break;
  430. case UART_SETPARITY:
  431. UartAvrDisable(devnum);
  432. #ifdef UCSR1C
  433. if (bv <= 2) {
  434. if (bv == 1)
  435. bv = 3;
  436. bv <<= 4;
  437. if (devnum)
  438. UCSR1C = (UCSR1C & 0xCF) | bv;
  439. #if !defined(MCU_AT90USB1287)
  440. else
  441. UCSR0C = (UCSR0C & 0xCF) | bv;
  442. #endif
  443. } else
  444. rc = -1;
  445. #endif
  446. if (bv)
  447. rc = -1;
  448. UartAvrEnable(devnum);
  449. break;
  450. case UART_GETPARITY:
  451. #ifdef UCSR1C
  452. if (devnum)
  453. bv = (UCSR1C & 0x30) >> 4;
  454. #if !defined(MCU_AT90USB1287)
  455. else
  456. bv = (UCSR0C & 0x30) >> 4;
  457. #endif
  458. if (bv == 3)
  459. bv = 1;
  460. #else
  461. bv = 0;
  462. #endif
  463. break;
  464. case UART_SETSTOPBITS:
  465. UartAvrDisable(devnum);
  466. #ifdef UCSR1C
  467. if (bv == 1 || bv == 2) {
  468. bv = (bv - 1) << 3;
  469. if (devnum)
  470. UCSR1C = (UCSR1C & 0xF7) | bv;
  471. #if !defined(MCU_AT90USB1287)
  472. else
  473. UCSR0C = (UCSR0C & 0xF7) | bv;
  474. #endif
  475. } else
  476. rc = -1;
  477. #else
  478. if (bv != 1)
  479. rc = -1;
  480. #endif
  481. UartAvrEnable(devnum);
  482. break;
  483. case UART_GETSTOPBITS:
  484. #ifdef UCSR1C
  485. if (devnum)
  486. *lvp = ((UCSR1C & 0x08) >> 3) + 1;
  487. #if !defined(MCU_AT90USB1287)
  488. else
  489. *lvp = ((UCSR0C & 0x08) >> 3) + 1;
  490. #endif
  491. #else
  492. *lvp = 1;
  493. #endif
  494. break;
  495. case UART_GETSTATUS:
  496. UartAvrGetStatus(dev, lvp);
  497. break;
  498. case UART_SETSTATUS:
  499. rc = -1;
  500. break;
  501. case UART_SETREADTIMEOUT:
  502. dcb->dcb_rtimeout = lv;
  503. break;
  504. case UART_GETREADTIMEOUT:
  505. *lvp = dcb->dcb_rtimeout;
  506. break;
  507. case UART_SETWRITETIMEOUT:
  508. dcb->dcb_wtimeout = lv;
  509. break;
  510. case UART_GETWRITETIMEOUT:
  511. *lvp = dcb->dcb_wtimeout;
  512. break;
  513. case UART_SETLOCALECHO:
  514. if (bv)
  515. dcb->dcb_modeflags |= UART_MF_LOCALECHO;
  516. else
  517. dcb->dcb_modeflags &= ~UART_MF_LOCALECHO;
  518. break;
  519. case UART_GETLOCALECHO:
  520. if (dcb->dcb_modeflags & UART_MF_LOCALECHO)
  521. *lvp = 1;
  522. else
  523. *lvp = 0;
  524. break;
  525. case UART_SETFLOWCONTROL:
  526. if (bv)
  527. dcb->dcb_modeflags |= UART_MF_LOCALECHO;
  528. else
  529. dcb->dcb_modeflags &= ~UART_MF_LOCALECHO;
  530. break;
  531. case UART_GETFLOWCONTROL:
  532. break;
  533. case UART_SETCOOKEDMODE:
  534. if (bv)
  535. dcb->dcb_modeflags |= UART_MF_COOKEDMODE;
  536. else
  537. dcb->dcb_modeflags &= ~UART_MF_COOKEDMODE;
  538. break;
  539. case UART_GETCOOKEDMODE:
  540. if (dcb->dcb_modeflags & UART_MF_COOKEDMODE)
  541. *lvp = 1;
  542. else
  543. *lvp = 0;
  544. break;
  545. default:
  546. rc = -1;
  547. break;
  548. }
  549. return rc;
  550. }
  551. /*!
  552. * \brief Initialize on chip uart device.
  553. *
  554. * Prepares the device for subsequent reading or writing.
  555. * Enables UART transmitter and receiver interrupts.
  556. *
  557. * \param dev Identifies the device to initialize.
  558. *
  559. * \return 0 on success, -1 otherwise.
  560. */
  561. int UartAvrInit(NUTDEVICE * dev)
  562. {
  563. IFSTREAM *ifs;
  564. UARTDCB *dcb;
  565. uint32_t baudrate = 9600;
  566. /*
  567. * We only support character devices for on-chip UARTs.
  568. */
  569. if (dev->dev_type != IFTYP_STREAM || dev->dev_irq != 0 ||
  570. (dev->dev_base != 0 && dev->dev_base != 1))
  571. return -1;
  572. /*
  573. * Initialize interface control block.
  574. */
  575. ifs = dev->dev_icb;
  576. memset(ifs, 0, sizeof(IFSTREAM));
  577. ifs->if_input = UartAvrInput;
  578. ifs->if_output = UartAvrOutput;
  579. ifs->if_flush = UartAvrFlush;
  580. /*
  581. * Initialize driver control block.
  582. */
  583. dcb = dev->dev_dcb;
  584. memset(dcb, 0, sizeof(UARTDCB));
  585. dcb->dcb_modeflags = UART_MF_NOBUFFER;
  586. /*
  587. * Register interrupt handler.
  588. */
  589. if (dev->dev_base) {
  590. #ifdef UDR1
  591. if (NutRegisterIrqHandler(&sig_UART1_RECV, RxComplete, dev))
  592. return -1;
  593. if (NutRegisterIrqHandler(&sig_UART1_TRANS, TxComplete, dev))
  594. #endif
  595. return -1;
  596. } else {
  597. if (NutRegisterIrqHandler(&sig_UART0_RECV, RxComplete, dev))
  598. return -1;
  599. if (NutRegisterIrqHandler(&sig_UART0_TRANS, TxComplete, dev))
  600. return -1;
  601. }
  602. /*
  603. * Set baudrate and handshake default. This will also
  604. * enable the UART functions.
  605. */
  606. UartAvrIOCtl(dev, UART_SETSPEED, &baudrate);
  607. return 0;
  608. }
  609. /*!
  610. * \brief Read from device.
  611. */
  612. int UartAvrRead(NUTFILE * fp, void *buffer, int size)
  613. {
  614. int rc;
  615. NUTDEVICE *dev;
  616. IFSTREAM *ifs;
  617. UARTDCB *dcb;
  618. uint8_t elmode;
  619. uint8_t ch;
  620. uint8_t *cp = buffer;
  621. dev = fp->nf_dev;
  622. ifs = (IFSTREAM *) dev->dev_icb;
  623. dcb = dev->dev_dcb;
  624. if (dcb->dcb_modeflags & UART_MF_COOKEDMODE)
  625. elmode = 1;
  626. else
  627. elmode = 0;
  628. /*
  629. * Call without data pointer discards receive buffer.
  630. */
  631. if (buffer == 0) {
  632. ifs->if_rd_idx = ifs->if_rx_idx;
  633. return 0;
  634. }
  635. /*
  636. * Get characters from receive buffer.
  637. */
  638. for (rc = 0; rc < size;) {
  639. if (ifs->if_rd_idx == ifs->if_rx_idx) {
  640. if (rc)
  641. break;
  642. while (ifs->if_rd_idx == ifs->if_rx_idx) {
  643. if (UartAvrInput(dev)) {
  644. return 0;
  645. }
  646. }
  647. }
  648. ch = ifs->if_rx_buf[ifs->if_rd_idx++];
  649. if (elmode && (ch == '\r' || ch == '\n')) {
  650. if (ifs->if_last_eol == 0 || ifs->if_last_eol == ch) {
  651. ifs->if_last_eol = ch;
  652. *cp++ = '\n';
  653. rc++;
  654. }
  655. } else {
  656. ifs->if_last_eol = 0;
  657. *cp++ = ch;
  658. rc++;
  659. }
  660. }
  661. return rc;
  662. }
  663. /*!
  664. * \brief Write to device.
  665. */
  666. int UartAvrPut(NUTDEVICE * dev, const void *buffer, int len, int pflg)
  667. {
  668. int rc;
  669. IFSTREAM *ifs;
  670. UARTDCB *dcb;
  671. const uint8_t *cp;
  672. uint8_t lbmode;
  673. uint8_t elmode;
  674. uint8_t ch;
  675. ifs = dev->dev_icb;
  676. dcb = dev->dev_dcb;
  677. if (dcb->dcb_modeflags & UART_MF_LINEBUFFER)
  678. lbmode = 1;
  679. else
  680. lbmode = 0;
  681. if (dcb->dcb_modeflags & UART_MF_COOKEDMODE)
  682. elmode = 1;
  683. else
  684. elmode = 0;
  685. /*
  686. * Call without data pointer starts transmission.
  687. */
  688. if (buffer == 0) {
  689. rc = UartAvrFlush(dev);
  690. return rc;
  691. }
  692. /*
  693. * Put characters in transmit buffer.
  694. */
  695. cp = buffer;
  696. for (rc = 0; rc < len;) {
  697. if ((uint8_t) (ifs->if_wr_idx + 1) == ifs->if_tx_idx) {
  698. if (UartAvrFlush(dev)) {
  699. return -1;
  700. }
  701. }
  702. ch = pflg ? PRG_RDB(cp) : *cp;
  703. if (elmode == 1 && ch == '\n') {
  704. elmode = 2;
  705. if (lbmode == 1)
  706. lbmode = 2;
  707. ch = '\r';
  708. } else {
  709. if (elmode == 2)
  710. elmode = 1;
  711. cp++;
  712. rc++;
  713. }
  714. ifs->if_tx_buf[ifs->if_wr_idx++] = ch;
  715. }
  716. if (lbmode > 1 || (dcb->dcb_modeflags & UART_MF_NOBUFFER) != 0) {
  717. if (UartAvrFlush(dev))
  718. rc = -1;
  719. }
  720. return rc;
  721. }
  722. int UartAvrWrite(NUTFILE * fp, const void *buffer, int len)
  723. {
  724. return UartAvrPut(fp->nf_dev, buffer, len, 0);
  725. }
  726. int UartAvrWrite_P(NUTFILE * fp, PGM_P buffer, int len)
  727. {
  728. return UartAvrPut(fp->nf_dev, (const char *) buffer, len, 1);
  729. }
  730. /*!
  731. * \brief Open a device or file.
  732. */
  733. NUTFILE *UartAvrOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  734. {
  735. NUTFILE *fp = NutHeapAlloc(sizeof(NUTFILE));
  736. UARTDCB *dcb;
  737. if (fp == 0)
  738. return NUTFILE_EOF;
  739. dcb = dev->dev_dcb;
  740. if (mode & _O_BINARY)
  741. dcb->dcb_modeflags &= ~UART_MF_COOKEDMODE;
  742. else
  743. dcb->dcb_modeflags |= UART_MF_COOKEDMODE;
  744. fp->nf_dev = dev;
  745. fp->nf_fcb = NULL;
  746. return fp;
  747. }
  748. /*!
  749. * \brief Close a device or file.
  750. */
  751. int UartAvrClose(NUTFILE * fp)
  752. {
  753. NutHeapFree(fp);
  754. return 0;
  755. }
  756. /*!
  757. * \brief Request file size.
  758. */
  759. long UartAvrSize(NUTFILE * fp)
  760. {
  761. NUTDEVICE *dev;
  762. IFSTREAM *ifs;
  763. dev = fp->nf_dev;
  764. ifs = (IFSTREAM *) dev->dev_icb;
  765. return ((uint8_t)(ifs->if_rx_idx - ifs->if_rd_idx));
  766. }
  767. /*@}*/