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 /drivers | |
| 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.
Diffstat (limited to 'drivers')
| -rwxr-xr-x | drivers/avr/i2c_slave.c | 25 | ||||
| -rwxr-xr-x | drivers/avr/i2c_slave.h | 13 |
2 files changed, 18 insertions, 20 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 |
