usart_cb.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #ifndef DEV_USART_CB_H_
  2. #define DEV_USART_CB_H_
  3. /*
  4. * Copyright (C) 2012 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. */
  33. #include <sys/device.h>
  34. #include <dev/circbuff.h>
  35. /*!
  36. * \defgroup xgUsartCb Generic USART Driver Frame
  37. * \ingroup xgDevSerial
  38. * \anchor xrUsartCb
  39. * \brief Universal synchronous/asynchronous receiver/transmitter device driver.
  40. *
  41. * This new driver frame and all related low level drivers had been
  42. * partly tested by running the new PPP HDLC driver on the Ethernut 3.1D
  43. * reference board. No other tests had been done. This early code is
  44. * incomplete and probably buggy.
  45. *
  46. * Quite early it had been discovered, that UART drivers require a
  47. * set of functions, which are common in all implementations. To
  48. * simplify porting the code to different driver strategies and
  49. * hardware interfaces, the UART drivers had been divided into a general,
  50. * upper level part, also called the frame, and a lower level hardware
  51. * dependant part.
  52. *
  53. * This is the second version of a USART driver frame. Although
  54. * optimized for the 8 bit AVR family, the
  55. * \ref xrUsart "original USART driver frame"
  56. * had been directly ported to almost all supported 32 bit platforms.
  57. * This resulted in several problems:
  58. *
  59. * - To support AVR's single bit operations, UART register accesses were
  60. * done using absolute addresses. For the 32 bit targets this creates
  61. * duplicate code without benefit.
  62. * - The new targets offer new features, which were added to the original
  63. * code, making it larger without benefit for devices, which do not
  64. * support such functions. Over the time, the code became less readable
  65. * and maintainable, and sometimes broke existing code.
  66. * - Many variable sizes do not fit well, either to 8 bit or 32 bit
  67. * targets.
  68. *
  69. * This new approach tries to solve the mentioned issues.
  70. *
  71. * - Low level driver functions are called with a pointer to the driver
  72. * control block, so that these functions may be re-used for all UART
  73. * interfaces. In any case, it is still up to the low level driver
  74. * designer whether he wants to re-use code or implement individual
  75. * routines for each interface.
  76. * - Lower level drivers may enhance or replace single functions of this
  77. * upper level driver. Actually this would have been possible with the
  78. * previous version as well, but was not well understood and documented.
  79. * - Low level drivers implement a local ioctl function to keep devices
  80. * specific enhancements local. This also significantly reduces the
  81. * the number of interface functions between the upper and lower level
  82. * driver.
  83. * - Wherever it makes sense, stdint fast types are preferred, providing
  84. * the best performance / size ratio on all targets.
  85. * - New generic circular buffer functions are used, which automatically
  86. * adjust to a maximum buffer size. This allows the AVR to use 8 bit
  87. * buffer indices without constraining 32 bit targets.
  88. *
  89. * There are a few substantial changes compared to the original driver:
  90. *
  91. * - The low level interface is not compatible. Existing implementations
  92. * must be re-written, if they want to use this new driver frame.
  93. * However, the old driver will be still available.
  94. * - Transmit watermarks had been removed. The transmitter is now started
  95. * as soon as data is available in the transmit buffer.
  96. */
  97. /*@{*/
  98. /*!
  99. * \brief Circular receive buffer structure type.
  100. */
  101. typedef struct _USARTCB_RXBUFF USARTCB_RXBUFF;
  102. /*!
  103. * \brief Circular receive buffer structure.
  104. *
  105. * See also \ref _USARTCB_TXBUFF.
  106. */
  107. struct _USARTCB_RXBUFF {
  108. volatile uint8_t *rxb_buf;
  109. cb_size_t rxb_siz;
  110. cb_size_t rxb_rdi;
  111. volatile cb_size_t rxb_wri;
  112. HANDLE rxb_que;
  113. volatile cb_size_t rxb_cnt;
  114. };
  115. /*!
  116. * \brief Circular transmit buffer structure type.
  117. */
  118. typedef struct _USARTCB_TXBUFF USARTCB_TXBUFF;
  119. /*!
  120. * \brief Circular transmit buffer structure.
  121. *
  122. * \todo Both, this transmit buffer and the related receive buffer
  123. * \ref _USARTCB_RXBUFF are directly "mapped" on to the generic
  124. * \ref _CIRCBUFF. However, all variables, which are modified
  125. * in the related receive or transmit interrupt are marked
  126. * volatile here, but are non-volatile in the generic buffer.
  127. * This seems to work, because all accesses to generic buffer
  128. * are done exclusively. Nevertheless, this is a miserable
  129. * hack and may fail with some compilers. We need to further
  130. * evaluate, if volatile is required or helpful at all or
  131. * whether it may be replaced by memory barriers. Furthermore,
  132. * instead of casting buffer structure types, we may use
  133. * unions for the volatile parts. But it is not yet known,
  134. * how compilers will handle unions of volatile and non-
  135. * volatile structure members.
  136. */
  137. struct _USARTCB_TXBUFF {
  138. uint8_t *txb_buf;
  139. cb_size_t txb_siz;
  140. volatile cb_size_t txb_rdi;
  141. cb_size_t txb_wri;
  142. HANDLE txb_que;
  143. volatile cb_size_t txb_cnt;
  144. };
  145. /*!
  146. * \brief USART device driver control structure type.
  147. *
  148. * See \ref USARTCB_DCB.
  149. */
  150. typedef struct _USARTCB_DCB USARTCB_DCB;
  151. /*!
  152. * \brief USART device driver control structure.
  153. */
  154. struct _USARTCB_DCB {
  155. /*! \brief Hardware interface specific information.
  156. *
  157. * In most cases this will simply contain the base address of the
  158. * hardware interface. However, complex drivers may use this entry
  159. * as a pointer to a private structure.
  160. */
  161. uintptr_t usart_hwif;
  162. /*! \brief Enable the low level driver.
  163. *
  164. * Called by the upper level driver during device registration.
  165. * The low level driver may do all required hardware initialization
  166. * but must not enable any receive or transmit interrupt, because
  167. * no buffers are available at this stage.
  168. */
  169. int (*usart_enable) (USARTCB_DCB *);
  170. /*! \brief Disable the low level driver.
  171. *
  172. * Once called, the low level driver must stop all activities
  173. * until it will be enabled again via _USARTCB_DCB::usart_enable.
  174. */
  175. int (*usart_disable) (USARTCB_DCB *);
  176. /*! \brief Perform USART hardware control functions.
  177. *
  178. * See \ref UsartCbIoCtrl.
  179. */
  180. int (*usart_control) (USARTCB_DCB *, int, void *);
  181. /*! \brief Mode flags.
  182. *
  183. * The flags indicate the mode the driver is currently running in.
  184. */
  185. uint32_t usart_mode;
  186. /*! \brief Capability flags.
  187. *
  188. * The flags indicate the driver's capabilities.
  189. *
  190. * \todo The capability flags are not yet used, but look useful.
  191. */
  192. uint32_t usart_caps;
  193. /*! \brief Set and query the current status. */
  194. uint32_t (*usart_status) (USARTCB_DCB *, uint32_t);
  195. /*! \brief Transmit buffer. */
  196. USARTCB_TXBUFF usart_tx_buff;
  197. /*! \brief Write timeout. */
  198. uint32_t usart_wr_tmo;
  199. /*! \brief Enable transmitter. */
  200. void (*usart_tx_start) (USARTCB_DCB *);
  201. /*! \brief Disable transmitter. */
  202. void (*usart_tx_stop) (USARTCB_DCB *);
  203. /*! \brief Receive buffer. */
  204. USARTCB_RXBUFF usart_rx_buff;
  205. /*! \brief Read timeout. */
  206. uint32_t usart_rd_tmo;
  207. /*! \brief Flag if last character was a carriage return. */
  208. uint_fast8_t usart_rx_cr;
  209. /*! \brief Receiver low watermark. */
  210. cb_size_t usart_rx_lowm;
  211. /*! \brief Receiver high watermark. */
  212. cb_size_t usart_rx_hiwm;
  213. /*! \brief Enable receiver. */
  214. void (*usart_rx_start) (USARTCB_DCB *);
  215. /*! \brief Disable receiver. */
  216. void (*usart_rx_stop) (USARTCB_DCB *);
  217. };
  218. /*!
  219. * \brief Initialize the USART device.
  220. * \internal
  221. *
  222. * This function will call the low level driver's _USARTCB_DCB::usart_enable
  223. * routine to initialize the hardware. Typically, these low level drivers
  224. * use a pointer to this function for their _NUTDEVICE::dev_init entry.
  225. *
  226. * \param dev Identifies the device to initialize. The validity is checked
  227. * only if NUTDEBUG_USE_ASSERT has been defined.
  228. *
  229. * \return 0 on success, -1 otherwise.
  230. *
  231. * \todo Set initial speed. However, the current implementation does
  232. * not allow to configure or disable USART_INITSPEED. Many
  233. * applications require individual baud rates at each device
  234. * and setting USART_INITSPEED here would result in wasted
  235. * code space.
  236. */
  237. extern int UsartCbInit(NUTDEVICE *dev);
  238. /*!
  239. * \brief Perform USART driver control functions.
  240. * \internal
  241. *
  242. * This function is called by the _ioctl() function of the C runtime
  243. * library.
  244. *
  245. * \param dev Identifies the device that receives the device-control
  246. * function.
  247. * \param req Requested control function, of which the following are
  248. * processed by the hardware independent part:
  249. * - \ref UART_SETREADTIMEOUT
  250. * - \ref UART_GETREADTIMEOUT
  251. * - \ref UART_SETWRITETIMEOUT
  252. * - \ref UART_GETWRITETIMEOUT
  253. * - \ref UART_SETCOOKEDMODE
  254. * - \ref UART_GETCOOKEDMODE
  255. * - \ref UART_SETRXBUFSIZ
  256. * - \ref UART_GETRXBUFSIZ
  257. * - \ref UART_SETTXBUFSIZ
  258. * - \ref UART_GETTXBUFSIZ
  259. * - \ref UART_SETRXBUFLWMARK
  260. * - \ref UART_GETRXBUFLWMARK
  261. * - \ref UART_SETRXBUFHWMARK
  262. * - \ref UART_GETRXBUFHWMARK
  263. * Any other function is passed to the ioctl function of the
  264. * low level driver.
  265. * \param conf Points to a buffer that contains any data required for
  266. * the given control function or receives data from that
  267. * function.
  268. *
  269. * \return 0 on success, -1 otherwise.
  270. *
  271. * \todo Right now this upper layer function is called first, passing
  272. * all unhandled functions to the lower level driver. It seems
  273. * to make more sense to do it the other way round, calling the
  274. * lower level first.
  275. *
  276. * \note Not all control functions may be supported on all platforms.
  277. * In any case applications should check the returned result.
  278. */
  279. extern int UsartCbIoCtrl(NUTDEVICE *dev, int req, void *conf);
  280. /*!
  281. * \brief Open a USART device.
  282. * \internal
  283. *
  284. * This function will initialize the circular buffer and then call the
  285. * low level driver's _USARTCB_DCB::usart_rx_start routine to enable
  286. * the interrupt driven receiver. Typically, these low level drivers
  287. * use a pointer to this function for their _NUTDEVICE::dev_open entry.
  288. *
  289. * Note, that by default the caller has to take care, that all parameters
  290. * passed to this function are valid. For debugging purposes
  291. * NUTDEBUG_USE_ASSERT may be defined to enable internal sanity checks.
  292. *
  293. * \param dev Pointer to the NUTDEVICE structure.
  294. * \param name Ignored, should point to an empty string.
  295. * \param mode Operation mode. Any of the following values may be or-ed:
  296. * - \ref _O_BINARY
  297. * - \ref _O_RDONLY
  298. * - \ref _O_WRONLY
  299. * \param acc Ignored, should be zero.
  300. *
  301. * \return Pointer to a NUTFILE structure if successful or NUTFILE_EOF
  302. * otherwise.
  303. *
  304. * \todo This function ignores _O_RDONLY and _O_WRONLY and always creates
  305. * receive and transmit buffers.
  306. * \todo The initial buffer sizes are currently set to 255, but should
  307. * be configurable. However, the current implementation does not
  308. * allow to configure or disable USART_TXBUFSIZ and USART_RXBUFSIZ.
  309. * \todo The initial watermarks are currently set to 1/4 and 3/4 of the
  310. * buffer size, but should be configurable. However, the current
  311. * implementation does not allow to configure or disable
  312. * USART_RXLOWMARK and USART_RXHIWMARK. The hard coded values do
  313. * not consider USART_RXBUFSIZ.
  314. */
  315. extern NUTFILE *UsartCbOpen(NUTDEVICE *dev, const char *name, int mode, int acc);
  316. /*!
  317. * \brief Close a USART device.
  318. * \internal
  319. *
  320. * If the transmit buffer contains any data, then the low level transmitter
  321. * will be enabled by calling the _USARTCB_DCB::usart_tx_start routine
  322. * and the function blocks the calling thread until either all data has
  323. * been transmitted or a write timeout occurred, whichever comes first.
  324. *
  325. * Then the low level functions _USARTCB_DCB::usart_rx_stop and
  326. * _USARTCB_DCB::usart_tx_stop are called to disable all interrupts
  327. * and the related circular buffers are released as well.
  328. *
  329. * Typical low level UART drivers contain a pointer to this function
  330. * in their _NUTDEVICE::dev_close entry.
  331. *
  332. * \param nfp Pointer to a _NUTFILE structure, obtained by a previous call
  333. * to UsartCbOpen(). The validity is checked only if
  334. * NUTDEBUG_USE_ASSERT has been defined.
  335. *
  336. * \return 0 if all buffered data had been transmitted or -1 otherwise.
  337. *
  338. * \todo We may support shared open and use dev_irq as an open counter.
  339. */
  340. extern int UsartCbClose(NUTFILE *fp);
  341. /*!
  342. * \brief Read from device.
  343. * \internal
  344. *
  345. * The function blocks the calling thread until at least one character
  346. * is available in the receive buffer or until a read timeout occurs,
  347. * whichever comes first.
  348. *
  349. * Typical low level UART drivers contain a pointer to this function
  350. * in their _NUTDEVICE::dev_read entry.
  351. *
  352. * Note, that by default the caller has to take care, that all parameters
  353. * passed to this function are valid. For debugging purposes
  354. * NUTDEBUG_USE_ASSERT may be defined to enable internal sanity checks.
  355. *
  356. * \param nfp Pointer to a \ref _NUTFILE structure, obtained by a
  357. * previous call to UsartCbOpen().
  358. * \param buffer Pointer to the buffer that receives the data. If NULL,
  359. * then all characters in the input buffer will be
  360. * discarded.
  361. * \param size Maximum number of bytes to read. If the buffer pointer
  362. * is NULL, then this should be 0.
  363. *
  364. * \return The number of bytes read, which may be less than the number
  365. * of bytes specified. A return value of -1 indicates an error,
  366. * while zero is returned in case of a timeout.
  367. */
  368. extern int UsartCbRead(NUTFILE *fp, void *buffer, int size);
  369. /*!
  370. * \brief Write to device.
  371. * \internal
  372. *
  373. * Depending on the available transmit buffer space, the calling function
  374. * may or may not be blocked. In any case, the low level transmitter will
  375. * be started by calling _USARTCB_DCB::usart_tx_start.
  376. *
  377. * Typical low level UART drivers contain a pointer to this function
  378. * in their _NUTDEVICE::dev_write entry.
  379. *
  380. * Note, that by default the caller has to take care, that all parameters
  381. * passed to this function are valid. For debugging purposes
  382. * NUTDEBUG_USE_ASSERT may be defined to enable internal sanity checks.
  383. *
  384. * \param nfp Pointer to a \ref _NUTFILE structure, obtained by a
  385. * previous call to UsartCbOpen().
  386. * \param buffer Pointer to the data to be written. If NULL, then the
  387. * the function blocks the calling thread until all
  388. * characters in the transmit buffer had been sent out or
  389. * until a write timeout occurs, whichever comes first.
  390. * \param len Number of bytes to write. If the buffer pointer
  391. * is NULL, then this should be 0.
  392. *
  393. * \return The number of bytes written, which, in case of a write timeout
  394. * may be less than the number of bytes requested by the caller.
  395. * A return value of -1 indicates an error.
  396. */
  397. extern int UsartCbWrite(NUTFILE *fp, const void *buffer, int len);
  398. #ifdef __HARVARD_ARCH__
  399. extern int UsartCbWrite_P(NUTFILE *nfp, PGM_P buffer, int len);
  400. #endif
  401. /*!
  402. * \brief Retrieves the number of characters in input buffer.
  403. * \internal
  404. *
  405. * This function allows to query the number of bytes in the input buffer
  406. * by using standard C function for querying the size of an open file.
  407. *
  408. * \param nfp Pointer to a _NUTFILE structure, obtained by a previous call
  409. * to UsartCbOpen(). The validity is checked only if
  410. * NUTDEBUG_USE_ASSERT has been defined.
  411. *
  412. * \return The number of bytes currently available in the receive buffer.
  413. */
  414. extern long UsartCbSize (NUTFILE *fp);
  415. /*@}*/
  416. #endif