i2c_bb: implement read/write

This commit is contained in:
Thomas Klaehn 2019-07-23 14:53:51 +02:00
parent fe1d96853c
commit b6ca09b58e
3 changed files with 51 additions and 9 deletions

View File

@ -4,7 +4,7 @@
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/gpio_ftdi"
"/usr/include/libgpio_ftdi"
],
"defines": [],
"compilerPath": "/usr/bin/clang",

View File

@ -11,9 +11,7 @@ struct i2c_bb {
int i2c_open(const struct i2c_bb *i2c);
int i2c_close(const struct i2c_bb *i2c);
void start_condition(const struct i2c_bb *i2c);
void stop_condition(const struct i2c_bb *i2c);
bool write_byte(const struct i2c_bb *i2c, unsigned char byte);
unsigned char read_byte(const struct i2c_bb *i2c, bool ack);
int i2_read(const struct i2c_bb *i2c, uint8_t slave_addr, char *buffer, unsigned int len);
int i2_write(const struct i2c_bb *i2c, uint8_t slave_addr, const char *buffer, unsigned int len);
#endif

View File

@ -10,6 +10,10 @@
static void write_bit(const struct i2c_bb *i2c, uint8_t bit);
static uint8_t read_bit(const struct i2c_bb *i2c);
static void start_condition(const struct i2c_bb *i2c);
static void stop_condition(const struct i2c_bb *i2c);
static bool write_byte(const struct i2c_bb *i2c, uint8_t byte);
static uint8_t read_byte(const struct i2c_bb *i2c, bool ack);
// cppcheck-suppress unusedFunction
int i2c_open(const struct i2c_bb *i2c)
@ -36,8 +40,48 @@ int i2c_close(const struct i2c_bb *i2c)
return res;
}
int i2_read(const struct i2c_bb *i2c, uint8_t slave_addr, char *buffer, unsigned int len)
{
unsigned int i;
assert(NULL != i2c);
if((NULL == buffer) || len == 0) {
return 0;
}
start_condition(i2c);
write_byte(i2c, (slave_addr & 0x80));
for(i = 0; i < len; i++) {
buffer[i] = read_byte(i2c, true);
}
stop_condition(i2c);
return (int)i;
}
int i2_write(const struct i2c_bb *i2c, uint8_t slave_addr, const char *buffer, unsigned int len)
{
unsigned int i;
assert(NULL != i2c);
if((NULL == buffer) || len == 0) {
return 0;
}
start_condition(i2c);
write_byte(i2c, (slave_addr & 0x7F));
for(i = 0; i < len; i++) {
write_byte(i2c, buffer[i]);
}
stop_condition(i2c);
return (int)i;
}
// cppcheck-suppress unusedFunction
void start_condition(const struct i2c_bb *i2c)
static void start_condition(const struct i2c_bb *i2c)
{
assert( NULL != i2c);
@ -51,7 +95,7 @@ void start_condition(const struct i2c_bb *i2c)
}
// cppcheck-suppress unusedFunction
void stop_condition(const struct i2c_bb *i2c)
static void stop_condition(const struct i2c_bb *i2c)
{
assert( NULL != i2c);
@ -64,7 +108,7 @@ void stop_condition(const struct i2c_bb *i2c)
}
// cppcheck-suppress unusedFunction
bool write_byte(const struct i2c_bb *i2c, unsigned char byte)
static bool write_byte(const struct i2c_bb *i2c, uint8_t byte)
{
uint8_t ack, i;
@ -81,7 +125,7 @@ bool write_byte(const struct i2c_bb *i2c, unsigned char byte)
}
// cppcheck-suppress unusedFunction
unsigned char read_byte(const struct i2c_bb *i2c, bool ack)
static uint8_t read_byte(const struct i2c_bb *i2c, bool ack)
{
uint8_t res = 0, i;