genchar.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file dev/genchar.c
  35. * \brief Generic character driver template.
  36. *
  37. * \verbatim
  38. *
  39. * $Log$
  40. * Revision 1.2 2008/08/11 06:59:42 haraldkipp
  41. * BSD types replaced by stdint types (feature request #1282721).
  42. *
  43. * Revision 1.1 2006/04/07 13:52:52 haraldkipp
  44. * Generic character driver sample added.
  45. *
  46. *
  47. * \endverbatim
  48. */
  49. #include <cfg/os.h>
  50. #include <stdlib.h>
  51. #include <string.h>
  52. #include <sys/file.h>
  53. #include <sys/event.h>
  54. #include <sys/timer.h>
  55. #include <dev/irqreg.h>
  56. #include <dev/genchar.h>
  57. /*!
  58. * \addtogroup xgDevGenChar
  59. */
  60. /*@{*/
  61. /* Sample hardware status port. */
  62. #ifndef GENDEV_SPORT
  63. #define GENDEV_SPORT 0x100
  64. #endif
  65. /* Sample hardware data port. */
  66. #ifndef GENDEV_DPORT
  67. #define GENDEV_DPORT 0x104
  68. #endif
  69. /* Sample hardware interrupt. */
  70. #ifndef GENDEV_SIGNAL
  71. #define GENDEV_SIGNAL sig_INTERRUPT1
  72. #endif
  73. /*!
  74. * \brief Device driver's private data structure.
  75. */
  76. typedef struct {
  77. HANDLE dcb_rrdy; /*!< Receiver ready queue. */
  78. volatile int dcb_rcnt; /*!< Number of bytes in the receive buffer. */
  79. uint8_t dcb_rbuff[16]; /*!< Receive buffer. */
  80. uint32_t dcb_rtimeout; /*!< Read timeout. */
  81. HANDLE dcb_trdy; /*!< Transmitter ready queue. */
  82. int dcb_tlen; /*!< Number of bytes in the transmit buffer. */
  83. volatile int dcb_tcnt; /*!< Number of bytes already transmitted. */
  84. uint8_t dcb_tbuff[16]; /*!< Transmit buffer. */
  85. uint32_t dcb_ttimeout; /*!< Write timeout. */
  86. } DEVDCB;
  87. static DEVDCB devdcb;
  88. /*!
  89. * \brief Driver interrupt entry.
  90. *
  91. * If polling is used, this routine is not required, of course.
  92. */
  93. static void GenCharInterrupt(void *arg)
  94. {
  95. NUTDEVICE *dev = (NUTDEVICE *)arg;
  96. DEVDCB *dcb = dev->dev_dcb;
  97. uint8_t st = inr(GENDEV_SPORT);
  98. /* Receive interrupt. */
  99. if (st) {
  100. /* Avoid buffer overflow. */
  101. if (dcb->dcb_rcnt < sizeof(dcb->dcb_rbuff)) {
  102. /* Get byte from device and increment receive counter. */
  103. dcb->dcb_rbuff[dcb->dcb_rcnt] = inr(GENDEV_DPORT);
  104. dcb->dcb_rcnt++;
  105. }
  106. /* Send event on first character. */
  107. if (dcb->dcb_rcnt == 1) {
  108. NutEventPostFromIrq(&dcb->dcb_rrdy);
  109. }
  110. }
  111. /* Transmit interrupt. */
  112. else {
  113. if (dcb->dcb_tcnt < dcb->dcb_tlen) {
  114. /* Send byte to device and increment transmit counter. */
  115. outr(GENDEV_DPORT, dcb->dcb_tbuff[dcb->dcb_tcnt]);
  116. dcb->dcb_tcnt++;
  117. }
  118. /* Transmit buffer empty, send event. */
  119. else {
  120. NutEventPostFromIrq(&dcb->dcb_trdy);
  121. /* Optionally disable further transmit interrupts. */
  122. }
  123. }
  124. }
  125. /*!
  126. * \brief Handle device I/O controls.
  127. *
  128. * This function is called by the ioctl() function of the C runtime
  129. * library. Applications use the ioctl() to control device specific
  130. * functions.
  131. *
  132. * \param dev Pointer to the device information structure.
  133. * \param req Requested control function. Possible values are defined
  134. * in the device driver's header file.
  135. * \return 0 on success or -1 if the function fails or is not supported.
  136. */
  137. static int GenCharIOCtl(NUTDEVICE * dev, int req, void *conf)
  138. {
  139. int rc = 0;
  140. DEVDCB *dcb = dev->dev_dcb;
  141. uint32_t *lvp = (uint32_t *) conf;
  142. switch (req) {
  143. case DEV_SETREADTIMEOUT:
  144. /* Set read timeout. */
  145. dcb->dcb_rtimeout = *lvp;
  146. break;
  147. case DEV_GETREADTIMEOUT:
  148. /* Query read timeout. */
  149. *lvp = dcb->dcb_rtimeout;
  150. break;
  151. case DEV_SETWRITETIMEOUT:
  152. /* Set write timeout. */
  153. dcb->dcb_ttimeout = *lvp;
  154. break;
  155. case DEV_GETWRITETIMEOUT:
  156. /* Query write timeout. */
  157. *lvp = dcb->dcb_ttimeout;
  158. break;
  159. /* Add attional ioctl functions here. */
  160. default:
  161. /* Unknown function. */
  162. rc = -1;
  163. break;
  164. }
  165. return rc;
  166. }
  167. /*!
  168. * \brief Initialize the device.
  169. *
  170. * This function is called by NutRegisterDevice(), using the
  171. * _NUTDEVICE::dev_init entry.
  172. *
  173. * \param dev Pointer to the device information structure.
  174. *
  175. * \return 0 on success, -1 otherwise.
  176. */
  177. static int GenCharInit(NUTDEVICE * dev)
  178. {
  179. /* Add hardware initialization here. */
  180. /* Register interrupt handler, if required. */
  181. if (NutRegisterIrqHandler(&GENDEV_SIGNAL, GenCharInterrupt, dev)) {
  182. return -1;
  183. }
  184. /* Set interrupt mode and enable interrupts. */
  185. NutIrqSetMode(&GENDEV_SIGNAL, NUT_IRQMODE_LOWLEVEL);
  186. NutIrqEnable(&GENDEV_SIGNAL);
  187. return 0;
  188. }
  189. /*!
  190. * \brief Read data from the device.
  191. *
  192. * This function is called by the low level input routines of the
  193. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_read
  194. * entry.
  195. *
  196. * The function may block the calling thread until at least one
  197. * character has been received or a timeout occurs.
  198. *
  199. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  200. * previous call to GenCharOpen().
  201. * \param buffer Pointer to the buffer that receives the data. If zero,
  202. * then all characters in the input buffer will be
  203. * removed.
  204. * \param size Maximum number of bytes to read.
  205. *
  206. * \return The number of bytes read, which may be less than the number
  207. * of bytes specified. A return value of -1 indicates an error,
  208. * while zero is returned in case of a timeout.
  209. */
  210. static int GenCharRead(NUTFILE * fp, void *buffer, int size)
  211. {
  212. int rc;
  213. int i;
  214. NUTDEVICE *dev = fp->nf_dev;
  215. DEVDCB *dcb = dev->dev_dcb;
  216. /* Call without data pointer discards receive buffer. */
  217. if (buffer == 0) {
  218. /* Atomic access to the receive counter. */
  219. NutIrqDisable(&GENDEV_SIGNAL);
  220. dcb->dcb_rcnt = 0;
  221. NutIrqEnable(&GENDEV_SIGNAL);
  222. return 0;
  223. }
  224. /*
  225. * Wait until at least one character is buffered or until a read
  226. * timeout occured.
  227. */
  228. for (;;) {
  229. /* Atomic access to the receive counter. */
  230. NutIrqDisable(&GENDEV_SIGNAL);
  231. rc = dcb->dcb_rcnt;
  232. NutIrqEnable(&GENDEV_SIGNAL);
  233. if (rc) {
  234. break;
  235. }
  236. if (NutEventWait(&dcb->dcb_rrdy, dcb->dcb_rtimeout)) {
  237. return 0; /* Timeout. */
  238. }
  239. }
  240. if (rc > size) {
  241. rc = size;
  242. }
  243. if (rc) {
  244. memcpy(buffer, dcb->dcb_rbuff, rc);
  245. /*
  246. * This sample driver simply moves remaining bytes to the front
  247. * of the buffer. A more sophisticated driver may use a circular
  248. * buffer.
  249. */
  250. NutIrqDisable(&GENDEV_SIGNAL);
  251. dcb->dcb_rcnt -= rc;
  252. for (i = 0; i < dcb->dcb_rcnt; i++) {
  253. dcb->dcb_rbuff[i] = dcb->dcb_rbuff[rc + i];
  254. }
  255. NutIrqEnable(&GENDEV_SIGNAL);
  256. }
  257. return rc;
  258. }
  259. /*!
  260. * \brief Write data to the device.
  261. *
  262. * This function is called by the low level output routines of the
  263. * \ref xrCrtLowio "C runtime library", using the
  264. * \ref _NUTDEVICE::dev_write entry.
  265. *
  266. * The function may block the calling thread.
  267. *
  268. * \param fp Pointer to a _NUTFILE structure, obtained by a previous
  269. * call to GenCharOpen().
  270. * \param buffer Pointer to the data to be written. If zero, then the
  271. * output buffer will be flushed.
  272. * \param len Number of bytes to write.
  273. *
  274. * \return The number of bytes written. With some devices this may be
  275. * less than the number of bytes specified if a timeout occured.
  276. * A return value of -1 indicates an error.
  277. */
  278. static int GenCharWrite(NUTFILE * fp, const void *buffer, int len)
  279. {
  280. int rc = 0;
  281. int cnt;
  282. int pend;
  283. const char *cp = buffer;
  284. NUTDEVICE *dev = fp->nf_dev;
  285. DEVDCB *dcb = dev->dev_dcb;
  286. while (rc < len) {
  287. /*
  288. * This sample driver waits on each output until all characters
  289. * had been tranmitted. A more sophisticated driver may add
  290. * new characters as soon as there is room left in the tranmit
  291. * buffer.
  292. */
  293. for (;;) {
  294. /* Atomic access to the transmit counter. */
  295. NutIrqDisable(&GENDEV_SIGNAL);
  296. pend = dcb->dcb_tlen - dcb->dcb_tcnt;
  297. NutIrqEnable(&GENDEV_SIGNAL);
  298. if (pend == 0) {
  299. break; /* All characters tranmitted. */
  300. }
  301. if (NutEventWait(&dcb->dcb_trdy, dcb->dcb_ttimeout)) {
  302. return rc; /* Timeout. */
  303. }
  304. }
  305. /* Call without data pointer flushes the buffer. */
  306. if (buffer == 0) {
  307. return 0;
  308. }
  309. /* Determine the number of bytes left to transmit. */
  310. cnt = len - rc;
  311. if (cnt > sizeof(dcb->dcb_tbuff)) {
  312. cnt = sizeof(dcb->dcb_tbuff);
  313. }
  314. /* Fill the tranmit buffer. */
  315. NutIrqDisable(&GENDEV_SIGNAL);
  316. dcb->dcb_tcnt = cnt;
  317. memcpy(dcb->dcb_tbuff, cp + rc, cnt);
  318. /* Add code here to enable tranmit interrupts. */
  319. NutIrqEnable(&GENDEV_SIGNAL);
  320. rc += cnt;
  321. }
  322. return rc;
  323. }
  324. #ifdef __HARVARD_ARCH__
  325. /*!
  326. * \brief Write program data to the device.
  327. *
  328. * Similar to GenCharWrite() except that the data is located in program
  329. * memory.
  330. *
  331. * This function is called by the low level output routines of the
  332. * \ref xrCrtLowio "C runtime library", using the _NUTDEVICE::dev_write_P
  333. * entry, which is available on Harvard architectures only.
  334. *
  335. * The function may block the calling thread.
  336. *
  337. * \param fp Pointer to a NUTFILE structure, obtained by a previous
  338. * call to UsartOpen().
  339. * \param buffer Pointer to the data in program space to be written.
  340. * \param len Number of bytes to write.
  341. *
  342. * \return The number of bytes written. With some devices this may be
  343. * less than the number of bytes specified if a timeout occured.
  344. * A return value of -1 indicates an error.
  345. */
  346. int GenCharWrite_P(NUTFILE * fp, PGM_P buffer, int len)
  347. {
  348. /* Our sample driver doesn't support this. */
  349. return -1;
  350. }
  351. #endif
  352. /*!
  353. * \brief Open the device.
  354. *
  355. * This function is called by the low level open routine of the C runtime
  356. * library, using the _NUTDEVICE::dev_open entry.
  357. *
  358. * \param dev Pointer to the NUTDEVICE structure.
  359. * \param name Character device drivers usually ignore this paramter.
  360. * \param mode Operation mode. Any of the following values may be or-ed:
  361. * - \ref _O_BINARY
  362. * - \ref _O_RDONLY
  363. * - \ref _O_WRONLY
  364. * \param acc Access type, usually ignored by Nut/OS drivers.
  365. *
  366. * \return Pointer to a NUTFILE structure if successful or NUTFILE_EOF if
  367. * the open failed.
  368. */
  369. static NUTFILE *GenCharOpen(NUTDEVICE * dev, const char *name, int mode, int acc)
  370. {
  371. NUTFILE *fp = (NUTFILE *) (dev->dev_dcb);
  372. if ((fp = malloc(sizeof(NUTFILE))) == 0) {
  373. return NUTFILE_EOF; /* No memory. */
  374. }
  375. fp->nf_dev = dev;
  376. fp->nf_fcb = NULL;
  377. return fp;
  378. }
  379. /*!
  380. * \brief Close the device.
  381. *
  382. * This function is called by the low level close routine of the C runtime
  383. * library, using the _NUTDEVICE::dev_close entry.
  384. *
  385. * \param fp Pointer to a _NUTFILE structure, obtained by a previous call
  386. * to GenCharOpen().
  387. *
  388. * \return 0 on success or -1 otherwise.
  389. */
  390. static int GenCharClose(NUTFILE * fp)
  391. {
  392. /* We may optionally flush our output and discard our input buffers. */
  393. /* Release all resources which we allocated in the open function. */
  394. if (fp) {
  395. free(fp);
  396. }
  397. return 0;
  398. }
  399. /*!
  400. * \brief Retrieves the number of characters in input buffer.
  401. *
  402. * This function is called by the low level size routine of the C runtime
  403. * library, using the _NUTDEVICE::dev_size entry.
  404. *
  405. * \param fp Pointer to a \ref _NUTFILE structure, obtained by a
  406. * previous call to UsartOpen().
  407. *
  408. * \return The number of bytes currently stored in input buffer.
  409. */
  410. long GenCharSize (NUTFILE *fp)
  411. {
  412. long rc;
  413. NUTDEVICE *dev = fp->nf_dev;
  414. DEVDCB *dcb = dev->dev_dcb;
  415. /* Atomic access to the receive buffer counter. */
  416. NutIrqDisable(&GENDEV_SIGNAL);
  417. rc = dcb->dcb_rcnt;
  418. NutIrqEnable(&GENDEV_SIGNAL);
  419. return rc;
  420. }
  421. /*!
  422. * \brief Device information structure.
  423. */
  424. NUTDEVICE devGenChar = {
  425. /*! \brief dev_next points to next device.
  426. *
  427. * This is used by Nut/OS to create a linked list of all registered
  428. * devices. Must be a NULL pointer initially.
  429. */
  430. 0,
  431. /*! \brief dev_name specifies the unique device name.
  432. *
  433. * Applications use this name to open a device.
  434. *
  435. * Each registered device must have a unique name. However, if more
  436. * than one driver is available for the same, all drivers may use
  437. * the same name. This way Nut/OS can make sure, that only one
  438. * driver is loaded for that device.
  439. */
  440. {'g', 'e', 'n', 'c', 'h', 'a', 'r', 0, 0},
  441. /*! \brief dev_type secifies the device type.
  442. *
  443. * In general it's up to the device driver to use this entry.
  444. * Nut/OS ignores this value, while Nut/Net checks for IFTYP_NET.
  445. *
  446. * This attribute had been used by old Nut/OS versions to determine
  447. * between polling (IFTYP_CHAR) and buffering (IFTYP_STREAM) drivers.
  448. * Since Nut/OS 3, this distinction makes no sense anymore. Since
  449. * Nut/OS 4, block device drivers make use of this type to automatically
  450. * detect a registered high level file system driver (IFTYP_FS).
  451. *
  452. * It might be possible, that later Nut/OS versions will check the
  453. * type. So character device drivers should set it to IFTYP_CHAR
  454. * or IFTYP_STREAM.
  455. */
  456. IFTYP_CHAR,
  457. /*! \brief dev_base specifies the hardware base address.
  458. *
  459. * NutRegisterDevice() will set this value, if the calling application
  460. * passed an address not equal zero. It's up to the device driver
  461. * to use this value. Though, for performance reasons most drivers
  462. * ignore this address and use hardcoded values, typically configured
  463. * by the Configurator during system build.
  464. *
  465. * Drivers may freely use this entry for other purposes.
  466. */
  467. 0,
  468. /*! \brief dev_irq specifies first interrupt number.
  469. *
  470. * NutRegisterDevice() will set this value, if the calling application
  471. * passed an interrupt number not equal zero. It's up to the device
  472. * driver to use this value. Though, most drivers ignore it and use
  473. * hardcoded interrupts, typically configured by the Configurator
  474. * during system build.
  475. *
  476. * Drivers may freely use this entry for other purposes.
  477. */
  478. 0,
  479. /*! \brief dev_icb points to the interface control block.
  480. *
  481. * Drivers may set this pointer to an internal structure during
  482. * initialization.
  483. *
  484. * Character device drivers typically do not use this entry. Network
  485. * or file system drivers may use it to link drivers of different
  486. * levels or to store hardware independent values.
  487. *
  488. * Drivers may freely use this entry for other purposes.
  489. */
  490. 0,
  491. /*! \brief dev_dcb points to the driver control block.
  492. *
  493. * Drivers will set this pointer to an internal structure during
  494. * initialization in order to store local data like pointers
  495. * to buffers or status and mode informations. The structure
  496. * may be either statically or dynamically allocated.
  497. *
  498. * Drivers may freely use this entry for other purposes.
  499. */
  500. &devdcb,
  501. /*! \brief dev_init initializes the driver and the device hardware.
  502. *
  503. * Points to the driver's initialization routine and is called by
  504. * NutRegisterDevice(). Drivers, which do not require any initialization
  505. * may set this entry to NULL.
  506. */
  507. GenCharInit,
  508. /*! \brief dev_ioctl implements driver specific control function.
  509. *
  510. * Currently this entry is required, because the C runtime doesn't
  511. * check the pointer value for NULL.
  512. */
  513. GenCharIOCtl,
  514. /*! \brief dev_read receives data from the device.
  515. *
  516. * Will be NULL for write-only devices.
  517. */
  518. GenCharRead,
  519. /*! \brief dev_write sends data to the device.
  520. *
  521. * Will be NULL for read-only devices.
  522. */
  523. GenCharWrite,
  524. #ifdef __HARVARD_ARCH__
  525. /*! \brief dev_write_P sends program space data to the device.
  526. *
  527. * This entry is available on Harvard architectures (AVR) only.
  528. *
  529. * Will be NULL for read-only devices.
  530. */
  531. GenCharWrite_P,
  532. #endif
  533. /*! \brief dev_open opens a device.
  534. *
  535. */
  536. GenCharOpen,
  537. /*! \brief dev_close, close a device.
  538. *
  539. */
  540. GenCharClose,
  541. /*! \brief dev_size queries device size information.
  542. *
  543. * This had been initially used by early file system drivers to query
  544. * the size of a previously opened file.
  545. *
  546. * Some character device drivers return the number of bytes currently
  547. * available in the input buffer.
  548. */
  549. GenCharSize,
  550. /*! \brief dev_select queries the blocking state for read or write
  551. * operations and possible errors on the device .
  552. */
  553. NULL,
  554. };
  555. /*@}*/