usart0cb_avr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * Copyright (C) 2012-2013 by egnite GmbH
  3. * Copyright (C) 2001-2003 by egnite Software GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. */
  35. /*!
  36. * \file arch/avr/usart0cb_avr.c
  37. * \brief Low level routines for UART0 on AVR.
  38. *
  39. * Contains those functions, which are implemented individually for UART0.
  40. *
  41. * Note, that it is essential for most AVR chips to use constants as port
  42. * addresses. This will allow the compiler to generate bit operations.
  43. * Otherwise, it will result in duplicate code for multiple UARTs. But
  44. * this is OK, because most applications will not use more than one UART.
  45. *
  46. * \verbatim
  47. * $Id$
  48. * \endverbatim
  49. */
  50. #include <cfg/arch/avr.h>
  51. #include <stdint.h>
  52. #include <dev/irqreg.h>
  53. #include <dev/usart.h>
  54. #include <sys/timer.h>
  55. #include <sys/atom.h>
  56. #include <arch/avr/usart_avrctl.h>
  57. #include <arch/avr/usart_cb_avr.h>
  58. #ifndef UART0_INIT_BAUDRATE
  59. #define UART0_INIT_BAUDRATE 115200
  60. #endif
  61. /*!
  62. * \name UART0 CTS Handshake Sense
  63. *
  64. * UART0_CTS_IRQ must be defined to enable CTS sensing.
  65. */
  66. #ifdef UART0_CTS_IRQ
  67. #undef UART_CTS_IRQ
  68. #define UART_CTS_IRQ UART0_CTS_IRQ
  69. #include <arch/avr/usart_avrcts.h>
  70. #endif
  71. /*!
  72. * \name UART0 RTS Handshake Control
  73. *
  74. * UART0_RTS_BIT and UART0_RTS_PORT must be defined to enable RTS control.
  75. */
  76. #if defined(UART0_RTS_BIT) && defined(UART0_RTS_AVRPORT)
  77. #if (UART0_RTS_AVRPORT == AVRPORTB)
  78. #define UART0_RTS_PORT PORTB
  79. #define UART0_RTS_DDR DDRB
  80. #elif (UART0_RTS_AVRPORT == AVRPORTD)
  81. #define UART0_RTS_PORT PORTD
  82. #define UART0_RTS_DDR DDRD
  83. #elif (UART0_RTS_AVRPORT == AVRPORTE)
  84. #define UART0_RTS_PORT PORTE
  85. #define UART0_RTS_DDR DDRE
  86. #elif (UART0_RTS_AVRPORT == AVRPORTF)
  87. #define UART0_RTS_PORT PORTF
  88. #define UART0_RTS_DDR DDRF
  89. #elif (UART0_RTS_AVRPORT == AVRPORTG)
  90. #define UART0_RTS_PORT PORTG
  91. #define UART0_RTS_DDR DDRG
  92. #elif (UART0_RTS_AVRPORT == AVRPORTH)
  93. #define UART0_RTS_PORT PORTH
  94. #define UART0_RTS_DDR DDRH
  95. #endif
  96. #endif
  97. static AVRUSART_IFC ifc_usart0 = { 0, 0 };
  98. static void AvrUsart0RxData(void *arg)
  99. {
  100. uint8_t ch;
  101. USARTCB_DCB *dcb;
  102. cb_size_t idx;
  103. USARTCB_RXBUFF *rxcb;
  104. uint8_t err;
  105. err = inb(UCSR0A) & (_BV(FE) | _BV(DOR) | _BV(UPE));
  106. ch = inb(UDR0);
  107. if (err == 0) {
  108. dcb = (USARTCB_DCB *) arg;
  109. rxcb = &dcb->usart_rx_buff;
  110. idx = (rxcb->rxb_wri + 1) & rxcb->rxb_siz;
  111. if (idx == rxcb->rxb_rdi) {
  112. /* Receive buffer overflow. */
  113. cbi(UCSR0B, RXCIE);
  114. } else {
  115. rxcb->rxb_buf[rxcb->rxb_wri] = ch;
  116. rxcb->rxb_wri = idx;
  117. if (rxcb->rxb_cnt++ == 0) {
  118. NutEventPostFromIrq(&rxcb->rxb_que);
  119. }
  120. /* If an RTS port has been defined and if RTS control has
  121. been enabled and if the number of bytes in the receive
  122. buffer reached the high watermark, then disable RTS to
  123. stop the remote transmitter. */
  124. #if defined(UART0_RTS_PORT) && defined(UART0_RTS_BIT)
  125. if ((dcb->usart_mode & USART_MF_RTSCONTROL) != 0 && rxcb->rxb_cnt >= dcb->usart_rx_hiwm) {
  126. sbi(UART0_RTS_PORT, UART0_RTS_BIT);
  127. }
  128. #endif
  129. }
  130. } else {
  131. ifc_usart0.uifc_errors |= err;
  132. }
  133. }
  134. #ifdef UART_CTS_BIT
  135. /*!
  136. * \brief USART0 CTS sense interrupt handler.
  137. *
  138. * This interrupt routine is called when the CTS line level is low.
  139. * Typical line drivers negate the signal, thus driving our port
  140. * low when CTS is active.
  141. *
  142. * This routine exists only if the hardware configuration defines a
  143. * port bit to sense the CTS signal.
  144. */
  145. static void AvrUsart0CtsActive(void *arg)
  146. {
  147. /* Enable transmit interrupt. */
  148. sbi(UCSR0B, UDRIE);
  149. /* Disable CTS sense interrupt. */
  150. cbi(EIMSK, UART_CTS_BIT);
  151. }
  152. #endif
  153. static void AvrUsart0TxData(void *arg)
  154. {
  155. USARTCB_DCB *dcb = (USARTCB_DCB *) arg;
  156. USARTCB_TXBUFF *txcb = &dcb->usart_tx_buff;
  157. cb_size_t idx;
  158. idx = txcb->txb_rdi;
  159. if (idx == txcb->txb_wri) {
  160. /* Transmit buffer underrun. */
  161. cbi(UCSR0B, UDRIE);
  162. NutEventPostFromIrq(&txcb->txb_que);
  163. } else {
  164. #if defined(UART_CTS_BIT) && defined(UART_CTS_PIN)
  165. /* If CTS has been disabled, we disable the transmit interrupts
  166. and return without sending anything. */
  167. if ((dcb->usart_mode & USART_MF_CTSSENSE) != 0 && bit_is_set(UART_CTS_PIN, UART_CTS_BIT)) {
  168. cbi(UCSR0B, UDRIE);
  169. sbi(EIMSK, UART_CTS_BIT);
  170. return;
  171. }
  172. #endif
  173. outb(UDR0, txcb->txb_buf[idx]);
  174. txcb->txb_rdi = (idx + 1) & txcb->txb_siz;
  175. if (txcb->txb_cnt-- == txcb->txb_siz) {
  176. NutEventPostFromIrq(&txcb->txb_que);
  177. }
  178. }
  179. }
  180. /*!
  181. * \brief Enable AVR USART0.
  182. */
  183. static int AvrUsart0Enable(USARTCB_DCB *dcb)
  184. {
  185. if (NutRegisterIrqHandler(&sig_UART0_RECV, AvrUsart0RxData, dcb)) {
  186. return -1;
  187. }
  188. if (NutRegisterIrqHandler(&sig_UART0_DATA, AvrUsart0TxData, dcb)) {
  189. return -1;
  190. }
  191. #if defined(UART_CTS_BIT)
  192. #if defined(UART_CTS_PORT)
  193. sbi(UART_CTS_PORT, UART_CTS_BIT);
  194. #endif
  195. #if defined(UART_CTS_DDR)
  196. cbi(UART_CTS_DDR, UART_CTS_BIT);
  197. #endif
  198. #endif
  199. #if defined(UART_CTS_SIGNAL)
  200. NutRegisterIrqHandler(&UART_CTS_SIGNAL, AvrUsart0CtsActive, 0);
  201. NutIrqSetMode(&UART_CTS_SIGNAL, NUT_IRQMODE_FALLINGEDGE);
  202. #endif
  203. #if UART0_INIT_BAUDRATE
  204. {
  205. uint32_t baud = UART0_INIT_BAUDRATE;
  206. AvrUsartControl(dcb, UART_SETSPEED, &baud);
  207. }
  208. #endif
  209. sbi(UCSR0B, TXEN);
  210. sbi(UCSR0B, RXEN);
  211. #if defined(UART0_RTS_DDR) && defined(UART0_RTS_BIT)
  212. sbi(UART0_RTS_DDR, UART0_RTS_BIT);
  213. #endif
  214. return 0;
  215. }
  216. static int AvrUsart0Disable(USARTCB_DCB *dcb)
  217. {
  218. return -1;
  219. }
  220. static void AvrUsart0TxStart(USARTCB_DCB *dcb)
  221. {
  222. USARTCB_TXBUFF *txcb = &dcb->usart_tx_buff;
  223. cb_size_t idx;
  224. #if defined(UART_CTS_BIT) && defined(UART_CTS_PIN)
  225. if ((dcb->usart_mode & USART_MF_CTSSENSE) != 0 && bit_is_set(UART_CTS_PIN, UART_CTS_BIT)) {
  226. sbi(EIMSK, UART_CTS_BIT);
  227. return;
  228. }
  229. #endif
  230. if ((inb(UCSR0A) & _BV(TXC0)) == 0 && (inb(UCSR0A) & _BV(UDRE0)) != 0) {
  231. idx = txcb->txb_rdi;
  232. if (idx != txcb->txb_wri) {
  233. txcb->txb_rdi = (idx + 1) & txcb->txb_siz;
  234. outb(UDR0, txcb->txb_buf[idx]);
  235. }
  236. }
  237. sbi(UCSR0B, UDRIE);
  238. }
  239. static void AvrUsart0TxStop(USARTCB_DCB *dcb)
  240. {
  241. /* Disable receive and transmit interrupts. */
  242. cbi(UCSR0B, TXCIE);
  243. cbi(UCSR0B, UDRIE);
  244. }
  245. static void AvrUsart0RxStart(USARTCB_DCB *dcb)
  246. {
  247. sbi(UCSR0B, RXCIE);
  248. #if defined(UART0_RTS_PORT) && defined(UART0_RTS_BIT)
  249. if (dcb->usart_rx_buff.rxb_cnt < dcb->usart_rx_hiwm) {
  250. /* Enable handshake. */
  251. cbi(UART0_RTS_PORT, UART0_RTS_BIT);
  252. }
  253. #endif
  254. }
  255. static void AvrUsart0RxStop(USARTCB_DCB *dcb)
  256. {
  257. /* Disable receive and transmit interrupts. */
  258. cbi(UCSR0B, RXCIE);
  259. }
  260. /*!
  261. * \brief Set and query the UART status.
  262. */
  263. static uint32_t AvrUsart0Status(USARTCB_DCB *dcb, uint32_t stat)
  264. {
  265. uint32_t rc = 0;
  266. /*
  267. * Set receiver error flags.
  268. */
  269. if (ifc_usart0.uifc_errors & _BV(FE0)) {
  270. rc |= UART_FRAMINGERROR;
  271. }
  272. if (ifc_usart0.uifc_errors & _BV(DOR0)) {
  273. rc |= UART_OVERRUNERROR;
  274. }
  275. if (ifc_usart0.uifc_errors & _BV(UPE0)) {
  276. rc |= UART_PARITYERROR;
  277. }
  278. ifc_usart0.uifc_errors = 0;
  279. return rc;
  280. }
  281. static USARTCB_DCB dcb_usart0 = {
  282. (uintptr_t) &ifc_usart0,/* usart_hwif. */
  283. AvrUsart0Enable, /* usart_enable(). */
  284. AvrUsart0Disable, /* usart_disable(). */
  285. AvrUsartControl, /* usart_control(). */
  286. 0, /* usart_mode. */
  287. #if defined(UART0_RTS_BIT)
  288. USART_MF_RTSCONTROL |
  289. #endif
  290. #if defined(UART_CTS_BIT)
  291. USART_MF_CTSSENSE |
  292. #endif
  293. USART_MF_COOKEDMODE, /* usart_caps. */
  294. AvrUsart0Status, /* usart_status(). */
  295. { NULL, 0, 0, 0, NULL, 0 }, /* usart_tx_buff. */
  296. 0, /* usart_wr_tmo. */
  297. AvrUsart0TxStart, /* usart_tx_start(). */
  298. AvrUsart0TxStop, /* usart_tx_stop(). */
  299. { NULL, 0, 0, 0, NULL, 0 }, /* usart_rx_buff. */
  300. 0, /* usart_rd_tmo. */
  301. 0, /* usart_rx_cr. */
  302. 0, /* usart_rx_lowm. */
  303. 0, /* usart_rx_hiwm. */
  304. AvrUsart0RxStart, /* usart_rx_start(). */
  305. AvrUsart0RxStop /* usart_rx_stop(). */
  306. };
  307. NUTDEVICE devUsart0CbAvr = {
  308. NULL, /* Pointer to next device, dev_next. */
  309. { 'u', 'a', 'r', 't', '0', 0, 0, 0, 0 }, /* Hardware device name, dev_name. */
  310. IFTYP_CHAR, /* Type of device, dev_type. */
  311. 0, /* Base address, dev_base (not used). */
  312. 0, /* First interrupt number, dev_irq (not used). */
  313. NULL, /* Interface control block, dev_icb (not used). */
  314. &dcb_usart0, /* Driver control block, dev_dcb. */
  315. UsartCbInit, /* Driver initialization routine, dev_init. */
  316. UsartCbIoCtrl, /* Driver specific control function, dev_ioctl. */
  317. UsartCbRead, /* Read from device, dev_read. */
  318. UsartCbWrite, /* Write to device, dev_write. */
  319. UsartCbWrite_P, /* Write to device, dev_write_P. */
  320. UsartCbOpen, /* Open a device or file, dev_open. */
  321. UsartCbClose, /* Close a device or file, dev_close. */
  322. UsartCbSize, /* Request file size, dev_size. */
  323. NULL /* Select function, optional, not yet implemented */
  324. };