lpc_debug-old.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (C) 2011-2012 by egnite 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. #include <cfg/uart.h>
  35. #include <dev/debug.h>
  36. #include <sys/timer.h>
  37. #include <arch/cm3/nxp/mach/lpc1700.h>
  38. #include <arch/cm3/nxp/lpc_debug.h>
  39. /*!
  40. * \addtogroup xgDevDebugLpc
  41. */
  42. /*@{*/
  43. /*!
  44. * \brief Open debug device.
  45. *
  46. * \param dev Pointer to a previously registered \ref NUTDEVICE structure.
  47. * \param name Ignored, typically points to an empty string.
  48. * \param mode Ignored, operation mode.
  49. * \param acc Ignored, should be zero.
  50. *
  51. * \return Pointer to a static NUTFILE structure.
  52. */
  53. NUTFILE *LpcDevDebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  54. {
  55. NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
  56. fp->nf_next = NULL;
  57. fp->nf_dev = dev;
  58. fp->nf_fcb = NULL;
  59. return fp;
  60. }
  61. /*!
  62. * \brief Close debug device.
  63. *
  64. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a previous
  65. * call to LpcDevDebugOpen().
  66. *
  67. * \return Always 0.
  68. */
  69. int LpcDevDebugClose(NUTFILE * fp)
  70. {
  71. return 0;
  72. }
  73. /*!
  74. * \brief Handle I/O controls for debug device 2.
  75. *
  76. * The debug device supports UART_SETSPEED only.
  77. *
  78. * \param dev Identifies the device that receives the device-control
  79. * function.
  80. * \param req Requested control function. May be set to one of the
  81. * following constants:
  82. * - UART_SETSPEED, conf points to an uint32_t value containing the baudrate.
  83. * \param conf Points to a variable that contains any data required for
  84. * the given control function or receives data from that
  85. * function.
  86. *
  87. * \return 0 on success, -1 otherwise.
  88. */
  89. int LpcDevDebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  90. {
  91. return -1;
  92. }
  93. /*!
  94. * \brief Send a single character to debug device.
  95. *
  96. * A newline character will be automatically prepended by a carriage
  97. * return.
  98. */
  99. static void DebugPut(const NUTDEVICE * dev, char ch)
  100. {
  101. if (ch == '\n') {
  102. DebugPut(dev, '\r');
  103. }
  104. while ((mem_rd32(dev->dev_base + UART_LSR_OFF) & UART_THRE) == 0);
  105. mem_wr32_mb(dev->dev_base + UART_THR_OFF, ch);
  106. }
  107. /*!
  108. * \brief Send characters to debug device 0.
  109. *
  110. * This function is called by the low level input routines of the
  111. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_write
  112. * entry.
  113. *
  114. * A newline character will be automatically prepended by a carriage
  115. * return.
  116. *
  117. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a previous
  118. * call to LpcDevDebugOpen().
  119. *
  120. * \return Number of characters sent.
  121. */
  122. int LpcDevDebugWrite(NUTFILE * fp, const void *buffer, int len)
  123. {
  124. int c = len;
  125. const char *cp = buffer;
  126. while (c--) {
  127. DebugPut(fp->nf_dev, *cp++);
  128. }
  129. return len;
  130. }
  131. #ifdef NUT_DEV_DEBUG_READ
  132. /*!
  133. * \brief Read characters from debug device.
  134. *
  135. * This function is called by the low level input routines of the
  136. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  137. * entry.
  138. *
  139. * The function will block the calling thread until at least one
  140. * character has been received.
  141. *
  142. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  143. * previous call to LpcDevDebugOpen().
  144. * \param buffer Pointer to the buffer that receives the data. If zero,
  145. * then all characters in the input buffer will be
  146. * removed.
  147. * \param size Maximum number of bytes to read.
  148. *
  149. * \return The number of bytes read, which may be less than the number
  150. * of bytes specified. A return value of -1 indicates an error,
  151. * while zero is returned in case of a timeout.
  152. */
  153. int LpcDevDebugRead(NUTFILE * fp, void *buffer, int size)
  154. {
  155. int rc;
  156. unsigned int ch;
  157. char *bp = (char *) buffer;
  158. /* Wait for the first character, forever. */
  159. for (rc = 0; rc < size; rc++) {
  160. while ((mem_rd32(fp->nf_dev->dev_base + UART_LSR_OFF) & UART_RDR) == 0) {
  161. NutSleep(1);
  162. if ((rc || bp == NULL) &&
  163. (mem_rd32(fp->nf_dev->dev_base + UART_LSR_OFF) & UART_RDR) == 0) {
  164. return rc;
  165. }
  166. }
  167. ch = mem_rd32_mb(fp->nf_dev->dev_base + UART_RBR_OFF);
  168. if (bp) {
  169. if (ch == '\r') {
  170. *bp++ = '\n';
  171. } else {
  172. *bp++ = (char) ch;
  173. }
  174. }
  175. }
  176. return rc;
  177. }
  178. /*!
  179. * \brief Retrieves the number of characters in input buffer.
  180. *
  181. * This function is called by the low level size routine of the C runtime
  182. * library, using the _NUTDEVICE::dev_size entry.
  183. *
  184. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a previous
  185. * call to LpcDevDebugOpen().
  186. *
  187. * \return The number of bytes currently stored in input buffer.
  188. */
  189. long LpcDevDebugSize(NUTFILE *fp)
  190. {
  191. if (mem_rd32(fp->nf_dev->dev_base + UART_LSR_OFF) & UART_RDR) {
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. #endif /* NUT_DEV_DEBUG_READ */
  197. /*@}*/