Reindent files using spaces
This commit is contained in:
committed by
Jeremy Soller
parent
720af4b2b0
commit
4963e04a83
@ -16,7 +16,7 @@ void delay_ticks(uint16_t ticks);
|
||||
// Warning: this will round to the nearest tick
|
||||
#define delay_ns(X) \
|
||||
delay_ticks((uint16_t)((((uint32_t)(X)) * 69UL + 89999UL) / 90000UL));
|
||||
|
||||
|
||||
void delay_ms(int ms);
|
||||
|
||||
#endif // _ARCH_DELAY_H
|
||||
|
@ -11,77 +11,77 @@
|
||||
#define TIMEOUT (F_CPU/1000)
|
||||
|
||||
int i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
uint32_t count;
|
||||
uint32_t count;
|
||||
|
||||
// reset TWI control register
|
||||
TWCR = 0;
|
||||
// transmit START condition
|
||||
TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
// reset TWI control register
|
||||
TWCR = 0;
|
||||
// transmit START condition
|
||||
TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
|
||||
// check if the start condition was successfully transmitted
|
||||
if((TWSR & 0xF8) != TW_START) return -1;
|
||||
// check if the start condition was successfully transmitted
|
||||
if((TWSR & 0xF8) != TW_START) return -1;
|
||||
|
||||
// load slave addr into data register
|
||||
TWDR = ((addr << 1) | read);
|
||||
// start transmission of addr
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
// load slave addr into data register
|
||||
TWDR = ((addr << 1) | read);
|
||||
// start transmission of addr
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
|
||||
// check if the device has acknowledged the READ / WRITE mode
|
||||
uint8_t twst = TW_STATUS & 0xF8;
|
||||
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1;
|
||||
// check if the device has acknowledged the READ / WRITE mode
|
||||
uint8_t twst = TW_STATUS & 0xF8;
|
||||
if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) return -1;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void i2c_stop(struct I2C * i2c) {
|
||||
// transmit STOP condition
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
|
||||
// transmit STOP condition
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
|
||||
}
|
||||
|
||||
int i2c_write(struct I2C * i2c, uint8_t * data, int length) {
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
// load data into data register
|
||||
TWDR = data[i];
|
||||
// start transmission of data
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
// timed out
|
||||
if (count == 0) return -1;
|
||||
// failed to receive ack
|
||||
if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
// load data into data register
|
||||
TWDR = data[i];
|
||||
// start transmission of data
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
// timed out
|
||||
if (count == 0) return -1;
|
||||
// failed to receive ack
|
||||
if((TWSR & 0xF8) != TW_MT_DATA_ACK) return -1;
|
||||
}
|
||||
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
|
||||
int i2c_read(struct I2C * i2c, uint8_t * data, int length) {
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
if ((i + 1) < length) {
|
||||
// start TWI module and acknowledge data after reception
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWEA);
|
||||
} else {
|
||||
// start receiving without acknowledging reception
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
}
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
// return received data from TWDR
|
||||
data[i] = TWDR;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < length; i++) {
|
||||
if ((i + 1) < length) {
|
||||
// start TWI module and acknowledge data after reception
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWEA);
|
||||
} else {
|
||||
// start receiving without acknowledging reception
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
}
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
// return received data from TWDR
|
||||
data[i] = TWDR;
|
||||
}
|
||||
|
||||
return i;
|
||||
return i;
|
||||
}
|
||||
|
@ -16,75 +16,75 @@ static void (* volatile i2c_slave_recv_cb)(uint8_t) = NULL;
|
||||
static uint8_t (* volatile i2c_slave_send_cb)() = NULL;
|
||||
|
||||
void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t), uint8_t (*send_cb)()){
|
||||
// ensure correct behavior by stopping before changing callbacks or address
|
||||
i2c_slave_stop();
|
||||
// ensure correct behavior by stopping before changing callbacks or address
|
||||
i2c_slave_stop();
|
||||
|
||||
// clear interrupts
|
||||
cli();
|
||||
// clear interrupts
|
||||
cli();
|
||||
|
||||
// setup callbacks
|
||||
i2c_slave_new_cb = new_cb;
|
||||
i2c_slave_recv_cb = recv_cb;
|
||||
i2c_slave_send_cb = send_cb;
|
||||
// load address into TWI address register
|
||||
TWAR = (address << 1);
|
||||
// set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt
|
||||
TWCR = BIT(TWIE) | BIT(TWEA) | BIT(TWINT) | BIT(TWEN);
|
||||
// setup callbacks
|
||||
i2c_slave_new_cb = new_cb;
|
||||
i2c_slave_recv_cb = recv_cb;
|
||||
i2c_slave_send_cb = send_cb;
|
||||
// load address into TWI address register
|
||||
TWAR = (address << 1);
|
||||
// set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt
|
||||
TWCR = BIT(TWIE) | BIT(TWEA) | BIT(TWINT) | BIT(TWEN);
|
||||
|
||||
// set interrupts
|
||||
sei();
|
||||
// set interrupts
|
||||
sei();
|
||||
}
|
||||
|
||||
void i2c_slave_stop(){
|
||||
// clear interrupts
|
||||
cli();
|
||||
// clear interrupts
|
||||
cli();
|
||||
|
||||
// clear acknowledge and enable bits
|
||||
TWCR &= ~(BIT(TWEA) | BIT(TWEN));
|
||||
// clear address
|
||||
TWAR = 0;
|
||||
// remove callbacks
|
||||
i2c_slave_new_cb = NULL;
|
||||
i2c_slave_recv_cb = NULL;
|
||||
i2c_slave_send_cb = NULL;
|
||||
// clear acknowledge and enable bits
|
||||
TWCR &= ~(BIT(TWEA) | BIT(TWEN));
|
||||
// clear address
|
||||
TWAR = 0;
|
||||
// remove callbacks
|
||||
i2c_slave_new_cb = NULL;
|
||||
i2c_slave_recv_cb = NULL;
|
||||
i2c_slave_send_cb = NULL;
|
||||
|
||||
// set interrupts
|
||||
sei();
|
||||
// set interrupts
|
||||
sei();
|
||||
}
|
||||
|
||||
ISR(TWI_vect) {
|
||||
uint8_t status = TW_STATUS;
|
||||
switch(status) {
|
||||
case TW_SR_SLA_ACK:
|
||||
// master has started a new transaction, call the new callback
|
||||
if (i2c_slave_new_cb != NULL) {
|
||||
i2c_slave_new_cb();
|
||||
}
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_SR_DATA_ACK:
|
||||
// received data from master, call the receive callback
|
||||
if(i2c_slave_send_cb != NULL){
|
||||
i2c_slave_recv_cb(TWDR);
|
||||
}
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_ST_SLA_ACK:
|
||||
case TW_ST_DATA_ACK:
|
||||
// master is requesting data, call the send callback
|
||||
if(i2c_slave_recv_cb != NULL) {
|
||||
TWDR = i2c_slave_send_cb();
|
||||
}
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_BUS_ERROR:
|
||||
// some sort of erroneous state, prepare TWI to be readdressed
|
||||
printf("TWI_vect bus error\n");
|
||||
TWCR = 0;
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
default:
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
}
|
||||
uint8_t status = TW_STATUS;
|
||||
switch(status) {
|
||||
case TW_SR_SLA_ACK:
|
||||
// master has started a new transaction, call the new callback
|
||||
if (i2c_slave_new_cb != NULL) {
|
||||
i2c_slave_new_cb();
|
||||
}
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_SR_DATA_ACK:
|
||||
// received data from master, call the receive callback
|
||||
if(i2c_slave_send_cb != NULL){
|
||||
i2c_slave_recv_cb(TWDR);
|
||||
}
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_ST_SLA_ACK:
|
||||
case TW_ST_DATA_ACK:
|
||||
// master is requesting data, call the send callback
|
||||
if(i2c_slave_recv_cb != NULL) {
|
||||
TWDR = i2c_slave_send_cb();
|
||||
}
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_BUS_ERROR:
|
||||
// some sort of erroneous state, prepare TWI to be readdressed
|
||||
printf("TWI_vect bus error\n");
|
||||
TWCR = 0;
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
default:
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <board/i2c.h>
|
||||
|
||||
void i2c_init(unsigned long baud) {
|
||||
TWAR = 0;
|
||||
TWBR = (uint8_t)(((F_CPU / baud) - 16 ) / 2);
|
||||
TWCR = 0;
|
||||
TWAR = 0;
|
||||
TWBR = (uint8_t)(((F_CPU / baud) - 16 ) / 2);
|
||||
TWCR = 0;
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <board/i2c.h>
|
||||
|
||||
void i2c_init(unsigned long baud) {
|
||||
TWAR = 0;
|
||||
TWBR = (uint8_t)(((F_CPU / baud) - 16 ) / 2);
|
||||
TWCR = 0;
|
||||
TWAR = 0;
|
||||
TWBR = (uint8_t)(((F_CPU / baud) - 16 ) / 2);
|
||||
TWCR = 0;
|
||||
}
|
||||
|
@ -4,7 +4,7 @@
|
||||
|
||||
// Main program while running in scratch ROM
|
||||
void main(void) {
|
||||
for (;;) {
|
||||
for (;;) {
|
||||
smfi_event();
|
||||
}
|
||||
}
|
||||
|
@ -11,9 +11,9 @@ int i2c_recv(struct I2C * i2c, uint8_t addr, uint8_t* data, int length) __reentr
|
||||
res = i2c_read(i2c, data, length);
|
||||
if (res < 0) return res;
|
||||
|
||||
i2c_stop(i2c);
|
||||
i2c_stop(i2c);
|
||||
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
int i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, int length) __reentrant {
|
||||
@ -25,9 +25,9 @@ int i2c_send(struct I2C * i2c, uint8_t addr, uint8_t* data, int length) __reentr
|
||||
res = i2c_write(i2c, data, length);
|
||||
if (res < 0) return res;
|
||||
|
||||
i2c_stop(i2c);
|
||||
i2c_stop(i2c);
|
||||
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
int i2c_get(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int length) __reentrant {
|
||||
@ -48,7 +48,7 @@ int i2c_set(struct I2C * i2c, uint8_t addr, uint8_t reg, uint8_t* data, int leng
|
||||
res = i2c_start(i2c, addr, false);
|
||||
if (res < 0) return res;
|
||||
|
||||
res = i2c_write(i2c, ®, 1);
|
||||
res = i2c_write(i2c, ®, 1);
|
||||
if (res < 0) return res;
|
||||
|
||||
return i2c_send(i2c, addr, data, length);
|
||||
|
@ -80,7 +80,7 @@ int i2c_start(struct I2C * i2c, uint8_t addr, bool read) __reentrant {
|
||||
*(i2c->trasla) = (addr << 1) | read;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void i2c_stop(struct I2C * i2c) {
|
||||
|
@ -72,7 +72,7 @@ int i2c_start(struct I2C * i2c, uint8_t addr, bool read) __reentrant {
|
||||
*(i2c->trasla) = (addr << 1) | read;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void i2c_stop(struct I2C * i2c) {
|
||||
|
Reference in New Issue
Block a user