diff --git a/.github/workflows/build-cores.yml b/.github/workflows/build-cores.yml index a9e6153e3..3e0bb1113 100644 --- a/.github/workflows/build-cores.yml +++ b/.github/workflows/build-cores.yml @@ -12,6 +12,7 @@ jobs: mbed_target: - ARDUINO_NANO33BLE - RASPBERRY_PI_PICO + - ARDUINO_NICLA_SENSE_ME cmake_build_type: - Debug - Release @@ -23,17 +24,12 @@ jobs: uses: actions/checkout@v4 with: submodules: recursive - - - name: Install Python Dependencies + + - name: Install Python Venv run: | - # Note: The Mbed container is too old to have python3-json5, but it's old enough that we can - # still install python packages globally. - # On newer Ubuntu versions, you would instead use: - # apt-get update - # xargs apt-get install -y < mbed-os/tools/requirements.apt.txt - - python3 -m pip install -r mbed-os/tools/requirements.txt - + apt-get update + apt-get install -y python3-venv + - name: Build run: | mkdir build diff --git a/CMakeLists.txt b/CMakeLists.txt index 09f59ea13..111d6c893 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ target_compile_definitions(mbed-os PUBLIC # It can't be added as a "regular" Mbed define in mbed-target.config.h because there's # some code in ArduinoBLE that looks for this define without including Arduino.h. target_compile_definitions(mbed-os PUBLIC ARDUINO_ARCH_MBED=1) +target_compile_definitions(mbed-core-flags INTERFACE ARDUINO_ARCH_MBED=1) # Create static library out of mbed-os # Note that this library MUST be linked with -Wl,--whole-archive to work. @@ -70,7 +71,7 @@ set(MBED_LIBS_TO_INSTALL mbed-storage-tdbstore ) -if("TARGET_USBDEVICE" IN_LIST MBED_TARGET_LABELS) +if("DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS) list(APPEND MBED_LIBS_TO_INSTALL mbed-usb mbed-usb-cdc-ecm diff --git a/cores/CMakeLists.txt b/cores/CMakeLists.txt index 5ee143417..f6fc1f39b 100644 --- a/cores/CMakeLists.txt +++ b/cores/CMakeLists.txt @@ -23,7 +23,7 @@ set(ARDUINO_CORE_SOURCES add_library(arduino-core STATIC ${ARDUINO_CORE_SOURCES}) target_link_libraries(arduino-core arduino-api arduino-variant mbed-core-flags mbed-rtos-flags) -if("TARGET_USBDEVICE" IN_LIST MBED_TARGET_LABELS) +if("DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS) target_link_libraries(arduino-core mbed-usb) endif() diff --git a/libraries/CMakeLists.txt b/libraries/CMakeLists.txt index b19646c75..9283dc13f 100644 --- a/libraries/CMakeLists.txt +++ b/libraries/CMakeLists.txt @@ -48,7 +48,7 @@ add_subdirectory(Wire) add_subdirectory(SPI) add_subdirectory(Scheduler) -if("TARGET_USBDEVICE" IN_LIST MBED_TARGET_LABELS) +if("DEVICE_USBDEVICE=1" IN_LIST MBED_TARGET_DEFINITIONS) add_subdirectory(USBHID) add_subdirectory(USBMIDI) add_subdirectory(USBMSD) diff --git a/libraries/Wire/Wire.cpp b/libraries/Wire/Wire.cpp index e77b00936..e744a8c47 100644 --- a/libraries/Wire/Wire.cpp +++ b/libraries/Wire/Wire.cpp @@ -23,9 +23,9 @@ #include "Wire.h" #include "pinDefinitions.h" -arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0) {} +arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(digitalPinToPinName(sda)), _scl(digitalPinToPinName(scl)), usedTxBuffer(0), txBufferOverflow(false) {} -arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0) {} +arduino::MbedI2C::MbedI2C(PinName sda, PinName scl) : _sda(sda), _scl(scl), usedTxBuffer(0), txBufferOverflow(false) {} void arduino::MbedI2C::begin() { end(); @@ -72,19 +72,30 @@ void arduino::MbedI2C::setClock(uint32_t freq) { void arduino::MbedI2C::beginTransmission(uint8_t address) { _address = address << 1; usedTxBuffer = 0; + txBufferOverflow = false; } uint8_t arduino::MbedI2C::endTransmission(bool stopBit) { - #ifndef TARGET_PORTENTA_H7 - if (usedTxBuffer == 0) { - // we are scanning, return 0 if the addresed device responds with an ACK - char buf[1]; - int ret = master->read(_address, buf, 1, !stopBit); - return ret; - } - #endif - if (master->write(_address, (const char *) txBuffer, usedTxBuffer, !stopBit) == mbed::I2C::Result::ACK) return 0; - return 2; + + mbed::I2C::Result writeResult = master->write(_address, (const char *) txBuffer, usedTxBuffer, !stopBit); + if (writeResult == mbed::I2C::Result::NACK) { + return 2; // Indicate NACK. No way to tell if received during address or data, but assume address. + } + else if (writeResult == mbed::I2C::Result::ACK) { + if (txBufferOverflow) { + return 1; + } + else { + // Success + return 0; + } + } + else if (writeResult == mbed::I2C::Result::TIMEOUT) { + return 5; // timeout + } + else { + return 4; // other error + } } uint8_t arduino::MbedI2C::endTransmission(void) { @@ -92,12 +103,9 @@ uint8_t arduino::MbedI2C::endTransmission(void) { } size_t arduino::MbedI2C::requestFrom(uint8_t address, size_t len, bool stopBit) { - if(len > BufferSize) - { - return 0; - } char buf[BufferSize]; + len = min(len, sizeof(buf)); auto ret = master->read(address << 1, buf, len, !stopBit); if (ret != mbed::I2C::Result::ACK) { @@ -114,13 +122,19 @@ size_t arduino::MbedI2C::requestFrom(uint8_t address, size_t len) { } size_t arduino::MbedI2C::write(uint8_t data) { - if (usedTxBuffer == BufferSize) return 0; + if (usedTxBuffer == BufferSize) { + txBufferOverflow = true; + return 0; + } txBuffer[usedTxBuffer++] = data; return 1; } size_t arduino::MbedI2C::write(const uint8_t* data, int len) { - if (usedTxBuffer + len > BufferSize) len = BufferSize - usedTxBuffer; + if (usedTxBuffer + len > BufferSize) { + txBufferOverflow = true; + len = BufferSize - usedTxBuffer; + } memcpy(txBuffer + usedTxBuffer, data, len); usedTxBuffer += len; return len; diff --git a/libraries/Wire/Wire.h b/libraries/Wire/Wire.h index 8a8b38622..960c99702 100644 --- a/libraries/Wire/Wire.h +++ b/libraries/Wire/Wire.h @@ -80,6 +80,7 @@ class MbedI2C : public HardwareI2C RingBufferN rxBuffer; uint8_t txBuffer[BufferSize]; uint32_t usedTxBuffer; + bool txBufferOverflow; voidFuncPtrParamInt onReceiveCb = NULL; voidFuncPtr onRequestCb = NULL; #ifdef DEVICE_I2CSLAVE diff --git a/mbed-os b/mbed-os index dbbc25923..87bc6de65 160000 --- a/mbed-os +++ b/mbed-os @@ -1 +1 @@ -Subproject commit dbbc25923bb92b709d971788072e2a9175fedc36 +Subproject commit 87bc6de65a3943d2f1cdba22da70af6b592e2ec7