device.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #ifndef _SYS_DEVICE_H_
  2. #define _SYS_DEVICE_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. /*
  36. * $Log: device.h,v $
  37. * Revision 1.7 2006/03/16 15:25:34 haraldkipp
  38. * Changed human readable strings from u_char to char to stop GCC 4 from
  39. * nagging about signedness.
  40. *
  41. * Revision 1.6 2006/01/05 16:45:34 haraldkipp
  42. * Added a new driver type IFTYP_FS.
  43. *
  44. * Revision 1.5 2005/08/02 17:46:49 haraldkipp
  45. * Major API documentation update.
  46. *
  47. * Revision 1.4 2004/06/07 15:07:00 olereinhardt
  48. * Added IFTYP_CAN
  49. *
  50. * Revision 1.3 2004/03/18 13:49:00 haraldkipp
  51. * Deprecated functions removed.
  52. * IFSTREAM structure taken from ifstream
  53. * header file.
  54. *
  55. * Revision 1.2 2004/03/16 16:48:44 haraldkipp
  56. * Added Jan Dubiec's H8/300 port.
  57. *
  58. * Revision 1.1.1.1 2003/05/09 14:41:19 haraldkipp
  59. * Initial using 3.2.1
  60. *
  61. * Revision 1.18 2003/05/06 17:58:04 harald
  62. * ATmega128 definitions moved to compiler include
  63. *
  64. * Revision 1.17 2003/03/31 14:34:08 harald
  65. * Added character device
  66. *
  67. * Revision 1.16 2003/02/04 18:00:52 harald
  68. * Version 3 released
  69. *
  70. * Revision 1.15 2003/01/14 16:35:04 harald
  71. * Definitions moved
  72. *
  73. * Revision 1.14 2002/11/02 15:17:01 harald
  74. * Library dependencies moved to compiler.h
  75. *
  76. * Revision 1.13 2002/09/15 16:46:28 harald
  77. * *** empty log message ***
  78. *
  79. * Revision 1.12 2002/08/08 17:24:21 harald
  80. * Using time constants by KU
  81. *
  82. * Revision 1.11 2002/07/03 16:45:41 harald
  83. * Using GCC 3.2
  84. *
  85. * Revision 1.10 2002/06/26 17:29:28 harald
  86. * First pre-release with 2.4 stack
  87. *
  88. */
  89. #include <sys/file.h>
  90. /*!
  91. * \file sys/device.h
  92. * \brief Nut/OS device definitions.
  93. */
  94. __BEGIN_DECLS
  95. // wait times for emulation and reality
  96. // has to be overworked
  97. #ifndef __EMULATION__
  98. #define WAIT5 5
  99. #define WAIT50 50
  100. #define WAIT100 100
  101. #define WAIT250 250
  102. #define WAIT500 500
  103. #else
  104. #define WAIT5 1
  105. #define WAIT50 5
  106. #define WAIT100 10
  107. #define WAIT250 25
  108. #define WAIT500 50
  109. #endif
  110. /*!
  111. * \addtogroup xgDevice
  112. */
  113. /*@{*/
  114. #define IFTYP_RAM 0 /*!< \brief RAM device */
  115. #define IFTYP_ROM 1 /*!< \brief ROM device */
  116. #define IFTYP_STREAM 2 /*!< \brief Stream device */
  117. #define IFTYP_NET 3 /*!< \brief Net device */
  118. #define IFTYP_TCPSOCK 4 /*!< \brief TCP socket */
  119. #define IFTYP_CHAR 5 /*!< \brief Character stream device */
  120. #define IFTYP_CAN 6 /*!< \brief CAN device */
  121. #define IFTYP_FS 16 /*!< \brief file system device */
  122. /*!
  123. * \brief Device structure type.
  124. */
  125. typedef struct _NUTDEVICE NUTDEVICE;
  126. /*!
  127. * \brief Device structure.
  128. *
  129. * Each device driver provides a global variable of this type.
  130. * Applications use NutRegisterDevice() to bind the device
  131. * driver to the application code. Except this call, applications
  132. * refer to device drivers by the name of the device when using
  133. * standard C functions like _open() or fopen().
  134. *
  135. * More than one device driver may be available for the same
  136. * hardware device. Typically these drivers provide the same
  137. * name for the device and applications must not refer to
  138. * more than one device driver with the same name.
  139. */
  140. struct _NUTDEVICE {
  141. /*!
  142. * \brief Link to the next device structure.
  143. */
  144. NUTDEVICE *dev_next;
  145. /*!
  146. * \brief Unique device name.
  147. */
  148. char dev_name[9];
  149. /*!
  150. * \brief Type of interface.
  151. *
  152. * May be any of the following:
  153. * - IFTYP_RAM
  154. * - IFTYP_ROM
  155. * - IFTYP_STREAM
  156. * - IFTYP_NET
  157. * - IFTYP_TCPSOCK
  158. * - IFTYP_CHAR
  159. */
  160. u_char dev_type;
  161. /*!
  162. * \brief Hardware base address.
  163. *
  164. * Will be set by calling NutRegisterDevice(). On some device
  165. * drivers this address may be fixed.
  166. */
  167. uptr_t dev_base;
  168. /*! \brief Interrupt registration number.
  169. *
  170. * Will be set by calling NutRegisterDevice(). On some device
  171. * drivers the interrupt may be fixed.
  172. */
  173. u_char dev_irq;
  174. /*! \brief Interface control block.
  175. *
  176. * With stream devices, this points to the IFSTREAM structure and
  177. * with network devices this is a pointer to the IFNET structure.
  178. */
  179. void *dev_icb;
  180. /*!
  181. * \brief Driver control block.
  182. *
  183. * Points to a device specific information block.
  184. */
  185. void *dev_dcb;
  186. /*!
  187. * \brief Driver initialization routine.
  188. *
  189. * This routine is called during device registration.
  190. */
  191. int (*dev_init) (NUTDEVICE *);
  192. /*!
  193. * \brief Driver control function.
  194. *
  195. * Used to modify or query device specific settings.
  196. */
  197. int (*dev_ioctl) (NUTDEVICE *, int, void *);
  198. /*!
  199. * \brief Read from device.
  200. */
  201. int (*dev_read) (NUTFILE *, void *, int);
  202. /*!
  203. * \brief Write to device.
  204. */
  205. int (*dev_write) (NUTFILE *, CONST void *, int);
  206. /*!
  207. * \brief Write to device.
  208. */
  209. #ifdef __HARVARD_ARCH__
  210. int (*dev_write_P) (NUTFILE *, PGM_P, int);
  211. #endif
  212. /*!
  213. * \brief Open a device or file.
  214. */
  215. NUTFILE * (*dev_open) (NUTDEVICE *, CONST char *, int, int);
  216. /*!
  217. * \brief Close a device or file.
  218. */
  219. int (*dev_close) (NUTFILE *);
  220. /*!
  221. * \brief Request file size.
  222. */
  223. long (*dev_size) (NUTFILE *);
  224. };
  225. /*!
  226. * \brief Device structure type.
  227. */
  228. typedef struct _NUTVIRTUALDEVICE NUTVIRTUALDEVICE;
  229. /*!
  230. * \brief Virtual device structure.
  231. */
  232. struct _NUTVIRTUALDEVICE {
  233. NUTVIRTUALDEVICE *vdv_next;
  234. NUTVIRTUALDEVICE *vdv_zero;
  235. u_char vdv_type;
  236. int (*vdv_read) (void *, void *, int);
  237. int (*vdv_write) (void *, CONST void *, int);
  238. #ifdef __HARVARD_ARCH__
  239. int (*vdv_write_P) (void *, PGM_P, int);
  240. #endif
  241. int (*vdv_ioctl) (void *, int, void *);
  242. };
  243. /*!
  244. * \brief Stream interface type.
  245. */
  246. typedef struct _IFSTREAM IFSTREAM;
  247. /*!
  248. * \brief Stream interface information structure.
  249. *
  250. * Deprecated structure. Device drivers should use
  251. * the device control block.
  252. */
  253. struct _IFSTREAM {
  254. int (*if_input)(NUTDEVICE *); /*!< \brief Wait for input. */
  255. int (*if_output)(NUTDEVICE *); /*!< \brief Initiate output. */
  256. int (*if_flush)(NUTDEVICE *); /*!< \brief Wait until output buffer empty. */
  257. volatile u_char if_rx_idx; /*!< \brief Next input index. */
  258. u_char if_rd_idx; /*!< \brief Next read index. */
  259. volatile u_char if_tx_idx; /*!< \brief Next output index. */
  260. u_char if_wr_idx; /*!< \brief Next write index. */
  261. volatile u_char if_tx_act; /*!< \brief Set if transmitter running. */
  262. u_char if_last_eol; /*!< \brief Last end of line character read. */
  263. u_char if_rx_buf[256]; /*!< \brief Input buffer. */
  264. u_char if_tx_buf[256]; /*!< \brief Output buffer. */
  265. };
  266. /*@}*/
  267. extern NUTDEVICE *nutDeviceList;
  268. extern int NutRegisterDevice(NUTDEVICE * dev, uptr_t base, u_char irq);
  269. extern NUTDEVICE *NutDeviceLookup(CONST char *name);
  270. __END_DECLS
  271. #endif