debug1.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 UART1.
  36. *
  37. * \verbatim
  38. * $Id: debug1.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 UART1_INIT_BAUDRATE
  48. #define UART1_INIT_BAUDRATE 115200
  49. #endif
  50. /*!
  51. * \addtogroup xgDevDebugAvr
  52. */
  53. /*@{*/
  54. #ifdef __AVR_ENHANCED__
  55. static NUTFILE dbgfile;
  56. static void DebugSetSpeed(uint32_t speed)
  57. {
  58. #if (NUT_CPU_FREQ == 8000000) || (NUT_CPU_FREQ == 12000000) || (NUT_CPU_FREQ == 16000000)
  59. /* We use double rate mode, so we can use 115200 bps
  60. * with 8.0, 12.0 and 16.0 crystals. */
  61. sbi(UCSR1A, U2X1);
  62. outb(UBRR1L, (uint8_t) ((((2UL * NutGetCpuClock()) / (speed * 8UL)) + 1UL) / 2UL) - 1UL);
  63. #else
  64. outb(UBRR1L, (uint8_t) ((((2UL * NutGetCpuClock()) / (speed * 16UL)) + 1UL) / 2UL) - 1UL);
  65. #endif
  66. }
  67. static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  68. {
  69. if(req == UART_SETSPEED) {
  70. DebugSetSpeed(*((uint32_t *) conf));
  71. return 0;
  72. }
  73. return -1;
  74. }
  75. static int DebugInit(NUTDEVICE * dev)
  76. {
  77. /* Note: Default baudrate has been set in nutinit.c */
  78. UCSR1B = BV(RXEN) | BV(TXEN);
  79. DebugSetSpeed(UART1_INIT_BAUDRATE);
  80. return 0;
  81. }
  82. static void DebugPut(char ch)
  83. {
  84. if(ch == '\n') {
  85. while((UCSR1A & BV(UDRE)) == 0);
  86. UDR1 = '\r';
  87. }
  88. while((UCSR1A & BV(UDRE)) == 0);
  89. UDR1 = ch;
  90. }
  91. static int DebugWrite(NUTFILE * fp, const void *buffer, int len)
  92. {
  93. int c = len;
  94. const char *cp = buffer;
  95. while(c--)
  96. DebugPut(*cp++);
  97. return len;
  98. }
  99. static int DebugWrite_P(NUTFILE * fp, PGM_P buffer, int len)
  100. {
  101. int c = len;
  102. PGM_P cp = buffer;
  103. while(c--) {
  104. DebugPut(PRG_RDB(cp));
  105. cp++;
  106. }
  107. return len;
  108. }
  109. static NUTFILE *DebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  110. {
  111. dbgfile.nf_dev = dev;
  112. dbgfile.nf_fcb = NULL;
  113. return &dbgfile;
  114. }
  115. #ifdef NUT_DEV_DEBUG_READ
  116. /*!
  117. * \brief Read characters from debug device.
  118. *
  119. * This function is called by the low level input routines of the
  120. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  121. * entry.
  122. *
  123. * The function will block the calling thread until at least one
  124. * character has been received.
  125. *
  126. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  127. * previous call to At91DevDebugOpen().
  128. * \param buffer Pointer to the buffer that receives the data. If zero,
  129. * then all characters in the input buffer will be
  130. * removed.
  131. * \param size Maximum number of bytes to read.
  132. *
  133. * \return The number of bytes read, which may be less than the number
  134. * of bytes specified. A return value of -1 indicates an error,
  135. * while zero is returned in case of a timeout.
  136. */
  137. int DebugRead(NUTFILE * fp, void *buffer, int size)
  138. {
  139. int rc;
  140. unsigned int ch;
  141. char *bp = (char *) buffer;
  142. /* Wait for the first character, forever. */
  143. for (rc = 0; rc < size; rc++) {
  144. while ((inb(UCSR1A) & _BV(RXC)) == 0) {
  145. NutSleep(1);
  146. if ((rc || bp == NULL) && (inb(UCSR1A) & _BV(RXC1)) == 0) {
  147. return rc;
  148. }
  149. }
  150. ch = inb(UDR1);
  151. if (bp) {
  152. if (ch == '\r') {
  153. *bp++ = '\n';
  154. } else {
  155. *bp++ = (char) ch;
  156. }
  157. }
  158. }
  159. return rc;
  160. }
  161. /*!
  162. * \brief Retrieves the number of characters in input buffer.
  163. *
  164. * This function is called by the low level size routine of the C runtime
  165. * library, using the _NUTDEVICE::dev_size entry.
  166. *
  167. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  168. * previous call to UsartOpen().
  169. *
  170. * \return The number of bytes currently stored in input buffer.
  171. */
  172. long DebugSize(NUTFILE *fp)
  173. {
  174. while (inb(UCSR1A) & _BV(RXC1)) {
  175. return 1;
  176. }
  177. return 0;
  178. }
  179. #endif
  180. /*!
  181. * \brief Close a device or file.
  182. */
  183. static int DebugClose(NUTFILE * fp)
  184. {
  185. return 0;
  186. }
  187. /*!
  188. * \brief UART 1 Device information structure.
  189. */
  190. NUTDEVICE devDebug1 = {
  191. 0, /*!< Pointer to next device. */
  192. {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}, /*!< Unique device name. */
  193. 0, /*!< Type of device. */
  194. 0, /*!< Base address. */
  195. 0, /*!< First interrupt number. */
  196. 0, /*!< Interface control block. */
  197. 0, /*!< Driver control block. */
  198. DebugInit, /*!< Driver initialization routine. */
  199. DebugIOCtl, /*!< Driver specific control function. */
  200. #ifdef NUT_DEV_DEBUG_READ
  201. DebugRead,
  202. #else
  203. NULL,
  204. #endif
  205. DebugWrite,
  206. DebugWrite_P,
  207. DebugOpen,
  208. DebugClose,
  209. #ifdef NUT_DEV_DEBUG_READ
  210. DebugSize,
  211. #else
  212. NULL,
  213. #endif
  214. NULL, /*!< Select function, optional, not yet implemented */
  215. };
  216. #endif
  217. /*@}*/