twi.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #include <avr/io.h>
  2. #include <util/twi.h>
  3. #include <avr/interrupt.h>
  4. #include <stdbool.h>
  5. #include <stdlib.h>
  6. #include "twi.h"
  7. #ifndef F_SCL
  8. #define F_SCL 100000UL // SCL frequency
  9. #endif
  10. #ifndef PRESCALER
  11. #define PRESCALER 1
  12. #endif
  13. // Limits the amount of we wait for any one i2c transaction.
  14. // Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
  15. // 9 bits, a single transaction will take around 90μs to complete.
  16. //
  17. // (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit
  18. // poll loop takes at least 8 clock cycles to execute
  19. #ifdef TWI_TIMEOUT
  20. #ifndef TWI_TX_SIZE
  21. #define TWI_TX_SIZE 9
  22. #endif
  23. #endif
  24. // Wait for an twi operation to finish
  25. inline static
  26. void twi_wait(uint8_t status) {
  27. #ifdef TWI_TIMEOUT
  28. uint16_t lim = 0;
  29. while ( !(TWCR & (_BV(status))) && lim < (TWI_TX_SIZE+1)*(F_CPU/F_SCL)/8)
  30. lim++;
  31. #else
  32. while ( !(TWCR & (_BV(status))) );
  33. #endif
  34. }
  35. void twi_master_init(void) {
  36. TWBR = (uint8_t) ((((F_CPU / F_SCL) / PRESCALER) - 16 ) / 2);
  37. }
  38. void twi_slave_init(uint8_t address) {
  39. TWAR = address << 0; // slave i2c address
  40. TWCR = _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWEN);
  41. }
  42. uint8_t twi_master_start(uint8_t address) {
  43. // reset TWI control register and transmit start
  44. TWCR = 0;
  45. TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTA);
  46. twi_wait(TWINT);
  47. // check if the start condition was successfully transmitted
  48. if (TW_STATUS != TW_START)
  49. return 1;
  50. // set slave address and transmit
  51. TWDR = address;
  52. TWCR = _BV(TWINT) | _BV(TWEN);
  53. twi_wait(TWINT);
  54. // check if the device has acknowledged the READ / WRITE mode
  55. if ( (TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MR_SLA_ACK) )
  56. return 1;
  57. return 0;
  58. }
  59. void twi_master_stop(void) {
  60. // transmit STOP condition
  61. TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWSTO);
  62. twi_wait(TWSTO);
  63. }
  64. uint8_t twi_write(uint8_t data) {
  65. // load data into data register and start transmission of data
  66. TWDR = data;
  67. TWCR = _BV(TWINT) | _BV(TWEN);
  68. twi_wait(TWINT);
  69. if ( TW_STATUS != TW_MT_DATA_ACK )
  70. return 1;
  71. return 0;
  72. }
  73. uint8_t twi_read(bool ack) {
  74. // start TWI module and acknowledge data after reception
  75. if (ack)
  76. TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
  77. else
  78. TWCR = _BV(TWINT) | _BV(TWEN);
  79. twi_wait(TWINT);
  80. return TWDR;
  81. }
  82. uint8_t twi_transmit(uint8_t address, uint8_t* data, uint16_t length) {
  83. if (twi_start(address | TW_WRITE))
  84. return 1;
  85. for (uint16_t i = 0; i < length; i++) {
  86. if (twi_write(data[i]))
  87. return 1;
  88. }
  89. twi_stop();
  90. return 0;
  91. }
  92. uint8_t twi_receive(uint8_t address, uint8_t* data, uint16_t length) {
  93. if (twi_start(address | TW_READ))
  94. return 1;
  95. for (uint16_t i = 0; i < (length-1); i++) {
  96. data[i] = twi_read(true);
  97. }
  98. data[(length-1)] = twi_read(false);
  99. twi_stop();
  100. return 0;
  101. }
  102. uint8_t twi_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length) {
  103. if (twi_start(devaddr | TW_WRITE))
  104. return 1;
  105. twi_write(regaddr);
  106. for (uint16_t i = 0; i < length; i++) {
  107. if (twi_write(data[i]))
  108. return 1;
  109. }
  110. twi_stop();
  111. return 0;
  112. }
  113. uint8_t twi_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
  114. {
  115. if (twi_start(devaddr))
  116. return 1;
  117. twi_write(regaddr);
  118. if (twi_start(devaddr | TW_READ))
  119. return 1;
  120. for (uint16_t i = 0; i < (length-1); i++) {
  121. data[i] = twi_read(true);
  122. }
  123. data[(length-1)] = twi_read(false);
  124. twi_stop();
  125. return 0;
  126. }
  127. uint8_t buffer_address = 0;
  128. ISR(TWI_vect) {
  129. uint8_t data;
  130. switch (TW_STATUS) {
  131. case TW_SR_SLA_ACK:
  132. buffer_address = 0xff;
  133. TWCR |= _BV(TWIE) | _BV(TWINT) | _BV(TWEA) | _BV(TWEN);
  134. break;
  135. case TW_SR_DATA_ACK:
  136. data = TWDR;
  137. if (buffer_address == 0xff) {
  138. // store address to read from later
  139. buffer_address = data;
  140. TWCR |= _BV(TWIE) | _BV(TWINT) | _BV(TWEA) | _BV(TWEN);
  141. } else {
  142. // store data from address and increment
  143. rxbuffer[buffer_address] = data;
  144. buffer_address++;
  145. if (buffer_address < 0xFF) {
  146. // ack
  147. TWCR |= _BV(TWIE) | _BV(TWINT) | _BV(TWEA) | _BV(TWEN);
  148. } else {
  149. // nack
  150. TWCR &= ~_BV(TWEA);
  151. TWCR |= _BV(TWIE) | _BV(TWINT) | _BV(TWEN);
  152. }
  153. }
  154. break;
  155. case TW_ST_SLA_ACK:
  156. case TW_ST_DATA_ACK:
  157. data = TWDR;
  158. if (buffer_address == 0xFF) {
  159. buffer_address = data;
  160. }
  161. TWDR = txbuffer[buffer_address];
  162. buffer_address++;
  163. if (buffer_address < 0xFF) {
  164. // ack
  165. TWCR |= _BV(TWIE) | _BV(TWINT) | _BV(TWEA) | _BV(TWEN);
  166. } else {
  167. // nack
  168. TWCR &= ~_BV(TWEA);
  169. TWCR |= _BV(TWIE) | _BV(TWINT) | _BV(TWEN);
  170. }
  171. break;
  172. case TW_BUS_ERROR:
  173. TWCR = 0;
  174. break;
  175. default:
  176. TWCR |= _BV(TWIE) | _BV(TWEA) | _BV(TWEN);
  177. break;
  178. }
  179. }