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