usart0at91.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright (C) 2005 by egnite Software GmbH
  3. * Copyright 2009 by egnite 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. * $Id: usart0at91.c 5472 2013-12-06 00:16:28Z olereinhardt $
  37. */
  38. #include <cfg/os.h>
  39. #include <cfg/clock.h>
  40. #include <cfg/arch.h>
  41. #include <cfg/uart.h>
  42. #include <cfg/arch/gpio.h>
  43. #include <string.h>
  44. #include <sys/atom.h>
  45. #include <sys/event.h>
  46. #include <sys/timer.h>
  47. #include <dev/irqreg.h>
  48. #include <dev/gpio.h>
  49. #include <dev/usartat91.h>
  50. #ifndef NUT_CPU_FREQ
  51. #ifdef NUT_PLL_CPUCLK
  52. #include <dev/cy2239x.h>
  53. #else /* !NUT_PLL_CPUCLK */
  54. #define NUT_CPU_FREQ 73728000UL
  55. #endif /* !NUT_PLL_CPUCLK */
  56. #endif /* !NUT_CPU_FREQ */
  57. /*
  58. * Local function prototypes.
  59. */
  60. static uint32_t At91UsartGetSpeed(void);
  61. static int At91UsartSetSpeed(uint32_t rate);
  62. static uint8_t At91UsartGetDataBits(void);
  63. static int At91UsartSetDataBits(uint8_t bits);
  64. static uint8_t At91UsartGetParity(void);
  65. static int At91UsartSetParity(uint8_t mode);
  66. static uint8_t At91UsartGetStopBits(void);
  67. static int At91UsartSetStopBits(uint8_t bits);
  68. static uint32_t At91UsartGetFlowControl(void);
  69. static int At91UsartSetFlowControl(uint32_t flags);
  70. static uint32_t At91UsartGetStatus(void);
  71. static int At91UsartSetStatus(uint32_t flags);
  72. static uint8_t At91UsartGetClockMode(void);
  73. static int At91UsartSetClockMode(uint8_t mode);
  74. static void At91UsartTxStart(void);
  75. static void At91UsartRxStart(void);
  76. static int At91UsartInit(void);
  77. static int At91UsartDeinit(void);
  78. /*!
  79. * \addtogroup xgNutArchArmAt91Us
  80. */
  81. /*@{*/
  82. /*!
  83. * \brief USART0 device control block structure.
  84. */
  85. static USARTDCB dcb_usart0 = {
  86. 0, /* dcb_modeflags */
  87. 0, /* dcb_statusflags */
  88. 0, /* dcb_rtimeout */
  89. 0, /* dcb_wtimeout */
  90. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_tx_rbf */
  91. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_rx_rbf */
  92. 0, /* dbc_last_eol */
  93. At91UsartInit, /* dcb_init */
  94. At91UsartDeinit, /* dcb_deinit */
  95. At91UsartTxStart, /* dcb_tx_start */
  96. At91UsartRxStart, /* dcb_rx_start */
  97. At91UsartSetFlowControl, /* dcb_set_flow_control */
  98. At91UsartGetFlowControl, /* dcb_get_flow_control */
  99. At91UsartSetSpeed, /* dcb_set_speed */
  100. At91UsartGetSpeed, /* dcb_get_speed */
  101. At91UsartSetDataBits, /* dcb_set_data_bits */
  102. At91UsartGetDataBits, /* dcb_get_data_bits */
  103. At91UsartSetParity, /* dcb_set_parity */
  104. At91UsartGetParity, /* dcb_get_parity */
  105. At91UsartSetStopBits, /* dcb_set_stop_bits */
  106. At91UsartGetStopBits, /* dcb_get_stop_bits */
  107. At91UsartSetStatus, /* dcb_set_status */
  108. At91UsartGetStatus, /* dcb_get_status */
  109. At91UsartSetClockMode, /* dcb_set_clock_mode */
  110. At91UsartGetClockMode, /* dcb_get_clock_mode */
  111. };
  112. /*!
  113. * \name AT91 USART0 Device
  114. */
  115. /*@{*/
  116. /*!
  117. * \brief USART0 device information structure.
  118. *
  119. * An application must pass a pointer to this structure to
  120. * NutRegisterDevice() before using the serial communication
  121. * driver of the AT91's on-chip USART0.
  122. *
  123. * The device is named \b uart0.
  124. *
  125. * \showinitializer
  126. */
  127. NUTDEVICE devUsartAt910 = {
  128. 0, /* Pointer to next device, dev_next. */
  129. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}, /* Unique device name, dev_name. */
  130. IFTYP_CHAR, /* Type of device, dev_type. */
  131. 0, /* Base address, dev_base (not used). */
  132. 0, /* First interrupt number, dev_irq (not used). */
  133. 0, /* Interface control block, dev_icb (not used). */
  134. &dcb_usart0, /* Driver control block, dev_dcb. */
  135. UsartInit, /* Driver initialization routine, dev_init. */
  136. UsartIOCtl, /* Driver specific control function, dev_ioctl. */
  137. UsartRead, /* Read from device, dev_read. */
  138. UsartWrite, /* Write to device, dev_write. */
  139. UsartOpen, /* Open a device or file, dev_open. */
  140. UsartClose, /* Close a device or file, dev_close. */
  141. UsartSize, /* Request file size, dev_size. */
  142. UsartSelect, /* Select function, dev_select */
  143. };
  144. /*@}*/
  145. /*@}*/
  146. /* Modem control includes hardware handshake. */
  147. #if defined(UART0_MODEM_CONTROL)
  148. #define UART_MODEM_CONTROL
  149. #define UART_HARDWARE_HANDSHAKE
  150. #elif defined(UART0_HARDWARE_HANDSHAKE)
  151. #define UART_HARDWARE_HANDSHAKE
  152. #endif
  153. /*
  154. ** SAM9260 and SAM9XE pins.
  155. */
  156. #if defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE)
  157. #define UART_RXTX_PINS (_BV(PB5_RXD0_A) | _BV(PB4_TXD0_A))
  158. #define UART_HDX_PIN _BV(PB26_RTS0_A)
  159. #define UART_RTS_PIN _BV(PB26_RTS0_A)
  160. #define UART_CTS_PIN _BV(PB27_CTS0_A)
  161. #define UART_MODEM_PINS (_BV(PB24_DTR0_A) | _BV(PB22_DSR0_A) | _BV(PB23_DCD0_A) | _BV(PB25_RI0_A))
  162. #define UART_RXTX_PINS_ENABLE() outr(PIOB_ASR, UART_RXTX_PINS); \
  163. outr(PIOB_PDR, UART_RXTX_PINS)
  164. #if defined(UART_HARDWARE_HANDSHAKE)
  165. #define UART_HDX_PIN_ENABLE() outr(PIOB_ASR, UART_HDX_PIN); \
  166. outr(PIOB_PDR, UART_HDX_PIN)
  167. #define UART_RTS_PIN_ENABLE() outr(PIOB_ASR, UART_RTS_PIN); \
  168. outr(PIOB_PDR, UART_RTS_PIN)
  169. #define UART_CTS_PIN_ENABLE() outr(PIOB_ASR, UART_CTS_PIN); \
  170. outr(PIOB_PDR, UART_CTS_PIN)
  171. #endif
  172. #if defined(UART_MODEM_CONTROL)
  173. #define UART_MODEM_PINS_ENABLE() outr(PIOB_ASR, UART_MODEM_PINS); \
  174. outr(PIOB_PDR, UART_MODEM_PINS)
  175. #endif
  176. /*
  177. ** SAM7S and SAM7SE pins.
  178. */
  179. #elif defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
  180. #define UART_RXTX_PINS (_BV(PA5_RXD0_A) | _BV(PA6_TXD0_A))
  181. #define UART_HDX_PIN _BV(PA7_RTS0_A)
  182. #define UART_RTS_PIN _BV(PA7_RTS0_A)
  183. #define UART_CTS_PIN _BV(PA8_CTS0_A)
  184. #define UART_RXTX_PINS_ENABLE() outr(PIOA_ASR, UART_RXTX_PINS); \
  185. outr(PIOA_PDR, UART_RXTX_PINS)
  186. #if defined(UART_HARDWARE_HANDSHAKE)
  187. #define UART_HDX_PIN_ENABLE() outr(PIOA_ASR, UART_HDX_PIN); \
  188. outr(PIOA_PDR, UART_HDX_PIN)
  189. #define UART_RTS_PIN_ENABLE() outr(PIOA_ASR, UART_RTS_PIN); \
  190. outr(PIOA_PDR, UART_RTS_PIN)
  191. #define UART_CTS_PIN_ENABLE() outr(PIOA_ASR, UART_CTS_PIN); \
  192. outr(PIOA_PDR, UART_CTS_PIN)
  193. #endif
  194. /*
  195. ** AT91SAM9G45 pins.
  196. */
  197. #elif defined(MCU_AT91SAM9G45)
  198. #define UART_RXTX_PINS (_BV(PB19_TXD0_A) | _BV(PB18_RXD0_A))
  199. #define UART_HDX_PIN _BV(PB17_RTS0_B)
  200. #define UART_RTS_PIN _BV(PB17_RTS0_B)
  201. #define UART_CTS_PIN _BV(PB15_CTS0_B)
  202. #define UART_RXTX_PINS_ENABLE() outr(PIOB_ASR, UART_RXTX_PINS); \
  203. outr(PIOB_PDR, UART_RXTX_PINS)
  204. #if defined(UART_HARDWARE_HANDSHAKE)
  205. #define UART_HDX_PIN_ENABLE() outr(PIOD_BSR, UART_HDX_PIN); \
  206. outr(PIOD_PDR, UART_HDX_PIN)
  207. #define UART_RTS_PIN_ENABLE() outr(PIOD_BSR, UART_RTS_PIN); \
  208. outr(PIOD_PDR, UART_RTS_PIN)
  209. #define UART_CTS_PIN_ENABLE() outr(PIOD_BSR, UART_CTS_PIN); \
  210. outr(PIOD_PDR, UART_CTS_PIN)
  211. #endif
  212. /*
  213. ** SAM7X pins.
  214. */
  215. #elif defined(MCU_AT91SAM7X)
  216. #define UART_RXTX_PINS (_BV(PA0_RXD0_A) | _BV(PA1_TXD0_A))
  217. #define UART_HDX_PIN _BV(PA3_RTS0_A)
  218. #define UART_RTS_PIN _BV(PA3_RTS0_A)
  219. #define UART_CTS_PIN _BV(PA4_CTS0_A)
  220. #define UART_RXTX_PINS_ENABLE() outr(PIOA_ASR, UART_RXTX_PINS); \
  221. outr(PIOA_PDR, UART_RXTX_PINS)
  222. #if defined(UART_HARDWARE_HANDSHAKE)
  223. #define UART_HDX_PIN_ENABLE() outr(PIOA_ASR, UART_HDX_PIN); \
  224. outr(PIOA_PDR, UART_HDX_PIN)
  225. #define UART_RTS_PIN_ENABLE() outr(PIOA_ASR, UART_RTS_PIN); \
  226. outr(PIOA_PDR, UART_RTS_PIN)
  227. #define UART_CTS_PIN_ENABLE() outr(PIOA_ASR, UART_CTS_PIN); \
  228. outr(PIOA_PDR, UART_CTS_PIN)
  229. #endif
  230. /*
  231. ** X40 pins.
  232. */
  233. #elif defined(MCU_AT91R40008)
  234. #define UART_RXTX_PINS (_BV(P15_RXD0) | _BV(P14_TXD0))
  235. #define UART_RXTX_PINS_ENABLE() outr(PIO_PDR, UART_RXTX_PINS)
  236. /*
  237. ** Add more targets here.
  238. **
  239. ** For unsupported targets you may also do basic initializations in
  240. ** your application code.
  241. */
  242. #endif
  243. /*
  244. ** CPLD logic, currently used on Ethernut 3 only.
  245. */
  246. #if defined(ETHERNUT3)
  247. #define UART_USES_NPL 1
  248. #endif
  249. /*
  250. ** Determine the CTS GPIO interrupt, based on the port ID.
  251. */
  252. #if defined(UART0_CTS_BIT) && !defined(UART0_CTS_SIGNAL)
  253. #if UART0_CTS_PIO_ID == PIOA_ID
  254. #define UART0_CTS_SIGNAL sig_GPIO1
  255. #elif UART0_CTS_PIO_ID == PIOB_ID
  256. #define UART0_CTS_SIGNAL sig_GPIO2
  257. #elif UART0_CTS_PIO_ID == PIOC_ID
  258. #define UART0_CTS_SIGNAL sig_GPIO3
  259. #else
  260. #define UART0_CTS_SIGNAL sig_GPIO
  261. #endif
  262. #endif
  263. /*
  264. ** Translate all macros for UART0 to generalized ones used by the
  265. ** source that will be included at the end of this file.
  266. */
  267. #if defined(UART0_HDX_BIT)
  268. #define UART_HDX_BIT UART0_HDX_BIT
  269. #endif
  270. #if defined(UART0_HDX_PIO_ID)
  271. #define UART_HDX_PIO_ID UART0_HDX_PIO_ID
  272. #endif
  273. #if defined(UART0_RTS_BIT)
  274. #define UART_RTS_BIT UART0_RTS_BIT
  275. #endif
  276. #if defined(UART0_RTS_PIO_ID)
  277. #define UART_RTS_PIO_ID UART0_RTS_PIO_ID
  278. #endif
  279. #if defined(UART0_CTS_BIT)
  280. #define UART_CTS_BIT UART0_CTS_BIT
  281. #endif
  282. #if defined(UART0_CTS_PIO_ID)
  283. #define UART_CTS_PIO_ID UART0_CTS_PIO_ID
  284. #endif
  285. #if defined(UART0_CTS_SIGNAL)
  286. #define UART_CTS_SIGNAL UART0_CTS_SIGNAL
  287. #endif
  288. #if defined(UART0_INIT_BAUDRATE)
  289. #define UART_INIT_BAUDRATE UART0_INIT_BAUDRATE
  290. #endif
  291. #define USARTn_BASE USART0_BASE
  292. #define US_ID US0_ID
  293. #define SIG_UART sig_UART0
  294. #define dcb_usart dcb_usart0
  295. #include "usartat91.c"