i2cbus_avr.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * Copyright (C) 2001-2005 by egnite Software GmbH
  3. * Copyright (C) 2012, Ole Reinhardt <ole.reinhardt@embedded-it.de>
  4. * Copyright (C) 2013 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. *
  36. */
  37. /*!
  38. * \file arch/avr/dev/i2cbus_avr.c
  39. * \brief AVR I2C bus driver.
  40. *
  41. * \verbatim
  42. * $Id$
  43. * \endverbatim
  44. */
  45. #include <cfg/twi.h>
  46. #include <dev/irqreg.h>
  47. #include <sys/nutdebug.h>
  48. #include <sys/timer.h>
  49. #include <sys/event.h>
  50. #include <dev/i2cbus_avr.h>
  51. #ifndef I2C_DEFAULT_SPEED
  52. #define I2C_DEFAULT_SPEED 100L
  53. #endif
  54. /*!
  55. * \brief Local data of the AVR TWI bus driver.
  56. *
  57. * This structure is passed to the interrupt handler.
  58. */
  59. typedef struct _AVR_TWICB {
  60. /*! \brief Slave address. */
  61. uint8_t icb_slave;
  62. /*! \brief System interrupt handler. */
  63. IRQ_HANDLER *icb_sig;
  64. /*! \brief I2C message. */
  65. NUTI2C_MSG *icb_msg;
  66. /*! \brief Thread waiting for completion. */
  67. HANDLE icb_queue;
  68. } AVR_TWICB;
  69. /*!
  70. * \brief TWI interrupt handler.
  71. *
  72. * The application thread will enable interrupts and initiate sending
  73. * a start condition. As soon as the hardware sent out this start
  74. * condition, the first interrupt of a transfer is triggered.
  75. *
  76. * A pointer to the interface control block structure is passed
  77. * to this interrupt function, which, among other things, contains
  78. * the slave address and number of bytes to transmit and to receive.
  79. * This enables the interrupt function to process the complete
  80. * transfer in the background. When done or in case of an error, the
  81. * waiting thread will be woken up.
  82. */
  83. static void TwInterrupt(void *arg)
  84. {
  85. AVR_TWICB *icb = (AVR_TWICB *) arg;
  86. NUTI2C_MSG *msg = icb->icb_msg;
  87. uint8_t twsr;
  88. uint8_t twcr;
  89. /* Read control register and clear start and stop conditions.
  90. * We also clear the ACK enable bit, which may be set further
  91. * down on specific states. Note, that the bus is frozen until
  92. * the TWINT bit is cleared. */
  93. twcr = inb(TWCR) & ~(_BV(TWSTA) | _BV(TWSTO) | _BV(TWEA));
  94. /* Read the status register, remove the prescaler bits and
  95. * handle the current state in a large switch statement. */
  96. twsr = inb(TWSR) & 0xF8;
  97. switch (twsr) {
  98. /*
  99. * 0x08: Start condition has been transmitted.
  100. * 0x10: Repeated start condition has been transmitted.
  101. */
  102. case TW_START:
  103. case TW_REP_START:
  104. /* If outgoing data is available, transmit SLA+W. Logic is in
  105. * master transmit mode. */
  106. if (msg->msg_widx < msg->msg_wlen) {
  107. outb(TWDR, icb->icb_slave);
  108. }
  109. /* If no outgoing data is available, transmit SLA+R. Logic will
  110. * switch to master receive mode. */
  111. else {
  112. outb(TWDR, icb->icb_slave | 1);
  113. }
  114. break;
  115. /*
  116. * 0x18: SLA+W has been transmitted and ACK has been received.
  117. * 0x28: Data byte has been transmitted and ACK has been received.
  118. */
  119. case TW_MT_SLA_ACK:
  120. case TW_MT_DATA_ACK:
  121. /* If outgoing data is left to send, put the next byte in the
  122. * data register. */
  123. if (msg->msg_widx < msg->msg_wlen) {
  124. outb(TWDR, msg->msg_wdat[msg->msg_widx++]);
  125. break;
  126. }
  127. /* All outgoing data has been sent. If a response is expected,
  128. * transmit a repeated start condition. */
  129. else if (msg->msg_rsiz) {
  130. twcr |= _BV(TWSTA);
  131. break;
  132. }
  133. /* If no more data, then fall through to send a stop condition
  134. * and wake up the waiting thread. */
  135. /*
  136. * 0x20: SLA+W has been transmitted, but not acknowledged.
  137. * 0x30: Data byte has been transmitted, but not acknowledged.
  138. * 0x38: Arbitration lost while in master mode.
  139. * 0x48: SLA+R has been transmitted, but not acknowledged.
  140. */
  141. case TW_MT_SLA_NACK:
  142. case TW_MT_DATA_NACK:
  143. case TW_MT_ARB_LOST:
  144. case TW_MR_SLA_NACK:
  145. /* Send stop condition and wake up the waiting thread. */
  146. twcr |= _BV(TWSTO);
  147. NutEventPostFromIrq(&icb->icb_queue);
  148. break;
  149. /*
  150. * 0x50: Data byte has been received and acknowledged.
  151. */
  152. case TW_MR_DATA_ACK:
  153. /* Store the data byte in the receive buffer. */
  154. msg->msg_rdat[msg->msg_ridx++] = inb(TWDR);
  155. /* Fall through to acknowledge this byte. */
  156. /*
  157. * 0x40: SLA+R has been transmitted and ACK has been received.
  158. */
  159. case TW_MR_SLA_ACK:
  160. /* Acknowledge next data bytes except the last one. */
  161. if (msg->msg_ridx + 1 < msg->msg_rsiz) {
  162. twcr |= _BV(TWEA);
  163. }
  164. break;
  165. /*
  166. * 0x58: Data byte has been received, but not acknowledged.
  167. */
  168. case TW_MR_DATA_NACK:
  169. /* Store the last data byte. */
  170. msg->msg_rdat[msg->msg_ridx++] = inb(TWDR);
  171. /* Send a stop condition. */
  172. twcr |= _BV(TWSTO);
  173. break;
  174. /*
  175. * Send stop and return immediately on all unexpected states.
  176. * However, when all bits are set, then we must not touch the
  177. * control register.
  178. */
  179. default:
  180. if (twsr != 0xf8) {
  181. outb(TWCR, twcr | _BV(TWSTO));
  182. }
  183. return;
  184. }
  185. /* Finally update the control register. If sending a stop condition,
  186. * then wake up the application. */
  187. outb(TWCR, twcr);
  188. if (twcr & _BV(TWSTO)) {
  189. NutEventPostFromIrq(&icb->icb_queue);
  190. }
  191. }
  192. /*!
  193. * \brief Configure the I2C bus controller.
  194. *
  195. * This function is called by the platform independent code via the
  196. * NUTI2C_BUS::bus_conf function pointer. Most implementations will
  197. * also call this function during initialization to set the
  198. * default configuration.
  199. *
  200. * Right now only the bus clock rate is configurable.
  201. */
  202. static int AvrTwiBusConf(NUTI2C_BUS *bus)
  203. {
  204. uint32_t twbr;
  205. uint8_t twps;
  206. uint32_t pck = NutClockGet(NUT_HWCLK_PERIPHERAL);
  207. /* Check parameter. */
  208. NUTASSERT(bus != NULL);
  209. /* Calculate the bit rate divider:
  210. TWBR = (CLOCK / (2 * RATE) - 8 */
  211. twbr = pck;
  212. twbr += bus->bus_rate;
  213. twbr >>= 1;
  214. twbr /= bus->bus_rate;
  215. twbr -= 8;
  216. /* Increase the pre-scaler until the divider fits. */
  217. for (twps = 0; twbr > 255 && twps < 4; twps++) {
  218. twbr >>= 2;
  219. }
  220. /* Select minimum rate on divider overflow. */
  221. if (twps > 3) {
  222. twps = 3;
  223. twbr = 255;
  224. }
  225. /* Set hardware divider registers and enable the bus. */
  226. outb(TWBR, (uint8_t) twbr);
  227. outb(TWSR, twps);
  228. outb(TWCR, _BV(TWEN));
  229. /* Calculate the actual rate:
  230. RATE = CLOCK / (2 * TWBR * 4^TWPS + 16) */
  231. twbr <<= (twps << 1);
  232. twbr += 8;
  233. bus->bus_rate = (pck + twbr) / (twbr << 1);
  234. return 0;
  235. }
  236. /*!
  237. * \brief Wait for specific bit values in the control register.
  238. *
  239. * Using a separate function saves us about 50 bytes of code.
  240. */
  241. static void AvrTwiBusWait(uint8_t msk, uint8_t val, uint8_t *wt)
  242. {
  243. while ((inb(TWCR) & msk) != val && wt) {
  244. NutSleep(1);
  245. wt--;
  246. }
  247. }
  248. /*!
  249. * \brief Probe the I2C bus for a specified slave address.
  250. *
  251. * This function is called by the platform independent code via the
  252. * NUTI2C_BUS::bus_probe function pointer. This may happen even if no
  253. * slave device had been attached to the bus and thus without any
  254. * previous call to NUTI2C_BUS::bus_init. However, in that case
  255. * NUTI2C_BUS::bus_configure will have been called.
  256. *
  257. * The bus scan is done in polling mode, interrupts are disabled.
  258. *
  259. * In order to reduce the code size, the bus timeout is limited to 255ms.
  260. */
  261. static int AvrTwiBusProbe(NUTI2C_BUS *bus, int sla)
  262. {
  263. int rc = -1;
  264. uint8_t wt = 255;
  265. /* Check parameter. */
  266. NUTASSERT(bus != NULL);
  267. NUTASSERT(bus->bus_icb != NULL);
  268. if (bus->bus_timeout < 256) {
  269. wt = bus->bus_timeout;
  270. }
  271. /* Send start condition and wait for TWINT. */
  272. outb(TWCR, _BV(TWINT) | _BV(TWSTA) | _BV(TWEN));
  273. AvrTwiBusWait(_BV(TWINT), _BV(TWINT), &wt);
  274. if (wt) {
  275. /* Send SLA+R and wait for TWINT. */
  276. outb(TWDR, (sla << 1) | 1);
  277. outb(TWCR, _BV(TWINT) | _BV(TWEN));
  278. AvrTwiBusWait(_BV(TWINT), _BV(TWINT), &wt);
  279. /* Check for acknowledge. */
  280. if (wt && (inb(TWSR) & 0xF8) == TW_MR_SLA_ACK) {
  281. /* If address has been acknowledged, then we found a slave.
  282. * In this case we must do a dummy transfer. */
  283. rc = 0;
  284. outb(TWCR, _BV(TWINT) | _BV(TWEN));
  285. AvrTwiBusWait(_BV(TWINT), _BV(TWINT), &wt);
  286. }
  287. }
  288. /* Send stop condition and wait for auto reset. */
  289. outb(TWCR, _BV(TWINT) | _BV(TWSTO) | _BV(TWEN));
  290. AvrTwiBusWait(_BV(TWSTO), 0, &wt);
  291. return wt ? rc : -1;
  292. }
  293. /*!
  294. * \brief I2C bus transfer.
  295. *
  296. * This function is called by the platform independent code via the
  297. * NUTI2C_BUS::bus_tran function pointer.
  298. */
  299. static int AvrTwiBusTran(NUTI2C_SLAVE *slave, NUTI2C_MSG *msg)
  300. {
  301. int rc;
  302. AVR_TWICB *icb;
  303. /* Check parameters. */
  304. NUTASSERT(slave != NULL);
  305. NUTASSERT(slave->slave_bus != NULL);
  306. NUTASSERT(slave->slave_bus->bus_icb != NULL);
  307. /* Setup the interface control block. This structure is accessed
  308. * by the interrupt handler. */
  309. icb = (AVR_TWICB *) slave->slave_bus->bus_icb;
  310. icb->icb_slave = (uint8_t) slave->slave_address << 1;
  311. icb->icb_msg = msg;
  312. /* Send start condition and enable TWI interrupts. */
  313. outb(TWCR, _BV(TWINT) | _BV(TWSTA) | _BV(TWEN));
  314. NutIrqEnable(icb->icb_sig);
  315. /* Wait for transfer complete and disable interrupts. */
  316. rc = NutEventWait(&icb->icb_queue, slave->slave_timeout);
  317. NutIrqDisable(icb->icb_sig);
  318. /* On timeouts we assume, that the bus is no longer active. If an
  319. * interrupt is still pending, then send a stop condition. */
  320. if (rc) {
  321. if (inb(TWCR) & _BV(TWINT)) {
  322. outb(TWCR, _BV(TWINT) | _BV(TWSTO) | _BV(TWEN));
  323. }
  324. }
  325. /* Otherwise return the number of bytes received. */
  326. else {
  327. rc = msg->msg_ridx;
  328. }
  329. return rc;
  330. }
  331. /*!
  332. * \brief Initialize TWI hardware.
  333. *
  334. * This function is called by the platform independent code via the
  335. * NUTI2C_BUS::bus_init function pointer when the application calls
  336. * NutRegisterI2cSlave().
  337. */
  338. static int AvrTwiBusInit(NUTI2C_BUS *bus)
  339. {
  340. AVR_TWICB *icb;
  341. /* Check parameter. */
  342. NUTASSERT(bus != NULL);
  343. NUTASSERT(bus->bus_icb != NULL);
  344. icb = (AVR_TWICB *) bus->bus_icb;
  345. /* Enable the bus with default settings. */
  346. AvrTwiBusConf(bus);
  347. /* Register IRQ Handler */
  348. return NutRegisterIrqHandler(icb->icb_sig, TwInterrupt, icb);
  349. }
  350. static AVR_TWICB twi0cb = {
  351. 0, /*!< \brief AVR_TWICB::icb_base */
  352. &sig_2WIRE_SERIAL, /*!< \brief AVR_TWICB::icb_sig */
  353. NULL, /*!< \brief AVR_TWICB::icb_msg */
  354. NULL /*!< \brief AVR_TWICB::icb_queue */
  355. };
  356. /*!
  357. * \brief I2C bus driver for AVR TWI hardware.
  358. *
  359. * This is an interrupt driven driver, which supports master mode only.
  360. */
  361. NUTI2C_BUS i2cBus0Avr = {
  362. &twi0cb, /*!< \brief NUTI2C_BUS::bus_icb */
  363. AvrTwiBusInit, /*!< \brief NUTI2C_BUS::bus_init */
  364. AvrTwiBusConf, /*!< \brief NUTI2C_BUS::bus_configure */
  365. AvrTwiBusProbe, /*!< \brief NUTI2C_BUS::bus_probe */
  366. AvrTwiBusTran, /*!< \brief NUTI2C_BUS::bus_transceive */
  367. 100, /*!< \brief NUTI2C_BUS::bus_timeout */
  368. I2C_DEFAULT_SPEED * 1000L, /*!< \brief NUTI2C_BUS::bus_rate */
  369. 0, /*!< \brief NUTI2C_BUS::bus_flags */
  370. NULL /*!< \brief NUTI2C_BUS::bus_mutex */
  371. };