scih8dbg.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2004 by Jan Dubiec. 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 JAN DUBIEC 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 JAN DUBIEC
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * $Log$
  31. * Revision 1.2 2005/08/02 17:46:46 haraldkipp
  32. * Major API documentation update.
  33. *
  34. * Revision 1.1 2005/07/26 18:02:40 haraldkipp
  35. * Moved from dev.
  36. *
  37. * Revision 1.1 2004/03/16 16:48:27 haraldkipp
  38. * Added Jan Dubiec's H8/300 port.
  39. *
  40. */
  41. #include <dev/debug.h>
  42. /*!
  43. * \addtogroup xgNutArchH8300hDevDebug
  44. */
  45. /*@{*/
  46. #include <sys/device.h>
  47. #include <sys/file.h>
  48. #include <h83068f.h>
  49. #define SSR_TDRE 0x80
  50. #define SSR_RDRF 0x40
  51. #define SSR_ORER 0x20
  52. #define SSR_FER 0x10
  53. #define SSR_PER 0x08
  54. #define SSR_TEND 0x04
  55. static NUTFILE dbgfile;
  56. /*
  57. * Returns apropriate H8/300H SCI structure pointer.
  58. */
  59. #define GET_SCI(p) ( ((p) == 0) ? &SCI0 : ( ((p) == 1) ? &SCI1 : &SCI2 ) )
  60. /*!
  61. * \brief Handle I/O controls for debug device.
  62. *
  63. * The debug device doesn't support any.
  64. *
  65. * \return Always -1.
  66. */
  67. static int DebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  68. {
  69. return -1;
  70. }
  71. /*!
  72. * \brief Initialize debug device.
  73. *
  74. * Simply enable the device. Baudrate divisor set to 7 for
  75. * 115.2 kBaud at 22.1184 MHz.
  76. *
  77. * \return Always 0.
  78. */
  79. static int DebugInit(NUTDEVICE * dev)
  80. {
  81. unsigned short i;
  82. volatile struct st_sci *sci = GET_SCI(dev->dev_base);
  83. /* serial port initialisation */
  84. sci->SCR.BYTE = 0x00;
  85. sci->SMR.BYTE = 0x00;
  86. // sci->BRR = 0x47; /* 9600 bps with 22.1184MHz crystal */
  87. sci->BRR = 0x05; /* 115200 bps with 22.1184MHz crystal */
  88. for (i = 0; i < 500; i++); /* wait for a while */
  89. sci->SCR.BYTE = 0x30;
  90. sci->SSR.BYTE &= 0x84;
  91. return 0;
  92. }
  93. /*!
  94. * \brief Send a single character to debug device.
  95. *
  96. * A carriage return character will be automatically appended
  97. * to any linefeed.
  98. */
  99. static void DebugPut(volatile struct st_sci *sci, char ch)
  100. {
  101. while (!(sci->SSR.BYTE & SSR_TDRE)); /* Wait until TDRE = 1 */
  102. sci->TDR = ch; /* Set transmit data */
  103. sci->SSR.BYTE &= ~SSR_TDRE;
  104. if (ch == '\n')
  105. DebugPut(sci, '\r');
  106. }
  107. /*!
  108. * \brief Read a single character from debug device.
  109. *
  110. * This function waits until a character is received.
  111. */
  112. //static int DebugGet(volatile struct st_sci* sci)
  113. //{
  114. // unsigned char c;
  115. /* clear any detected errors */
  116. // sci->SSR.BYTE &= ~(SSR_ORER | SSR_FER | SSR_PER); /* Clear error bits */
  117. /* wait for a byte */
  118. // while(!( sci->SSR.BYTE & SSR_RDRF));
  119. /* got one-- return it */
  120. // c = sci->RDR;
  121. // sci->SSR.BYTE &= ~SSR_RDRF;
  122. // return (int) c;
  123. //}
  124. /*!
  125. * \brief Read a single character from debug device.
  126. *
  127. * Non blocking version of DebugGet(). If I/O operation is pending
  128. * this function returns -1.
  129. */
  130. static int DebugGetNB(volatile struct st_sci *sci)
  131. {
  132. unsigned char c;
  133. /* clear any detected errors */
  134. if ((sci->SSR.BYTE & (SSR_ORER | SSR_FER | SSR_PER))) {
  135. sci->SSR.BYTE &= ~(SSR_ORER | SSR_FER | SSR_PER); /* Clear error bits */
  136. return -1;
  137. }
  138. /* wait for a byte */
  139. if (!(sci->SSR.BYTE & SSR_RDRF))
  140. return -1;
  141. /* got one-- return it */
  142. c = sci->RDR;
  143. sci->SSR.BYTE &= ~SSR_RDRF;
  144. return (int) c;
  145. }
  146. /*!
  147. * \brief Read one character from debug device.
  148. *
  149. * This function doesn't wait for a character. If I/O operation is pending,
  150. * it returns -1
  151. * \return Number of characters read.
  152. */
  153. static int DebugRead(NUTFILE * fp, void *buffer, int len)
  154. {
  155. int c, l = 0;
  156. char *cp = buffer;
  157. volatile struct st_sci *sci = GET_SCI(fp->nf_dev->dev_base);
  158. while (l < len) {
  159. c = DebugGetNB(sci);
  160. if (c == -1) {
  161. *cp++ = '\0';
  162. return 0;
  163. }
  164. cp[l++] = (unsigned char) c;
  165. }
  166. return l;
  167. }
  168. /*!
  169. * \brief Send characters to debug device.
  170. *
  171. * A carriage return character will be automatically appended
  172. * to any linefeed.
  173. *
  174. * \return Number of characters sent.
  175. */
  176. static int DebugWrite(NUTFILE * fp, CONST void *buffer, int len)
  177. {
  178. int c = len;
  179. CONST char *cp = buffer;
  180. volatile struct st_sci *sci = GET_SCI(fp->nf_dev->dev_base);
  181. while (c--)
  182. DebugPut(sci, *cp++);
  183. return len;
  184. }
  185. /*!
  186. * \brief Open debug device.
  187. *
  188. * \return Pointer to a static NUTFILE structure.
  189. */
  190. static NUTFILE *DebugOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc)
  191. {
  192. dbgfile.nf_dev = dev;
  193. dbgfile.nf_fcb = NULL;
  194. return &dbgfile;
  195. }
  196. /*!
  197. * \brief Close debug device.
  198. *
  199. * \return Always 0.
  200. */
  201. static int DebugClose(NUTFILE * fp)
  202. {
  203. return 0;
  204. }
  205. /*!
  206. * \brief Debug device information structure.
  207. */
  208. NUTDEVICE devDebug0 = {
  209. 0, /*!< Pointer to next device. */
  210. {'s', 'c', 'i', '0', 'd', 'b', 'g', 0, 0}
  211. , /*!< Unique device name. */
  212. 0, /*!< Type of device. */
  213. 0, /*!< Base address. */
  214. 0, /*!< First interrupt number. */
  215. 0, /*!< Interface control block. */
  216. 0, /*!< Driver control block. */
  217. DebugInit, /*!< Driver initialization routine. */
  218. DebugIOCtl, /*!< Driver specific control function. */
  219. DebugRead,
  220. DebugWrite,
  221. /* Write from program space data to device. */
  222. DebugOpen,
  223. DebugClose,
  224. 0,
  225. 0, /*!< Select function, optional, not yet implemented */
  226. };
  227. NUTDEVICE devDebug1 = {
  228. 0, /*!< Pointer to next device. */
  229. {'s', 'c', 'i', '1', 'd', 'b', 'g', 0, 0}
  230. , /*!< Unique device name. */
  231. 0, /*!< Type of device. */
  232. 1, /*!< Base address. */
  233. 0, /*!< First interrupt number. */
  234. 0, /*!< Interface control block. */
  235. 0, /*!< Driver control block. */
  236. DebugInit, /*!< Driver initialization routine. */
  237. DebugIOCtl, /*!< Driver specific control function. */
  238. DebugRead,
  239. DebugWrite,
  240. /* Write from program space data to device. */
  241. DebugOpen,
  242. DebugClose,
  243. 0,
  244. 0, /*!< Select function, optional, not yet implemented */
  245. };
  246. NUTDEVICE devDebug2 = {
  247. 0, /*!< Pointer to next device. */
  248. {'s', 'c', 'i', '2', 'd', 'b', 'g', 0, 0}
  249. , /*!< Unique device name. */
  250. 0, /*!< Type of device. */
  251. 2, /*!< Base address. */
  252. 0, /*!< First interrupt number. */
  253. 0, /*!< Interface control block. */
  254. 0, /*!< Driver control block. */
  255. DebugInit, /*!< Driver initialization routine. */
  256. DebugIOCtl, /*!< Driver specific control function. */
  257. DebugRead,
  258. DebugWrite,
  259. /* Write from program space data to device. */
  260. DebugOpen,
  261. DebugClose,
  262. 0,
  263. 0, /*!< Select function, optional, not yet implemented */
  264. };
  265. /*@}*/