2019-07-15 23:32:13 +06:00
|
|
|
/* Copyright (C) 2019 Elia Ritterbusch
|
|
|
|
+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-07-12 14:50:55 +06:00
|
|
|
/* Library made by: g4lvanix
|
|
|
|
* Github repository: https://github.com/g4lvanix/I2C-slave-lib
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <util/twi.h>
|
|
|
|
#include <avr/interrupt.h>
|
2018-07-18 22:55:57 +06:00
|
|
|
#include <stdbool.h>
|
2018-07-12 14:50:55 +06:00
|
|
|
|
|
|
|
#include "i2c_slave.h"
|
|
|
|
|
2019-01-22 22:57:13 +06:00
|
|
|
volatile uint8_t i2c_slave_reg[I2C_SLAVE_REG_COUNT];
|
|
|
|
|
|
|
|
static volatile uint8_t buffer_address;
|
2019-08-31 00:19:03 +06:00
|
|
|
static volatile bool slave_has_register_set = false;
|
2019-01-22 22:57:13 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
void i2c_slave_init(uint8_t address) {
|
2018-07-18 22:55:57 +06:00
|
|
|
// load address into TWI address register
|
2019-03-12 23:23:28 +06:00
|
|
|
TWAR = address;
|
2018-07-18 22:55:57 +06:00
|
|
|
// set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt
|
|
|
|
TWCR = (1 << TWIE) | (1 << TWEA) | (1 << TWINT) | (1 << TWEN);
|
2018-07-12 14:50:55 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
void i2c_slave_stop(void) {
|
2018-07-18 22:55:57 +06:00
|
|
|
// clear acknowledge and enable bits
|
|
|
|
TWCR &= ~((1 << TWEA) | (1 << TWEN));
|
2018-07-12 14:50:55 +06:00
|
|
|
}
|
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
ISR(TWI_vect) {
|
2018-07-18 22:55:57 +06:00
|
|
|
uint8_t ack = 1;
|
2019-01-22 22:57:13 +06:00
|
|
|
|
2019-08-31 00:19:03 +06:00
|
|
|
switch (TW_STATUS) {
|
2018-07-18 22:55:57 +06:00
|
|
|
case TW_SR_SLA_ACK:
|
|
|
|
// The device is now a slave receiver
|
|
|
|
slave_has_register_set = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TW_SR_DATA_ACK:
|
|
|
|
// This device is a slave receiver and has received data
|
|
|
|
// First byte is the location then the bytes will be writen in buffer with auto-incriment
|
2019-08-31 00:19:03 +06:00
|
|
|
if (!slave_has_register_set) {
|
2018-07-18 22:55:57 +06:00
|
|
|
buffer_address = TWDR;
|
|
|
|
|
2019-01-22 22:57:13 +06:00
|
|
|
if (buffer_address >= I2C_SLAVE_REG_COUNT) { // address out of bounds dont ack
|
2019-08-31 00:19:03 +06:00
|
|
|
ack = 0;
|
|
|
|
buffer_address = 0;
|
2018-07-18 22:55:57 +06:00
|
|
|
}
|
2019-08-31 00:19:03 +06:00
|
|
|
slave_has_register_set = true; // address has been receaved now fill in buffer
|
2018-07-18 22:55:57 +06:00
|
|
|
} else {
|
2019-01-22 22:57:13 +06:00
|
|
|
i2c_slave_reg[buffer_address] = TWDR;
|
2018-07-18 22:55:57 +06:00
|
|
|
buffer_address++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TW_ST_SLA_ACK:
|
|
|
|
case TW_ST_DATA_ACK:
|
|
|
|
// This device is a slave transmitter and master has requested data
|
2019-01-22 22:57:13 +06:00
|
|
|
TWDR = i2c_slave_reg[buffer_address];
|
2018-07-18 22:55:57 +06:00
|
|
|
buffer_address++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TW_BUS_ERROR:
|
|
|
|
// We got an error, reset i2c
|
|
|
|
TWCR = 0;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-01-22 22:57:13 +06:00
|
|
|
// Reset i2c state machine to be ready for next interrupt
|
2018-07-18 22:55:57 +06:00
|
|
|
TWCR |= (1 << TWIE) | (1 << TWINT) | (ack << TWEA) | (1 << TWEN);
|
2019-07-15 23:32:13 +06:00
|
|
|
}
|