diff --git a/slave/slave/lib/i2c.c b/slave/slave/lib/i2c.c --- a/slave/slave/lib/i2c.c +++ b/slave/slave/lib/i2c.c @@ -4,3 +4,201 @@ * Created: 11/7/2012 7:18:23 PM * Author: kripperger */ + +#include +#include +#include "../config.h" +#include "i2c.h" + + +/* I2C clock in Hz */ +#define SCL_CLOCK 100000L + + + +/************************************************************************* + Initialization of the I2C bus interface. Need to be called only once +*************************************************************************/ +void i2c_init(void) +{ + /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */ + + TWSR = 0; /* no prescaler */ + TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */ + +}/* i2c_init */ + + + +/************************************************************************* + Issues a start condition and sends address and transfer direction. + return 0 = device accessible, 1= failed to access device +*************************************************************************/ +unsigned char i2c_start(unsigned char address) +{ + uint8_t twst; + + // send START condition + TWCR = (1< + + +/** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */ +#define I2C_READ 1 + +/** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */ +#define I2C_WRITE 0 + + + +void i2c_init(void); //initialize the I2C master interace. Need to be called only once + +void i2c_stop(void); //Terminates the data transfer and releases the I2C bus + +unsigned char i2c_start(unsigned char addr); //Issues a start condition and sends address and transfer direction + //addr is the address and transfer direction of I2C device + //Returns 0 device accessible + //Returns 1 failed to access device + +unsigned char i2c_rep_start(unsigned char addr); //Issues a repeated start condition and sends address and transfer direction + //addr is the address and transfer direction of I2C device + //Returns 0 device accessible + //Returns 1 failed to access device + +void i2c_start_wait(unsigned char addr); //Issues a start condition and sends address and transfer direction. If device is busy, use ack polling to wait until device ready + //addr is the address and transfer direction of I2C device + +unsigned char i2c_write(unsigned char data); //Send one byte to I2C device + //data is the byte to be transfered + //Returns 0 write successful + //Returns 1 write failed + +unsigned char i2c_readAck(void); //read one byte from the I2C device, request more data from device + //Returns byte read from I2C device + +unsigned char i2c_readNak(void); //read one byte from the I2C device, read is followed by a stop condition + //Returns byte read from I2C device + +unsigned char i2c_read(unsigned char ack); //read one byte from the I2C device. Implemented as a macro, which calls either i2c_readAck or i2c_readNak +#define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak(); + //When ack is 1: send ack, request more data from device + //When ack is 0: send nak, read is followed by a stop condition + //Returns byte read from I2C device diff --git a/slave/slave/lib/led.c b/slave/slave/lib/led.c --- a/slave/slave/lib/led.c +++ b/slave/slave/lib/led.c @@ -6,6 +6,7 @@ */ #include + #include "led.h" void led_configure() { diff --git a/slave/slave/slave.c b/slave/slave/slave.c --- a/slave/slave/slave.c +++ b/slave/slave/slave.c @@ -13,8 +13,10 @@ #include "config.h" +#include #include #include +#include //#include "lib/serial.h" #include "lib/led.h" @@ -39,6 +41,7 @@ int main(void) uint8_t moduleID = io_getModuleId(); //Slave Module ID from rotary dip switch + while(1) {