Skip to content

Commit

Permalink
Convert AVR32 i2c_{send,recv} to take 7-bit addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
martinwguy committed Mar 19, 2012
1 parent 42af821 commit 939cdbc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/platform/avr32/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,16 +372,19 @@ static void ARBITRATION_LOST(void)
* This interface will also allow us to implement collision detection and retry.
*/

// i2c_write() sends data to a slave.
// It returns the number of bytes that were sent and acknowledged,
// or -1 if the slave did not acknowledge its address.
// i2c_send() sends data to a slave.
// "address" is a 7-bit slave address.
// "stop" says whether we should send a final stop bit or not.
// It returns the number of bytes that were sent and acknowledged
// or -1 if the slave did not acknowledge its address.

int i2c_send(u8 address, const u8 *data, unsigned len, bool stop)
{
int retval;

i2c_start_cond();
if( i2c_write_byte( address ) ) {
// Convert 7-bit address to 8-bit address with bottom bit clear for "write"
if( i2c_write_byte( address << 1 ) ) {
// There was no acknowledgement from the slave
retval = -1;
} else {
Expand All @@ -398,14 +401,15 @@ int i2c_send(u8 address, const u8 *data, unsigned len, bool stop)
return( retval );
}

// i2c_read() reads "count" bytes from a slave device.
// i2c_recv() reads "count" bytes from a 7-bit slave device.
// It returns the number of bytes sent or -1 if the slave did not acknowledge its address.
int i2c_recv(u8 address, u8 *data, unsigned len, bool stop)
{
int retval;

i2c_start_cond();
if( i2c_write_byte( address ) ) {
// Convert 7-bit address to 8-bit address with bottom bit set for "read"
if( i2c_write_byte( ( address << 1 ) | 1 ) ) {
// There was no acknowledgement from the slave
retval = -1;
} else {
Expand All @@ -424,5 +428,5 @@ int i2c_recv(u8 address, u8 *data, unsigned len, bool stop)
// Returns 1 if it is present or 0 if it didn't acknowledge its address.
bool i2c_probe(u8 slave_address)
{
return( i2c_send(slave_address, NULL, 0, true) == 0 );
return( i2c_send( slave_address, NULL, 0, true ) == 0 );
}
10 changes: 5 additions & 5 deletions src/platform/avr32/lcd.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
// For reliable operation, we set it to 10% less than the highest "normal" speed.
#define LCD_BUS_FREQ 50000

// I2C slave addresses for command bytes and data strings
// 7-bit I2C slave addresses for command bytes and data strings
// Command address is followed by a single byte giving the command to perform
// Data address is followed by multiple bytes of ASCII data to display
// on the character display at the current cursor location.
#define LCD_CMD 0x7C // Send commands
#define LCD_GETPOS 0x7D // Read the cursor position
#define LCD_DATA 0x7E // Send data
#define LCD_BUTTONS 0x7F // Read the status of the buttons
#define LCD_CMD (0x7C>>1) // Send commands
#define LCD_GETPOS (0x7D>>1) // Read the cursor position
#define LCD_DATA (0x7E>>1) // Send data
#define LCD_BUTTONS (0x7F>>1) // Read the status of the buttons

// Bits indicating which buttons are held down in the reply to LCD_BUTTONS
#define LCD_BUTTON_SELECT 1
Expand Down

0 comments on commit 939cdbc

Please sign in to comment.