xyControl  0.1
Quadrotor Flight Controller on AVR Basis
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups
Files | Macros | Functions
I2C Driver

Using the AVR TWI/I2C Hardware. More...

Files

file  twi.h
 I2C API Header.
 

Macros

#define TWI_READ   1
 I2C Read Bit. More...
 
#define TWI_WRITE   0
 I2C Write Bit. More...
 

Functions

void twiInit (void)
 Initialize the I2C Hardware. More...
 
void twiStop (void)
 Stop the I2C Hardware. More...
 
unsigned char twiStart (unsigned char addr)
 Start an I2C Transfer. More...
 
unsigned char twiRepStart (unsigned char addr)
 Start a repeated I2C Transfer. More...
 
void twiStartWait (unsigned char addr)
 Start an I2C Transfer and poll until ready. More...
 
unsigned char twiWrite (unsigned char data)
 Write to the I2C Slave. More...
 
unsigned char twiReadAck (void)
 Read from the I2C Slave and request more data. More...
 
unsigned char twiReadNak (void)
 Read from the I2C Slave and deny more data. More...
 

Detailed Description

Using the AVR TWI/I2C Hardware.

Macro Definition Documentation

#define TWI_READ   1

I2C Read Bit.

Definition at line 43 of file twi.h.

Referenced by accRead(), gyroRead(), and magRead().

#define TWI_WRITE   0

I2C Write Bit.

Definition at line 44 of file twi.h.

Referenced by accRead(), gyroRead(), magRead(), magWriteRegister(), and motorTask().

Function Documentation

void twiInit ( void  )

Initialize the I2C Hardware.

Definition at line 26 of file twi.c.

Referenced by xyInit().

27 {
28  /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
29 
30  TWSR = 0; /* no prescaler */
31  TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
32 
33 }/* i2c_init */
unsigned char twiReadAck ( void  )

Read from the I2C Slave and request more data.

Returns
Data read

Definition at line 179 of file twi.c.

Referenced by accRead(), gyroRead(), and magRead().

180 {
181  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
182  while(!(TWCR & (1<<TWINT)));
183 
184  return TWDR;
185 
186 }/* i2c_readAck */
unsigned char twiReadNak ( void  )

Read from the I2C Slave and deny more data.

Returns
Data read

Definition at line 194 of file twi.c.

Referenced by accRead(), gyroRead(), and magRead().

195 {
196  TWCR = (1<<TWINT) | (1<<TWEN);
197  while(!(TWCR & (1<<TWINT)));
198 
199  return TWDR;
200 
201 }/* i2c_readNak */
unsigned char twiRepStart ( unsigned char  addr)

Start a repeated I2C Transfer.

Parameters
addrSlave Address (with Read/Write bit)
Returns
0 on success, 1 on error

Definition at line 127 of file twi.c.

References twiStart().

Referenced by accRead(), gyroRead(), and magRead().

128 {
129  return twiStart( address );
130 
131 }/* i2c_rep_start */
unsigned char twiStart ( unsigned char  addr)

Start an I2C Transfer.

Parameters
addrSlave Address (with Read/Write bit)
Returns
0 on success, 1 on error

Definition at line 40 of file twi.c.

Referenced by accRead(), gyroRead(), magRead(), magWriteRegister(), motorTask(), and twiRepStart().

41 {
42  uint8_t twst;
43 
44  // send START condition
45  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
46 
47  // wait until transmission completed
48  while(!(TWCR & (1<<TWINT)));
49 
50  // check value of TWI Status Register. Mask prescaler bits.
51  twst = TW_STATUS & 0xF8;
52  if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
53 
54  // send device address
55  TWDR = address;
56  TWCR = (1<<TWINT) | (1<<TWEN);
57 
58  // wail until transmission completed and ACK/NACK has been received
59  while(!(TWCR & (1<<TWINT)));
60 
61  // check value of TWI Status Register. Mask prescaler bits.
62  twst = TW_STATUS & 0xF8;
63  if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
64 
65  return 0;
66 
67 }/* i2c_start */
void twiStartWait ( unsigned char  addr)

Start an I2C Transfer and poll until ready.

Parameters
addrSlave Address (with Read/Write bit)

Definition at line 76 of file twi.c.

77 {
78  uint8_t twst;
79 
80 
81  while ( 1 )
82  {
83  // send START condition
84  TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
85 
86  // wait until transmission completed
87  while(!(TWCR & (1<<TWINT)));
88 
89  // check value of TWI Status Register. Mask prescaler bits.
90  twst = TW_STATUS & 0xF8;
91  if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
92 
93  // send device address
94  TWDR = address;
95  TWCR = (1<<TWINT) | (1<<TWEN);
96 
97  // wail until transmission completed
98  while(!(TWCR & (1<<TWINT)));
99 
100  // check value of TWI Status Register. Mask prescaler bits.
101  twst = TW_STATUS & 0xF8;
102  if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
103  {
104  /* device busy, send stop condition to terminate write operation */
105  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
106 
107  // wait until stop condition is executed and bus released
108  while(TWCR & (1<<TWSTO));
109 
110  continue;
111  }
112  //if( twst != TW_MT_SLA_ACK) return 1;
113  break;
114  }
115 
116 }/* i2c_start_wait */
void twiStop ( void  )

Stop the I2C Hardware.

Definition at line 137 of file twi.c.

Referenced by magWriteRegister(), and motorTask().

138 {
139  /* send stop condition */
140  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
141 
142  // wait until stop condition is executed and bus released
143  while(TWCR & (1<<TWSTO));
144 
145 }/* i2c_stop */
unsigned char twiWrite ( unsigned char  data)

Write to the I2C Slave.

Parameters
dataData to send
Returns
0 on success, 1 on error

Definition at line 155 of file twi.c.

Referenced by accRead(), gyroRead(), magRead(), magWriteRegister(), and motorTask().

156 {
157  uint8_t twst;
158 
159  // send data to the previously addressed device
160  TWDR = data;
161  TWCR = (1<<TWINT) | (1<<TWEN);
162 
163  // wait until transmission completed
164  while(!(TWCR & (1<<TWINT)));
165 
166  // check value of TWI Status Register. Mask prescaler bits
167  twst = TW_STATUS & 0xF8;
168  if( twst != TW_MT_DATA_ACK) return 1;
169  return 0;
170 
171 }/* i2c_write */