diff options
author | James Churchill <pelrun@gmail.com> | 2019-01-23 02:57:13 +1000 |
---|---|---|
committer | Drashna Jaelre <drashna@live.com> | 2019-01-22 08:57:13 -0800 |
commit | 8cf7265f8fae6f94b17c5d78ed41e52b22e2b218 (patch) | |
tree | 232852c96d17fc612a78f788ab8ce713b99c3ffe | |
parent | 127ec5f1e38e9472990f956a4b76192c07508feb (diff) | |
download | qmk_firmware-8cf7265f8fae6f94b17c5d78ed41e52b22e2b218.tar.gz qmk_firmware-8cf7265f8fae6f94b17c5d78ed41e52b22e2b218.zip |
Rename i2c_slave functions so it can coexist with i2c_master (#4875)
Also merges tx/rx buffers, as only one is necessary.
-rwxr-xr-x | drivers/avr/i2c_slave.c | 25 | ||||
-rwxr-xr-x | drivers/avr/i2c_slave.h | 13 | ||||
-rw-r--r-- | keyboards/dc01/arrow/matrix.c | 12 | ||||
-rw-r--r-- | keyboards/dc01/numpad/matrix.c | 12 | ||||
-rw-r--r-- | keyboards/dc01/right/matrix.c | 12 |
5 files changed, 36 insertions, 38 deletions
diff --git a/drivers/avr/i2c_slave.c b/drivers/avr/i2c_slave.c index 27696ca01..18a29a45a 100755 --- a/drivers/avr/i2c_slave.c +++ b/drivers/avr/i2c_slave.c | |||
@@ -9,23 +9,26 @@ | |||
9 | 9 | ||
10 | #include "i2c_slave.h" | 10 | #include "i2c_slave.h" |
11 | 11 | ||
12 | void i2c_init(uint8_t address){ | 12 | volatile uint8_t i2c_slave_reg[I2C_SLAVE_REG_COUNT]; |
13 | |||
14 | static volatile uint8_t buffer_address; | ||
15 | static volatile bool slave_has_register_set = false; | ||
16 | |||
17 | void i2c_slave_init(uint8_t address){ | ||
13 | // load address into TWI address register | 18 | // load address into TWI address register |
14 | TWAR = (address << 1); | 19 | TWAR = (address << 1); |
15 | // set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt | 20 | // set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt |
16 | TWCR = (1 << TWIE) | (1 << TWEA) | (1 << TWINT) | (1 << TWEN); | 21 | TWCR = (1 << TWIE) | (1 << TWEA) | (1 << TWINT) | (1 << TWEN); |
17 | } | 22 | } |
18 | 23 | ||
19 | void i2c_stop(void){ | 24 | void i2c_slave_stop(void){ |
20 | // clear acknowledge and enable bits | 25 | // clear acknowledge and enable bits |
21 | TWCR &= ~((1 << TWEA) | (1 << TWEN)); | 26 | TWCR &= ~((1 << TWEA) | (1 << TWEN)); |
22 | } | 27 | } |
23 | 28 | ||
24 | ISR(TWI_vect){ | 29 | ISR(TWI_vect){ |
25 | uint8_t ack = 1; | 30 | uint8_t ack = 1; |
26 | // temporary stores the received data | 31 | |
27 | //uint8_t data; | ||
28 | |||
29 | switch(TW_STATUS){ | 32 | switch(TW_STATUS){ |
30 | case TW_SR_SLA_ACK: | 33 | case TW_SR_SLA_ACK: |
31 | // The device is now a slave receiver | 34 | // The device is now a slave receiver |
@@ -38,13 +41,13 @@ ISR(TWI_vect){ | |||
38 | if(!slave_has_register_set){ | 41 | if(!slave_has_register_set){ |
39 | buffer_address = TWDR; | 42 | buffer_address = TWDR; |
40 | 43 | ||
41 | if (buffer_address >= RX_BUFFER_SIZE){ // address out of bounds dont ack | 44 | if (buffer_address >= I2C_SLAVE_REG_COUNT) { // address out of bounds dont ack |
42 | ack = 0; | 45 | ack = 0; |
43 | buffer_address = 0; | 46 | buffer_address = 0; |
44 | } | 47 | } |
45 | slave_has_register_set = true; // address has been receaved now fill in buffer | 48 | slave_has_register_set = true; // address has been receaved now fill in buffer |
46 | } else { | 49 | } else { |
47 | rxbuffer[buffer_address] = TWDR; | 50 | i2c_slave_reg[buffer_address] = TWDR; |
48 | buffer_address++; | 51 | buffer_address++; |
49 | } | 52 | } |
50 | break; | 53 | break; |
@@ -52,7 +55,7 @@ ISR(TWI_vect){ | |||
52 | case TW_ST_SLA_ACK: | 55 | case TW_ST_SLA_ACK: |
53 | case TW_ST_DATA_ACK: | 56 | case TW_ST_DATA_ACK: |
54 | // This device is a slave transmitter and master has requested data | 57 | // This device is a slave transmitter and master has requested data |
55 | TWDR = txbuffer[buffer_address]; | 58 | TWDR = i2c_slave_reg[buffer_address]; |
56 | buffer_address++; | 59 | buffer_address++; |
57 | break; | 60 | break; |
58 | 61 | ||
@@ -63,6 +66,6 @@ ISR(TWI_vect){ | |||
63 | break; | 66 | break; |
64 | } | 67 | } |
65 | 68 | ||
66 | // Reset i2c state mahcine to be ready for next interrupt | 69 | // Reset i2c state machine to be ready for next interrupt |
67 | TWCR |= (1 << TWIE) | (1 << TWINT) | (ack << TWEA) | (1 << TWEN); | 70 | TWCR |= (1 << TWIE) | (1 << TWINT) | (ack << TWEA) | (1 << TWEN); |
68 | } \ No newline at end of file | 71 | } \ No newline at end of file |
diff --git a/drivers/avr/i2c_slave.h b/drivers/avr/i2c_slave.h index 1c3b9ecc0..7b5dcbdc3 100755 --- a/drivers/avr/i2c_slave.h +++ b/drivers/avr/i2c_slave.h | |||
@@ -8,16 +8,11 @@ | |||
8 | #ifndef I2C_SLAVE_H | 8 | #ifndef I2C_SLAVE_H |
9 | #define I2C_SLAVE_H | 9 | #define I2C_SLAVE_H |
10 | 10 | ||
11 | #define TX_BUFFER_SIZE 30 | 11 | #define I2C_SLAVE_REG_COUNT 30 |
12 | #define RX_BUFFER_SIZE 30 | ||
13 | 12 | ||
14 | volatile uint8_t buffer_address; | 13 | extern volatile uint8_t i2c_slave_reg[I2C_SLAVE_REG_COUNT]; |
15 | static volatile bool slave_has_register_set = false; | ||
16 | volatile uint8_t txbuffer[TX_BUFFER_SIZE]; | ||
17 | volatile uint8_t rxbuffer[RX_BUFFER_SIZE]; | ||
18 | 14 | ||
19 | void i2c_init(uint8_t address); | 15 | void i2c_slave_init(uint8_t address); |
20 | void i2c_stop(void); | 16 | void i2c_slave_stop(void); |
21 | ISR(TWI_vect); | ||
22 | 17 | ||
23 | #endif // I2C_SLAVE_H \ No newline at end of file | 18 | #endif // I2C_SLAVE_H \ No newline at end of file |
diff --git a/keyboards/dc01/arrow/matrix.c b/keyboards/dc01/arrow/matrix.c index 68abb6791..85591f602 100644 --- a/keyboards/dc01/arrow/matrix.c +++ b/keyboards/dc01/arrow/matrix.c | |||
@@ -195,14 +195,14 @@ uint8_t matrix_scan(void) | |||
195 | debouncing = false; | 195 | debouncing = false; |
196 | } | 196 | } |
197 | # endif | 197 | # endif |
198 | 198 | ||
199 | if (USB_DeviceState != DEVICE_STATE_Configured){ | 199 | if (USB_DeviceState != DEVICE_STATE_Configured){ |
200 | txbuffer[1] = 0x55; | 200 | i2c_slave_reg[1] = 0x55; |
201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ | 201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ |
202 | txbuffer[i+2] = matrix[i]; //send matrix over i2c | 202 | i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c |
203 | } | 203 | } |
204 | } | 204 | } |
205 | 205 | ||
206 | matrix_scan_quantum(); | 206 | matrix_scan_quantum(); |
207 | return 1; | 207 | return 1; |
208 | } | 208 | } |
@@ -396,9 +396,9 @@ static void unselect_cols(void) | |||
396 | 396 | ||
397 | //this replases tmk code | 397 | //this replases tmk code |
398 | void matrix_setup(void){ | 398 | void matrix_setup(void){ |
399 | 399 | ||
400 | if (USB_DeviceState != DEVICE_STATE_Configured){ | 400 | if (USB_DeviceState != DEVICE_STATE_Configured){ |
401 | i2c_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c | 401 | i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c |
402 | sei(); //enable interupts | 402 | sei(); //enable interupts |
403 | } | 403 | } |
404 | } \ No newline at end of file | 404 | } \ No newline at end of file |
diff --git a/keyboards/dc01/numpad/matrix.c b/keyboards/dc01/numpad/matrix.c index f9a9a7f63..39637241d 100644 --- a/keyboards/dc01/numpad/matrix.c +++ b/keyboards/dc01/numpad/matrix.c | |||
@@ -195,14 +195,14 @@ uint8_t matrix_scan(void) | |||
195 | debouncing = false; | 195 | debouncing = false; |
196 | } | 196 | } |
197 | # endif | 197 | # endif |
198 | 198 | ||
199 | if (USB_DeviceState != DEVICE_STATE_Configured){ | 199 | if (USB_DeviceState != DEVICE_STATE_Configured){ |
200 | txbuffer[1] = 0x55; | 200 | i2c_slave_reg[1] = 0x55; |
201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ | 201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ |
202 | txbuffer[i+2] = matrix[i]; //send matrix over i2c | 202 | i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c |
203 | } | 203 | } |
204 | } | 204 | } |
205 | 205 | ||
206 | matrix_scan_quantum(); | 206 | matrix_scan_quantum(); |
207 | return 1; | 207 | return 1; |
208 | } | 208 | } |
@@ -396,9 +396,9 @@ static void unselect_cols(void) | |||
396 | 396 | ||
397 | //this replases tmk code | 397 | //this replases tmk code |
398 | void matrix_setup(void){ | 398 | void matrix_setup(void){ |
399 | 399 | ||
400 | if (USB_DeviceState != DEVICE_STATE_Configured){ | 400 | if (USB_DeviceState != DEVICE_STATE_Configured){ |
401 | i2c_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c | 401 | i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c |
402 | sei(); //enable interupts | 402 | sei(); //enable interupts |
403 | } | 403 | } |
404 | } \ No newline at end of file | 404 | } \ No newline at end of file |
diff --git a/keyboards/dc01/right/matrix.c b/keyboards/dc01/right/matrix.c index aa2e880d0..50fe19b8d 100644 --- a/keyboards/dc01/right/matrix.c +++ b/keyboards/dc01/right/matrix.c | |||
@@ -195,14 +195,14 @@ uint8_t matrix_scan(void) | |||
195 | debouncing = false; | 195 | debouncing = false; |
196 | } | 196 | } |
197 | # endif | 197 | # endif |
198 | 198 | ||
199 | if (USB_DeviceState != DEVICE_STATE_Configured){ | 199 | if (USB_DeviceState != DEVICE_STATE_Configured){ |
200 | txbuffer[1] = 0x55; | 200 | i2c_slave_reg[1] = 0x55; |
201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ | 201 | for (uint8_t i = 0; i < MATRIX_ROWS; i++){ |
202 | txbuffer[i+2] = matrix[i]; //send matrix over i2c | 202 | i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c |
203 | } | 203 | } |
204 | } | 204 | } |
205 | 205 | ||
206 | matrix_scan_quantum(); | 206 | matrix_scan_quantum(); |
207 | return 1; | 207 | return 1; |
208 | } | 208 | } |
@@ -396,9 +396,9 @@ static void unselect_cols(void) | |||
396 | 396 | ||
397 | //this replases tmk code | 397 | //this replases tmk code |
398 | void matrix_setup(void){ | 398 | void matrix_setup(void){ |
399 | 399 | ||
400 | if (USB_DeviceState != DEVICE_STATE_Configured){ | 400 | if (USB_DeviceState != DEVICE_STATE_Configured){ |
401 | i2c_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c | 401 | i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c |
402 | sei(); //enable interupts | 402 | sei(); //enable interupts |
403 | } | 403 | } |
404 | } \ No newline at end of file | 404 | } \ No newline at end of file |