usart0cb_at91npl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2012 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/arm/atmel/usart0cb_at91npl.c
  37. * \brief Low level routines for UART0 on Ethernut 3.
  38. *
  39. * Contains those functions, which are implemented individually for UART0.
  40. *
  41. * \verbatim
  42. * $Id$
  43. * \endverbatim
  44. */
  45. #include <dev/irqreg.h>
  46. #include <dev/npl.h>
  47. #include <dev/usart.h>
  48. #include <arch/arm/atmel/usart_at91ctl.h>
  49. #include <arch/arm/atmel/usart_cb_at91npl.h>
  50. /*
  51. * For maximum performance we may use native interrupts.
  52. */
  53. #ifdef USART0_NATIVE_IRQ
  54. static void Usart0IrqEntry(void) NUT_NAKED_FUNC;
  55. void Usart0IrqEntry(void)
  56. {
  57. IRQ_ENTRY();
  58. NplUsartCbInterrupt(devUsart0CbNpl.dev_dcb);
  59. IRQ_EXIT();
  60. }
  61. #endif
  62. /*!
  63. * \brief Enable UART0 on Ethernut 3.
  64. */
  65. static int Usart0Enable(USARTCB_DCB *dcb)
  66. {
  67. uint8_t scr;
  68. #ifdef USART0_NATIVE_IRQ
  69. /* Set the vector. */
  70. mem_wr32(AIC_SVR(US0_ID), (unsigned int)Usart0CbIrqEntry);
  71. /* Initialize to edge triggered with defined priority. */
  72. mem_wr32(AIC_SMR(US0_ID), AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 4);
  73. /* Clear interrupt */
  74. mem_wr32(AIC_ICCR, _BV(US0_ID));
  75. #else
  76. if (NutRegisterIrqHandler(&sig_UART0, NplUsartCbInterrupt, dcb)) {
  77. return -1;
  78. }
  79. #endif
  80. if (NplRegisterIrqHandler(&sig_RSCTS, NplCtsInterrupt, dcb)) {
  81. return -1;
  82. }
  83. /* Enable receive and transmit. */
  84. #if defined (P15_RXD0) && defined (P14_TXD0)
  85. outr(PIO_PDR, _BV(P15_RXD0) | _BV(P14_TXD0));
  86. #elif defined (PA0_RXD0_A) && defined (PA1_TXD0_A)
  87. outr(PIOA_PDR, _BV(PA0_RXD0_A) | _BV(PA1_TXD0_A));
  88. #elif defined (PA5_RXD0_A) && defined (PA6_TXD0_A)
  89. outr(PIOA_PDR, _BV(PA5_RXD0_A) | _BV(PA6_TXD0_A));
  90. #elif defined (PB5_RXD0_A) && defined (PB4_TXD0_A)
  91. outr(PIOB_PDR, _BV(PB5_RXD0_A) | _BV(PB4_TXD0_A));
  92. #elif defined (PB18_RXD0_A) && defined (PB19_TXD0_A)
  93. outr(PIOB_PDR, _BV(PB18_RXD0_A) | _BV(PB19_TXD0_A));
  94. #else
  95. #warning Undefined pin
  96. #endif
  97. mem_wr32(dcb->usart_hwif + US_CR_OFF, US_RXEN | US_TXEN);
  98. NutIrqEnable(&sig_UART0);
  99. /* Enable handshake control. */
  100. scr = mem_rd8(NPL_RSCR);
  101. if ((scr & NPL_RSUS1P) == 0) {
  102. dcb->usart_mode |= USART_MF_RTSCONTROL;
  103. if ((scr & NPL_RSUS1E) == 0) {
  104. dcb->usart_mode |= USART_MF_DTRCONTROL;
  105. }
  106. }
  107. return 0;
  108. }
  109. /*!
  110. * \brief Disable UART0 on Ethernut 3.
  111. */
  112. static int Usart0Disable(USARTCB_DCB *dcb)
  113. {
  114. dcb->usart_rx_stop(dcb);
  115. dcb->usart_tx_stop(dcb);
  116. NutIrqDisable(&sig_UART0);
  117. #if defined (P15_RXD0) && defined (P14_TXD0)
  118. mem_wr32(PIO_PER, _BV(P15_RXD0) | _BV(P14_TXD0));
  119. #elif defined (PA0_RXD0_A) && defined (PA1_TXD0_A)
  120. mem_wr32(PIOA_PER, _BV(PA0_RXD0_A) | _BV(PA1_TXD0_A));
  121. #elif defined (PA5_RXD0_A) && defined (PA6_TXD0_A)
  122. mem_wr32(PIOA_PER, _BV(PA5_RXD0_A) | _BV(PA6_TXD0_A));
  123. #elif defined (PB5_RXD0_A) && defined (PB4_TXD0_A)
  124. mem_wr32(PIOB_PDR, _BV(PB5_RXD0_A) | _BV(PB4_TXD0_A));
  125. #elif defined (PB18_RXD0_A) && defined (PB19_TXD0_A)
  126. mem_wr32(PIOB_PDR, _BV(PB18_RXD0_A) | _BV(PB19_TXD0_A));
  127. #else
  128. #warning Undefined pin
  129. #endif
  130. return 0;
  131. }
  132. /*!
  133. * \brief Set and query the status of UART0 on Ethernut 3.
  134. */
  135. static uint32_t Usart0Status(USARTCB_DCB *dcb, uint32_t stat)
  136. {
  137. uint32_t rc = 0;
  138. uint8_t scr;
  139. uint16_t slr;
  140. scr = mem_rd8(NPL_RSCR);
  141. if ((scr & NPL_RSUS1P) == 0) {
  142. if (stat) {
  143. if (stat & USART_SF_RTSOFF) {
  144. scr &= ~NPL_RSRTS;
  145. } else {
  146. scr |= NPL_RSRTS;
  147. }
  148. if ((scr & NPL_RSUS1E) == 0 && (stat & USART_SF_DTROFF) == 0) {
  149. scr |= NPL_RSDTR;
  150. } else {
  151. scr &= ~NPL_RSDTR;
  152. }
  153. mem_wr8(NPL_RSCR, scr);
  154. }
  155. if ((scr & NPL_RSRTS) == 0) {
  156. rc |= USART_SF_RTSOFF;
  157. }
  158. if ((scr & NPL_RSDTR) == 0) {
  159. rc |= USART_SF_DTROFF;
  160. }
  161. mem_wr16_mb(NPL_SCR, NPL_RSDSR | NPL_RSDCD | NPL_RSCTS);
  162. slr = mem_rd16(NPL_SLR);
  163. if ((slr & NPL_RSCTS) == 0) {
  164. rc |= USART_SF_CTSOFF;
  165. }
  166. if ((slr & NPL_RSDCD) == 0) {
  167. rc |= USART_SF_DCDOFF;
  168. }
  169. if ((scr & NPL_RSUS1E) == 0 && (slr & NPL_RSDSR) == 0) {
  170. rc |= USART_SF_DSROFF;
  171. }
  172. }
  173. if ((mem_rd32(dcb->usart_hwif + US_IMR_OFF) & US_TXRDY) == 0) {
  174. rc |= USART_SF_TXDISABLED;
  175. }
  176. if ((mem_rd32(dcb->usart_hwif + US_IMR_OFF) & US_RXRDY) == 0) {
  177. rc |= USART_SF_RXDISABLED;
  178. }
  179. return rc;
  180. }
  181. /*!
  182. * \brief Set hardware flow control of UART0 on Ethernut 3.
  183. */
  184. static int Usart0SetFlowControl(USARTCB_DCB *dcb, uint32_t mode)
  185. {
  186. uint32_t mask = 0;
  187. uint8_t scr;
  188. scr = mem_rd8(NPL_RSCR);
  189. if ((scr & NPL_RSUS1P) == 0) {
  190. mask |= USART_MF_RTSCONTROL | USART_MF_CTSSENSE | USART_MF_DCDSENSE;
  191. if ((scr & NPL_RSUS1E) == 0) {
  192. mask |= USART_MF_DTRCONTROL | USART_MF_DSRSENSE;
  193. }
  194. }
  195. if ((mode & mask) != mode) {
  196. return -1;
  197. }
  198. dcb->usart_mode &= ~mask;
  199. dcb->usart_mode |= mode;
  200. return 0;
  201. }
  202. /*!
  203. * \brief Handle Ethernut 3 UART0 specific functions.
  204. *
  205. * We only handle hardware flow control. Anything else is passed to the
  206. * general Ethernut 3 UART handler.
  207. */
  208. static int Usart0Control(USARTCB_DCB *dcb, int req, void *conf)
  209. {
  210. uint32_t *u32vp = (uint32_t *) conf;
  211. if (req == UART_SETFLOWCONTROL) {
  212. return Usart0SetFlowControl(dcb, *u32vp);
  213. }
  214. return At91UsartHwControl(dcb, req, conf);
  215. }
  216. #define USART0_CAPS \
  217. USART_MF_RTSCONTROL | USART_MF_DTRCONTROL | \
  218. USART_MF_CTSSENSE | \
  219. USART_MF_COOKEDMODE
  220. static USARTCB_DCB dcb_usart0 = {
  221. USART0_BASE, /* usart_hwif. */
  222. Usart0Enable, /* usart_enable(). */
  223. Usart0Disable, /* usart_disable(). */
  224. Usart0Control, /* usart_control(). */
  225. 0, /* usart_mode. */
  226. USART0_CAPS, /* usart_caps. */
  227. Usart0Status, /* usart_status(). */
  228. { NULL, 0, 0, 0, NULL, 0 }, /* usart_tx_buff. */
  229. 0, /* usart_wr_tmo. */
  230. NplUsartCbTxStart, /* usart_tx_start(). */
  231. At91UsartHwTxStop, /* usart_tx_stop(). */
  232. { NULL, 0, 0, 0, NULL, 0 }, /* usart_rx_buff. */
  233. 0, /* usart_rd_tmo. */
  234. 0, /* usart_rx_cr. */
  235. 0, /* usart_rx_lowm. */
  236. 0, /* usart_rx_hiwm. */
  237. NplUsartCbRxStart, /* usart_rx_start(). */
  238. At91UsartHwRxStop /* usart_rx_stop(). */
  239. };
  240. NUTDEVICE devUsart0CbNpl = {
  241. NULL, /* Pointer to next device, dev_next. */
  242. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}, /* Hardware device name, dev_name. */
  243. IFTYP_CHAR, /* Type of device, dev_type. */
  244. 0, /* Base address, dev_base (not used). */
  245. 0, /* First interrupt number, dev_irq (not used). */
  246. NULL, /* Interface control block, dev_icb (not used). */
  247. &dcb_usart0, /* Driver control block, dev_dcb. */
  248. UsartCbInit, /* Driver initialization routine, dev_init. */
  249. UsartCbIoCtrl, /* Driver specific control function, dev_ioctl. */
  250. UsartCbRead, /* Read from device, dev_read. */
  251. UsartCbWrite, /* Write to device, dev_write. */
  252. UsartCbOpen, /* Open a device or file, dev_open. */
  253. UsartCbClose, /* Close a device or file, dev_close. */
  254. UsartCbSize, /* Request file size, dev_size. */
  255. NULL, /* Select function, optional, not yet implemented */
  256. };