i2c.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <avr/io.h>
  2. #include <util/delay_basic.h>
  3. #include <stdbool.h>
  4. #include "i2c.h"
  5. #define SCL_BIT PD3
  6. #define SDA_BIT PD2
  7. #define SCL_M (1 << SCL_BIT)
  8. #define SDA_M (1 << SDA_BIT)
  9. /* Open drain: PORT bits stay 0, DDR=1 drives low, DDR=0 releases (pull-up). */
  10. #define SDA_LOW() (DDRD |= SDA_M)
  11. #define SDA_HIGH() (DDRD &= ~SDA_M)
  12. #define SCL_LOW() (DDRD |= SCL_M)
  13. #define SCL_HIGH() (DDRD &= ~SCL_M)
  14. /*
  15. * Bit timing (16 MHz). Data bits are inlined with cycle-exact pads so that
  16. * delay 0 gives at least T_BIT cycles per SCL period (400 kHz, Fast mode) with
  17. * tLOW >= 1.3 us and tHIGH >= 0.6 us. Each delay unit adds 3 cycles to each
  18. * half period: the period is 40 cycles at delay 0 and 38 + 6 * delay above:
  19. * 0 = 400 kHz, 5 = 235 kHz, 10 = 163 kHz, 47 = 50 kHz.
  20. * Reads run somewhat slower (~314 kHz at delay 0); both stay within Fast mode.
  21. * The pads below are tuned against the generated code (see wr()/rd()); the
  22. * SCL edge is followed by a read-back, so slow rise or clock stretching only
  23. * ever lengthens the high phase.
  24. */
  25. static uint8_t dly = 0;
  26. static bool bus_err;
  27. /* START/STOP/recovery only: not on the per-bit path, >= 0.8 us at delay 0 */
  28. static void d(void) { if (dly) _delay_loop_1(dly); }
  29. /* 3 cycles per unit; n stays in a register for the whole byte */
  30. static inline __attribute__((always_inline)) void xdelay(uint8_t n)
  31. {
  32. if (n)
  33. __asm__ volatile ("1: dec %0\n\tbrne 1b" : "+r" (n));
  34. }
  35. void i2c_set_delay(uint8_t v) { dly = v; }
  36. void i2c_init(void)
  37. {
  38. PORTD &= ~(SCL_M | SDA_M);
  39. DDRD &= ~(SCL_M | SDA_M);
  40. }
  41. static void __attribute__((noinline)) scl_wait(void) /* stretched or slow rise */
  42. {
  43. uint16_t t = 0; /* ~16 ms max */
  44. while (!(PIND & SCL_M)) if (--t == 0) { bus_err = true; break; }
  45. }
  46. /*
  47. * Release SCL and honour clock stretching. The input synchronizer needs a
  48. * cycle before PIND shows the released pin, hence the nop: without it the
  49. * check always failed and every bit took the slow path.
  50. */
  51. static inline __attribute__((always_inline)) void scl_up(void)
  52. {
  53. SCL_HIGH();
  54. __asm__ volatile ("nop");
  55. if (!(PIND & SCL_M)) scl_wait();
  56. }
  57. static void scl_release(void) { scl_up(); }
  58. /*
  59. * One SCL pulse for a data or ACK bit, SDA already set up. Returns the SDA
  60. * level sampled at the end of the high phase. PAD_LOW/PAD_HIGH make up the
  61. * difference between the instruction overhead and the timing targets.
  62. */
  63. /*
  64. * Counted on avr-gcc 15 -Os at delay 0, sbi/cbi = 2 cycles, from the SCL
  65. * edge to the next one. wr(): data low 13 (bit 1) / 16 (bit 0), ACK low 9 +
  66. * ACK_PAD, high 10. rd(): see there. Recount after compiler or code changes.
  67. */
  68. #define PAD_LOW 12u /* data low 25 / 28, ACK low 24 */
  69. #define PAD_HIGH 6u /* high 16 */
  70. #define ACK_PAD 3u
  71. static inline __attribute__((always_inline)) uint8_t clock_bit(uint8_t n)
  72. {
  73. __builtin_avr_delay_cycles(PAD_LOW);
  74. xdelay(n);
  75. scl_up();
  76. __builtin_avr_delay_cycles(PAD_HIGH);
  77. xdelay(n);
  78. uint8_t sda = PIND & SDA_M;
  79. SCL_LOW();
  80. return sda;
  81. }
  82. static void start(void)
  83. {
  84. SDA_HIGH(); d();
  85. scl_release(); d();
  86. SDA_LOW(); d();
  87. SCL_LOW();
  88. }
  89. static void stop(void)
  90. {
  91. SDA_LOW(); d();
  92. scl_release(); d();
  93. SDA_HIGH(); d();
  94. }
  95. static bool wr(uint8_t b) /* returns true on ACK */
  96. {
  97. uint8_t n = dly;
  98. for (uint8_t i = 0; i < 8; i++) {
  99. if (b & 0x80) SDA_HIGH(); else SDA_LOW();
  100. b <<= 1;
  101. clock_bit(n);
  102. }
  103. SDA_HIGH();
  104. __builtin_avr_delay_cycles(ACK_PAD); /* this path is shorter than a data bit */
  105. return !clock_bit(n);
  106. }
  107. static uint8_t rd(bool ack)
  108. {
  109. uint8_t n = dly, v = 0;
  110. SDA_HIGH();
  111. for (uint8_t i = 0; i < 8; i++)
  112. v = (v << 1) | (clock_bit(n) ? 1 : 0);
  113. if (ack) SDA_LOW(); else SDA_HIGH();
  114. clock_bit(n);
  115. SDA_HIGH();
  116. return v;
  117. }
  118. /* Diagnostic: optionally enable weak internal pull-ups, return bit0=SCL, bit1=SDA levels */
  119. uint8_t i2c_lines(uint8_t pullup)
  120. {
  121. if (pullup) PORTD |= (SCL_M | SDA_M); else PORTD &= ~(SCL_M | SDA_M);
  122. DDRD &= ~(SCL_M | SDA_M);
  123. d(); d();
  124. uint8_t p = PIND;
  125. return ((p & SCL_M) ? 1 : 0) | ((p & SDA_M) ? 2 : 0);
  126. }
  127. uint8_t i2c_recover(void)
  128. {
  129. bus_err = false;
  130. for (uint8_t i = 0; i < 9 && !(PIND & SDA_M); i++) {
  131. SCL_LOW(); d(); d();
  132. scl_release(); d(); d();
  133. }
  134. stop();
  135. return (bus_err || !(PIND & SDA_M) || !(PIND & SCL_M)) ? I2C_BUS_ERR : I2C_OK;
  136. }
  137. /* ---- split-phase API (one transaction spread over several USB requests) ---- */
  138. uint8_t i2c_start(uint8_t addr_rw, bool repeated)
  139. {
  140. if (!repeated) {
  141. bus_err = false;
  142. if (!(PIND & SDA_M) || !(PIND & SCL_M)) {
  143. if (i2c_recover() != I2C_OK) return I2C_BUS_ERR;
  144. }
  145. }
  146. start();
  147. bool ack = wr(addr_rw);
  148. if (bus_err) return I2C_BUS_ERR;
  149. return ack ? I2C_OK : I2C_NACK_ADDR;
  150. }
  151. uint8_t i2c_write(uint8_t b)
  152. {
  153. bool ack = wr(b);
  154. if (bus_err) return I2C_BUS_ERR;
  155. return ack ? I2C_OK : I2C_NACK_DATA;
  156. }
  157. uint8_t i2c_read(bool ack) { return rd(ack); }
  158. bool i2c_bus_error(void) { return bus_err; }
  159. void i2c_stop(void) { stop(); }
  160. uint8_t i2c_xfer(uint8_t addr, const uint8_t *w, uint8_t wlen, uint8_t *r, uint8_t rlen)
  161. {
  162. uint8_t st = I2C_OK;
  163. bus_err = false;
  164. if (!(PIND & SDA_M) || !(PIND & SCL_M)) {
  165. if (i2c_recover() != I2C_OK) return I2C_BUS_ERR;
  166. }
  167. if (wlen || !rlen) {
  168. start();
  169. if (!wr(addr << 1)) { st = I2C_NACK_ADDR; goto done; }
  170. for (uint8_t i = 0; i < wlen; i++)
  171. if (!wr(w[i])) { st = I2C_NACK_DATA; goto done; }
  172. }
  173. if (rlen) {
  174. start(); /* (repeated) START */
  175. if (!wr((addr << 1) | 1)) { st = I2C_NACK_ADDR; goto done; }
  176. for (uint8_t i = 0; i < rlen; i++)
  177. r[i] = rd(i < rlen - 1);
  178. }
  179. done:
  180. stop();
  181. return bus_err ? I2C_BUS_ERR : st;
  182. }