i2c_bb: implement read/write
This commit is contained in:
parent
fe1d96853c
commit
b6ca09b58e
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@ -4,7 +4,7 @@
|
||||
"name": "Linux",
|
||||
"includePath": [
|
||||
"${workspaceFolder}/**",
|
||||
"/usr/include/gpio_ftdi"
|
||||
"/usr/include/libgpio_ftdi"
|
||||
],
|
||||
"defines": [],
|
||||
"compilerPath": "/usr/bin/clang",
|
||||
|
@ -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
|
||||
|
52
src/i2c.c
52
src/i2c.c
@ -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;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user