Use BIT macro
Mostly done with the cocci script. macro.h was then added manually.
This commit is contained in:
committed by
Jeremy Soller
parent
9a3ecba010
commit
720af4b2b0
@@ -6,6 +6,7 @@
|
||||
|
||||
#include <board/cpu.h>
|
||||
#include <common/i2c.h>
|
||||
#include <common/macro.h>
|
||||
|
||||
#define TIMEOUT (F_CPU/1000)
|
||||
|
||||
@@ -15,10 +16,10 @@ int i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
// reset TWI control register
|
||||
TWCR = 0;
|
||||
// transmit START condition
|
||||
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
|
||||
TWCR = BIT(TWINT) | BIT(TWSTA) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
|
||||
// check if the start condition was successfully transmitted
|
||||
@@ -27,10 +28,10 @@ int i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
// load slave addr into data register
|
||||
TWDR = ((addr << 1) | read);
|
||||
// start transmission of addr
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
count = TIMEOUT;
|
||||
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
|
||||
// check if the device has acknowledged the READ / WRITE mode
|
||||
@@ -42,7 +43,7 @@ int i2c_start(struct I2C * i2c, uint8_t addr, bool read) {
|
||||
|
||||
void i2c_stop(struct I2C * i2c) {
|
||||
// transmit STOP condition
|
||||
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWSTO);
|
||||
}
|
||||
|
||||
int i2c_write(struct I2C * i2c, uint8_t * data, int length) {
|
||||
@@ -51,10 +52,10 @@ int i2c_write(struct I2C * i2c, uint8_t * data, int length) {
|
||||
// load data into data register
|
||||
TWDR = data[i];
|
||||
// start transmission of data
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
// timed out
|
||||
if (count == 0) return -1;
|
||||
// failed to receive ack
|
||||
@@ -69,14 +70,14 @@ int i2c_read(struct I2C * i2c, uint8_t * data, int length) {
|
||||
for (i = 0; i < length; i++) {
|
||||
if ((i + 1) < length) {
|
||||
// start TWI module and acknowledge data after reception
|
||||
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
|
||||
TWCR = BIT(TWINT) | BIT(TWEN) | BIT(TWEA);
|
||||
} else {
|
||||
// start receiving without acknowledging reception
|
||||
TWCR = (1<<TWINT) | (1<<TWEN);
|
||||
TWCR = BIT(TWINT) | BIT(TWEN);
|
||||
}
|
||||
// wait for end of transmission
|
||||
uint32_t count = TIMEOUT;
|
||||
while(!(TWCR & (1<<TWINT)) && count > 0) count -= 1;
|
||||
while(!(TWCR & BIT(TWINT)) && count > 0) count -= 1;
|
||||
if (count == 0) return -1;
|
||||
// return received data from TWDR
|
||||
data[i] = TWDR;
|
||||
|
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <board/cpu.h>
|
||||
#include <arch/i2c_slave.h>
|
||||
#include <common/macro.h>
|
||||
|
||||
static void (* volatile i2c_slave_new_cb)() = NULL;
|
||||
static void (* volatile i2c_slave_recv_cb)(uint8_t) = NULL;
|
||||
@@ -28,7 +29,7 @@ void i2c_slave_init(uint8_t address, void (*new_cb)(), void (*recv_cb)(uint8_t),
|
||||
// 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 = (1<<TWIE) | (1<<TWEA) | (1<<TWINT) | (1<<TWEN);
|
||||
TWCR = BIT(TWIE) | BIT(TWEA) | BIT(TWINT) | BIT(TWEN);
|
||||
|
||||
// set interrupts
|
||||
sei();
|
||||
@@ -39,7 +40,7 @@ void i2c_slave_stop(){
|
||||
cli();
|
||||
|
||||
// clear acknowledge and enable bits
|
||||
TWCR &= ~((1<<TWEA) | (1<<TWEN));
|
||||
TWCR &= ~(BIT(TWEA) | BIT(TWEN));
|
||||
// clear address
|
||||
TWAR = 0;
|
||||
// remove callbacks
|
||||
@@ -59,14 +60,14 @@ ISR(TWI_vect) {
|
||||
if (i2c_slave_new_cb != NULL) {
|
||||
i2c_slave_new_cb();
|
||||
}
|
||||
TWCR = (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
||||
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 = (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
case TW_ST_SLA_ACK:
|
||||
case TW_ST_DATA_ACK:
|
||||
@@ -74,16 +75,16 @@ ISR(TWI_vect) {
|
||||
if(i2c_slave_recv_cb != NULL) {
|
||||
TWDR = i2c_slave_send_cb();
|
||||
}
|
||||
TWCR = (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
||||
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 = (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
default:
|
||||
TWCR = (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
||||
TWCR = BIT(TWIE) | BIT(TWINT) | BIT(TWEA) | BIT(TWEN);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,8 @@
|
||||
#ifndef _ARCH_GPIO_H
|
||||
#define _ARCH_GPIO_H
|
||||
|
||||
#include <common/macro.h>
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
@@ -18,7 +20,7 @@ struct Gpio {
|
||||
.pin = &PIN ## BLOCK, \
|
||||
.ddr = &DDR ## BLOCK, \
|
||||
.port = &PORT ## BLOCK, \
|
||||
.value = (1 << NUMBER), \
|
||||
.value = BIT(NUMBER), \
|
||||
}
|
||||
|
||||
bool gpio_get(struct Gpio * gpio);
|
||||
|
Reference in New Issue
Block a user