usart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. #ifndef _DEV_USART_H_
  2. #define _DEV_USART_H_
  3. /*
  4. * Copyright (C) 2001-2003 by egnite Software GmbH. 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. * $Log$
  36. * Revision 1.7 2008/08/11 06:59:59 haraldkipp
  37. * BSD types replaced by stdint types (feature request #1282721).
  38. *
  39. * Revision 1.6 2004/11/12 11:55:39 freckle
  40. * marked rbf_blockcnt and rbf_blockptr as volatile
  41. *
  42. * Revision 1.5 2004/11/12 11:25:43 freckle
  43. * added rbf_blockcnt and rbf_blockptr to _RINGBUF (if UART_BLOCKING_READ is
  44. * defined). added USART_MF_BLOCKREAD mode define
  45. *
  46. * Revision 1.4 2004/11/08 18:14:09 haraldkipp
  47. * Marked RINGBUF members volatile, which are modified within
  48. * interrupt routines.
  49. *
  50. * Revision 1.3 2004/05/24 20:19:49 drsung
  51. * Added function UsartSize to return number of chars in input buffer.
  52. *
  53. * Revision 1.2 2004/03/16 16:48:28 haraldkipp
  54. * Added Jan Dubiec's H8/300 port.
  55. *
  56. * Revision 1.1 2003/12/18 09:33:58 haraldkipp
  57. * First check in
  58. *
  59. */
  60. #include <cfg/crt.h>
  61. #include <dev/uart.h>
  62. /*!
  63. * \file dev/usart.h
  64. * \brief Synchronous/asynchronous serial device definitions.
  65. */
  66. /*!
  67. * \addtogroup xgUsart
  68. */
  69. /*@{*/
  70. /*!
  71. * \name Ring Buffer
  72. */
  73. /*@{*/
  74. /*! \brief Initial receive buffer size.
  75. * \showinitializer
  76. */
  77. #ifndef USART_RXBUFSIZ
  78. #define USART_RXBUFSIZ 256
  79. #endif
  80. /*! \brief Receiver's initial high water mark.
  81. *
  82. * Disables receiver handshake.
  83. * \showinitializer
  84. */
  85. #ifndef USART_RXHIWMARK
  86. #define USART_RXHIWMARK 240
  87. #endif
  88. /*! \brief Receiver's initial low water mark.
  89. *
  90. * Enables receiver handshake.
  91. * \showinitializer
  92. */
  93. #ifndef USART_RXLOWMARK
  94. #define USART_RXLOWMARK 208
  95. #endif
  96. /*! \brief Initial transmit buffer size.
  97. * \showinitializer
  98. */
  99. #ifndef USART_TXBUFSIZ
  100. #define USART_TXBUFSIZ 64
  101. #endif
  102. /*! \brief Transmitter's initial high water mark.
  103. *
  104. * Starts the transmitter.
  105. * \showinitializer
  106. */
  107. #ifndef USART_TXHIWMARK
  108. #define USART_TXHIWMARK 56
  109. #endif
  110. /*! \brief Transmitter's initial low water mark.
  111. *
  112. * Wakes up transmitting threads.
  113. * \showinitializer
  114. */
  115. #ifndef USART_TXLOWMARK
  116. #define USART_TXLOWMARK 40
  117. #endif
  118. #ifndef ENABLE
  119. #define ENABLE 1
  120. #endif
  121. #ifndef DISABLE
  122. #define DISABLE 0
  123. #endif
  124. /*!
  125. * \typedef RINGBUF
  126. * \brief Character device ring buffer type.
  127. */
  128. typedef struct _RINGBUF RINGBUF;
  129. /*!
  130. * \struct _RINGBUF usart.h dev/usart.h
  131. * \brief Character device ring buffer structure.
  132. */
  133. struct _RINGBUF {
  134. /*! \brief Buffer head pointer.
  135. *
  136. * Changed by the receiver interrupt.
  137. */
  138. uint8_t * volatile rbf_head;
  139. /*! \brief Buffer tail pointer.
  140. *
  141. * Changed by the transmitter interrupt.
  142. */
  143. uint8_t * volatile rbf_tail;
  144. /*! \brief First buffer address.
  145. */
  146. uint8_t *rbf_start;
  147. /*! \brief Last buffer address.
  148. */
  149. uint8_t *rbf_last;
  150. /*! \brief Total buffer size.
  151. *
  152. * Zero, if no buffer available.
  153. */
  154. size_t rbf_siz;
  155. /*! \brief Number of bytes in the buffer.
  156. *
  157. * Changed by receiver and transmitter interrupts.
  158. */
  159. volatile size_t rbf_cnt;
  160. /*! \brief Buffer low watermark.
  161. *
  162. * If the number of bytes in the buffer reaches this value, then
  163. * the previously disabled buffer input is enabled again.
  164. */
  165. size_t rbf_lwm;
  166. /*! \brief Buffer high watermark.
  167. *
  168. * If the number of bytes in the buffer reaches this value, then
  169. * buffer input is disabled.
  170. */
  171. size_t rbf_hwm;
  172. /*! \brief Queue of waiting threads.
  173. *
  174. * Consuming threads are added to this queue when the buffer is empty.
  175. * Producing threads are added to this queue when the buffer is full.
  176. */
  177. HANDLE rbf_que;
  178. /*! \brief Number of bytes for block-read
  179. *
  180. * If this is zero, incoming bytes are stored in ringbuffer
  181. * If this not zero, incoming bytes are stored in rbf_blockptr
  182. * Changed by the receiver interrupt.
  183. */
  184. size_t volatile rbf_blockcnt;
  185. /*! \brief Address for block-read
  186. *
  187. * If bf_blockbytes is not zero, incoming bytes are stored here
  188. * Changed by the receiver interrupt.
  189. */
  190. uint8_t* volatile rbf_blockptr;
  191. /*!
  192. * \brief Drivers / Ringbuffers wait queue list. Needed for select
  193. */
  194. WQLIST *wq_list;
  195. };
  196. /*@}*/
  197. /*!
  198. * \name Initial UART Configuration
  199. */
  200. /*@{*/
  201. /*! \brief Initial bit rate.
  202. *
  203. * \showinitializer
  204. */
  205. #define USART_INITSPEED 115200
  206. /*@}*/
  207. #define USART_MF_RTSCONTROL 0x0001 /*!< DTE output. */
  208. #define USART_MF_CTSSENSE 0x0002 /*!< DTE input. */
  209. #define USART_MF_DTRCONTROL 0x0004 /*!< DTE output. */
  210. #define USART_MF_DSRSENSE 0x0008 /*!< DTE input. */
  211. #define USART_MF_DCDSENSE 0x0010 /*!< DTE input. */
  212. #define USART_MF_SENSEMASK 0x001A /*!< Handshake sense mask. */
  213. #define USART_MF_CONTROLMASK 0x0005 /*!< Handshake control mask. */
  214. /*! \brief Software handshake.
  215. *
  216. * It is recommended to set a proper read timeout with software handshake.
  217. * In this case a timeout may occur, if the communication peer lost our
  218. * last XON character. The application may then use ioctl() to disable the
  219. * receiver and do the read again. This will send out another XON.
  220. */
  221. #define USART_MF_XONXOFF 0x0020
  222. /*! \brief Echo configuration
  223. *
  224. * For RS232 mode it defines if any received character is
  225. * echoed back to the sender. For 485 mode it defines
  226. * if on switch to transmitting mode the receiver is left
  227. * enabled.
  228. */
  229. #define USART_MF_LOCALECHO 0x0040 /*!< Should be used in stream, not device. */
  230. #define USART_MF_COOKEDMODE 0x0080 /*!< Should be used in stream, not device. */
  231. #define USART_MF_NOBUFFER 0x0100 /*!< No buffering. */
  232. #define USART_MF_LINEBUFFER 0x0200 /*!< Line buffered. */
  233. #define USART_MF_BUFFERMASK 0x0300 /*!< Masks buffering mode flags. */
  234. #define USART_MF_HALFDUPLEX 0x0400 /*!< Half duplex control. */
  235. #define USART_MF_BLOCKREAD 0x0800 /*!< Block read mode enabled */
  236. #define USART_MF_BLOCKWRITE 0x1000 /*!< Block write mode enabled */
  237. #define USART_MF_OWIHALFDUPLEX 0x2000 /*!< OWI half duplex control. */
  238. #define USART_MF_FLOWMASK (USART_MF_XONXOFF| USART_MF_HALFDUPLEX|USART_MF_LOCALECHO|USART_MF_BLOCKREAD|USART_MF_BLOCKWRITE| USART_MF_OWIHALFDUPLEX)
  239. #define USART_SF_RTSOFF 0x0001 /*!< Set if RTS line is off. */
  240. #define USART_SF_CTSOFF 0x0002 /*!< Set if CTS line is off. */
  241. #define USART_SF_DTROFF 0x0004 /*!< Set if DTR line is off. */
  242. #define USART_SF_DSROFF 0x0008 /*!< Set if DSR line is off. */
  243. #define USART_SF_DCDOFF 0x0010 /*!< Set if DCD line is off. */
  244. #define USART_SF_TXDISABLED 0x0040 /*!< Transmitter disabled. */
  245. #define USART_SF_RXDISABLED 0x0080 /*!< Receiver disabled. */
  246. /*!
  247. * \struct _USARTDCB usart.h dev/usart.h
  248. * \brief USART device low level information structure.
  249. *
  250. */
  251. struct _USARTDCB {
  252. /*! \brief Mode flags.
  253. */
  254. uint32_t dcb_modeflags;
  255. /*! \brief Status flags.
  256. */
  257. uint32_t dcb_statusflags;
  258. /*! \brief Read timeout.
  259. */
  260. uint32_t dcb_rtimeout;
  261. /*! \brief Write timeout.
  262. */
  263. uint32_t dcb_wtimeout;
  264. /*! \brief Output ring buffer.
  265. */
  266. RINGBUF dcb_tx_rbf;
  267. /*! \brief Input ring buffer.
  268. */
  269. RINGBUF dcb_rx_rbf;
  270. /*! \brief Last EOL character.
  271. */
  272. uint8_t dcb_last_eol;
  273. /*!
  274. * \fn dcb_init
  275. * \brief Driver control initialization.
  276. */
  277. int (*dcb_init) (void);
  278. /*!
  279. * \brief Driver control de-initialization.
  280. */
  281. int (*dcb_deinit) (void);
  282. /*!
  283. * \brief Driver control write notification.
  284. */
  285. void (*dcb_tx_start) (void);
  286. /*!
  287. * \brief Driver control read notification.
  288. */
  289. void (*dcb_rx_start) (void);
  290. /*!
  291. * \brief Set handshake mode.
  292. */
  293. int (*dcb_set_flow_control) (uint32_t flags);
  294. /*!
  295. * \brief Get handshake mode.
  296. */
  297. uint32_t(*dcb_get_flow_control) (void);
  298. /*!
  299. * \brief Set hardware speed.
  300. */
  301. int (*dcb_set_speed) (uint32_t rate);
  302. /*!
  303. * \brief Get hardware speed.
  304. */
  305. uint32_t(*dcb_get_speed) (void);
  306. /*!
  307. * \brief Set hardware data bits.
  308. */
  309. int (*dcb_set_data_bits) (uint8_t bits);
  310. /*!
  311. * \brief Get hardware data bits.
  312. */
  313. uint8_t(*dcb_get_data_bits) (void);
  314. /*!
  315. * \brief Set hardware parity mode.
  316. */
  317. int (*dcb_set_parity) (uint8_t bits);
  318. /*!
  319. * \brief Get hardware parity mode.
  320. */
  321. uint8_t(*dcb_get_parity) (void);
  322. /*!
  323. * \brief Set hardware stop bits.
  324. */
  325. int (*dcb_set_stop_bits) (uint8_t bits);
  326. /*!
  327. * \brief Get hardware stop bits.
  328. */
  329. uint8_t(*dcb_get_stop_bits) (void);
  330. /*!
  331. * \brief Set hardware status.
  332. */
  333. int (*dcb_set_status) (uint32_t flags);
  334. /*!
  335. * \brief Get hardware status.
  336. */
  337. uint32_t(*dcb_get_status) (void);
  338. /*!
  339. * \brief Set clock mode.
  340. */
  341. int (*dcb_set_clock_mode) (uint8_t mode);
  342. /*!
  343. * \brief Get clock mode.
  344. */
  345. uint8_t(*dcb_get_clock_mode) (void);
  346. };
  347. /*!
  348. * USART device low level information type.
  349. */
  350. typedef struct _USARTDCB USARTDCB;
  351. /*@}*/
  352. extern int UsartInit(NUTDEVICE * dev);
  353. extern int UsartIOCtl(NUTDEVICE * dev, int req, void *conf);
  354. extern int UsartRead(NUTFILE * fp, void *buffer, int size);
  355. extern int UsartWrite(NUTFILE * fp, const void *buffer, int len);
  356. #ifdef __HARVARD_ARCH__
  357. extern int UsartWrite_P(NUTFILE * fp, PGM_P buffer, int len);
  358. #endif
  359. extern NUTFILE *UsartOpen(NUTDEVICE * dev, const char *name, int mode, int acc);
  360. extern int UsartClose(NUTFILE * fp);
  361. extern long UsartSize (NUTFILE *fp);
  362. #ifndef CRT_DISABLE_SELECT_POLL
  363. extern int UsartSelect (NUTFILE *fp, int flags, HANDLE *wq, select_cmd_t cmd);
  364. #else
  365. #define UsartSelect NULL
  366. #endif
  367. #endif