keyboard layout for qmk firmware
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.3 KiB

  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. #ifdef USE_I2C
  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. // Read one byte from the i2c slave. If ack=1 the slave is acknowledged,
  73. // if ack=0 the acknowledge bit is not set.
  74. // returns: byte read from i2c device
  75. uint8_t i2c_master_read(int ack) {
  76. TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
  77. i2c_delay();
  78. return TWDR;
  79. }
  80. void i2c_reset_state(void) {
  81. TWCR = 0;
  82. }
  83. void i2c_slave_init(uint8_t address) {
  84. TWAR = address << 0; // slave i2c address
  85. // TWEN - twi enable
  86. // TWEA - enable address acknowledgement
  87. // TWINT - twi interrupt flag
  88. // TWIE - enable the twi interrupt
  89. TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
  90. }
  91. ISR(TWI_vect);
  92. ISR(TWI_vect) {
  93. uint8_t ack = 1;
  94. switch(TW_STATUS) {
  95. case TW_SR_SLA_ACK:
  96. // this device has been addressed as a slave receiver
  97. slave_has_register_set = false;
  98. break;
  99. case TW_SR_DATA_ACK:
  100. // this device has received data as a slave receiver
  101. // The first byte that we receive in this transaction sets the location
  102. // of the read/write location of the slaves memory that it exposes over
  103. // i2c. After that, bytes will be written at slave_buffer_pos, incrementing
  104. // slave_buffer_pos after each write.
  105. if(!slave_has_register_set) {
  106. slave_buffer_pos = TWDR;
  107. // don't acknowledge the master if this memory loctaion is out of bounds
  108. if ( slave_buffer_pos >= SLAVE_BUFFER_SIZE ) {
  109. ack = 0;
  110. slave_buffer_pos = 0;
  111. }
  112. slave_has_register_set = true;
  113. } else {
  114. i2c_slave_buffer[slave_buffer_pos] = TWDR;
  115. BUFFER_POS_INC();
  116. }
  117. break;
  118. case TW_ST_SLA_ACK:
  119. case TW_ST_DATA_ACK:
  120. // master has addressed this device as a slave transmitter and is
  121. // requesting data.
  122. TWDR = i2c_slave_buffer[slave_buffer_pos];
  123. BUFFER_POS_INC();
  124. break;
  125. case TW_BUS_ERROR: // something went wrong, reset twi state
  126. TWCR = 0;
  127. default:
  128. break;
  129. }
  130. // Reset everything, so we are ready for the next TWI interrupt
  131. TWCR |= (1<<TWIE) | (1<<TWINT) | (ack<<TWEA) | (1<<TWEN);
  132. }
  133. #endif