usart1at91.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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: usart1at91.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 USART1 device control block structure.
  84. */
  85. static USARTDCB dcb_usart1 = {
  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 USART1 Device
  114. */
  115. /*@{*/
  116. /*!
  117. * \brief USART1 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 USART1.
  122. *
  123. * The device is named \b uart1.
  124. *
  125. * \showinitializer
  126. */
  127. NUTDEVICE devUsartAt911 = {
  128. 0, /* Pointer to next device, dev_next. */
  129. {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}, /* Unique device name, dev_name. */
  130. IFTYP_CHAR, /* Type of device, dev_type. */
  131. 1, /* 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_usart1, /* 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(UART1_MODEM_CONTROL)
  148. #define UART_MODEM_CONTROL
  149. #define UART_HARDWARE_HANDSHAKE
  150. #elif defined(UART1_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(PB7_RXD1_A) | _BV(PB6_TXD1_A))
  158. #define UART_HDX_PIN _BV(PB28_RTS1_A)
  159. #define UART_RTS_PIN _BV(PB28_RTS1_A)
  160. #define UART_CTS_PIN _BV(PB29_CTS1_A)
  161. #define UART_RXTX_PINS_ENABLE() outr(PIOB_ASR, UART_RXTX_PINS); \
  162. outr(PIOB_PDR, UART_RXTX_PINS)
  163. #if defined(UART_HARDWARE_HANDSHAKE)
  164. #define UART_HDX_PIN_ENABLE() outr(PIOB_ASR, UART_HDX_PIN); \
  165. outr(PIOB_PDR, UART_HDX_PIN)
  166. #define UART_RTS_PIN_ENABLE() outr(PIOB_ASR, UART_RTS_PIN); \
  167. outr(PIOB_PDR, UART_RTS_PIN)
  168. #define UART_CTS_PIN_ENABLE() outr(PIOB_ASR, UART_CTS_PIN); \
  169. outr(PIOB_PDR, UART_CTS_PIN)
  170. #endif
  171. /*
  172. ** AT91SAM9G45 pins.
  173. */
  174. #elif defined(MCU_AT91SAM9G45)
  175. #define UART_RXTX_PINS (_BV(PB4_TXD1_A) | _BV(PB5_RXD1_A))
  176. #define UART_HDX_PIN _BV(PD16_RTS1_A)
  177. #define UART_RTS_PIN _BV(PD16_RTS1_A)
  178. #define UART_CTS_PIN _BV(PD17_CTS1_A)
  179. #define UART_RXTX_PINS_ENABLE() outr(PIOB_ASR, UART_RXTX_PINS); \
  180. outr(PIOB_PDR, UART_RXTX_PINS)
  181. #if defined(UART_HARDWARE_HANDSHAKE)
  182. #define UART_HDX_PIN_ENABLE() outr(PIOD_ASR, UART_HDX_PIN); \
  183. outr(PIOD_PDR, UART_HDX_PIN)
  184. #define UART_RTS_PIN_ENABLE() outr(PIOD_ASR, UART_RTS_PIN); \
  185. outr(PIOD_PDR, UART_RTS_PIN)
  186. #define UART_CTS_PIN_ENABLE() outr(PIOD_ASR, UART_CTS_PIN); \
  187. outr(PIOD_PDR, UART_CTS_PIN)
  188. #endif
  189. #elif defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
  190. #define UART_RXTX_PINS (_BV(PA21_RXD1_A) | _BV(PA22_TXD1_A))
  191. #define UART_HDX_PIN _BV(PA24_RTS1_A)
  192. #define UART_RTS_PIN _BV(PA24_RTS1_A)
  193. #define UART_CTS_PIN _BV(PA25_CTS1_A)
  194. #define UART_MODEM_PINS (_BV(PB27_DTR1_A) | _BV(PB28_DSR1_A) | _BV(PB26_DCD1_A) | _BV(PB29_RI1_A))
  195. #define UART_RXTX_PINS_ENABLE() outr(PIOA_ASR, UART_RXTX_PINS); \
  196. outr(PIOA_PDR, UART_RXTX_PINS)
  197. #if defined(UART_HARDWARE_HANDSHAKE)
  198. #define UART_HDX_PIN_ENABLE() outr(PIOA_ASR, UART_HDX_PIN); \
  199. outr(PIOA_PDR, UART_HDX_PIN)
  200. #define UART_RTS_PIN_ENABLE() outr(PIOA_ASR, UART_RTS_PIN); \
  201. outr(PIOA_PDR, UART_RTS_PIN)
  202. #define UART_CTS_PIN_ENABLE() outr(PIOA_ASR, UART_CTS_PIN); \
  203. outr(PIOA_PDR, UART_CTS_PIN)
  204. #endif
  205. #if defined(UART_MODEM_CONTROL)
  206. #define UART_MODEM_PINS_ENABLE() outr(PIOB_ASR, UART_MODEM_PINS); \
  207. outr(PIOB_PDR, UART_MODEM_PINS)
  208. #endif
  209. /*
  210. ** SAM7X pins.
  211. */
  212. #elif defined(MCU_AT91SAM7X)
  213. #define UART_RXTX_PINS (_BV(PA5_RXD1_A) | _BV(PA6_TXD1_A))
  214. #define UART_HDX_PIN _BV(PA8_RTS1_A)
  215. #define UART_RTS_PIN _BV(PA8_RTS1_A)
  216. #define UART_CTS_PIN _BV(PA9_CTS1_A)
  217. #define UART_MODEM_PINS (_BV(PB25_DTR1_B) | _BV(PB24_DSR1_B) | _BV(PB23_DCD1_B) | _BV(PB26_RI1_B))
  218. #define UART_RXTX_PINS_ENABLE() outr(PIOA_ASR, UART_RXTX_PINS); \
  219. outr(PIOA_PDR, UART_RXTX_PINS)
  220. #if defined(UART_HARDWARE_HANDSHAKE)
  221. #define UART_HDX_PIN_ENABLE() outr(PIOA_ASR, UART_HDX_PIN); \
  222. outr(PIOA_PDR, UART_HDX_PIN)
  223. #define UART_RTS_PIN_ENABLE() outr(PIOA_ASR, UART_RTS_PIN); \
  224. outr(PIOA_PDR, UART_RTS_PIN)
  225. #define UART_CTS_PIN_ENABLE() outr(PIOA_ASR, UART_CTS_PIN); \
  226. outr(PIOA_PDR, UART_CTS_PIN)
  227. #endif
  228. #if defined(UART_MODEM_CONTROL)
  229. #define UART_MODEM_PINS_ENABLE() outr(PIOB_BSR, UART_MODEM_PINS); \
  230. outr(PIOB_PDR, UART_MODEM_PINS)
  231. #endif
  232. /*
  233. ** X40 pins.
  234. */
  235. #elif defined(MCU_AT91R40008)
  236. #define UART_RXTX_PINS (_BV(P22_RXD1) | _BV(P21_TXD1))
  237. #define UART_RXTX_PINS_ENABLE() outr(PIO_PDR, UART_RXTX_PINS)
  238. /*
  239. ** Add more targets here.
  240. **
  241. ** For unsupported targets you may also do basic initializations in
  242. ** your application code.
  243. */
  244. #endif
  245. /*
  246. ** CPLD logic, currently used on Ethernut 3 only.
  247. */
  248. #if defined(ETHERNUT3)
  249. #define UART_USES_NPL 1
  250. #endif
  251. /*
  252. ** Determine the CTS GPIO interrupt, based on the port ID.
  253. */
  254. #if defined(UART1_CTS_BIT) && !defined(UART1_CTS_SIGNAL)
  255. #if UART1_CTS_PIO_ID == PIOA_ID
  256. #define UART1_CTS_SIGNAL sig_GPIO1
  257. #elif UART1_CTS_PIO_ID == PIOB_ID
  258. #define UART1_CTS_SIGNAL sig_GPIO2
  259. #elif UART1_CTS_PIO_ID == PIOC_ID
  260. #define UART1_CTS_SIGNAL sig_GPIO3
  261. #else
  262. #define UART1_CTS_SIGNAL sig_GPIO
  263. #endif
  264. #endif
  265. /*
  266. ** Translate all macros for UART1 to generalized ones used by the
  267. ** source that will be included at the end of this file.
  268. */
  269. #if defined(UART1_HDX_BIT)
  270. #define UART_HDX_BIT UART1_HDX_BIT
  271. #endif
  272. #if defined(UART1_HDX_PIO_ID)
  273. #define UART_HDX_PIO_ID UART1_HDX_PIO_ID
  274. #endif
  275. #if defined(UART1_RTS_BIT)
  276. #define UART_RTS_BIT UART1_RTS_BIT
  277. #endif
  278. #if defined(UART1_RTS_PIO_ID)
  279. #define UART_RTS_PIO_ID UART1_RTS_PIO_ID
  280. #endif
  281. #if defined(UART1_CTS_BIT)
  282. #define UART_CTS_BIT UART1_CTS_BIT
  283. #endif
  284. #if defined(UART1_CTS_PIO_ID)
  285. #define UART_CTS_PIO_ID UART1_CTS_PIO_ID
  286. #endif
  287. #if defined(UART1_CTS_SIGNAL)
  288. #define UART_CTS_SIGNAL UART1_CTS_SIGNAL
  289. #endif
  290. #if defined(UART1_INIT_BAUDRATE)
  291. #define UART_INIT_BAUDRATE UART1_INIT_BAUDRATE
  292. #endif
  293. #define USARTn_BASE USART1_BASE
  294. #define US_ID US1_ID
  295. #define SIG_UART sig_UART1
  296. #define dcb_usart dcb_usart1
  297. #include "usartat91.c"