Skip to content

Commit

Permalink
Fix some build issues, fix Wire::endTransmission() return values
Browse files Browse the repository at this point in the history
  • Loading branch information
multiplemonomials committed May 17, 2024
1 parent e7f7f9a commit 8e980b4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
16 changes: 6 additions & 10 deletions .github/workflows/build-cores.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
mbed_target:
- ARDUINO_NANO33BLE
- RASPBERRY_PI_PICO
- ARDUINO_NICLA_SENSE_ME
cmake_build_type:
- Debug
- Release
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cores/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
50 changes: 32 additions & 18 deletions libraries/Wire/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -72,32 +72,40 @@ 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) {
return endTransmission(true);
}

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) {
Expand All @@ -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;
Expand Down
1 change: 1 addition & 0 deletions libraries/Wire/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class MbedI2C : public HardwareI2C
RingBufferN<BufferSize> rxBuffer;
uint8_t txBuffer[BufferSize];
uint32_t usedTxBuffer;
bool txBufferOverflow;
voidFuncPtrParamInt onReceiveCb = NULL;
voidFuncPtr onRequestCb = NULL;
#ifdef DEVICE_I2CSLAVE
Expand Down
2 changes: 1 addition & 1 deletion mbed-os
Submodule mbed-os updated 324 files

0 comments on commit 8e980b4

Please sign in to comment.