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