i2c.c 4.8 KB

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