AVR library
Macros | Functions
i2c.c File Reference

I2C master routines. More...

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

Macros

#define I2C_RETRIES   10
 
#define I2C_START   0
 
#define I2C_STOP   1
 
#define I2C_DATA_TX   2
 
#define I2C_DATA_RX_NACK   3
 
#define I2C_DATA_RX_ACK   4
 

Functions

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

Detailed Description

I2C master routines.

I2C methods are not interrupt driven - they wait until I2C operation completes. If you're using CMT and would prefer switching to another task while I2C operation is in progress, you can define I2C_USE_CMT in swdefs.h. This requires CMT_MUTEX_FUNC.

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