diff --git a/Kconfig b/Kconfig index 4d4215905..750bd2965 100644 --- a/Kconfig +++ b/Kconfig @@ -41,4 +41,9 @@ config ARDUINO_MAX_TONES Specify the maximum number of tones that can be played simultaneously with tone(). If set to a negative value, the maximum number will be determined from the system's digital pin configuration. + +module = ARDUINO_API +module-str = arduino_api +source "subsys/logging/Kconfig.template.log_config" + endif diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index 7fc64b030..cdd153893 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -7,6 +7,9 @@ #include #include +#include +LOG_MODULE_REGISTER(arduino_wire, CONFIG_ARDUINO_API_LOG_LEVEL); + arduino::ZephyrI2C::ZephyrI2C(const struct device *i2c) : i2c_dev(i2c) { } @@ -42,16 +45,26 @@ uint8_t arduino::ZephyrI2C::endTransmission(void) { // TODO for ADS1115 size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len, bool stopBit) { - int ret = i2c_read(i2c_dev, rxRingBuffer.buffer, len, address); + /* Use stack-allocated buffer for reading */ + uint8_t readbuff[sizeof(rxRingBuffer.buffer)]; + if (len > sizeof(readbuff)) { + LOG_ERR("requested read length (%zu) exceeds buffer size (%zu)", len, sizeof(readbuff)); + return 0; + } + + int ret = i2c_read(i2c_dev, readbuff, len, address); if (ret != 0) { - printk("\n\nERR: i2c burst read fails\n\n\n"); + LOG_ERR("burst read failed"); return 0; } - ret = ring_buf_put(&rxRingBuffer.rb, rxRingBuffer.buffer, len); + + /* Flush the receive buffer so another read() call returns the correct data */ + flush(); + ret = ring_buf_put(&rxRingBuffer.rb, readbuff, len); if (ret == 0) { - printk("\n\nERR: buff put fails\n\n\n"); + LOG_ERR("failed to put data in ring buffer"); return 0; } return len; @@ -62,6 +75,10 @@ size_t arduino::ZephyrI2C::requestFrom(uint8_t address, size_t len) { // TODO fo } size_t arduino::ZephyrI2C::write(uint8_t data) { // TODO for ADS1115 + if (usedTxBuffer >= sizeof(txBuffer)) { + LOG_ERR("tx buffer is full"); + return 0; + } txBuffer[usedTxBuffer++] = data; return 1; } @@ -77,15 +94,13 @@ size_t arduino::ZephyrI2C::write(const uint8_t *buffer, size_t size) { int arduino::ZephyrI2C::read() { uint8_t buf[1]; - if (ring_buf_peek(&rxRingBuffer.rb, buf, 1) > 0) { - int ret = ring_buf_get(&rxRingBuffer.rb, buf, 1); - if (ret == 0) { - printk("\n\nERR: buff empty\n\n\n"); - return 0; - } - return (int)buf[0]; + + if(!ring_buf_get(&rxRingBuffer.rb, buf, 1)) { + LOG_ERR("buffer is empty"); + return -1; // no data available } - return EXIT_FAILURE; + + return (int)buf[0]; } int arduino::ZephyrI2C::available() { // TODO for ADS1115 @@ -94,14 +109,16 @@ int arduino::ZephyrI2C::available() { // TODO for ADS1115 int arduino::ZephyrI2C::peek() { uint8_t buf[1]; - int bytes_read = ring_buf_peek(&rxRingBuffer.rb, buf, 1); - if (bytes_read == 0){ - return 0; - } + if (!ring_buf_peek(&rxRingBuffer.rb, buf, 1)){ + // No data available + return -1; + } return (int)buf[0]; } -void arduino::ZephyrI2C::flush() {} +void arduino::ZephyrI2C::flush() { + ring_buf_reset(&rxRingBuffer.rb); +} void arduino::ZephyrI2C::onReceive(voidFuncPtrParamInt cb) {} void arduino::ZephyrI2C::onRequest(voidFuncPtr cb) {}