dev_debug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * Copyright (C) 2011 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. /*!
  35. * \file arch/arm/dev/zero/dev_debug.c
  36. * \brief Debug output device for Zero CPU.
  37. *
  38. * This code implements a polling UART driver for an imaginary CPU.
  39. * It may serve as a template when porting Nut/OS to a new target.
  40. * Implementers should look out for 'TODO' comments.
  41. *
  42. * We intentionally do not check, whether the parameters passed to
  43. * any function of this driver are valid, using NUTASSERT or similar.
  44. * There are two main reasons:
  45. * - The driver code is quite simple (and should be kept simple)
  46. * - If an error occurs, this debug device will be used to report it
  47. *
  48. * If you choose add more functions to this driver, avoid Nut/OS API
  49. * calls, or make sure, that these calls will not introduce a context
  50. * switch or allocate heap memory. The read function may be an exception,
  51. * if its use is limited to normal application context.
  52. *
  53. * \verbatim
  54. * $Id$
  55. * \endverbatim
  56. */
  57. /* The Configurator will place UART specific configuration parameters
  58. in nutbld/include/cfg/uart.h. If nothing specific has been configured,
  59. the compiler will include the original nut/include/cfg/uart.h. */
  60. #include <cfg/uart.h>
  61. /*
  62. * TODO: Include architecture specific header files, if required.
  63. *
  64. * To implement your UART hardware, you need to access several
  65. * hardware registers and therefore need to include the header files,
  66. * where they are defined. However, if properly configured, this is usually
  67. * done automatically via compiler.h, which in turn is included almost
  68. * anywhere.
  69. *
  70. * Sometimes the deep level of included headers may become tricky to
  71. * follow or something may get broken by later changes. It is legitimate
  72. * to include essential header files, even if they are already included
  73. * indirectly by other header files.
  74. */
  75. /*
  76. * TODO: Add your target CPU or board to this file.
  77. *
  78. * Example:
  79. * #elif defined(MCU_xxxxx)
  80. * extern NUTDEVICE devDebug0;
  81. *
  82. */
  83. #include <dev/debug.h>
  84. #ifdef NUT_DEV_DEBUG_READ
  85. /* If input is enabled and if it calls NutSleep(), then we need this. */
  86. #include <sys/timer.h>
  87. #endif
  88. /*!
  89. * \addtogroup xgDevDebugZero
  90. */
  91. /*@{*/
  92. /*!
  93. * \brief Handle I/O controls for the debug device.
  94. *
  95. * Most debug device drivers support UART_SETSPEED only, to allow to
  96. * configure the baud rate. However, none is really required and the
  97. * driver may choose a fixed baud rate when initialized.
  98. *
  99. * Nut/OS calls this function via the _NUTDEVICE::dev_ioctl pointer.
  100. * In order to save code space, drivers may choose to eliminate this
  101. * function and point to the related function of the NULL device driver.
  102. *
  103. * Applications must not call this function, but use the ioctl()
  104. * function instead.
  105. *
  106. * \param dev Identifies the device that receives the device-control
  107. * request.
  108. * \param req Requested control function.
  109. * \param conf Points to a buffer that contains any data required for
  110. * the given control function or receives data from that
  111. * function.
  112. *
  113. * \return 0 on success, -1 if the function fails or is not available.
  114. */
  115. static int ZeroDebugIOCtl(NUTDEVICE * dev, int req, void *conf)
  116. {
  117. /*
  118. * TODO: You may implement some UART_xxx functions.
  119. *
  120. * However, all Nut/OS samples should work fine when leaving
  121. * it as it is.
  122. */
  123. return -1;
  124. }
  125. /*!
  126. * \brief Send a single character to debug device.
  127. *
  128. * The function will automatically prepend any newline character
  129. * (ASCII 10) with a carriage return character (ASCII 13).
  130. *
  131. * \param ch The character to send.
  132. */
  133. static void ZeroDebugPut(char ch)
  134. {
  135. if (ch == '\n') {
  136. /* Prepend NL with CR. */
  137. ZeroDebugPut('\r');
  138. }
  139. /*
  140. * TODO: Write the character to UART transmit register.
  141. *
  142. * Consult the datasheet of your hardware. You should
  143. * check the status register first and then write to
  144. * the transmit data register. This way the last character
  145. * is transmitted, while the application continues.
  146. */
  147. }
  148. /*!
  149. * \brief Send a buffer contents to the debug device.
  150. *
  151. * This function is called by the low level input routines of the
  152. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  153. * entry.
  154. *
  155. * Applications must not call this function, but use the stdio
  156. * functions instead.
  157. *
  158. * The debug driver is not interrupt driven and typically has no
  159. * internal buffer and no timeout control. Thus, it can be safely
  160. * used in interrupt routines. Of course, this will significantly
  161. * decrease interrupt latency and overall system performance.
  162. *
  163. * \param dev Identifies the device to send to. This may be used by
  164. * the driver to retrieve the \ref NUTDEVICE pointer.
  165. * \param buffer Pointer to the data to be written.
  166. * \param len Number of characters to write. If 0, then the caller
  167. * requests to flush the drivers internal output buffer.
  168. * In this case the pointer to the data is ignored.
  169. *
  170. * \return The number of characters written, which may be less than the
  171. * number of characters specified, if a timeout occurred.
  172. * A return value of -1 indicates an error.
  173. */
  174. static int ZeroDebugWrite(NUTFILE * fp, const void *buffer, int len)
  175. {
  176. int c = len;
  177. const char *cp = (const char *) buffer;
  178. while (c--) {
  179. ZeroDebugPut(*cp++);
  180. }
  181. /* Assume, that we always successfully sent all data. */
  182. return len;
  183. }
  184. #ifdef NUT_DEV_DEBUG_READ
  185. /*!
  186. * \brief Read characters from debug device.
  187. *
  188. * This function is called by the low level input routines of the
  189. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  190. * entry. The function is optional and available only, if
  191. * \ref NUT_DEV_DEBUG_READ is configured,
  192. *
  193. * The debug driver is not interrupt driven and typically has no
  194. * internal buffer and no timeout control. It will block the calling
  195. * thread until at least one character has been received. Characters,
  196. * that are received before this function is called, may get lost.
  197. *
  198. * Typically it suspends the current thread for 1 millisecond, if no
  199. * input character is available. Therefore, input functions are not
  200. * allowed in interrupt routines.
  201. *
  202. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  203. * previous call to ZeroDebugOpen().
  204. * \param buffer Pointer to the buffer that receives the data. If NULL,
  205. * then all characters in the input buffer will be
  206. * removed.
  207. * \param size Maximum number of bytes to read. If the buffer pointer
  208. * is NULL, then this must be zero as well.
  209. *
  210. * \return The number of bytes read, which may be less than the number
  211. * of bytes specified. A return value of -1 indicates an error,
  212. * while zero is returned in case of a timeout.
  213. */
  214. static int ZeroDebugRead(NUTFILE * fp, void *buffer, int size)
  215. {
  216. int rc;
  217. unsigned int ch;
  218. char *bp = (char *) buffer;
  219. /* Dummy flag to simulate data availability. */
  220. int available = 0;
  221. /* Wait for the first character, forever. */
  222. for (rc = 0; rc < size; rc++) {
  223. /*
  224. * TODO: Check, if a character is available.
  225. *
  226. * Consult the datasheet of your target CPU about how to check
  227. * the status register.
  228. */
  229. while (!available) {
  230. /* Nothing available, take a small nap to let other threads
  231. continue. Due to this call we can not do any input in
  232. interrupt context. */
  233. NutSleep(1);
  234. if ((rc || bp == NULL) && !available) {
  235. /* Still no new data. Return, if we already got something
  236. or if we had been called to flush the buffer. */
  237. return rc;
  238. }
  239. }
  240. /*
  241. * TODO: Get the character from the receiver.
  242. *
  243. * Consult the datasheet of your hardware about how to get the
  244. * last received character from the receiver data register.
  245. */
  246. ch = 0;
  247. if (bp) {
  248. if (ch == '\r') {
  249. *bp++ = '\n';
  250. } else {
  251. *bp++ = (char) ch;
  252. }
  253. }
  254. }
  255. return rc;
  256. }
  257. /*!
  258. * \brief Retrieves the number of characters in input buffer.
  259. *
  260. * Applications must not call this function, they may use kbhit()
  261. * instead. Although not standard, it is quite popular.
  262. *
  263. * This function is called by the low level size routine of the C runtime
  264. * library, using the _NUTDEVICE::dev_size entry. It is used by Nut/OS to
  265. * implement kbhit(). The function is optional and available only, if
  266. * \ref NUT_DEV_DEBUG_READ is configured,
  267. *
  268. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  269. * previous call to ZeroDebugOpen().
  270. *
  271. * \return The number of bytes currently stored in input buffer. The
  272. * debug driver is not interrupt driven and has no input buffer.
  273. * Thus, it will only return 0 or 1., unless the hardware
  274. * provides a FIFO.
  275. *
  276. */
  277. static long ZeroDebugSize(NUTFILE *fp)
  278. {
  279. /*
  280. * TODO: Return 0 if the receiver ready flag is inactive.
  281. *
  282. * You should only check the UART status register for the receiver
  283. * ready flag. Do not retrieve the character from the data register.
  284. *
  285. * If available, you may query the number of characters in the
  286. * hardware FIFO. However, in most cases this is not worth the
  287. * effort, because almost all applications just want to check,
  288. * if something is available before entering the blocking
  289. * read function.
  290. */
  291. return 1;
  292. }
  293. #else /* NUT_DEV_DEBUG_READ */
  294. /* The driver is write-only. */
  295. #define ZeroDebugRead NULL
  296. #define ZeroDebugSize NULL
  297. #endif /* NUT_DEV_DEBUG_READ */
  298. /*!
  299. * \brief Open debug device.
  300. *
  301. * \return Pointer to a static NUTFILE structure.
  302. */
  303. static NUTFILE *ZeroDebugOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  304. {
  305. NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
  306. fp->nf_dev = dev;
  307. fp->nf_fcb = NULL;
  308. return fp;
  309. }
  310. /*!
  311. * \brief Close debug device.
  312. *
  313. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  314. * previous call to ZeroDebugOpen().
  315. *
  316. * \return Always 0.
  317. */
  318. static int ZeroDebugClose(NUTFILE * fp)
  319. {
  320. /* Nothing to do for this simple driver. */
  321. return 0;
  322. }
  323. /*!
  324. * \brief Initialize debug device.
  325. *
  326. * This function is called by NutRegisterDevice(), using the
  327. * _NUTDEVICE::dev_init entry.
  328. *
  329. * Applications must not call this function, they must call
  330. * NutRegisterDevice() instead.
  331. *
  332. * \param dev Pointer to the device information structure.
  333. *
  334. * \return Always 0.
  335. */
  336. static int ZeroDebugInit(NUTDEVICE * dev)
  337. {
  338. /*
  339. * TODO: Initialize the UART hardware.
  340. *
  341. * Most targets require to enable specific clocks and to configure
  342. * peripheral pins.
  343. *
  344. * It is recommended to setup the UART for
  345. * - 115200 Baud
  346. * - 1 start bit
  347. * - 1 stop bit
  348. * - no parity
  349. */
  350. return 0;
  351. }
  352. /*
  353. * While most drivers allocate this structure from the heap during
  354. * the open call and release it during close, this driver uses a
  355. * static structure. Therefore, concurrent open calls are not
  356. * allowed.
  357. */
  358. static NUTFILE dbgfile;
  359. /*!
  360. * \brief Debug device information structure.
  361. *
  362. * Usually, the device structure is the only public symbol that may be
  363. * referenced by the application code using
  364. *
  365. * \code
  366. * #include <dev/debug.h>
  367. *
  368. * {
  369. * ...
  370. * NutRegisterDevice(&devDebug0, 0, 0);
  371. * ...
  372. * }
  373. * \endcode
  374. *
  375. * If not referenced, the driver code (and this structure) will not be
  376. * included in the final binary.
  377. *
  378. * The name of the structure may differ among platforms. Portable
  379. * applications should avoid it and instead make use of dev/board.h.
  380. *
  381. * \code
  382. * #include <dev/board.h>
  383. *
  384. * {
  385. * ...
  386. * NutRegisterDevice(&DEV_DEBUG, 0, 0);
  387. * ...
  388. * }
  389. * \endcode
  390. *
  391. * While output is supported by default, input may be not. If input is
  392. * required, applications may replace \ref DEV_DEBUG by \ref DEV_CONSOLE.
  393. * In this case the debug driver is selected only, if it has input
  394. * capability (see \ref NUT_DEV_DEBUG_READ). Otherwise an interrupt
  395. * driven UART driver will be used.
  396. *
  397. * Note, that this polling driver has certain advantages
  398. * - very low memory usage
  399. * - allows stdio output functions in interrupt context
  400. * - allows stdio output functions in early system stage
  401. * - no internal buffering, output is synchronous
  402. * - atomic output with multiple threads
  403. *
  404. * but also some disadvantages
  405. * - concurrent threads are blocked during output
  406. * - most or all UART settings are hard coded
  407. * - may not work with non-ASCII (binary) data
  408. * - often only output is supported, not input
  409. * - only one instance (open) is allowed
  410. *
  411. * When used with Harvard architectures, additional functions may
  412. * be offered to access data in program space.
  413. */
  414. NUTDEVICE devDebug0 = {
  415. NULL, /*!< _NUTDEVICE::dev_next, must be NULL */
  416. {'u', 'a', 'r', 't', '0', 0, 0, 0, 0}
  417. , /*!< _NUTDEVICE::dev_name, use for all UART0 drivers. */
  418. IFTYP_CHAR, /*!< _NUTDEVICE::dev_type, probably not used, may be 0. */
  419. 0, /*!< _NUTDEVICE::dev_base, not used by this driver. */
  420. 0, /*!< _NUTDEVICE::dev_irq, not used by this driver. */
  421. NULL, /*!< _NUTDEVICE::dev_icb, not used by this driver. */
  422. &dbgfile, /*!< _NUTDEVICE::dev_dcb, stores the \ref NUTFILE handle. */
  423. ZeroDebugInit, /*!< _NUTDEVICE::dev_init. */
  424. ZeroDebugIOCtl, /*!< _NUTDEVICE::dev_ioctl. */
  425. ZeroDebugRead, /*!< _NUTDEVICE::dev_read, optional, may be NULL. */
  426. ZeroDebugWrite, /*!< _NUTDEVICE::dev_write. */
  427. ZeroDebugOpen, /*!< _NUTDEVICE::dev_open. */
  428. ZeroDebugClose, /*!< _NUTDEVICE::dev_close. */
  429. ZeroDebugSize, /*!< _NUTDEVICE::dev_size, optional, may be NULL. */
  430. NULL, /*!< _NUTDEVICE::dev_select, optional, not yet implemented */
  431. };
  432. /*@}*/