usart1avr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (C) 2001-2007 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. * The 9-bit communication had been contributed by Brett Abbott,
  33. * Digital Telemetry Limited.
  34. *
  35. * Dave Smart contributed the synchronous mode support.
  36. */
  37. /*!
  38. * \file arch/avr/dev/usart1avr.c
  39. * \brief AVR USART1 support.
  40. *
  41. * \verbatim
  42. * $Id: usart1avr.c 5472 2013-12-06 00:16:28Z olereinhardt $
  43. * \endverbatim
  44. */
  45. #include <cfg/os.h>
  46. #include <cfg/arch/avr.h>
  47. #include <string.h>
  48. #include <sys/atom.h>
  49. #include <sys/event.h>
  50. #include <sys/timer.h>
  51. #include <dev/irqreg.h>
  52. #include <dev/usartavr.h>
  53. /*!
  54. * \name UART1 RTS Handshake Control
  55. *
  56. * UART1_RTS_BIT must be defined in arch/avr.h
  57. */
  58. #ifdef UART1_RTS_BIT
  59. #if (UART1_RTS_AVRPORT == AVRPORTB)
  60. #define UART_RTS_PORT PORTB
  61. #define UART_RTS_DDR DDRB
  62. #elif (UART1_RTS_AVRPORT == AVRPORTD)
  63. #define UART_RTS_PORT PORTD
  64. #define UART_RTS_DDR DDRD
  65. #elif (UART1_RTS_AVRPORT == AVRPORTE)
  66. #define UART_RTS_PORT PORTE
  67. #define UART_RTS_DDR DDRE
  68. #elif (UART1_RTS_AVRPORT == AVRPORTF)
  69. #define UART_RTS_PORT PORTF
  70. #define UART_RTS_DDR DDRF
  71. #elif (UART1_RTS_AVRPORT == AVRPORTG)
  72. #define UART_RTS_PORT PORTG
  73. #define UART_RTS_DDR DDRG
  74. #elif (UART1_RTS_AVRPORT == AVRPORTH)
  75. #define UART_RTS_PORT PORTH
  76. #define UART_RTS_DDR DDRH
  77. #endif
  78. #define UART_RTS_BIT UART1_RTS_BIT
  79. #endif /* UART1_RTS_BIT */
  80. #ifdef UART1_DTR_BIT
  81. #if (UART1_DTR_AVRPORT == AVRPORTB)
  82. #define UART_DTR_PORT PORTB
  83. #define UART_DTR_DDR DDRB
  84. #elif (UART1_DTR_AVRPORT == AVRPORTD)
  85. #define UART_DTR_PORT PORTD
  86. #define UART_DTR_DDR DDRD
  87. #elif (UART1_DTR_AVRPORT == AVRPORTE)
  88. #define UART_DTR_PORT PORTE
  89. #define UART_DTR_DDR DDRE
  90. #elif (UART1_DTR_AVRPORT == AVRPORTF)
  91. #define UART_DTR_PORT PORTF
  92. #define UART_DTR_DDR DDRF
  93. #elif (UART1_DTR_AVRPORT == AVRPORTG)
  94. #define UART_DTR_PORT PORTG
  95. #define UART_DTR_DDR DDRG
  96. #elif (UART1_DTR_AVRPORT == AVRPORTH)
  97. #define UART_DTR_PORT PORTH
  98. #define UART_DTR_DDR DDRH
  99. #endif
  100. #define UART_DTR_BIT UART1_DTR_BIT
  101. #endif /* UART1_DTR_BIT */
  102. /*!
  103. * \name UART1 Half Duplex Control
  104. *
  105. * UART1_HDX_BIT must be defined in arch/avr.h
  106. */
  107. #ifdef UART1_HDX_BIT
  108. #if (UART1_HDX_AVRPORT == AVRPORTB)
  109. #define UART_HDX_PORT PORTB
  110. #define UART_HDX_DDR DDRB
  111. #elif (UART1_HDX_AVRPORT == AVRPORTD)
  112. #define UART_HDX_PORT PORTD
  113. #define UART_HDX_DDR DDRD
  114. #elif (UART1_HDX_AVRPORT == AVRPORTE)
  115. #define UART_HDX_PORT PORTE
  116. #define UART_HDX_DDR DDRE
  117. #elif (UART1_HDX_AVRPORT == AVRPORTF)
  118. #define UART_HDX_PORT PORTF
  119. #define UART_HDX_DDR DDRF
  120. #elif (UART1_HDX_AVRPORT == AVRPORTG)
  121. #define UART_HDX_PORT PORTG
  122. #define UART_HDX_DDR DDRG
  123. #elif (UART1_HDX_AVRPORT == AVRPORTH)
  124. #define UART_HDX_PORT PORTH
  125. #define UART_HDX_DDR DDRH
  126. #endif /* UART1_HDX_AVRPORT */
  127. #define UART_HDX_BIT UART1_HDX_BIT
  128. #endif /* UART1_HDX_BIT */
  129. #ifdef __AVR_ENHANCED__
  130. /*
  131. * Local function prototypes.
  132. */
  133. static uint32_t AvrUsartGetSpeed(void);
  134. static int AvrUsartSetSpeed(uint32_t rate);
  135. static uint8_t AvrUsartGetDataBits(void);
  136. static int AvrUsartSetDataBits(uint8_t bits);
  137. static uint8_t AvrUsartGetParity(void);
  138. static int AvrUsartSetParity(uint8_t mode);
  139. static uint8_t AvrUsartGetStopBits(void);
  140. static int AvrUsartSetStopBits(uint8_t bits);
  141. static uint32_t AvrUsartGetFlowControl(void);
  142. static int AvrUsartSetFlowControl(uint32_t flags);
  143. static uint32_t AvrUsartGetStatus(void);
  144. static int AvrUsartSetStatus(uint32_t flags);
  145. static uint8_t AvrUsartGetClockMode(void);
  146. static int AvrUsartSetClockMode(uint8_t mode);
  147. static void AvrUsartTxStart(void);
  148. static void AvrUsartRxStart(void);
  149. static int AvrUsartInit(void);
  150. static int AvrUsartDeinit(void);
  151. /*!
  152. * \addtogroup xgUsartAvr
  153. */
  154. /*@{*/
  155. /*!
  156. * \brief USART1 device control block structure.
  157. */
  158. static USARTDCB dcb_usart1 = {
  159. 0, /* dcb_modeflags */
  160. 0, /* dcb_statusflags */
  161. 0, /* dcb_rtimeout */
  162. 0, /* dcb_wtimeout */
  163. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_tx_rbf */
  164. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_rx_rbf */
  165. 0, /* dbc_last_eol */
  166. AvrUsartInit, /* dcb_init */
  167. AvrUsartDeinit, /* dcb_deinit */
  168. AvrUsartTxStart, /* dcb_tx_start */
  169. AvrUsartRxStart, /* dcb_rx_start */
  170. AvrUsartSetFlowControl, /* dcb_set_flow_control */
  171. AvrUsartGetFlowControl, /* dcb_get_flow_control */
  172. AvrUsartSetSpeed, /* dcb_set_speed */
  173. AvrUsartGetSpeed, /* dcb_get_speed */
  174. AvrUsartSetDataBits, /* dcb_set_data_bits */
  175. AvrUsartGetDataBits, /* dcb_get_data_bits */
  176. AvrUsartSetParity, /* dcb_set_parity */
  177. AvrUsartGetParity, /* dcb_get_parity */
  178. AvrUsartSetStopBits, /* dcb_set_stop_bits */
  179. AvrUsartGetStopBits, /* dcb_get_stop_bits */
  180. AvrUsartSetStatus, /* dcb_set_status */
  181. AvrUsartGetStatus, /* dcb_get_status */
  182. AvrUsartSetClockMode, /* dcb_set_clock_mode */
  183. AvrUsartGetClockMode, /* dcb_get_clock_mode */
  184. };
  185. /*!
  186. * \name AVR USART1 Device
  187. */
  188. /*@{*/
  189. /*!
  190. * \brief USART1 device information structure.
  191. *
  192. * An application must pass a pointer to this structure to
  193. * NutRegisterDevice() before using the serial communication
  194. * driver of the AVR's on-chip USART1.
  195. *
  196. * The device is named \b uart1.
  197. *
  198. * \showinitializer
  199. */
  200. NUTDEVICE devUsartAvr1 = {
  201. 0, /* Pointer to next device, dev_next. */
  202. {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}, /* Unique device name, dev_name. */
  203. IFTYP_CHAR, /* Type of device, dev_type. */
  204. 1, /* Base address, dev_base. */
  205. 0, /* First interrupt number, dev_irq (not used). */
  206. 0, /* Interface control block, dev_icb (not used). */
  207. &dcb_usart1, /* Driver control block, dev_dcb. */
  208. UsartInit, /* Driver initialization routine, dev_init. */
  209. UsartIOCtl, /* Driver specific control function, dev_ioctl. */
  210. UsartRead, /* Read from device, dev_read. */
  211. UsartWrite, /* Write to device, dev_write. */
  212. UsartWrite_P, /* Write data from program space to device, dev_write_P. */
  213. UsartOpen, /* Open a device or file, dev_open. */
  214. UsartClose, /* Close a device or file, dev_close. */
  215. UsartSize, /* Request file size, dev_size. */
  216. UsartSelect, /* Select function, dev_select. */
  217. };
  218. /*@}*/
  219. /*!
  220. * \name UART1 CTS Handshake Sense
  221. *
  222. * \ref UART1_CTS_IRQ must be defined in arch/avr.h
  223. */
  224. // added extra ifdef as test below is true even if UART1_CTS_IRQ is undef
  225. #ifdef UART1_CTS_IRQ
  226. #if (UART1_CTS_IRQ == INT0)
  227. #define UART_CTS_SIGNAL sig_INTERRUPT0
  228. #define UART_CTS_BIT 0
  229. #define UART_CTS_PORT PORTD
  230. #define UART_CTS_PIN PIND
  231. #define UART_CTS_DDR DDRD
  232. #elif (UART1_CTS_IRQ == INT1)
  233. #define UART_CTS_SIGNAL sig_INTERRUPT1
  234. #define UART_CTS_BIT 1
  235. #define UART_CTS_PORT PORTD
  236. #define UART_CTS_PIN PIND
  237. #define UART_CTS_DDR DDRD
  238. #elif (UART1_CTS_IRQ == INT2)
  239. #define UART_CTS_SIGNAL sig_INTERRUPT2
  240. #define UART_CTS_BIT 2
  241. #define UART_CTS_PORT PORTD
  242. #define UART_CTS_PIN PIND
  243. #define UART_CTS_DDR DDRD
  244. #elif (UART1_CTS_IRQ == INT3)
  245. #define UART_CTS_SIGNAL sig_INTERRUPT3
  246. #define UART_CTS_BIT 3
  247. #define UART_CTS_PORT PORTD
  248. #define UART_CTS_PIN PIND
  249. #define UART_CTS_DDR DDRD
  250. #elif (UART1_CTS_IRQ == INT4)
  251. #define UART_CTS_SIGNAL sig_INTERRUPT4
  252. #define UART_CTS_BIT 4
  253. #define UART_CTS_PORT PORTE
  254. #define UART_CTS_PIN PINE
  255. #define UART_CTS_DDR DDRE
  256. #elif (UART1_CTS_IRQ == INT5)
  257. #define UART_CTS_SIGNAL sig_INTERRUPT5
  258. #define UART_CTS_BIT 5
  259. #define UART_CTS_PORT PORTE
  260. #define UART_CTS_PIN PINE
  261. #define UART_CTS_DDR DDRE
  262. #elif (UART1_CTS_IRQ == INT6)
  263. #define UART_CTS_SIGNAL sig_INTERRUPT6
  264. #define UART_CTS_BIT 6
  265. #define UART_CTS_PORT PORTE
  266. #define UART_CTS_PIN PINE
  267. #define UART_CTS_DDR DDRE
  268. #elif (UART1_CTS_IRQ == INT7)
  269. #define UART_CTS_SIGNAL sig_INTERRUPT7
  270. #define UART_CTS_BIT 7
  271. #define UART_CTS_PORT PORTE
  272. #define UART_CTS_PIN PINE
  273. #define UART_CTS_DDR DDRE
  274. #endif
  275. #else
  276. // alternate way to specify the cts line
  277. #define UART_CTS_PORT UART1_CTS_PORT
  278. #define UART_CTS_PIN UART1_CTS_PIN
  279. #define UART_CTS_DDR UART1_CTS_DDR
  280. // only set CTS_BIT if used and IRQ available
  281. #ifdef UART1_CTS_BIT
  282. #define UART_CTS_SIGNAL UART1_CTS_SIGNAL
  283. #define UART_CTS_BIT UART1_CTS_BIT
  284. #endif
  285. #endif
  286. /*@}*/
  287. /*@}*/
  288. #define UDRn UDR1
  289. #define UCSRnA UCSR1A
  290. #define UCSRnB UCSR1B
  291. #define UCSRnC UCSR1C
  292. #define UBRRnL UBRR1L
  293. #define UBRRnH UBRR1H
  294. #ifdef __IMAGECRAFT__
  295. #define TXB8 TXB81
  296. #if defined(ATMega2560) || defined(ATMega2561) || defined(MCU_AT90USB1287)
  297. #define UMSEL UMSEL01
  298. #else
  299. #define UMSEL UMSEL1
  300. #endif
  301. #define U2X U2X1
  302. #define UCSZ2 UCSZ12
  303. #define UCSZ1 UCSZ11
  304. #define UCSZ0 UCSZ10
  305. #define UPM0 UPM10
  306. #define UPM1 UPM11
  307. #define USBS USBS1
  308. #define UPE UPE1
  309. #define MPCM MPCM1
  310. #define UCPOL UCPOL1
  311. #endif
  312. #define sig_UART_RECV sig_UART1_RECV
  313. #define sig_UART_DATA sig_UART1_DATA
  314. #define sig_UART_TRANS sig_UART1_TRANS
  315. /* avr-libc names the vector as in the datasheets. As Atmel naming is
  316. * inconsistant, so is the avr-libc naming.
  317. * Equalize!
  318. */
  319. #if !defined(USART1_RX_vect) && defined(UART1_RX_vect)
  320. #define USART1_RX_vect UART1_RX_vect
  321. #endif
  322. #define SIG_AVRUART_RECV USART1_RX_vect
  323. #if !defined(USART1_UDRE_vect) && defined(UART1_UDRE_vect)
  324. #define USART1_UDRE_vect UART1_UDRE_vect
  325. #endif
  326. #define SIG_AVRUART_DATA USART1_UDRE_vect
  327. #if !defined(USART1_TX_vect) && defined(UART1_TX_vect)
  328. #define USART1_TX_vect UART1_TX_vect
  329. #endif
  330. #define SIG_AVRUART_TRANS USART1_TX_vect
  331. #define dcb_usart dcb_usart1
  332. #ifdef NUTTRACER
  333. #define TRACE_INT_UART_CTS TRACE_INT_UART1_CTS
  334. #define TRACE_INT_UART_RXCOMPL TRACE_INT_UART1_RXCOMPL
  335. #define TRACE_INT_UART_TXEMPTY TRACE_INT_UART1_TXEMPTY
  336. #endif
  337. /* Define to allow IRQ handler to read all bytes from RX FIFO. */
  338. #ifdef UART1_READMULTIBYTE
  339. #define UART_READMULTIBYTE
  340. #endif
  341. /* Define to use native interrupt handler. */
  342. #ifdef USE_USART1
  343. #define USE_USART
  344. #endif
  345. /* Define to bypass software flow control. */
  346. #ifdef UART1_NO_SW_FLOWCONTROL
  347. #define UART_NO_SW_FLOWCONTROL
  348. #endif
  349. #include "usartavr.c"
  350. #endif