i2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <util/twi.h>
  2. #include <avr/io.h>
  3. #include <stdlib.h>
  4. #include <avr/interrupt.h>
  5. #include <util/twi.h>
  6. #include <stdbool.h>
  7. #include "i2c.h"
  8. #include "split_flags.h"
  9. #if defined(USE_I2C) || defined(EH)
  10. // Limits the amount of we wait for any one i2c transaction.
  11. // Since were running SCL line 100kHz (=> 10μs/bit), and each transactions is
  12. // 9 bits, a single transaction will take around 90μs to complete.
  13. //
  14. // (F_CPU/SCL_CLOCK) => # of μC cycles to transfer a bit
  15. // poll loop takes at least 8 clock cycles to execute
  16. #define I2C_LOOP_TIMEOUT (9+1)*(F_CPU/SCL_CLOCK)/8
  17. #define BUFFER_POS_INC() (slave_buffer_pos = (slave_buffer_pos+1)%SLAVE_BUFFER_SIZE)
  18. volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
  19. static volatile uint8_t slave_buffer_pos;
  20. static volatile bool slave_has_register_set = false;
  21. // Wait for an i2c operation to finish
  22. inline static
  23. void i2c_delay(void) {
  24. uint16_t lim = 0;
  25. while(!(TWCR & (1<<TWINT)) && lim < I2C_LOOP_TIMEOUT)
  26. lim++;
  27. // easier way, but will wait slightly longer
  28. // _delay_us(100);
  29. }
  30. // Setup twi to run at 100kHz
  31. void i2c_master_init(void) {
  32. // no prescaler
  33. TWSR = 0;
  34. // Set TWI clock frequency to SCL_CLOCK. Need TWBR>10.
  35. // Check datasheets for more info.
  36. TWBR = ((F_CPU/SCL_CLOCK)-16)/2;
  37. }
  38. // Start a transaction with the given i2c slave address. The direction of the
  39. // transfer is set with I2C_READ and I2C_WRITE.
  40. // returns: 0 => success
  41. // 1 => error
  42. uint8_t i2c_master_start(uint8_t address) {
  43. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTA);
  44. i2c_delay();
  45. // check that we started successfully
  46. if ( (TW_STATUS != TW_START) && (TW_STATUS != TW_REP_START))
  47. return 1;
  48. TWDR = address;
  49. TWCR = (1<<TWINT) | (1<<TWEN);
  50. i2c_delay();
  51. if ( (TW_STATUS != TW_MT_SLA_ACK) && (TW_STATUS != TW_MR_SLA_ACK) )
  52. return 1; // slave did not acknowledge
  53. else
  54. return 0; // success
  55. }
  56. // Finish the i2c transaction.
  57. void i2c_master_stop(void) {
  58. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  59. uint16_t lim = 0;
  60. while(!(TWCR & (1<<TWSTO)) && lim < I2C_LOOP_TIMEOUT)
  61. lim++;
  62. }
  63. // Write one byte to the i2c slave.
  64. // returns 0 => slave ACK
  65. // 1 => slave NACK
  66. uint8_t i2c_master_write(uint8_t data) {
  67. TWDR = data;
  68. TWCR = (1<<TWINT) | (1<<TWEN);
  69. i2c_delay();
  70. // check if the slave acknowledged us
  71. return (TW_STATUS == TW_MT_DATA_ACK) ? 0 : 1;
  72. }
  73. uint8_t i2c_master_write_data(void *const TXdata, uint8_t dataLen) {
  74. uint8_t *data = (uint8_t *)TXdata;
  75. int err = 0;
  76. for (int i = 0; i < dataLen; i++) {
  77. err = i2c_master_write(data[i]);
  78. if ( err )
  79. return err;
  80. }
  81. return err;
  82. }
  83. // Read one byte from the i2c slave. If ack=1 the slave is acknowledged,
  84. // if ack=0 the acknowledge bit is not set.
  85. // returns: byte read from i2c device
  86. uint8_t i2c_master_read(int ack) {
  87. TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
  88. i2c_delay();
  89. return TWDR;
  90. }
  91. void i2c_reset_state(void) {
  92. TWCR = 0;
  93. }
  94. void i2c_slave_init(uint8_t address) {
  95. TWAR = address << 0; // slave i2c address
  96. // TWEN - twi enable
  97. // TWEA - enable address acknowledgement
  98. // TWINT - twi interrupt flag
  99. // TWIE - enable the twi interrupt
  100. TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
  101. }
  102. ISR(TWI_vect);
  103. ISR(TWI_vect) {
  104. uint8_t ack = 1;
  105. switch(TW_STATUS) {
  106. case TW_SR_SLA_ACK:
  107. // this device has been addressed as a slave receiver
  108. slave_has_register_set = false;
  109. break;
  110. case TW_SR_DATA_ACK:
  111. // this device has received data as a slave receiver
  112. // The first byte that we receive in this transaction sets the location
  113. // of the read/write location of the slaves memory that it exposes over
  114. // i2c. After that, bytes will be written at slave_buffer_pos, incrementing
  115. // slave_buffer_pos after each write.
  116. if(!slave_has_register_set) {
  117. slave_buffer_pos = TWDR;
  118. // don't acknowledge the master if this memory loctaion is out of bounds
  119. if ( slave_buffer_pos >= SLAVE_BUFFER_SIZE ) {
  120. ack = 0;
  121. slave_buffer_pos = 0;
  122. }
  123. slave_has_register_set = true;
  124. } else {
  125. i2c_slave_buffer[slave_buffer_pos] = TWDR;
  126. if ( slave_buffer_pos == I2C_BACKLIT_START) {
  127. BACKLIT_DIRTY = true;
  128. } else if ( slave_buffer_pos == (I2C_RGB_START+3)) {
  129. RGB_DIRTY = true;
  130. }
  131. BUFFER_POS_INC();
  132. }
  133. break;
  134. case TW_ST_SLA_ACK:
  135. case TW_ST_DATA_ACK:
  136. // master has addressed this device as a slave transmitter and is
  137. // requesting data.
  138. TWDR = i2c_slave_buffer[slave_buffer_pos];
  139. BUFFER_POS_INC();
  140. break;
  141. case TW_BUS_ERROR: // something went wrong, reset twi state
  142. TWCR = 0;
  143. default:
  144. break;
  145. }
  146. // Reset everything, so we are ready for the next TWI interrupt
  147. TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
  148. }
  149. #endif