forked from len0rd/rockbox
Generic I2C: fix various problems with this (now) unused driver
Explicit if SDA is input or output in static functions Do not use logf() but return codes Check for overflow when adding an interface Indent on 4 spaces Rewrite i2c_read_data() and i2c_write_data() using goto for error cases Make subaddress optional (not sent if == -1) git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19360 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
43f238f6dc
commit
95d58d3058
2 changed files with 94 additions and 59 deletions
|
@ -23,14 +23,17 @@
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "logf.h"
|
|
||||||
#include "generic_i2c.h"
|
#include "generic_i2c.h"
|
||||||
|
|
||||||
|
#define MAX_I2C_INTERFACES 5
|
||||||
|
|
||||||
int i2c_num_ifs = 0;
|
int i2c_num_ifs = 0;
|
||||||
struct i2c_interface *i2c_if[5];
|
struct i2c_interface *i2c_if[MAX_I2C_INTERFACES];
|
||||||
|
|
||||||
static void i2c_start(struct i2c_interface *iface)
|
static void i2c_start(struct i2c_interface *iface)
|
||||||
{
|
{
|
||||||
|
iface->sda_output();
|
||||||
|
|
||||||
iface->sda_hi();
|
iface->sda_hi();
|
||||||
iface->scl_hi();
|
iface->scl_hi();
|
||||||
iface->sda_lo();
|
iface->sda_lo();
|
||||||
|
@ -40,6 +43,8 @@ static void i2c_start(struct i2c_interface *iface)
|
||||||
|
|
||||||
static void i2c_stop(struct i2c_interface *iface)
|
static void i2c_stop(struct i2c_interface *iface)
|
||||||
{
|
{
|
||||||
|
iface->sda_output();
|
||||||
|
|
||||||
iface->sda_lo();
|
iface->sda_lo();
|
||||||
iface->scl_hi();
|
iface->scl_hi();
|
||||||
iface->delay_su_sto();
|
iface->delay_su_sto();
|
||||||
|
@ -48,6 +53,7 @@ static void i2c_stop(struct i2c_interface *iface)
|
||||||
|
|
||||||
static void i2c_ack(struct i2c_interface *iface, bool ack)
|
static void i2c_ack(struct i2c_interface *iface, bool ack)
|
||||||
{
|
{
|
||||||
|
iface->sda_output();
|
||||||
iface->scl_lo();
|
iface->scl_lo();
|
||||||
if ( ack )
|
if ( ack )
|
||||||
iface->sda_lo();
|
iface->sda_lo();
|
||||||
|
@ -84,6 +90,8 @@ static unsigned char i2c_inb(struct i2c_interface *iface, bool ack)
|
||||||
int i;
|
int i;
|
||||||
unsigned char byte = 0;
|
unsigned char byte = 0;
|
||||||
|
|
||||||
|
iface->sda_input();
|
||||||
|
|
||||||
/* clock in each bit, MSB first */
|
/* clock in each bit, MSB first */
|
||||||
for ( i=0x80; i; i>>=1 ) {
|
for ( i=0x80; i; i>>=1 ) {
|
||||||
iface->sda_input();
|
iface->sda_input();
|
||||||
|
@ -103,19 +111,21 @@ static unsigned char i2c_inb(struct i2c_interface *iface, bool ack)
|
||||||
|
|
||||||
static void i2c_outb(struct i2c_interface *iface, unsigned char byte)
|
static void i2c_outb(struct i2c_interface *iface, unsigned char byte)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
/* clock out each bit, MSB first */
|
iface->sda_output();
|
||||||
for (i=0x80; i; i>>=1) {
|
|
||||||
if (i & byte)
|
/* clock out each bit, MSB first */
|
||||||
iface->sda_hi();
|
for (i=0x80; i; i>>=1) {
|
||||||
else
|
if (i & byte)
|
||||||
iface->sda_lo();
|
iface->sda_hi();
|
||||||
iface->delay_su_dat();
|
else
|
||||||
iface->scl_hi();
|
iface->sda_lo();
|
||||||
iface->delay_thigh();
|
iface->delay_su_dat();
|
||||||
iface->scl_lo();
|
iface->scl_hi();
|
||||||
iface->delay_hd_dat();
|
iface->delay_thigh();
|
||||||
|
iface->scl_lo();
|
||||||
|
iface->delay_hd_dat();
|
||||||
}
|
}
|
||||||
|
|
||||||
iface->sda_hi();
|
iface->sda_hi();
|
||||||
|
@ -143,24 +153,33 @@ int i2c_write_data(int bus_address, int address,
|
||||||
|
|
||||||
i2c_start(iface);
|
i2c_start(iface);
|
||||||
i2c_outb(iface, iface->address & 0xfe);
|
i2c_outb(iface, iface->address & 0xfe);
|
||||||
if (i2c_getack(iface)) {
|
if (!i2c_getack(iface))
|
||||||
i2c_outb(iface, address);
|
{
|
||||||
if (i2c_getack(iface)) {
|
ret = -2;
|
||||||
for(i = 0;i < count;i++) {
|
goto end;
|
||||||
i2c_outb(iface, buf[i]);
|
|
||||||
if (!i2c_getack(iface)) {
|
|
||||||
ret = -3;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = -2;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
logf("i2c_write_data() - no ack\n");
|
|
||||||
ret = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (address != -1)
|
||||||
|
{
|
||||||
|
i2c_outb(iface, address);
|
||||||
|
if (!i2c_getack(iface))
|
||||||
|
{
|
||||||
|
ret = -3;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0;i < count;i++)
|
||||||
|
{
|
||||||
|
i2c_outb(iface, buf[i]);
|
||||||
|
if (!i2c_getack(iface))
|
||||||
|
{
|
||||||
|
ret = -4;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
end:
|
||||||
i2c_stop(iface);
|
i2c_stop(iface);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -174,33 +193,49 @@ int i2c_read_data(int bus_address, int address,
|
||||||
if(!iface)
|
if(!iface)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
i2c_start(iface);
|
if (address != -1)
|
||||||
i2c_outb(iface, iface->address & 0xfe);
|
{
|
||||||
if (i2c_getack(iface)) {
|
i2c_start(iface);
|
||||||
i2c_outb(iface, address);
|
i2c_outb(iface, iface->address & 0xfe);
|
||||||
if (i2c_getack(iface)) {
|
if (!i2c_getack(iface))
|
||||||
i2c_start(iface);
|
{
|
||||||
i2c_outb(iface, iface->address | 1);
|
|
||||||
if (i2c_getack(iface)) {
|
|
||||||
for(i = 0;i < count-1;i++)
|
|
||||||
buf[i] = i2c_inb(iface, true);
|
|
||||||
|
|
||||||
buf[i] = i2c_inb(iface, false);
|
|
||||||
} else {
|
|
||||||
ret = -3;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ret = -2;
|
ret = -2;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
i2c_outb(iface, address);
|
||||||
|
if (!i2c_getack(iface))
|
||||||
|
{
|
||||||
|
ret = -3;
|
||||||
|
goto end;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
ret = -1;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i2c_start(iface);
|
||||||
|
i2c_outb(iface, iface->address | 1);
|
||||||
|
if (!i2c_getack(iface))
|
||||||
|
{
|
||||||
|
ret = -4;
|
||||||
|
goto end;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0;i < count-1;i++)
|
||||||
|
buf[i] = i2c_inb(iface, true);
|
||||||
|
|
||||||
|
buf[i] = i2c_inb(iface, false);
|
||||||
|
|
||||||
|
end:
|
||||||
i2c_stop(iface);
|
i2c_stop(iface);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void i2c_add_node(struct i2c_interface *iface)
|
int i2c_add_node(struct i2c_interface *iface)
|
||||||
{
|
{
|
||||||
|
if (i2c_num_ifs == MAX_I2C_INTERFACES)
|
||||||
|
return -1;
|
||||||
|
|
||||||
i2c_if[i2c_num_ifs++] = iface;
|
i2c_if[i2c_num_ifs++] = iface;
|
||||||
|
|
||||||
|
iface->scl_output();
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -47,7 +47,7 @@ struct i2c_interface
|
||||||
void (*delay_thigh)(void); /* SCL high period (tHIGH) 4.0us/0.6us */
|
void (*delay_thigh)(void); /* SCL high period (tHIGH) 4.0us/0.6us */
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void i2c_add_node(struct i2c_interface *iface);
|
extern int i2c_add_node(struct i2c_interface *iface);
|
||||||
extern int i2c_write_data(int bus_address, int address,
|
extern int i2c_write_data(int bus_address, int address,
|
||||||
const unsigned char* buf, int count);
|
const unsigned char* buf, int count);
|
||||||
extern int i2c_read_data(int bus_address, int address,
|
extern int i2c_read_data(int bus_address, int address,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue