diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 66c0617..d11f98b 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,7 +4,7 @@ "name": "Linux", "includePath": [ "${workspaceFolder}/**", - "/usr/include/gpio_ftdi" + "/usr/include/libgpio_ftdi" ], "defines": [], "compilerPath": "/usr/bin/clang", diff --git a/inc/i2c_bb.h b/inc/i2c_bb.h index 6fc3501..3f4a68a 100644 --- a/inc/i2c_bb.h +++ b/inc/i2c_bb.h @@ -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 diff --git a/src/i2c.c b/src/i2c.c index 4ef73a8..8c30fd3 100644 --- a/src/i2c.c +++ b/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;