debug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * \file arch/avr32/dev/debug0.c
  36. * \brief AVR32 debug output device.
  37. *
  38. * \verbatim
  39. *
  40. * $Log: debug.c,v $
  41. *
  42. * \endverbatim
  43. */
  44. #include <cfg/os.h>
  45. #include <cfg/clock.h>
  46. #include <sys/atom.h>
  47. #include <sys/device.h>
  48. #include <sys/file.h>
  49. #include <sys/timer.h>
  50. #include <dev/gpio.h>
  51. #include <dev/uart.h>
  52. #include <avr32/io.h>
  53. #include <arch/avr32/gpio.h>
  54. #include <arch/avr32/pm.h>
  55. #if defined(AVR32_USART0_TXD_0_0_PIN)
  56. #define AVR32_USART0_TXD_PIN AVR32_USART0_TXD_0_0_PIN
  57. #define AVR32_USART0_TXD_FUNCTION AVR32_USART0_TXD_0_0_FUNCTION
  58. #elif defined(AVR32_USART0_TXD_0_PIN)
  59. #define AVR32_USART0_TXD_PIN AVR32_USART0_TXD_0_PIN
  60. #define AVR32_USART0_TXD_FUNCTION AVR32_USART0_TXD_0_FUNCTION
  61. #endif
  62. #if defined(AVR32_USART1_TXD_0_0_PIN)
  63. #define AVR32_USART1_TXD_PIN AVR32_USART1_TXD_0_0_PIN
  64. #define AVR32_USART1_TXD_FUNCTION AVR32_USART1_TXD_0_0_FUNCTION
  65. #elif defined(AVR32_USART1_TXD_0_PIN)
  66. #define AVR32_USART1_TXD_PIN AVR32_USART1_TXD_0_PIN
  67. #define AVR32_USART1_TXD_FUNCTION AVR32_USART1_TXD_0_FUNCTION
  68. #endif
  69. #if defined(AVR32_USART2_TXD_0_0_PIN)
  70. #define AVR32_USART2_TXD_PIN AVR32_USART2_TXD_0_0_PIN
  71. #define AVR32_USART2_TXD_FUNCTION AVR32_USART2_TXD_0_0_FUNCTION
  72. #elif defined(AVR32_USART2_TXD_0_PIN)
  73. #define AVR32_USART2_TXD_PIN AVR32_USART2_TXD_0_PIN
  74. #define AVR32_USART2_TXD_FUNCTION AVR32_USART2_TXD_0_FUNCTION
  75. #endif
  76. #if defined(AVR32_USART3_TXD_0_0_PIN)
  77. #define AVR32_USART3_TXD_PIN AVR32_USART3_TXD_0_0_PIN
  78. #define AVR32_USART3_TXD_FUNCTION AVR32_USART3_TXD_0_0_FUNCTION
  79. #elif defined(AVR32_USART3_TXD_0_PIN)
  80. #define AVR32_USART3_TXD_PIN AVR32_USART3_TXD_0_PIN
  81. #define AVR32_USART3_TXD_FUNCTION AVR32_USART3_TXD_0_FUNCTION
  82. #endif
  83. /*!
  84. * \addtogroup xgDevDebugAvr32
  85. */
  86. /*@{*/
  87. /*!
  88. * \brief Handle I/O controls for debug device 0.
  89. *
  90. * The debug device supports UART_SETSPEED only.
  91. *
  92. * \return 0 on success, -1 otherwise.
  93. */
  94. static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  95. {
  96. volatile avr32_usart_t *usart = (avr32_usart_t *) dev->dev_base;
  97. if (req == UART_SETSPEED) {
  98. const uint32_t baudrate = (*((uint32_t *) conf));
  99. const uint32_t pba_hz = NutArchClockGet(NUT_HWCLK_PERIPHERAL_A);
  100. unsigned int over = (pba_hz >= 16 * baudrate) ? 16 : 8;
  101. unsigned int cd_fp = ((1 << AVR32_USART_BRGR_FP_SIZE) * pba_hz + (over * baudrate) / 2) / (over * baudrate);
  102. unsigned int cd = cd_fp >> AVR32_USART_BRGR_FP_SIZE;
  103. unsigned int fp = cd_fp & ((1 << AVR32_USART_BRGR_FP_SIZE) - 1);
  104. if (cd < 1 || cd > (1 << AVR32_USART_BRGR_CD_SIZE) - 1)
  105. return -1;
  106. usart->mr = (usart->mr & ~(AVR32_USART_MR_USCLKS_MASK |
  107. AVR32_USART_MR_SYNC_MASK |
  108. AVR32_USART_MR_OVER_MASK)) |
  109. AVR32_USART_MR_USCLKS_MCK << AVR32_USART_MR_USCLKS_OFFSET |
  110. ((over == 16) ? AVR32_USART_MR_OVER_X16 : AVR32_USART_MR_OVER_X8) << AVR32_USART_MR_OVER_OFFSET;
  111. usart->brgr = cd << AVR32_USART_BRGR_CD_OFFSET | fp << AVR32_USART_BRGR_FP_OFFSET;
  112. return 0;
  113. }
  114. return -1;
  115. }
  116. /*!
  117. * \brief Initialize debug device 0.
  118. *
  119. * \return Always 0.
  120. */
  121. static int DebugInit(NUTDEVICE * dev)
  122. {
  123. volatile avr32_usart_t *usart = (avr32_usart_t *) dev->dev_base;
  124. /* Disable all USART interrupts.
  125. ** Interrupts needed should be set explicitly on every reset. */
  126. NutEnterCritical();
  127. usart->idr = 0xFFFFFFFF;
  128. usart->csr;
  129. NutExitCritical();
  130. /* Reset mode and other registers that could cause unpredictable behavior after reset. */
  131. usart->mr = 0;
  132. usart->rtor = 0;
  133. usart->ttgr = 0;
  134. /* Shutdown TX and RX (will be re-enabled when setup has successfully completed),
  135. ** reset status bits and turn off DTR and RTS. */
  136. usart->cr = AVR32_USART_CR_RSTRX_MASK |
  137. AVR32_USART_CR_RSTTX_MASK | AVR32_USART_CR_RSTSTA_MASK | AVR32_USART_CR_RSTIT_MASK | AVR32_USART_CR_RSTNACK_MASK |
  138. #ifdef AVR32_USART_CR_DTRDIS_MASK
  139. AVR32_USART_CR_DTRDIS_MASK |
  140. #endif
  141. AVR32_USART_CR_RTSDIS_MASK;
  142. usart->mr |= (8 - 5) << AVR32_USART_MR_CHRL_OFFSET | /* 8 bit character length */
  143. AVR32_USART_MR_PAR_NONE << AVR32_USART_MR_PAR_OFFSET | /* No parity */
  144. AVR32_USART_MR_CHMODE_NORMAL << AVR32_USART_MR_CHMODE_OFFSET | /* */
  145. AVR32_USART_MR_NBSTOP_1 << AVR32_USART_MR_NBSTOP_OFFSET;
  146. /* Set normal mode. */
  147. usart->mr = (usart->mr & ~AVR32_USART_MR_MODE_MASK) | AVR32_USART_MR_MODE_NORMAL << AVR32_USART_MR_MODE_OFFSET;
  148. /* Enable input and output. */
  149. usart->cr = AVR32_USART_CR_TXEN_MASK;
  150. return 0;
  151. }
  152. /*!
  153. * \brief Send a single character to debug device.
  154. *
  155. * A newline character will be automatically prepended
  156. * by a carriage return.
  157. */
  158. static void DebugPut(const NUTDEVICE * dev, char ch)
  159. {
  160. volatile avr32_usart_t *usart = (avr32_usart_t *) dev->dev_base;
  161. /* Prepend new line by carriage return */
  162. if (ch == '\n') {
  163. DebugPut(dev, '\r');
  164. }
  165. /* Wait for TX Ready. */
  166. while (!(usart->csr & AVR32_USART_CSR_TXRDY_MASK));
  167. /* Send out character */
  168. usart->thr = (ch << AVR32_USART_THR_TXCHR_OFFSET) & AVR32_USART_THR_TXCHR_MASK;
  169. }
  170. /*!
  171. * \brief Send characters to debug device 0.
  172. *
  173. * A carriage return character will be automatically appended
  174. * to any linefeed.
  175. *
  176. * \return Number of characters sent.
  177. */
  178. static int DebugWrite(NUTFILE * fp, const void *buffer, int len)
  179. {
  180. int c = len;
  181. const char *cp = buffer;
  182. while (c--) {
  183. DebugPut(fp->nf_dev, *cp++);
  184. }
  185. return len;
  186. }
  187. /*!
  188. * \brief Open debug device 0.
  189. *
  190. * \return Pointer to a static NUTFILE structure.
  191. */
  192. static NUTFILE *DebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  193. {
  194. NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
  195. fp->nf_dev = dev;
  196. fp->nf_fcb = NULL;
  197. return fp;
  198. }
  199. /*!
  200. * \brief Close debug device 0.
  201. *
  202. * \return Always 0.
  203. */
  204. static int DebugClose(NUTFILE * fp)
  205. {
  206. return 0;
  207. }
  208. #ifdef AVR32_USART0_TXD_PIN
  209. static int Debug0Init(NUTDEVICE * dev)
  210. {
  211. /* Assign GPIO to USART. */
  212. gpio_enable_module_pin(AVR32_USART0_TXD_PIN, AVR32_USART0_TXD_FUNCTION);
  213. pm_enable_module(&AVR32_PM, AVR32_USART0_CLK_PBA);
  214. return DebugInit(dev);
  215. };
  216. static NUTFILE dbgfile0;
  217. /*!
  218. * \brief Debug device information structure.
  219. */
  220. NUTDEVICE devDebug0 = {
  221. 0, /*!< Pointer to next device, dev_next. */
  222. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}
  223. , /*!< Unique device name, dev_name. */
  224. 0, /*!< Type of device, dev_type. */
  225. (uintptr_t) (&AVR32_USART0), /*!< Base address, dev_base. */
  226. 0, /*!< First interrupt number, dev_irq. */
  227. 0, /*!< Interface control block, dev_icb. */
  228. &dbgfile0, /*!< Driver control block, dev_dcb. */
  229. Debug0Init, /*!< Driver initialization routine, dev_init. */
  230. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  231. 0, /*!< dev_read. */
  232. DebugWrite, /*!< dev_write. */
  233. DebugOpen, /*!< dev_opem. */
  234. DebugClose, /*!< dev_close. */
  235. 0, /*!< dev_size. */
  236. 0, /*!< dev_select, optional, not yet implemented */
  237. };
  238. #endif /* AVR32_USART0_TXD_PIN */
  239. #ifdef AVR32_USART1_TXD_PIN
  240. static int Debug1Init(NUTDEVICE * dev)
  241. {
  242. /* Assign GPIO to USART. */
  243. gpio_enable_module_pin(AVR32_USART1_TXD_PIN, AVR32_USART1_TXD_FUNCTION);
  244. pm_enable_module(&AVR32_PM, AVR32_USART1_CLK_PBA);
  245. return DebugInit(dev);
  246. };
  247. static NUTFILE dbgfile1;
  248. /*!
  249. * \brief Debug device information structure.
  250. */
  251. NUTDEVICE devDebug1 = {
  252. 0, /*!< Pointer to next device, dev_next. */
  253. {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}
  254. , /*!< Unique device name, dev_name. */
  255. 0, /*!< Type of device, dev_type. */
  256. (uintptr_t) (&AVR32_USART1), /*!< Base address, dev_base. */
  257. 0, /*!< First interrupt number, dev_irq. */
  258. 0, /*!< Interface control block, dev_icb. */
  259. &dbgfile1, /*!< Driver control block, dev_dcb. */
  260. Debug1Init, /*!< Driver initialization routine, dev_init. */
  261. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  262. 0, /*!< dev_read. */
  263. DebugWrite, /*!< dev_write. */
  264. DebugOpen, /*!< dev_opem. */
  265. DebugClose, /*!< dev_close. */
  266. 0, /*!< dev_size. */
  267. 0, /*!< dev_select, optional, not yet implemented */
  268. };
  269. #endif /* AVR32_USART1_TXD_PIN */
  270. #ifdef AVR32_USART2_TXD_PIN
  271. static int Debug2Init(NUTDEVICE * dev)
  272. {
  273. /* Assign GPIO to USART. */
  274. gpio_enable_module_pin(AVR32_USART2_TXD_PIN, AVR32_USART2_TXD_FUNCTION);
  275. pm_enable_module(&AVR32_PM, AVR32_USART2_CLK_PBA);
  276. return DebugInit(dev);
  277. };
  278. static NUTFILE dbgfile2;
  279. /*!
  280. * \brief Debug device information structure.
  281. */
  282. NUTDEVICE devDebug2 = {
  283. 0, /*!< Pointer to next device, dev_next. */
  284. {'u', 'a', 'r', 't', '2', 0, 0, 0, 0}
  285. , /*!< Unique device name, dev_name. */
  286. 0, /*!< Type of device, dev_type. */
  287. (uintptr_t) (&AVR32_USART2), /*!< Base address, dev_base. */
  288. 0, /*!< First interrupt number, dev_irq. */
  289. 0, /*!< Interface control block, dev_icb. */
  290. &dbgfile2, /*!< Driver control block, dev_dcb. */
  291. Debug2Init, /*!< Driver initialization routine, dev_init. */
  292. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  293. 0, /*!< dev_read. */
  294. DebugWrite, /*!< dev_write. */
  295. DebugOpen, /*!< dev_opem. */
  296. DebugClose, /*!< dev_close. */
  297. 0, /*!< dev_size. */
  298. 0, /*!< dev_select, optional, not yet implemented */
  299. };
  300. #endif /* AVR32_USART2_TXD_PIN */
  301. #ifdef AVR32_USART3_TXD_PIN
  302. static int Debug3Init(NUTDEVICE * dev)
  303. {
  304. /* Assign GPIO to USART. */
  305. gpio_enable_module_pin(AVR32_USART3_TXD_PIN, AVR32_USART3_TXD_FUNCTION);
  306. pm_enable_module(&AVR32_PM, AVR32_USART3_CLK_PBA);
  307. return DebugInit(dev);
  308. };
  309. static NUTFILE dbgfile3;
  310. /*!
  311. * \brief Debug device information structure.
  312. */
  313. NUTDEVICE devDebug3 = {
  314. 0, /*!< Pointer to next device, dev_next. */
  315. {'u', 'a', 'r', 't', '3', 0, 0, 0, 0}
  316. , /*!< Unique device name, dev_name. */
  317. 0, /*!< Type of device, dev_type. */
  318. (uintptr_t) (&AVR32_USART3), /*!< Base address, dev_base. */
  319. 0, /*!< First interrupt number, dev_irq. */
  320. 0, /*!< Interface control block, dev_icb. */
  321. &dbgfile3, /*!< Driver control block, dev_dcb. */
  322. Debug3Init, /*!< Driver initialization routine, dev_init. */
  323. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  324. 0, /*!< dev_read. */
  325. DebugWrite, /*!< dev_write. */
  326. DebugOpen, /*!< dev_opem. */
  327. DebugClose, /*!< dev_close. */
  328. 0, /*!< dev_size. */
  329. 0, /*!< dev_select, optional, not yet implemented */
  330. };
  331. #endif /* AVR32_USART3_TXD_PIN */
  332. /*@}*/