AVR library
Macros | Functions | Variables
swi2c.c File Reference

Software (bitbang) I2C. More...

#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#include "hwdefs.h"
#include "swdefs.h"

Macros

#define SWI2C_DELAY   _delay_us(swi2c_br)
 

Functions

void i2c_init (uint8_t br)
 Init I2C. More...
 
uint8_t i2c_readbuf (const uint8_t adr, uint8_t *const data, const uint8_t len)
 Read from I2C. More...
 
uint8_t i2c_writebuf (const uint8_t adr, uint8_t *const data, const uint8_t len)
 Write to I2C. More...
 

Variables

uint8_t swi2c_br
 

Detailed Description

Software (bitbang) I2C.

Define SCL_PORT, SCL_BIT, SDA_PORT and SDA_BIT in hwdefs.h.

You can also override SWI2C_DELAY in swdefs.h. Defining SWI2C_REVBITS in swdefs.h will transfer LSB first instead of MSB. Defining SWI2C_RAWADR in swdefs.h will transfer i2c address exactly as given, without setting R/W bit (bit 0).

Author
Matej Kogovsek (matej.nosp@m.@ham.nosp@m.radio.nosp@m..si)
Note
This file is part of mat-avr-lib

Function Documentation

void i2c_init ( uint8_t  br)

Init I2C.

Parameters
[in]brCopied to TWBR
uint8_t i2c_readbuf ( const uint8_t  adr,
uint8_t *const  data,
const uint8_t  len 
)

Read from I2C.

Parameters
[in]adrI2C address
[out]datapointer to caller allocated buffer for data
[in]lennumber of bytes to read (len <= sizeof(buf))
Returns
0 on success, non-zero otherwise
uint8_t i2c_writebuf ( const uint8_t  adr,
uint8_t *const  data,
const uint8_t  len 
)

Write to I2C.

Parameters
[in]adrI2C address
[in]datapointer to data
[in]lennumber of bytes to write (len <= sizeof(buf))
Returns
0 on success, non-zero otherwise