twi_master.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * TWI-master Medium Access Control
  3. *
  4. * Copyright (c) 2013 Michael Buesch <m@bues.ch>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "twi_master.h"
  21. #include "twi_master_sync.h"
  22. #include "util.h"
  23. #include <avr/io.h>
  24. #include <avr/interrupt.h>
  25. #include <util/twi.h>
  26. #include <string.h>
  27. #ifndef TWI_SCL_HZ
  28. # warning "No TWI_SCL_HZ defined. Defaulting to 100 KHz."
  29. # define TWI_SCL_HZ 100000ul
  30. #endif
  31. #define TWI_SYNC
  32. enum twi_transfer_status_flags {
  33. TWI_XFER_READ = 0x80,
  34. TWI_XFER_STAT_MASK = 0x0F,
  35. TWI_XFER_STAT_SHIFT = 0,
  36. };
  37. struct twi_context {
  38. struct twi_transfer *first_xfer;
  39. struct twi_transfer *last_xfer;
  40. };
  41. static struct twi_context twi;
  42. static inline void TWCR_write(uint8_t additional_flags)
  43. {
  44. mb();
  45. TWCR = (1 << TWEN) | (1 << TWINT) | additional_flags;
  46. }
  47. static void send_start_condition(void)
  48. {
  49. /* Send start condition. */
  50. TWCR_write((1 << TWIE) | (1 << TWSTA));
  51. }
  52. static void send_stop_condition(void)
  53. {
  54. /* Send stop condition. */
  55. TWCR_write(1 << TWSTO);
  56. }
  57. static void transfer_set_status(struct twi_transfer *xfer,
  58. enum twi_status status)
  59. {
  60. xfer->status = (xfer->status & ~TWI_XFER_STAT_MASK) |
  61. (status << TWI_XFER_STAT_SHIFT);
  62. }
  63. static enum twi_status transfer_get_status(const struct twi_transfer *xfer)
  64. {
  65. return (xfer->status & TWI_XFER_STAT_MASK) >> TWI_XFER_STAT_SHIFT;
  66. }
  67. static void stop_transfer(struct twi_transfer *xfer,
  68. enum twi_status new_status)
  69. {
  70. send_stop_condition();
  71. transfer_set_status(xfer, new_status);
  72. twi.first_xfer = xfer->next;
  73. if (twi.first_xfer)
  74. send_start_condition();
  75. else
  76. twi.last_xfer = NULL;
  77. if (xfer->callback)
  78. xfer->callback(xfer, new_status);
  79. }
  80. static void handle_tw_status(struct twi_transfer *xfer, uint8_t twstat)
  81. {
  82. uint8_t *buffer = xfer->buffer;
  83. switch (twstat) {
  84. default:
  85. stop_transfer(xfer, TWI_STAT_BUSERROR);
  86. break;
  87. case TW_START:
  88. case TW_REP_START:
  89. if (xfer->status & TWI_XFER_READ)
  90. TWDR = (xfer->address << 1) | 1;
  91. else
  92. TWDR = (xfer->address << 1);
  93. TWCR_write(1 << TWIE);
  94. break;
  95. case TW_MT_DATA_ACK:
  96. case TW_MR_DATA_ACK:
  97. case TW_MR_DATA_NACK:
  98. case TW_MT_SLA_ACK:
  99. if (xfer->status & TWI_XFER_READ) {
  100. buffer[xfer->offset++] = TWDR;
  101. if (xfer->offset == xfer->read_size) {
  102. stop_transfer(xfer, TWI_STAT_FINISHED);
  103. } else {
  104. if (xfer->offset + 1 == xfer->read_size)
  105. TWCR_write(1 << TWIE);
  106. else
  107. TWCR_write((1 << TWIE) | (1 << TWEA));
  108. }
  109. } else {
  110. if (xfer->offset == xfer->write_size) {
  111. if (xfer->read_size) {
  112. xfer->status |= TWI_XFER_READ;
  113. xfer->offset = 0;
  114. send_start_condition();
  115. } else
  116. stop_transfer(xfer, TWI_STAT_FINISHED);
  117. } else {
  118. TWDR = buffer[xfer->offset++];
  119. TWCR_write(1 << TWIE);
  120. }
  121. }
  122. break;
  123. case TW_MR_SLA_ACK:
  124. if (xfer->offset + 1 == xfer->read_size)
  125. TWCR_write(1 << TWIE);
  126. else
  127. TWCR_write((1 << TWIE) | (1 << TWEA));
  128. break;
  129. }
  130. }
  131. static void twi_interrupt_handler(void)
  132. {
  133. struct twi_transfer *xfer;
  134. uint8_t twstat;
  135. mb();
  136. twstat = TW_STATUS;
  137. xfer = twi.first_xfer;
  138. if (xfer)
  139. handle_tw_status(xfer, twstat);
  140. mb();
  141. }
  142. ISR(TWI_vect)
  143. {
  144. twi_interrupt_handler();
  145. }
  146. void twi_init(void)
  147. {
  148. #ifdef TWI_SYNC
  149. i2c_init();
  150. #else
  151. memset(&twi, 0, sizeof(twi));
  152. TWSR = 0;
  153. TWBR = ((F_CPU / TWI_SCL_HZ) - 16) / 2;
  154. TWAR = 0;
  155. #endif
  156. }
  157. void twi_transfer(struct twi_transfer *xfer)
  158. {
  159. #ifdef TWI_SYNC
  160. twi_size_t i;
  161. uint8_t *buffer = xfer->buffer;
  162. if (xfer->write_size) {
  163. i2c_start(xfer->address << 1);
  164. for (i = 0; i < xfer->write_size; i++)
  165. i2c_write(buffer[i]);
  166. if (!xfer->read_size)
  167. i2c_stop();
  168. }
  169. if (xfer->read_size) {
  170. i2c_start((xfer->address << 1) | 1);
  171. for (i = 0; i < xfer->read_size; i++) {
  172. if (i + 1 == xfer->read_size)
  173. buffer[i] = i2c_readNak();
  174. else
  175. buffer[i] = i2c_readAck();
  176. }
  177. i2c_stop();
  178. }
  179. if (xfer->callback)
  180. xfer->callback(xfer, TWI_STAT_FINISHED);
  181. #else /* TWI_SYNC */
  182. uint8_t sreg;
  183. xfer->offset = 0;
  184. xfer->next = NULL;
  185. xfer->status = 0;
  186. if (!xfer->write_size)
  187. xfer->status |= TWI_XFER_READ;
  188. transfer_set_status(xfer, TWI_STAT_INPROGRESS);
  189. sreg = irq_disable_save();
  190. if (twi.last_xfer)
  191. twi.last_xfer->next = xfer;
  192. twi.last_xfer = xfer;
  193. if (!twi.first_xfer) {
  194. twi.first_xfer = xfer;
  195. send_start_condition();
  196. }
  197. irq_restore(sreg);
  198. #endif
  199. }
  200. enum twi_status twi_transfer_get_status(const struct twi_transfer *xfer)
  201. {
  202. enum twi_status status;
  203. uint8_t sreg;
  204. #ifdef TWI_SYNC
  205. return TWI_STAT_FINISHED;
  206. #endif
  207. sreg = irq_disable_save();
  208. status = transfer_get_status(xfer);
  209. irq_restore(sreg);
  210. return status;
  211. }
  212. enum twi_status twi_transfer_wait(struct twi_transfer *xfer,
  213. uint16_t timeout_ms)
  214. {
  215. enum twi_status status;
  216. uint32_t timeout = (uint32_t)timeout_ms * 100;
  217. while (1) {
  218. status = twi_transfer_get_status(xfer);
  219. if (status != TWI_STAT_INPROGRESS)
  220. break;
  221. if (timeout-- == 0) {
  222. twi_transfer_cancel(xfer);
  223. status = TWI_STAT_TIMEOUT;
  224. break;
  225. }
  226. _delay_us(10);
  227. if (!irqs_enabled()) {
  228. if (TWCR & (1 << TWINT))
  229. twi_interrupt_handler();
  230. }
  231. }
  232. return status;
  233. }
  234. void twi_transfer_cancel(struct twi_transfer *xfer)
  235. {
  236. uint8_t sreg;
  237. #ifdef TWI_SYNC
  238. return;
  239. #endif
  240. sreg = irq_disable_save();
  241. if (transfer_get_status(xfer) == TWI_STAT_INPROGRESS)
  242. stop_transfer(xfer, TWI_STAT_CANCELLED);
  243. irq_restore(sreg);
  244. }