uart.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. #ifndef _DEV_UART_H
  2. #define _DEV_UART_H
  3. /*
  4. * Copyright (C) 2001-2004 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. /*
  36. * $Log: uart.h,v $
  37. * Revision 1.4 2005/06/26 12:40:59 chaac
  38. * Added support for raw mode to AHDLC driver.
  39. *
  40. * Revision 1.3 2004/11/12 11:14:32 freckle
  41. * added UART_GETBLOCKREAD & UART_SETBLOCKREAD defines
  42. *
  43. * Revision 1.2 2004/05/24 20:19:08 drsung
  44. * Added function UartAvrSize to return number of chars in input buffer.
  45. *
  46. * Revision 1.1 2004/03/16 16:48:28 haraldkipp
  47. * Added Jan Dubiec's H8/300 port.
  48. *
  49. * Revision 1.2 2003/12/15 19:29:18 haraldkipp
  50. * USART driver support added
  51. *
  52. * Revision 1.1.1.1 2003/05/09 14:41:23 haraldkipp
  53. * Initial using 3.2.1
  54. *
  55. * Revision 1.11 2003/05/06 17:58:28 harald
  56. * Handshakes added
  57. *
  58. * Revision 1.10 2003/03/31 14:53:25 harald
  59. * Prepare release 3.1
  60. *
  61. * Revision 1.9 2003/02/04 18:00:54 harald
  62. * Version 3 released
  63. *
  64. * Revision 1.8 2003/01/14 16:35:24 harald
  65. * Definitions moved
  66. *
  67. * Revision 1.7 2002/11/02 15:17:01 harald
  68. * Library dependencies moved to compiler.h
  69. *
  70. * Revision 1.6 2002/06/26 17:29:30 harald
  71. * First pre-release with 2.4 stack
  72. *
  73. */
  74. #include <sys/device.h>
  75. /*!
  76. * \file dev/uart.h
  77. * \brief UART I/O function prototypes.
  78. */
  79. /*!
  80. * \addtogroup xgUARTIOCTL
  81. *
  82. * \brief UART _ioctl() commands.
  83. *
  84. * These commands are used to control and retrieve hardware specific
  85. * configurations. The definitions are kept independent from the
  86. * underlying hardware, but not all commands may be fully implemented
  87. * in each UART driver.
  88. *
  89. * The _ioctl() function expects three parameters:
  90. * - A device descriptor.
  91. * - A command code, any of the UART_... commands listed below.
  92. * - A pointer to a configuration parameter, in most cases an unsigned long.
  93. */
  94. /*@{*/
  95. /*! \brief UART _ioctl() command code to set the line speed.
  96. *
  97. * The configuration parameter specifies the input and output bit rate
  98. * per second.
  99. */
  100. #define UART_SETSPEED 0x0101
  101. /*! \brief UART _ioctl() command code to query the line speed.
  102. *
  103. * The configuration parameter is set to the input and output bit rate
  104. * per second.
  105. */
  106. #define UART_GETSPEED 0x0102
  107. /*! \brief UART _ioctl() command code to set the number of data bits.
  108. *
  109. * The configuration parameter specifies the number of data bits, 5, 6,
  110. * 7, 8 or 9.
  111. */
  112. #define UART_SETDATABITS 0x0103
  113. /*! \brief UART _ioctl() command code to query the number of data bits.
  114. *
  115. * The configuration parameter is set to the number of data bits, 5, 6,
  116. * 7, 8 or 9.
  117. */
  118. #define UART_GETDATABITS 0x0104
  119. /*! \brief UART _ioctl() command code to set the parity mode.
  120. *
  121. * The configuration parameter specifies the type of the parity bit,
  122. * 0 (none), 1 (odd) or 2 (even).
  123. */
  124. #define UART_SETPARITY 0x0105
  125. /*! \brief UART _ioctl() command code to query the parity mode.
  126. *
  127. * The configuration parameter is set to the type of the parity bit,
  128. * 0 (none), 1 (odd) or 2 (even).
  129. */
  130. #define UART_GETPARITY 0x0106
  131. /*! \brief UART _ioctl() command code to set the number of stop bits.
  132. *
  133. * The configuration parameter specifies the number of stop bits, 1 or 2.
  134. */
  135. #define UART_SETSTOPBITS 0x0107
  136. /*! \brief UART _ioctl() command code to query the number of stop bits.
  137. *
  138. * The configuration parameter is set to the number of stop bits, 1 or 2.
  139. */
  140. #define UART_GETSTOPBITS 0x0108
  141. /*! \brief UART _ioctl() command code to set the status.
  142. *
  143. * The configuration parameter specifies the status to set.
  144. */
  145. #define UART_SETSTATUS 0x0109
  146. /*! \brief UART _ioctl() command code to query the status.
  147. *
  148. * The configuration parameter is set to the current status.
  149. */
  150. #define UART_GETSTATUS 0x010a
  151. /*! \brief UART _ioctl() command code to set the read timeout.
  152. *
  153. * The configuration parameter specifies the read timeout in
  154. * milliseconds.
  155. */
  156. #define UART_SETREADTIMEOUT 0x010b
  157. /*! \brief UART _ioctl() command code to query the read timeout.
  158. *
  159. * The configuration parameter is set to the read timeout in
  160. * milliseconds.
  161. */
  162. #define UART_GETREADTIMEOUT 0x010c
  163. /*! \brief UART _ioctl() command code to set the write timeout.
  164. *
  165. * The configuration parameter specifies the write timeout in
  166. * milliseconds.
  167. */
  168. #define UART_SETWRITETIMEOUT 0x010d
  169. /*! \brief UART _ioctl() command code to query the write timeout.
  170. *
  171. * The configuration parameter is set to the write timeout in
  172. * milliseconds.
  173. */
  174. #define UART_GETWRITETIMEOUT 0x010e
  175. /*! \brief UART _ioctl() command code to set the local echo mode.
  176. *
  177. * The configuration parameter specifies the local echo mode,
  178. * 0 (off) or 1 (on).
  179. */
  180. #define UART_SETLOCALECHO 0x010f
  181. /*! \brief UART _ioctl() command code to query the local echo mode.
  182. *
  183. * The configuration parameter is set to the local echo mode,
  184. * 0 (off) or 1 (on).
  185. */
  186. #define UART_GETLOCALECHO 0x0110
  187. /*! \brief UART _ioctl() command code to set the flow control mode.
  188. *
  189. * The configuration parameter specifies the flow control mode.
  190. */
  191. #define UART_SETFLOWCONTROL 0x0111
  192. /*! \brief UART _ioctl() command code to query the flow control mode.
  193. *
  194. * The configuration parameter is set to the flow control mode.
  195. */
  196. #define UART_GETFLOWCONTROL 0x0112
  197. /*! \brief UART _ioctl() command code to set the cooking mode.
  198. *
  199. * The configuration parameter specifies the character cooking mode,
  200. * 0 (raw) or 1 (EOL translation).
  201. */
  202. #define UART_SETCOOKEDMODE 0x0113
  203. /*! \brief UART _ioctl() command code to query the cooking mode.
  204. *
  205. * The configuration parameter is set to the character cooking mode,
  206. * 0 (raw) or 1 (EOL translation).
  207. */
  208. #define UART_GETCOOKEDMODE 0x0114
  209. /*! \brief UART _ioctl() command code to set the buffering mode.
  210. *
  211. * The configuration parameter specifies the buffering mode.
  212. */
  213. #define UART_SETBUFFERMODE 0x0115
  214. /*! \brief UART _ioctl() command code to query the buffering mode.
  215. *
  216. * The configuration parameter is set to the buffering mode.
  217. */
  218. #define UART_GETBUFFERMODE 0x0116
  219. /*! \brief UART _ioctl() command code to set the network interface mode.
  220. *
  221. * The configuration parameter specifies the network interface mode.
  222. */
  223. #define HDLC_SETIFNET 0x0117
  224. /*! \brief UART _ioctl() command code to query the network interface mode.
  225. *
  226. * The configuration parameter is set to the network interface mode.
  227. */
  228. #define HDLC_GETIFNET 0x0118
  229. /*! \brief UART _ioctl() command code to set the clock mode.
  230. *
  231. * The configuration parameter specifies the clock mode.
  232. */
  233. #define UART_SETCLOCKMODE 0x0119
  234. /*! \brief UART _ioctl() command code to query the clock mode.
  235. *
  236. * The configuration parameter is set to the clock mode.
  237. */
  238. #define UART_GETCLOCKMODE 0x011a
  239. /*! \brief UART _ioctl() command code to set the transmit buffer size.
  240. *
  241. * The configuration parameter specifies the number of bytes.
  242. */
  243. #define UART_SETTXBUFSIZ 0x011b
  244. /*! \brief UART _ioctl() command code to query the transmit buffer size.
  245. *
  246. * The configuration parameter specifies the number of bytes.
  247. */
  248. #define UART_GETTXBUFSIZ 0x011c
  249. /*! \brief UART _ioctl() command code to set the receive buffer size.
  250. *
  251. * The configuration parameter specifies the number of bytes.
  252. */
  253. #define UART_SETRXBUFSIZ 0x011d
  254. /*! \brief UART _ioctl() command code to query the receive buffer size.
  255. *
  256. * The configuration parameter specifies the number of bytes.
  257. */
  258. #define UART_GETRXBUFSIZ 0x011e
  259. /*! \brief UART _ioctl() command code to set the transmit buffer low watermark.
  260. *
  261. * The configuration parameter specifies the number of bytes.
  262. */
  263. #define UART_SETTXBUFLWMARK 0x0120
  264. /*! \brief UART _ioctl() command code to query the transmit buffer low watermark.
  265. *
  266. * The configuration parameter specifies the number of bytes.
  267. */
  268. #define UART_GETTXBUFLWMARK 0x0121
  269. /*! \brief UART _ioctl() command code to set the transmit buffer high watermark.
  270. *
  271. * The configuration parameter specifies the number of bytes.
  272. */
  273. #define UART_SETTXBUFHWMARK 0x0122
  274. /*! \brief UART _ioctl() command code to query the transmit buffer high watermark.
  275. *
  276. * The configuration parameter specifies the number of bytes.
  277. */
  278. #define UART_GETTXBUFHWMARK 0x0123
  279. /*! \brief UART _ioctl() command code to set the receive buffer low watermark.
  280. *
  281. * The configuration parameter specifies the number of bytes.
  282. */
  283. #define UART_SETRXBUFLWMARK 0x0124
  284. /*! \brief UART _ioctl() command code to query the receive buffer low watermark.
  285. *
  286. * The configuration parameter specifies the number of bytes.
  287. */
  288. #define UART_GETRXBUFLWMARK 0x0125
  289. /*! \brief UART _ioctl() command code to set the receive buffer high watermark.
  290. *
  291. * The configuration parameter specifies the number of bytes.
  292. */
  293. #define UART_SETRXBUFHWMARK 0x0126
  294. /*! \brief UART _ioctl() command code to query the receive buffer high watermark.
  295. *
  296. * The configuration parameter specifies the number of bytes.
  297. */
  298. #define UART_GETRXBUFHWMARK 0x0127
  299. /*! \brief UART _ioctl() command code to set the block read mode
  300. *
  301. * The configuration parameter specifies the block read mode.
  302. */
  303. #define UART_SETBLOCKREAD 0x0128
  304. /*! \brief UART _ioctl() command code to query the receive buffer high watermark.
  305. *
  306. * The configuration parameter specifies the block read mode.
  307. */
  308. #define UART_GETBLOCKREAD 0x0129
  309. /*! \brief UART _ioctl() command code to set physical device to the raw mode.
  310. *
  311. * The configuration parameter specifies the raw mode for device. In raw mode
  312. * data encapsulation is not allowed to be done. This allows other processing to
  313. * be done on physical device.
  314. */
  315. #define UART_SETRAWMODE 0x012a
  316. /*! \brief UART _ioctl() command code to query the raw mode.
  317. *
  318. * The configuration parameter specified the raw mode.
  319. */
  320. #define UART_GETRAWMODE 0x012b
  321. /*!
  322. * \addtogroup xgUARTStatus
  323. * \brief UART device status flags,
  324. *
  325. * A combination of these status flags is used by the _ioctl() commands
  326. * \ref UART_SETSTATUS and \ref UART_GETSTATUS.
  327. */
  328. /*@{*/
  329. /*! \brief Framing error.
  330. *
  331. * \ref UART_SETSTATUS will clear this error.
  332. */
  333. #define UART_FRAMINGERROR 0x00000001UL
  334. /*! \brief Overrun error.
  335. *
  336. * \ref UART_SETSTATUS will clear this error.
  337. */
  338. #define UART_OVERRUNERROR 0x00000002UL
  339. /*! \brief Parity error.
  340. *
  341. * \ref UART_SETSTATUS will clear this error.
  342. */
  343. #define UART_PARITYERROR 0x00000004UL
  344. /*! \brief UART errors.
  345. *
  346. * \ref UART_SETSTATUS will clear all errors.
  347. */
  348. #define UART_ERRORS (UART_FRAMINGERROR | UART_OVERRUNERROR | UART_PARITYERROR)
  349. /*! \brief Receiver buffer empty.
  350. */
  351. #define UART_RXBUFFEREMPTY 0x00000040UL
  352. /*! \brief Transmitter buffer empty.
  353. *
  354. * \ref UART_SETSTATUS will immediately clear the buffer. It will not
  355. * wait until the remaining characters have been transmitted.
  356. */
  357. #define UART_TXBUFFEREMPTY 0x00000080UL
  358. /*! \brief RTS handshake output enabled.
  359. */
  360. #define UART_RTSENABLED 0x00000100UL
  361. /*! \brief RTS handshake output disabled.
  362. */
  363. #define UART_RTSDISABLED 0x00000200UL
  364. /*! \brief CTS handshake input enabled.
  365. */
  366. #define UART_CTSENABLED 0x00000400UL
  367. /*! \brief CTS handshake input disabled.
  368. */
  369. #define UART_CTSDISABLED 0x00000800UL
  370. /*! \brief DTR handshake output enabled.
  371. */
  372. #define UART_DTRENABLED 0x00001000UL
  373. /*! \brief DTR handshake output disabled.
  374. */
  375. #define UART_DTRDISABLED 0x00002000UL
  376. /*! \brief Receiver enabled.
  377. */
  378. #define UART_RXENABLED 0x00010000UL
  379. /*! \brief Receiver enabled.
  380. */
  381. #define UART_RXDISABLED 0x00020000UL
  382. /*! \brief Transmitter enabled.
  383. */
  384. #define UART_TXENABLED 0x00040000UL
  385. /*! \brief Transmitter enabled.
  386. */
  387. #define UART_TXDISABLED 0x00080000UL
  388. /*! \brief Receive address frames only.
  389. *
  390. * Used in multidrop communication. May only work if 9 databits have
  391. * been configured.
  392. */
  393. #define UART_RXADDRFRAME 0x00100000UL
  394. /*! \brief Receive all frames.
  395. *
  396. * Used in multidrop communication.
  397. */
  398. #define UART_RXNORMFRAME 0x00200000UL
  399. /*! \brief Transmit as address frame.
  400. *
  401. * Used in multidrop communication. May only work if 9 databits have
  402. * been configured.
  403. */
  404. #define UART_TXADDRFRAME 0x00400000UL
  405. /*! \brief Transmit as normal frame.
  406. *
  407. * Used in multidrop communication.
  408. */
  409. #define UART_TXNORMFRAME 0x00800000UL
  410. /*@}*/
  411. /*!
  412. * \addtogroup xgUARTHS
  413. * \brief UART handshake modes.
  414. *
  415. * Any of these values may be used by the _ioctl() commands
  416. * \ref UART_SETFLOWCONTROL and \ref UART_GETFLOWCONTROL.
  417. */
  418. /*@{*/
  419. /*! \brief RTS / CTS hardware handshake.
  420. *
  421. * Nut/OS uses DTE definitions, where RTS is output and CTS is input.
  422. */
  423. #define UART_HS_RTSCTS 0x0003
  424. /*! \brief Full modem hardware handshake.
  425. *
  426. * Not supported yet by the standard drivers.
  427. */
  428. #define UART_HS_MODEM 0x001F
  429. /*! \brief XON / XOFF software handshake.
  430. *
  431. * It is recommended to set a proper read timeout with software handshake.
  432. * In this case a timeout may occur, if the communication peer lost our
  433. * last XON character. The application may then use ioctl() to disable the
  434. * receiver and do the read again. This will send out another XON.
  435. */
  436. #define UART_HS_SOFT 0x0020
  437. /*@}*/
  438. /*!
  439. * \addtogroup xgUARTClock
  440. * \brief UART device clock modes.
  441. *
  442. * Any of these values may be used by the _ioctl() commands
  443. * \ref UART_SETCLOCKMODE and \ref UART_GETCLOCKMODE. Most drivers
  444. * require to set the bit rate after modifying the clock mode. In order
  445. * to avoid unknown clock output frequencies in master mode, set the
  446. * clock mode to \ref UART_SYNCSLAVE first, than use \ref UART_SETSPEED
  447. * to select the bit rate and finally switch to \ref UART_SYNCMASTER or
  448. * \ref UART_NSYNCMASTER.
  449. */
  450. /*@{*/
  451. #define UART_SYNC 0x01
  452. #define UART_MASTER 0x02
  453. #define UART_NCLOCK 0x04
  454. #define UART_HIGHSPEED 0x20
  455. /*! \brief Normal asynchronous mode.
  456. */
  457. #define UART_ASYNC 0x00
  458. /*! \brief Synchronous slave mode.
  459. *
  460. * Transmit data changes on rising edge and receive data is sampled on
  461. * the falling edge of the clock input.
  462. */
  463. #define UART_SYNCSLAVE UART_SYNC
  464. /*! \brief Synchronous master mode.
  465. *
  466. * Transmit data changes on rising edge and receive data is sampled on
  467. * the falling edge of the clock output.
  468. */
  469. #define UART_SYNCMASTER (UART_SYNC | UART_MASTER)
  470. /*! \brief Synchronous slave mode, clock negated.
  471. *
  472. * Similar to \ref UART_SYNCSLAVE, but transmit data changes on falling
  473. * edge and receive data is sampled on the rising edge of the clock input.
  474. */
  475. #define UART_NSYNCSLAVE (UART_SYNC | UART_NCLOCK)
  476. /*! \brief Synchronous master mode, clock negated
  477. *
  478. * Similar to \ref UART_SYNCMASTER, but transmit data changes on falling
  479. * edge and receive data is sampled on the rising edge of the clock output.
  480. */
  481. #define UART_NSYNCMASTER (UART_SYNC | UART_NCLOCK | UART_MASTER)
  482. /*! \brief Asynchronous high speed mode.
  483. *
  484. * More deviation sensitive than normal mode, but supports higher speed.
  485. */
  486. #define UART_ASYNC_HS UART_HIGHSPEED
  487. /*@}*/
  488. /*@}*/
  489. __BEGIN_DECLS
  490. /*
  491. * The following prototypes are misplaced and should be removed
  492. * from here.
  493. *
  494. * A note by Jan Dubiec: I suggest move them to dev/uart[01].c;
  495. * more, those files could be even merged.
  496. *
  497. */
  498. extern int UartAvrInit(NUTDEVICE * dev);
  499. extern int UartAvrIOCtl(NUTDEVICE * dev, int req, void *conf);
  500. extern int UartAvrInput(NUTDEVICE * dev);
  501. extern int UartAvrOutput(NUTDEVICE * dev);
  502. extern int UartAvrFlush(NUTDEVICE * dev);
  503. extern int UartAvrGetRaw(u_char * cp);
  504. extern int UartAvrPutRaw(u_char ch);
  505. extern int UartAvrRead(NUTFILE * fp, void *buffer, int size);
  506. extern int UartAvrWrite(NUTFILE * fp, CONST void *buffer, int len);
  507. #ifdef __HARVARD_ARCH__
  508. extern int UartAvrWrite_P(NUTFILE * fp, PGM_P buffer, int len);
  509. #endif
  510. extern NUTFILE *UartAvrOpen(NUTDEVICE * dev, CONST char *name, int mode, int acc);
  511. extern long UartAvrSize(NUTFILE * fp);
  512. extern int UartAvrClose(NUTFILE * fp);
  513. __END_DECLS
  514. #endif