aboutsummaryrefslogtreecommitdiff
path: root/quantum/split_common/i2c.c
diff options
context:
space:
mode:
authorThat-Canadian <Poole.Chris.11@gmail.com>2018-07-16 19:25:02 -0700
committerJack Humbert <jack.humb@gmail.com>2018-07-16 22:25:02 -0400
commit0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c (patch)
tree15a411d5ed6ad203982337448cfde11ed26ce7b7 /quantum/split_common/i2c.c
parentb2877470ced1deb9651ecb39f6a82f5ef380b399 (diff)
downloadqmk_firmware-0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c.tar.gz
qmk_firmware-0fab3bbde33f82301a8c5e177c3c0ceb7ad2219c.zip
Lets split eh (#3120)
* Line ending stuff again * Added Let's Split Eh? Files and updated #USE_IC2 checks to also include th EH revision (can only be used in I2C) * Added personal keymap, updated some of the EH files * Created new keyboard file for testing "lets_split_eh" will merge into lets_split once fully functional * Added split code from lets_split, removed pro micro imports and LED code THIS IS WORKING CODE, WITHOUT RGB AND BACKLIGHT * Took back original Lets Slit files for the lets_split keyboard, working in the lets_split_eh folder for now * Updated eh.c * More rework of the I2C code, added global flags for split boards. * Introduced RGB over I2C, having weird edge case issues at the moment though * Fixed weird I2C edgecase with RGB, although still would like to track down route cause.. * Changed RGB keycodes (static ones) to activate on key-up instead of key-down to elimate weird ghosting issue over I2C * Lots of changes, mainly externalized the Split keyboard code and added logic for only including when needed. - Added makefile option "SPLIT_KEYBOARD" that when = yes will include the split keyboard files and custom matrix - Split keyboard files placed into quantum/split_common/ - Added define option for config files "SPLIT_HAND_PIN" FOr using high/low pin to determine handedness, low = right hand, high = left hand - Cleaned up split logic for RGB and Backlight so it is only exectuted / included when needed * Updated documentation for the new makefile options and #defines specific to split keyboards * Added a bit more info to docs, so people aren't confused * Modifed Let's Split to use externalized code, also added left and right hand eeprom files to the split_common folder * Removed some debugging from eh.c * Small changes to keyboard configs. Also added a default keymap (just a copy of my that_canadian keymap). * Added a README file to the Let's Split Eh? * Changed it so RGB static updates are done on key-up ONLY for split boards rather than all boards. Also fixed leftover un-used variable in rgblight.c * Updated default keymap and my keymap for Let's Split Eh? Updated the comments so it reflects RGB control, and removed audio functions. * Fixed lets_split_eh not having a default version * Removed "eh" references from lets_split folder for now * Took lets_split folder from master to fix travis build errors, weird my local was overriding. * Changed LAYOUT_ortho_4x12_kc -> LAYOUT_kc_ortho_4x12 to match bakingpy and others * Removed rules.mk from my lets_split keymap, not needed * Updated the config_options doc to better explain the usage of "#define SPLIT_HAND_PIN"
Diffstat (limited to 'quantum/split_common/i2c.c')
-rw-r--r--quantum/split_common/i2c.c187
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
22volatile uint8_t i2c_slave_buffer[SLAVE_BUFFER_SIZE];
23
24static volatile uint8_t slave_buffer_pos;
25static volatile bool slave_has_register_set = false;
26
27// Wait for an i2c operation to finish
28inline static
29void 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
39void 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
51uint8_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.
73void 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
84uint8_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
94uint8_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
113uint8_t i2c_master_read(int ack) {
114 TWCR = (1<<TWINT) | (1<<TWEN) | (ack<<TWEA);
115
116 i2c_delay();
117 return TWDR;
118}
119
120void i2c_reset_state(void) {
121 TWCR = 0;
122}
123
124void 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
133ISR(TWI_vect);
134
135ISR(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