From b8eccc7a385ed611ec9de8be05ce8a8878357940 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 27 Mar 2026 22:46:47 +0900 Subject: [PATCH] hw-i2c: consolidate platform gems into ports/ directories Move hw-esp32-i2c and hw-rp2040-i2c into hw-i2c/ports/esp32/ and hw-i2c/ports/rp2040/ using the new ports build system. Platform sources are now compiled automatically based on conf.ports setting. Separate platform gems are no longer needed. Co-authored-by: Claude --- mrbgems/hw-esp32-i2c/mrbgem.rake | 7 ---- mrbgems/hw-i2c/README.md | 41 +++++++------------ .../src => hw-i2c/ports/esp32}/i2c.c | 5 --- .../src => hw-i2c/ports/rp2040}/i2c.c | 5 --- mrbgems/hw-rp2040-i2c/mrbgem.rake | 7 ---- 5 files changed, 14 insertions(+), 51 deletions(-) delete mode 100644 mrbgems/hw-esp32-i2c/mrbgem.rake rename mrbgems/{hw-esp32-i2c/src => hw-i2c/ports/esp32}/i2c.c (95%) rename mrbgems/{hw-rp2040-i2c/src => hw-i2c/ports/rp2040}/i2c.c (93%) delete mode 100644 mrbgems/hw-rp2040-i2c/mrbgem.rake diff --git a/mrbgems/hw-esp32-i2c/mrbgem.rake b/mrbgems/hw-esp32-i2c/mrbgem.rake deleted file mode 100644 index 8c743bc01..000000000 --- a/mrbgems/hw-esp32-i2c/mrbgem.rake +++ /dev/null @@ -1,7 +0,0 @@ -MRuby::Gem::Specification.new('hw-esp32-i2c') do |spec| - spec.license = 'MIT' - spec.author = 'mruby developers' - spec.summary = 'I2C HAL for ESP32' - - spec.add_dependency 'hw-i2c' -end diff --git a/mrbgems/hw-i2c/README.md b/mrbgems/hw-i2c/README.md index bdfad87f1..fe10f5aa7 100644 --- a/mrbgems/hw-i2c/README.md +++ b/mrbgems/hw-i2c/README.md @@ -4,27 +4,27 @@ This gem provides the `I2C` class for communicating with I2C devices from mruby. ## Architecture -The I2C support is split into a common gem and platform-specific HAL gems: +Platform-specific HAL implementations are in `ports/` directories: -- **hw-i2c** (this gem) - Ruby API, C bindings, and HAL function declarations -- **hw-esp32-i2c** - HAL implementation for ESP32 (using ESP-IDF I2C master driver) -- **hw-rp2040-i2c** - HAL implementation for RP2040 (using Pico SDK) +- `ports/esp32/` - ESP32 using ESP-IDF I2C master driver +- `ports/rp2040/` - RP2040 using Pico SDK -The platform gems depend on hw-i2c, so you only need to specify the platform gem in your build configuration. +The build system automatically compiles matching port sources based +on `conf.ports` setting. ## Build Configuration ```ruby # For ESP32 MRuby::CrossBuild.new('esp32') do |conf| - # ... - conf.gem "#{root}/mrbgems/hw-esp32-i2c" + conf.ports :esp32 + conf.gem core: 'hw-i2c' end # For RP2040 MRuby::CrossBuild.new('rp2040') do |conf| - # ... - conf.gem "#{root}/mrbgems/hw-rp2040-i2c" + conf.ports :rp2040 + conf.gem core: 'hw-i2c' end ``` @@ -126,38 +126,25 @@ found = i2c.scan ## HAL Interface -To add support for a new platform, create a gem (e.g., `hw-myboard-i2c`) that depends on `hw-i2c` and implements the following C functions declared in ``: +To add support for a new platform, create a `ports//` +directory and implement the following C functions declared in +``: ```c -/* Convert platform-specific unit name string to unit number. - Return MRB_I2C_ERROR_UNIT for unknown names. */ int mrb_i2c_unit_name_to_num(const char *name); - -/* Initialize an I2C bus unit. - sda/scl: GPIO pin numbers (-1 for platform default if available). - Return MRB_I2C_OK on success. */ mrb_i2c_status mrb_i2c_init(int unit, uint32_t freq, int8_t sda, int8_t scl); - -/* Read len bytes from device at addr. - Return number of bytes read on success, negative on error. */ int mrb_i2c_read(int unit, uint8_t addr, uint8_t *dst, size_t len, uint32_t timeout_us); - -/* Write len bytes to device at addr. - Return number of bytes written on success, negative on error. */ int mrb_i2c_write(int unit, uint8_t addr, const uint8_t *src, size_t len, uint32_t timeout_us); - -/* Atomic write-then-read using repeated START condition. - Write wlen bytes from src, then read rlen bytes into dst. - Return number of bytes read on success, negative on error. */ int mrb_i2c_write_read(int unit, uint8_t addr, const uint8_t *src, size_t wlen, uint8_t *dst, size_t rlen, uint32_t timeout_us); ``` -The gem must also provide empty `mrb__gem_init()` and `mrb__gem_final()` functions (with hyphens replaced by underscores). +The port sources are compiled automatically when the build +configuration includes a matching `conf.ports` tag. ### Error Codes diff --git a/mrbgems/hw-esp32-i2c/src/i2c.c b/mrbgems/hw-i2c/ports/esp32/i2c.c similarity index 95% rename from mrbgems/hw-esp32-i2c/src/i2c.c rename to mrbgems/hw-i2c/ports/esp32/i2c.c index 3384084ff..cfb7aedd6 100644 --- a/mrbgems/hw-esp32-i2c/src/i2c.c +++ b/mrbgems/hw-i2c/ports/esp32/i2c.c @@ -115,8 +115,3 @@ mrb_i2c_write_read(int unit, uint8_t addr, const uint8_t *src, size_t wlen, i2c_master_bus_rm_device(dev); return (err == ESP_OK) ? (int)rlen : -1; } - -#include - -void mrb_hw_esp32_i2c_gem_init(mrb_state *mrb) {} -void mrb_hw_esp32_i2c_gem_final(mrb_state *mrb) {} diff --git a/mrbgems/hw-rp2040-i2c/src/i2c.c b/mrbgems/hw-i2c/ports/rp2040/i2c.c similarity index 93% rename from mrbgems/hw-rp2040-i2c/src/i2c.c rename to mrbgems/hw-i2c/ports/rp2040/i2c.c index 98de36e95..a0fac95cc 100644 --- a/mrbgems/hw-rp2040-i2c/src/i2c.c +++ b/mrbgems/hw-i2c/ports/rp2040/i2c.c @@ -62,8 +62,3 @@ mrb_i2c_write_read(int unit, uint8_t addr, const uint8_t *src, size_t wlen, /* read with nostop=false (STOP after read) */ return i2c_read_timeout_us(inst, addr, dst, rlen, false, timeout_us); } - -#include - -void mrb_hw_rp2040_i2c_gem_init(mrb_state *mrb) {} -void mrb_hw_rp2040_i2c_gem_final(mrb_state *mrb) {} diff --git a/mrbgems/hw-rp2040-i2c/mrbgem.rake b/mrbgems/hw-rp2040-i2c/mrbgem.rake deleted file mode 100644 index d637ec229..000000000 --- a/mrbgems/hw-rp2040-i2c/mrbgem.rake +++ /dev/null @@ -1,7 +0,0 @@ -MRuby::Gem::Specification.new('hw-rp2040-i2c') do |spec| - spec.license = 'MIT' - spec.author = 'mruby developers' - spec.summary = 'I2C HAL for RP2040' - - spec.add_dependency 'hw-i2c' -end