debug_lpc2xxx.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /****************************************************************************
  2. * This file is part of the Ethernut port for the LPC2XXX
  3. *
  4. * Copyright (c) 2005 by Michael Fischer. 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 author nor the names of its contributors may
  16. * be used to endorse or promote products derived from this software
  17. * 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
  23. * THE 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. ****************************************************************************
  33. *
  34. * History:
  35. *
  36. * 24.09.05 mifi First Version
  37. * The CrossWorks for ARM toolchain will be used.
  38. ****************************************************************************/
  39. #include <string.h>
  40. #include <arch/arm/lpc2xxx.h>
  41. #include <dev/debug.h>
  42. #include <sys/device.h>
  43. #include <sys/file.h>
  44. static NUTFILE dbgfile;
  45. /*!
  46. * \brief Handle I/O controls for debug device.
  47. *
  48. * The debug device doesn't support any.
  49. *
  50. * \return Always -1.
  51. */
  52. int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  53. {
  54. return -1;
  55. }
  56. /*!
  57. * \brief Initialize debug device.
  58. *
  59. * Simply enable the device. 115.2 kBaud at 14.7456 MHz.
  60. *
  61. * \return Always 0.
  62. */
  63. int DebugInit(NUTDEVICE * dev)
  64. {
  65. if (dev->dev_name[4] == '0') {
  66. U0LCR = _BV(7);
  67. U0DLL = 0x08;
  68. U0DLM = 0x00;
  69. U0LCR = 0x03;
  70. U0IER = 0x00;
  71. U0FCR = 0x00;
  72. PINSEL0 |= 0x00000005;
  73. } else if (dev->dev_name[4] == '1') {
  74. U1LCR = _BV(7);
  75. U1DLL = 0x08;
  76. U1DLM = 0x00;
  77. U1LCR = 0x03;
  78. U1IER = 0x00;
  79. U1FCR = 0x00;
  80. PINSEL0 |= 0x00050000;
  81. }
  82. return 0;
  83. }
  84. /*!
  85. * \brief Send a single character to debug device 0.
  86. *
  87. * A carriage return character will be automatically appended
  88. * to any linefeed.
  89. */
  90. void DebugPut0(char ch)
  91. {
  92. if(ch == '\n') {
  93. while ((U0LSR & U0LSR_THRE) == 0);
  94. U0THR = '\r';
  95. }
  96. while ((U0LSR & U0LSR_THRE) == 0);
  97. U0THR = ch;
  98. }
  99. /*!
  100. * \brief Send a single character to debug device 1.
  101. *
  102. * A carriage return character will be automatically appended
  103. * to any linefeed.
  104. */
  105. void DebugPut1(char ch)
  106. {
  107. if(ch == '\n') {
  108. while ((U1LSR & U1LSR_THRE) == 0);
  109. U1THR = '\r';
  110. }
  111. while ((U1LSR & U1LSR_THRE) == 0);
  112. U1THR = ch;
  113. }
  114. /*!
  115. * \brief Send characters to debug device.
  116. *
  117. * A carriage return character will be automatically appended
  118. * to any linefeed.
  119. *
  120. * \return Number of characters sent.
  121. */
  122. int DebugWrite(NUTFILE * fp, const void *buffer, int len)
  123. {
  124. int c = len;
  125. const char *cp = buffer;
  126. NUTDEVICE *dev = fp->nf_dev;
  127. if (dev->dev_name[4] == '0') {
  128. while(c--)
  129. DebugPut0(*cp++);
  130. } else if (dev->dev_name[4] == '1') {
  131. while(c--)
  132. DebugPut1(*cp++);
  133. }
  134. return len;
  135. }
  136. /*!
  137. * \brief Open debug device.
  138. *
  139. * \return Pointer to a static NUTFILE structure.
  140. */
  141. NUTFILE *DebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  142. {
  143. dbgfile.nf_dev = dev;
  144. dbgfile.nf_fcb = NULL;
  145. return (&dbgfile);
  146. }
  147. /*!
  148. * \brief Close debug device.
  149. *
  150. * \return Always 0.
  151. */
  152. int DebugClose(NUTFILE * fp)
  153. {
  154. return (0);
  155. }
  156. /*!
  157. * \brief Debug device 0 information structure.
  158. */
  159. NUTDEVICE devDebug0 = {
  160. 0, /*!< Pointer to next device, dev_next. */
  161. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}, /*!< Unique device name, dev_name. */
  162. 0, /*!< Type of device, dev_type. */
  163. 0xFFFD0000, /*!< Base address, dev_base. */
  164. 0, /*!< First interrupt number, dev_irq. */
  165. 0, /*!< Interface control block, dev_icb. */
  166. 0, /*!< Driver control block, dev_dcb. */
  167. DebugInit, /*!< Driver initialization routine, dev_init. */
  168. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  169. 0, /*!< dev_read. */
  170. DebugWrite, /*!< dev_write. */
  171. DebugOpen, /*!< dev_opem. */
  172. DebugClose, /*!< dev_close. */
  173. 0, /*!< dev_size. */
  174. 0, /*!< dev_select, optional, not yet implemented */
  175. };
  176. /*!
  177. * \brief Debug device 1 information structure.
  178. */
  179. NUTDEVICE devDebug1 = {
  180. 0, /*!< Pointer to next device, dev_next. */
  181. {'u', 'a', 'r', 't', '1', 0, 0, 0, 0}, /*!< Unique device name, dev_name. */
  182. 0, /*!< Type of device, dev_type. */
  183. 0xFFFCC000, /*!< Base address, dev_base. */
  184. 0, /*!< First interrupt number, dev_irq. */
  185. 0, /*!< Interface control block, dev_icb. */
  186. 0, /*!< Driver control block, dev_dcb. */
  187. DebugInit, /*!< Driver initialization routine, dev_init. */
  188. DebugIOCtl, /*!< Driver specific control function, dev_ioctl. */
  189. 0, /*!< dev_read. */
  190. DebugWrite, /*!< dev_write. */
  191. DebugOpen, /*!< dev_opem. */
  192. DebugClose, /*!< dev_close. */
  193. 0, /*!< dev_size. */
  194. 0, /*!< dev_select, optional, not yet implemented */
  195. };