usart.h 10 KB

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