diff options
| author | yiancar <yiangosyiangou@cytanet.com.cy> | 2018-07-12 11:50:55 +0300 |
|---|---|---|
| committer | Jack Humbert <jack.humb@gmail.com> | 2018-07-15 13:00:50 -0400 |
| commit | 60797c8439f472c68f374acc11337e362a8836eb (patch) | |
| tree | 85fc762a7ed6ce50043f9796c5170b95f14eb33c | |
| parent | 3d9fda3629c09819f2b7c07c68e3504bdf4cd0d3 (diff) | |
| download | qmk_firmware-60797c8439f472c68f374acc11337e362a8836eb.tar.gz qmk_firmware-60797c8439f472c68f374acc11337e362a8836eb.zip | |
Add I2C slave library
- Add I2C slave library for general use.
| -rwxr-xr-x | drivers/avr/i2c_slave.c | 100 | ||||
| -rwxr-xr-x | drivers/avr/i2c_slave.h | 19 |
2 files changed, 119 insertions, 0 deletions
diff --git a/drivers/avr/i2c_slave.c b/drivers/avr/i2c_slave.c new file mode 100755 index 000000000..3edf85b12 --- /dev/null +++ b/drivers/avr/i2c_slave.c | |||
| @@ -0,0 +1,100 @@ | |||
| 1 | /* Library made by: g4lvanix | ||
| 2 | * Github repository: https://github.com/g4lvanix/I2C-slave-lib | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include <avr/io.h> | ||
| 6 | #include <util/twi.h> | ||
| 7 | #include <avr/interrupt.h> | ||
| 8 | |||
| 9 | #include "i2c_slave.h" | ||
| 10 | |||
| 11 | void i2c_init(uint8_t address){ | ||
| 12 | // load address into TWI address register | ||
| 13 | TWAR = (address << 1); | ||
| 14 | // set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt | ||
| 15 | TWCR = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN); | ||
| 16 | } | ||
| 17 | |||
| 18 | void i2c_stop(void){ | ||
| 19 | // clear acknowledge and enable bits | ||
| 20 | TWCR &= ~( (1<<TWEA) | (1<<TWEN) ); | ||
| 21 | } | ||
| 22 | |||
| 23 | ISR(TWI_vect){ | ||
| 24 | |||
| 25 | // temporary stores the received data | ||
| 26 | uint8_t data; | ||
| 27 | |||
| 28 | // own address has been acknowledged | ||
| 29 | if( (TWSR & 0xF8) == TW_SR_SLA_ACK ){ | ||
| 30 | buffer_address = 0xFF; | ||
| 31 | // clear TWI interrupt flag, prepare to receive next byte and acknowledge | ||
| 32 | TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN); | ||
| 33 | } | ||
| 34 | else if( (TWSR & 0xF8) == TW_SR_DATA_ACK ){ // data has been received in slave receiver mode | ||
| 35 | |||
| 36 | // save the received byte inside data | ||
| 37 | data = TWDR; | ||
| 38 | |||
| 39 | // check wether an address has already been transmitted or not | ||
| 40 | if(buffer_address == 0xFF){ | ||
| 41 | |||
| 42 | buffer_address = data; | ||
| 43 | |||
| 44 | // clear TWI interrupt flag, prepare to receive next byte and acknowledge | ||
| 45 | TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN); | ||
| 46 | } | ||
| 47 | else{ // if a databyte has already been received | ||
| 48 | |||
| 49 | // store the data at the current address | ||
| 50 | rxbuffer[buffer_address] = data; | ||
| 51 | |||
| 52 | // increment the buffer address | ||
| 53 | buffer_address++; | ||
| 54 | |||
| 55 | // if there is still enough space inside the buffer | ||
| 56 | if(buffer_address < 0xFF){ | ||
| 57 | // clear TWI interrupt flag, prepare to receive next byte and acknowledge | ||
| 58 | TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN); | ||
| 59 | } | ||
| 60 | else{ | ||
| 61 | // Don't acknowledge | ||
| 62 | TWCR &= ~(1<<TWEA); | ||
| 63 | // clear TWI interrupt flag, prepare to receive last byte. | ||
| 64 | TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEN); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | else if( (TWSR & 0xF8) == TW_ST_DATA_ACK ){ // device has been addressed to be a transmitter | ||
| 69 | |||
| 70 | // copy data from TWDR to the temporary memory | ||
| 71 | data = TWDR; | ||
| 72 | |||
| 73 | // if no buffer read address has been sent yet | ||
| 74 | if( buffer_address == 0xFF ){ | ||
| 75 | buffer_address = data; | ||
| 76 | } | ||
| 77 | |||
| 78 | // copy the specified buffer address into the TWDR register for transmission | ||
| 79 | TWDR = txbuffer[buffer_address]; | ||
| 80 | // increment buffer read address | ||
| 81 | buffer_address++; | ||
| 82 | |||
| 83 | // if there is another buffer address that can be sent | ||
| 84 | if(buffer_address < 0xFF){ | ||
| 85 | // clear TWI interrupt flag, prepare to send next byte and receive acknowledge | ||
| 86 | TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN); | ||
| 87 | } | ||
| 88 | else{ | ||
| 89 | // Don't acknowledge | ||
| 90 | TWCR &= ~(1<<TWEA); | ||
| 91 | // clear TWI interrupt flag, prepare to receive last byte. | ||
| 92 | TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEN); | ||
| 93 | } | ||
| 94 | |||
| 95 | } | ||
| 96 | else{ | ||
| 97 | // if none of the above apply prepare TWI to be addressed again | ||
| 98 | TWCR |= (1<<TWIE) | (1<<TWEA) | (1<<TWEN); | ||
| 99 | } | ||
| 100 | } | ||
diff --git a/drivers/avr/i2c_slave.h b/drivers/avr/i2c_slave.h new file mode 100755 index 000000000..3fda7f8c0 --- /dev/null +++ b/drivers/avr/i2c_slave.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | /* Library made by: g4lvanix | ||
| 2 | * Github repository: https://github.com/g4lvanix/I2C-slave-lib | ||
| 3 | |||
| 4 | Info: Inititate the library by giving the required address. | ||
| 5 | Read or write to the necessary buffer according to the opperation. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #ifndef I2C_SLAVE_H | ||
| 9 | #define I2C_SLAVE_H | ||
| 10 | |||
| 11 | volatile uint8_t buffer_address; | ||
| 12 | volatile uint8_t txbuffer[0xFF]; | ||
| 13 | volatile uint8_t rxbuffer[0xFF]; | ||
| 14 | |||
| 15 | void i2c_init(uint8_t address); | ||
| 16 | void i2c_stop(void); | ||
| 17 | ISR(TWI_vect); | ||
| 18 | |||
| 19 | #endif // I2C_SLAVE_H | ||
