device.h 9.1 KB

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