debug0.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C) 2001-2003 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. /*!
  34. * \file arch/avr/dev/debug0.c
  35. * \brief AVR debug output device using UART0.
  36. *
  37. * \verbatim
  38. * $Id: debug0.c 5472 2013-12-06 00:16:28Z olereinhardt $
  39. * \endverbatim
  40. */
  41. #include <dev/debug.h>
  42. #include <cfg/os.h>
  43. #include <cfg/uart.h>
  44. #include <sys/device.h>
  45. #include <sys/file.h>
  46. #include <sys/timer.h>
  47. #ifndef UART0_INIT_BAUDRATE
  48. #define UART0_INIT_BAUDRATE 115200
  49. #endif
  50. /*!
  51. * \addtogroup xgDevDebugAvr
  52. */
  53. /*@{*/
  54. static NUTFILE dbgfile;
  55. static void DebugSetSpeed(uint32_t speed)
  56. {
  57. #if defined(__AVR_ENHANCED__) && ((NUT_CPU_FREQ == 8000000) || (NUT_CPU_FREQ == 12000000) || (NUT_CPU_FREQ == 16000000))
  58. /* On enhanced MCUs with 8.0, 12.0 or 16.0 MHz we use double rate mode,
  59. * so we can use 115200 bps with 8/12.0 MHz crystals
  60. * and 57600 with 16.0 MHz crystals.
  61. */
  62. sbi(UCSR0A, U2X0);
  63. outb(UBRR, (uint8_t) ((((2UL * NutGetCpuClock()) / (speed * 8UL)) + 1UL) / 2UL) - 1UL);
  64. #else
  65. outb(UBRR, (uint8_t) ((((2UL * NutGetCpuClock()) / (speed * 16UL)) + 1UL) / 2UL) - 1UL);
  66. #endif
  67. }
  68. /*!
  69. * \brief Handle I/O controls for debug device 0.
  70. *
  71. * The debug device doesn't support any.
  72. *
  73. * \return Always -1.
  74. */
  75. static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  76. {
  77. if(req == UART_SETSPEED) {
  78. DebugSetSpeed(*((uint32_t *) conf));
  79. return 0;
  80. }
  81. return -1;
  82. }
  83. /*!
  84. * \brief Initialize debug device 0.
  85. *
  86. * Simply enable the device. Baudrate divisor set to 7 for
  87. * 115.2 kBaud at 14.7456 MHz.
  88. *
  89. * \return Always 0.
  90. */
  91. static int DebugInit(NUTDEVICE * dev)
  92. {
  93. /* Note: Default baudrate has been set in nutinit.c */
  94. UCR = BV(RXEN) | BV(TXEN);
  95. DebugSetSpeed(UART0_INIT_BAUDRATE);
  96. return 0;
  97. }
  98. /*!
  99. * \brief Send a single character to debug device 0.
  100. *
  101. * A carriage return character will be automatically appended
  102. * to any linefeed.
  103. */
  104. static void DebugPut(char ch)
  105. {
  106. if(ch == '\n') {
  107. while((USR & BV(UDRE)) == 0);
  108. UDR = '\r';
  109. }
  110. while((USR & BV(UDRE)) == 0);
  111. UDR = ch;
  112. }
  113. /*!
  114. * \brief Send characters to debug device 0.
  115. *
  116. * A carriage return character will be automatically appended
  117. * to any linefeed.
  118. *
  119. * \return Number of characters sent.
  120. */
  121. static int DebugWrite(NUTFILE * fp, const void *buffer, int len)
  122. {
  123. int c = len;
  124. const char *cp = buffer;
  125. while(c--)
  126. DebugPut(*cp++);
  127. return len;
  128. }
  129. /*!
  130. * \brief Send characters from progam memory to debug device 0.
  131. *
  132. * A carriage return character will be automatically appended
  133. * to any linefeed.
  134. *
  135. * \return Number of characters sent.
  136. */
  137. static int DebugWrite_P(NUTFILE * fp, PGM_P buffer, int len)
  138. {
  139. int c = len;
  140. PGM_P cp = buffer;
  141. while(c--) {
  142. DebugPut(PRG_RDB(cp));
  143. cp++;
  144. }
  145. return len;
  146. }
  147. #ifdef NUT_DEV_DEBUG_READ
  148. /*!
  149. * \brief Read characters from debug device.
  150. *
  151. * This function is called by the low level input routines of the
  152. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  153. * entry.
  154. *
  155. * The function will block the calling thread until at least one
  156. * character has been received.
  157. *
  158. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  159. * previous call to At91DevDebugOpen().
  160. * \param buffer Pointer to the buffer that receives the data. If zero,
  161. * then all characters in the input buffer will be
  162. * removed.
  163. * \param size Maximum number of bytes to read.
  164. *
  165. * \return The number of bytes read, which may be less than the number
  166. * of bytes specified. A return value of -1 indicates an error,
  167. * while zero is returned in case of a timeout.
  168. */
  169. int DebugRead(NUTFILE * fp, void *buffer, int size)
  170. {
  171. int rc;
  172. unsigned int ch;
  173. char *bp = (char *) buffer;
  174. /* Wait for the first character, forever. */
  175. for (rc = 0; rc < size; rc++) {
  176. while ((inb(USR) & _BV(RXC)) == 0) {
  177. NutSleep(1);
  178. if ((rc || bp == NULL) && (inb(USR) & _BV(RXC)) == 0) {
  179. return rc;
  180. }
  181. }
  182. ch = inb(UDR);
  183. if (bp) {
  184. if (ch == '\r') {
  185. *bp++ = '\n';
  186. } else {
  187. *bp++ = (char) ch;
  188. }
  189. }
  190. }
  191. return rc;
  192. }
  193. /*!
  194. * \brief Retrieves the number of characters in input buffer.
  195. *
  196. * This function is called by the low level size routine of the C runtime
  197. * library, using the _NUTDEVICE::dev_size entry.
  198. *
  199. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  200. * previous call to UsartOpen().
  201. *
  202. * \return The number of bytes currently stored in input buffer.
  203. */
  204. long DebugSize(NUTFILE *fp)
  205. {
  206. while (inb(USR) & _BV(RXC)) {
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. #endif
  212. /*!
  213. * \brief Open debug device 0.
  214. *
  215. * \return Pointer to a static NUTFILE structure.
  216. */
  217. static NUTFILE *DebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  218. {
  219. dbgfile.nf_dev = dev;
  220. dbgfile.nf_fcb = NULL;
  221. return &dbgfile;
  222. }
  223. /*!
  224. * \brief Close debug device 0.
  225. *
  226. * \return Always 0.
  227. */
  228. static int DebugClose(NUTFILE * fp)
  229. {
  230. return 0;
  231. }
  232. /*!
  233. * \brief Debug device 0 information structure.
  234. */
  235. NUTDEVICE devDebug0 = {
  236. 0, /*!< Pointer to next device. */
  237. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}, /*!< Unique device name. */
  238. 0, /*!< Type of device. */
  239. 0, /*!< Base address. */
  240. 0, /*!< First interrupt number. */
  241. 0, /*!< Interface control block. */
  242. 0, /*!< Driver control block. */
  243. DebugInit, /*!< Driver initialization routine. */
  244. DebugIOCtl, /*!< Driver specific control function. */
  245. #ifdef NUT_DEV_DEBUG_READ
  246. DebugRead,
  247. #else
  248. NULL,
  249. #endif
  250. DebugWrite,
  251. DebugWrite_P,
  252. DebugOpen,
  253. DebugClose,
  254. #ifdef NUT_DEV_DEBUG_READ
  255. DebugSize,
  256. #else
  257. NULL,
  258. #endif
  259. NULL, /*!< Select function, optional, not yet implemented */
  260. };
  261. /*@}*/