From 07a4d03876441ed32e70fb6baa871d68f23bbd2e Mon Sep 17 00:00:00 2001 From: michaeluman Date: Wed, 5 Jun 2024 09:57:37 -0700 Subject: [PATCH] feat(i2c): add baud rate setting functionality Add a new function `i2c_setbaud` allowing the baud rate of the I2C connection to be set. This feature provides the ability to configure the communication speed to standard values (100kbps or 400kbps). To support this, `stdbool.h` is included for returning boolean status from the new function. The implementation checks if the requested baud rate matches the current one, and updates it if necessary through a serialized command, with feedback obtained immediately by calling `i2c_getstatus`. This commit includes both the prototype in `i2cdriver.h` and the definition in `i2cdriver.c`. --- c/common/i2cdriver.c | 15 +++++++++++++++ c/common/i2cdriver.h | 3 +++ 2 files changed, 18 insertions(+) diff --git a/c/common/i2cdriver.c b/c/common/i2cdriver.c index 6185eb2..1585a04 100644 --- a/c/common/i2cdriver.c +++ b/c/common/i2cdriver.c @@ -11,6 +11,7 @@ #define __STDC_FORMAT_MACROS #include #include +#include #include "i2cdriver.h" @@ -323,6 +324,20 @@ void i2c_getstatus(I2CDriver *sd) sd->mode = mode[0]; } +bool i2c_setbaud(I2CDriver *sd, unsigned int kbaud) +{ + if (kbaud == sd->speed) { + return true; + } + if ((kbaud != 100) || (kbaud != 400)) { + return false; + } + uint8_t ch = (kbaud == 100)?'1':'4'; + writeToSerialPort(sd->port, &ch, 1); + i2c_getstatus(sd); + return (bool)(sd->speed == kbaud);; +} + void i2c_scan(I2CDriver *sd, uint8_t devices[128]) { charCommand(sd, 'd'); diff --git a/c/common/i2cdriver.h b/c/common/i2cdriver.h index 94510db..1a8b9d2 100644 --- a/c/common/i2cdriver.h +++ b/c/common/i2cdriver.h @@ -2,6 +2,7 @@ #define I2CDRIVER_H #include +#include #if defined(WIN32) #include @@ -36,6 +37,8 @@ void i2c_read(I2CDriver *sd, uint8_t bytes[], size_t nn); int i2c_start(I2CDriver *sd, uint8_t dev, uint8_t op); void i2c_stop(I2CDriver *sd); +bool i2c_setbaud(I2CDriver *sd, int kbaud); + void i2c_monitor(I2CDriver *sd, int enable); void i2c_capture(I2CDriver *sd);