usartDat91.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Copyright (C) 2005 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. * $Log$
  34. * Revision 0.01 2009/09/20 ulrichprinz
  35. * First checkin of using DBGU as limited standard USART.
  36. *
  37. */
  38. #define NUT_DEPRECATED
  39. #include <cfg/os.h>
  40. #include <cfg/clock.h>
  41. #include <cfg/arch.h>
  42. #include <cfg/uart.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/debug.h>
  49. #include <arch/arm/atmel/at91_dbgu.h>
  50. #include <dev/usartat91.h>
  51. #ifndef NUT_CPU_FREQ
  52. #ifdef NUT_PLL_CPUCLK
  53. #include <dev/cy2239x.h>
  54. #else /* !NUT_PLL_CPUCLK */
  55. #define NUT_CPU_FREQ 73728000UL
  56. #endif /* !NUT_PLL_CPUCLK */
  57. #endif /* !NUT_CPU_FREQ */
  58. /*
  59. * Local function prototypes.
  60. *
  61. * Commented functions are not supported by DBGU
  62. *
  63. */
  64. static uint32_t At91UsartGetSpeed(void);
  65. static int At91UsartSetSpeed(uint32_t rate);
  66. static uint8_t At91UsartGetDataBits(void);
  67. static int At91UsartSetDataBits( uint8_t bits);
  68. static uint8_t At91UsartGetParity(void);
  69. static int At91UsartSetParity(uint8_t mode);
  70. static uint8_t At91UsartGetStopBits(void);
  71. static int At91UsartSetStopBits(uint8_t bits);
  72. static uint32_t At91UsartGetStatus(void);
  73. static int At91UsartSetStatus(uint32_t flags);
  74. static uint8_t At91UsartGetClockMode(void);
  75. static int At91UsartSetClockMode(uint8_t mode);
  76. static void At91UsartTxStart(void);
  77. static void At91UsartRxStart(void);
  78. static int At91UsartInit(void);
  79. static int At91UsartDeinit(void);
  80. static uint32_t At91UsartGetFlowControl(void);
  81. static int At91UsartSetFlowControl(uint32_t flags);
  82. extern IRQ_HANDLER sig_DBGU;
  83. /*!
  84. * \addtogroup xgNutArchArmAt91Us
  85. */
  86. /*@{*/
  87. /*!
  88. * \brief DBGU device control block structure.
  89. */
  90. static USARTDCB dcb_dbgu = {
  91. 0, /* dcb_modeflags */
  92. 0, /* dcb_statusflags */
  93. 0, /* dcb_rtimeout */
  94. 0, /* dcb_wtimeout */
  95. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_tx_rbf */
  96. {0, 0, 0, 0, 0, 0, 0, 0}, /* dcb_rx_rbf */
  97. 0, /* dbc_last_eol */
  98. At91UsartInit, /* dcb_init */
  99. At91UsartDeinit, /* dcb_deinit */
  100. At91UsartTxStart, /* dcb_tx_start */
  101. At91UsartRxStart, /* dcb_rx_start */
  102. At91UsartSetFlowControl, /* dcb_set_flow_control */
  103. At91UsartGetFlowControl, /* dcb_get_flow_control */
  104. At91UsartSetSpeed, /* dcb_set_speed */
  105. At91UsartGetSpeed, /* dcb_get_speed */
  106. At91UsartSetDataBits, /* dcb_set_data_bits */
  107. At91UsartGetDataBits, /* dcb_get_data_bits */
  108. At91UsartSetParity, /* dcb_set_parity */
  109. At91UsartGetParity, /* dcb_get_parity */
  110. At91UsartSetStopBits, /* dcb_set_stop_bits */
  111. At91UsartGetStopBits, /* dcb_get_stop_bits */
  112. At91UsartSetStatus, /* dcb_set_status */
  113. At91UsartGetStatus, /* dcb_get_status */
  114. At91UsartSetClockMode, /* dcb_set_clock_mode */
  115. At91UsartGetClockMode, /* dcb_get_clock_mode */
  116. };
  117. /*!
  118. * \name AT91 DBGU Device
  119. */
  120. /*@{*/
  121. /*!
  122. * \brief USART0 device information structure.
  123. *
  124. * An application must pass a pointer to this structure to
  125. * NutRegisterDevice() before using the serial communication
  126. * driver of the AT91's on-chip USART0.
  127. *
  128. * The device is named \b uart0.
  129. *
  130. * \showinitializer
  131. */
  132. NUTDEVICE devDbguAt91 = {
  133. 0, /* Pointer to next device, dev_next. */
  134. {'u', 'a', 'r', 't', 'd', 0, 0, 0, 0}, /* Unique device name, dev_name. */
  135. IFTYP_CHAR, /* Type of device, dev_type. */
  136. DBGU_BASE, /* Base address, dev_base (not used). */
  137. 0, /* First interrupt number, dev_irq (not used). */
  138. 0, /* Interface control block, dev_icb (not used). */
  139. &dcb_dbgu, /* Driver control block, dev_dcb. */
  140. UsartInit, /* Driver initialization routine, dev_init. */
  141. UsartIOCtl, /* Driver specific control function, dev_ioctl. */
  142. UsartRead, /* Read from device, dev_read. */
  143. UsartWrite, /* Write to device, dev_write. */
  144. UsartOpen, /* Open a device or file, dev_open. */
  145. UsartClose, /* Close a device or file, dev_close. */
  146. UsartSize, /* Request file size, dev_size. */
  147. UsartSelect /* Select function, dev_select */
  148. };
  149. /*@}*/
  150. /*@}*/
  151. /* Modem control includes hardware handshake. */
  152. /*
  153. * Hardware driven control signals are not available
  154. * with the DBUG unit of most chips.
  155. */
  156. #undef UART_MODEM_CONTROL
  157. #undef UART_HARDWARE_HANDSHAKE
  158. #if defined(UART_MODEM_CONTROL)
  159. #define UART_MODEM_CONTROL
  160. #define UART_HARDWARE_HANDSHAKE
  161. #elif defined(UART_HARDWARE_HANDSHAKE)
  162. #define UART_HARDWARE_HANDSHAKE
  163. #endif
  164. /*
  165. ** SAM9260 and SAM9XE pins.
  166. */
  167. #if defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE)
  168. #define UART_RXTX_PINS (_BV(PB14_DRXD_A) | _BV(PB15_DTXD_A))
  169. #undef UART_HDX_PIN
  170. #undef UART_RTS_PIN
  171. #undef UART_CTS_PIN
  172. #undef UART_MODEM_PINS
  173. #define UART_RXTX_PINS_ENABLE() outr(PIOB_ASR, UART_RXTX_PINS); \
  174. outr(PIOB_PDR, UART_RXTX_PINS)
  175. #if defined(UART_HARDWARE_HANDSHAKE)
  176. #define UART_HDX_PIN_ENABLE() outr(PIOB_ASR, UART_HDX_PIN); \
  177. outr(PIOB_PDR, UART_HDX_PIN)
  178. #define UART_RTS_PIN_ENABLE() outr(PIOB_ASR, UART_RTS_PIN); \
  179. outr(PIOB_PDR, UART_RTS_PIN)
  180. #define UART_CTS_PIN_ENABLE() outr(PIOB_ASR, UART_CTS_PIN); \
  181. outr(PIOB_PDR, UART_CTS_PIN)
  182. #endif
  183. #if defined(UART_MODEM_CONTROL)
  184. #define UART_MODEM_PINS_ENABLE() outr(PIOB_ASR, UART_MODEM_PINS); \
  185. outr(PIOB_PDR, UART_MODEM_PINS)
  186. #endif
  187. /*
  188. ** SAM7S and SAM7SE pins.
  189. */
  190. #elif defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7SE)
  191. #define UART_RXTX_PINS (_BV(PA9_DRXD_A) | _BV(PA10_DTXD_A))
  192. #undef UART_HDX_PIN
  193. #undef UART_RTS_PIN
  194. #undef UART_CTS_PIN
  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. /*
  206. ** SAM7X pins.
  207. */
  208. #elif defined(MCU_AT91SAM7X)
  209. #define UART_RXTX_PINS (_BV(PA27_DRXD_A) | _BV(PA28_DTXD_A))
  210. #undef UART_HDX_PIN
  211. #undef UART_RTS_PIN
  212. #undef UART_CTS_PIN
  213. #define UART_RXTX_PINS_ENABLE() outr(PIOA_ASR, UART_RXTX_PINS); \
  214. outr(PIOA_PDR, UART_RXTX_PINS)
  215. #if defined(UART_HARDWARE_HANDSHAKE)
  216. #define UART_HDX_PIN_ENABLE() outr(PIOA_ASR, UART_HDX_PIN); \
  217. outr(PIOA_PDR, UART_HDX_PIN)
  218. #define UART_RTS_PIN_ENABLE() outr(PIOA_ASR, UART_RTS_PIN); \
  219. outr(PIOA_PDR, UART_RTS_PIN)
  220. #define UART_CTS_PIN_ENABLE() outr(PIOA_ASR, UART_CTS_PIN); \
  221. outr(PIOA_PDR, UART_CTS_PIN)
  222. #endif
  223. #endif
  224. /*
  225. ** CPLD logic, currently used on Ethernut 3 only.
  226. */
  227. #if defined(ETHERNUT3)
  228. #define UART_USES_NPL 1
  229. #endif
  230. /*
  231. ** Determine the CTS GPIO interrupt, based on the port ID.
  232. */
  233. #if defined(UART0_CTS_BIT) && !defined(UART0_CTS_SIGNAL)
  234. #if UART0_CTS_PIO_ID == PIOA_ID
  235. #define UART0_CTS_SIGNAL sig_GPIO1
  236. #elif UART0_CTS_PIO_ID == PIOB_ID
  237. #define UART0_CTS_SIGNAL sig_GPIO2
  238. #elif UART0_CTS_PIO_ID == PIOC_ID
  239. #define UART0_CTS_SIGNAL sig_GPIO3
  240. #else
  241. #define UART0_CTS_SIGNAL sig_GPIO
  242. #endif
  243. #endif
  244. /*
  245. ** Translate all macros for UART0 to generalized ones used by the
  246. ** source that will be included at the end of this file.
  247. */
  248. #if defined(UARTD_HDX_BIT)
  249. #define UART_HDX_BIT UARTD_HDX_BIT
  250. #endif
  251. #if defined(UARTD_HDX_PIO_ID)
  252. #define UART_HDX_PIO_ID UARTD_HDX_PIO_ID
  253. #endif
  254. #if defined(UARTD_RTS_BIT)
  255. #define UART_RTS_BIT UARTD_RTS_BIT
  256. #endif
  257. #if defined(UARTD_RTS_PIO_ID)
  258. #define UART_RTS_PIO_ID UARTD_RTS_PIO_ID
  259. #endif
  260. #if defined(UARTD_CTS_BIT)
  261. #define UART_CTS_BIT UARTD_CTS_BIT
  262. #endif
  263. #if defined(UARTD_CTS_PIO_ID)
  264. #define UART_CTS_PIO_ID UARTD_CTS_PIO_ID
  265. #endif
  266. #if defined(UARTD_CTS_SIGNAL)
  267. #define UART_CTS_SIGNAL UARTD_CTS_SIGNAL
  268. #endif
  269. #if defined(UARTD_INIT_BAUDRATE)
  270. #define UART_INIT_BAUDRATE UARTD_INIT_BAUDRATE
  271. #endif
  272. #define USARTn_BASE DBGU_BASE
  273. #define US_ID SYSC_ID
  274. #define SIG_UART syssig_DBGU
  275. #define dcb_usart dcb_dbgu
  276. #include "usartat91.c"