mcf5_sci_debug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright 2012 by Embedded Technologies s.r.o
  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. #include <dev/debug.h>
  33. #include <dev/gpio.h>
  34. /*!
  35. * \brief Handle I/O controls for the debug device.
  36. *
  37. * \param dev Identifies the device that receives the device-control
  38. * request.
  39. * \param req Requested control function.
  40. * \param conf Points to a buffer that contains any data required for
  41. * the given control function or receives data from that
  42. * function.
  43. *
  44. * \return 0 on success, -1 if the function fails or is not available.
  45. */
  46. static int IOCtl(NUTDEVICE * dev, int req, void *conf)
  47. {
  48. return -1;
  49. }
  50. /*!
  51. * \brief Send a single character to debug device.
  52. *
  53. * The function will automatically prepend any newline character
  54. * (ASCII 10) with a carriage return character (ASCII 13).
  55. *
  56. * \param ch The character to send.
  57. */
  58. static void Put(uintptr_t devnum, char ch)
  59. {
  60. /* Prepend NL with CR. */
  61. if (ch == '\n') {
  62. Put(devnum, '\r');
  63. }
  64. /* Wait until space is available in the FIFO */
  65. // while (!(MCF_UART_USR(devnum) & MCF_UART_USR_TXRDY))
  66. // ;
  67. /* Send the character */
  68. // MCF_UART_UTB(devnum) = (uint8_t) ch;
  69. }
  70. /*!
  71. * \brief Send a buffer contents to the debug device.
  72. *
  73. * This function is called by the low level input routines of the
  74. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  75. * entry.
  76. *
  77. * Applications must not call this function, but use the stdio
  78. * functions instead.
  79. *
  80. * The debug driver is not interrupt driven and has no internal buffer
  81. * and no timeout control. Thus, it can be safely used in interrupt routines.
  82. * Of course, this will significantly decrease interrupt latency and overall
  83. * system performance.
  84. *
  85. * \param fp Identifies the device to send to. This may be used by
  86. * the driver to retrieve the \ref NUTDEVICE pointer.
  87. * \param buffer Pointer to the data to be written.
  88. * \param len Number of characters to write. If 0, then the caller
  89. * requests to flush the drivers internal output buffer.
  90. * In this case the pointer to the data is ignored.
  91. *
  92. * \return The number of characters written, which is the same as the
  93. * number of characters specified.
  94. */
  95. static int Write(NUTFILE * fp, const void *buffer, int len)
  96. {
  97. int c = len;
  98. const char *cp = (const char *) buffer;
  99. uintptr_t devnum = fp->nf_dev->dev_base;
  100. while (c--) {
  101. Put(devnum, *cp++);
  102. }
  103. return len;
  104. }
  105. /*!
  106. * \brief Read bytes from file
  107. *
  108. * \return Number of characters read.
  109. */
  110. static int Read(NUTFILE * nf, void *buffer, int len)
  111. {
  112. return 0;
  113. }
  114. /*!
  115. * \brief Open debug device.
  116. *
  117. * \return Pointer to a static NUTFILE structure.
  118. */
  119. static NUTFILE *Open(NUTDEVICE * dev, const char *name, int mode, int acc)
  120. {
  121. NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
  122. fp->nf_dev = dev;
  123. fp->nf_fcb = NULL;
  124. return fp;
  125. }
  126. /*!
  127. * \brief Close debug device.
  128. *
  129. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  130. * previous call to ZeroDebugOpen().
  131. *
  132. * \return Always 0.
  133. */
  134. static int Close(NUTFILE * fp)
  135. {
  136. /* Nothing to do for this simple driver. */
  137. return 0;
  138. }
  139. /*!
  140. * \brief Initialize debug device.
  141. *
  142. * This function is called by NutRegisterDevice(), using the
  143. * _NUTDEVICE::dev_init entry.
  144. *
  145. * Applications must not call this function, they must call
  146. * NutRegisterDevice() instead.
  147. *
  148. * \param dev Pointer to the device information structure.
  149. *
  150. * \return Always 0.
  151. */
  152. static int Init(NUTDEVICE * dev)
  153. {
  154. uint8_t devnum = dev->dev_base;
  155. // uint32_t sysclk = NutGetCpuClock();
  156. // int baud = 115200;
  157. // register uint16_t ubgs;
  158. /* Enable SCI Port on GPIO */
  159. switch (devnum) {
  160. case 1:
  161. GpioPinConfigSet(SCI1_TXD_PORT, SCI1_TXD_PIN, SCI1_TXD_PERIPHERAL);
  162. GpioPinConfigSet(SCI1_RXD_PORT, SCI1_RXD_PIN, SCI1_RXD_PERIPHERAL);
  163. break;
  164. case 2:
  165. GpioPinConfigSet(SCI2_TXD_PORT, SCI2_TXD_PIN, SCI2_TXD_PERIPHERAL);
  166. GpioPinConfigSet(SCI2_RXD_PORT, SCI2_RXD_PIN, SCI2_RXD_PERIPHERAL);
  167. break;
  168. case 3:
  169. GpioPinConfigSet(SCI3_TXD_PORT, SCI3_TXD_PIN, SCI3_TXD_PERIPHERAL);
  170. GpioPinConfigSet(SCI3_RXD_PORT, SCI3_RXD_PIN, SCI3_RXD_PERIPHERAL);
  171. break;
  172. default:
  173. return 0;
  174. }
  175. /* Reset Transmitter */
  176. // MCF_UART_UCR(devnum) = MCF_UART_UCR_RESET_TX;
  177. /* Reset Receiver */
  178. // MCF_UART_UCR(devnum) = MCF_UART_UCR_RESET_RX;
  179. /* Reset Mode Register */
  180. // MCF_UART_UCR(devnum) = MCF_UART_UCR_RESET_MR;
  181. /* No parity, 8-bits per character */
  182. // MCF_UART_UMR(devnum) = MCF_UART_UMR_PM_NONE | MCF_UART_UMR_BC_8;
  183. /* Normal mode(No echo or loopback), 1 stop bit */
  184. // MCF_UART_UMR(devnum) = MCF_UART_UMR_CM_NORMAL | MCF_UART_UMR_SB_STOP_BITS_1;
  185. /* Select Rx and Tx clocks */
  186. // MCF_UART_UCSR(devnum) = MCF_UART_UCSR_RCS_SYS_CLK | MCF_UART_UCSR_TCS_SYS_CLK;
  187. /* Calculate baud settings */
  188. // ubgs = (uint16_t)(sysclk / (baud * 32));
  189. // MCF_UART_UBG1(devnum) = (uint8_t)((ubgs & 0xFF00) >> 8);
  190. // MCF_UART_UBG2(devnum) = (uint8_t)(ubgs & 0x00FF);
  191. /* Enable receiver and transmitter */
  192. // MCF_UART_UCR(devnum) = MCF_UART_UCR_TX_ENABLED | MCF_UART_UCR_RX_ENABLED;
  193. return 0;
  194. }
  195. /*
  196. * While most drivers allocate this structure from the heap during
  197. * the open call and release it during close, this driver uses a
  198. * static structure. Therefore, concurrent open calls are not
  199. * allowed.
  200. */
  201. static NUTFILE dbgfile;
  202. /*!
  203. * \brief Debug device information structure.
  204. *
  205. * Usually, the device structure is the only public symbol that may be
  206. * referenced by the application code using
  207. *
  208. * \code
  209. * #include <dev/debug.h>
  210. *
  211. * {
  212. * ...
  213. * NutRegisterDevice(&devDebug0, 0, 0);
  214. * ...
  215. * }
  216. * \endcode
  217. *
  218. * If not referenced, the driver code (and this structure) will not be
  219. * included in the final binary.
  220. *
  221. * The name of the structure may differ among platforms. Portable
  222. * applications should avoid it and instead make use of dev/board.h.
  223. *
  224. * \code
  225. * #include <dev/board.h>
  226. *
  227. * {
  228. * ...
  229. * NutRegisterDevice(&DEV_DEBUG, 0, 0);
  230. * ...
  231. * }
  232. * \endcode
  233. *
  234. * While output is supported by default, input may be not. If input is
  235. * required, applications may replace \ref DEV_DEBUG by \ref DEV_CONSOLE.
  236. * In this case the debug driver is selected only, if it has input
  237. * capability (see \ref NUT_DEV_DEBUG_READ). Otherwise an interrupt
  238. * driven SCI driver will be used.
  239. *
  240. * Note, that this polling driver has certain advantages
  241. * - very low memory usage
  242. * - allows stdio output functions in interrupt context
  243. * - allows stdio output functions in early system stage
  244. * - no internal buffering, output is synchronous
  245. * - atomic output with multiple threads
  246. *
  247. * but also some disadvantages
  248. * - concurrent threads are blocked during output
  249. * - most or all SCI settings are hard coded
  250. * - may not work with non-ASCII (binary) data
  251. * - often only output is supported, not input
  252. * - only one instance (open) is allowed
  253. *
  254. * When used with Harvard architectures, additional functions may
  255. * be offered to access data in program space.
  256. */
  257. NUTDEVICE devDebug1 = {
  258. NULL, /*!< _NUTDEVICE::dev_next, must be NULL */
  259. { 's', 'c', 'i', '1', 0, 0, 0, 0, 0 }, /*!< _NUTDEVICE::dev_name, use for all sci1 drivers. */
  260. IFTYP_CHAR, /*!< _NUTDEVICE::dev_type, probably not used, may be 0. */
  261. 1, /*!< _NUTDEVICE::dev_base, device number = 2(COM2). */
  262. 0, /*!< _NUTDEVICE::dev_irq, not used by this driver. */
  263. NULL, /*!< _NUTDEVICE::dev_icb, not used by this driver. */
  264. &dbgfile, /*!< _NUTDEVICE::dev_dcb, stores the \ref NUTFILE handle. */
  265. Init, /*!< _NUTDEVICE::dev_init. */
  266. IOCtl, /*!< _NUTDEVICE::dev_ioctl. */
  267. Read, /*!< _NUTDEVICE::dev_read, optional, may be NULL. */
  268. Write, /*!< _NUTDEVICE::dev_write. */
  269. Open, /*!< _NUTDEVICE::dev_open. */
  270. Close, /*!< _NUTDEVICE::dev_close. */
  271. NULL /*!< _NUTDEVICE::dev_size, optional, may be NULL. */
  272. NULL, /*!< _NUTDEVICE::dev_select, optional, may be NULL */
  273. };
  274. NUTDEVICE devDebug2 = {
  275. NULL, /*!< _NUTDEVICE::dev_next, must be NULL */
  276. { 's', 'c', 'i', '2', 0, 0, 0, 0, 0 }, /*!< _NUTDEVICE::dev_name, use for all sci2 drivers. */
  277. IFTYP_CHAR, /*!< _NUTDEVICE::dev_type, probably not used, may be 0. */
  278. 2, /*!< _NUTDEVICE::dev_base, device number = 2(COM2). */
  279. 0, /*!< _NUTDEVICE::dev_irq, not used by this driver. */
  280. NULL, /*!< _NUTDEVICE::dev_icb, not used by this driver. */
  281. &dbgfile, /*!< _NUTDEVICE::dev_dcb, stores the \ref NUTFILE handle. */
  282. Init, /*!< _NUTDEVICE::dev_init. */
  283. IOCtl, /*!< _NUTDEVICE::dev_ioctl. */
  284. Read, /*!< _NUTDEVICE::dev_read, optional, may be NULL. */
  285. Write, /*!< _NUTDEVICE::dev_write. */
  286. Open, /*!< _NUTDEVICE::dev_open. */
  287. Close, /*!< _NUTDEVICE::dev_close. */
  288. NULL /*!< _NUTDEVICE::dev_size, optional, may be NULL. */
  289. NULL, /*!< _NUTDEVICE::dev_select, optional, may be NULL */
  290. };
  291. NUTDEVICE devDebug3 = {
  292. NULL, /*!< _NUTDEVICE::dev_next, must be NULL */
  293. { 's', 'c', 'i', '3', 0, 0, 0, 0, 0 }, /*!< _NUTDEVICE::dev_name, use for all sci3 drivers. */
  294. IFTYP_CHAR, /*!< _NUTDEVICE::dev_type, probably not used, may be 0. */
  295. 3, /*!< _NUTDEVICE::dev_base, device number = 2(COM2). */
  296. 0, /*!< _NUTDEVICE::dev_irq, not used by this driver. */
  297. NULL, /*!< _NUTDEVICE::dev_icb, not used by this driver. */
  298. &dbgfile, /*!< _NUTDEVICE::dev_dcb, stores the \ref NUTFILE handle. */
  299. Init, /*!< _NUTDEVICE::dev_init. */
  300. IOCtl, /*!< _NUTDEVICE::dev_ioctl. */
  301. Read, /*!< _NUTDEVICE::dev_read, optional, may be NULL. */
  302. Write, /*!< _NUTDEVICE::dev_write. */
  303. Open, /*!< _NUTDEVICE::dev_open. */
  304. Close, /*!< _NUTDEVICE::dev_close. */
  305. NULL /*!< _NUTDEVICE::dev_size, optional, may be NULL. */
  306. NULL, /*!< _NUTDEVICE::dev_select, optional, may be NULL */
  307. };