orgua / OneWireHub

OneWire slave device emulator
GNU General Public License v3.0
343 stars 86 forks source link

Support for Raspberry RP2040 #123

Open ahorn42 opened 1 year ago

ahorn42 commented 1 year ago

Hello,

has someone already tried to run OneWireHub on a Raspberry RP2040 like device? Funnily these µC are cheaper than most 1-wire devices, and I would like to use some RP2040 to fake a bunch of DS2450.

After renaming MEM_SIZE to MEMORY_SIZE (or something else, as MEM_SIZE seems to be a macro in the RP2040 arduino wrapper) I got the code compiling, but I could not get it working, so that the emulated 1-wire devices could be read from another µC.

I already found the platform.h file but I am not sure, how to adapt this to RP2040 and also I have no clue, if this is really the issue. Any ideas how to go on?

best regards, ahorn

ahorn42 commented 1 year ago

Just found the calibrate_by_bus_timing script and it seems to work now with IPL = 25 :)

Unfortunately I still don't understand what the other values mean that were configured in platform.h

#define PIN_TO_BASEREG(pin)             (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))
using io_reg_t = uint8_t; // define special datatype for register-access

Above is the sample for AVR, I kind of have an idea what it should do, but I think I need some further hints how to adapt this to the RP2040 ;)

orgua commented 1 year ago

That's mostly Atmel-related lowlevel-stuff to access pins that was needed to allow the fast overdrive-mode of Onewire. Maybe it's time to remove all that error-prone platform-glue and use arduino-commands by default. I will order a RP2040 and look into either porting the lib or advance to pure-arduino as a major v3-step

hattesen commented 1 year ago

@orgua, it sounds great that you plan to look at supporting the RP2040 in OneWireHub.

Ideally an RP2040 implementation should make use of the RP2040 PIO (an IO coprocessor), which allows implementation of serial protocols with single cycle (@125MHz) precision timing, leaving the application/device implementation free of bit-banging code, timing critical sections and avoiding having to disable interrupts.

If taking on the PIO is too much for the initial port to RP2040, if you define an API with a sufficiently high abstraction level (addressing and sending bytes), it would be reasonably easy for someone to add a PIO implementation later (via a pull request).

Here are a few inspirational resources I have collected that all implement a 1-wire protocol master using the RP2040 PIO for the time-critical serial encoding/decoding:

mr-miky commented 6 days ago

Just found the calibrate_by_bus_timing script and it seems to work now with IPL = 25 :)

Unfortunately I still don't understand what the other values mean that were configured in platform.h

#define PIN_TO_BASEREG(pin)             (portInputRegister(digitalPinToPort(pin)))
#define PIN_TO_BITMASK(pin)             (digitalPinToBitMask(pin))
#define DIRECT_READ(base, mask)         (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask)   ((*((base)+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask)  ((*((base)+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask)    ((*((base)+2)) &= ~(mask))
#define DIRECT_WRITE_HIGH(base, mask)   ((*((base)+2)) |= (mask))
using io_reg_t = uint8_t; // define special datatype for register-access

Above is the sample for AVR, I kind of have an idea what it should do, but I think I need some further hints how to adapt this to the RP2040 ;)

Could you test this ??

#define PIN_TO_BASEREG(pin)             (0)
#define PIN_TO_BITMASK(pin)             (1ul << pin)
#define DIRECT_READ(base, pin)          !!(PIN_TO_BITMASK(pin) & sio_hw->gpio_in)  //digitalRead(pin)
#define DIRECT_WRITE_LOW(base, pin)     sio_hw->gpio_clr = PIN_TO_BITMASK(pin)     // digitalWrite(pin, LOW)
#define DIRECT_WRITE_HIGH(base, pin)    sio_hw->gpio_set = PIN_TO_BITMASK(pin)     //digitalWrite(pin, HIGH)
#define DIRECT_MODE_INPUT(base, pin)    sio_hw->gpio_oe_clr = PIN_TO_BITMASK(pin)  // pinMode(pin,INPUT)
#define DIRECT_MODE_OUTPUT(base, pin)   sio_hw->gpio_oe_set = PIN_TO_BITMASK(pin)  // pinMode(pin,OUTPUT)
using io_reg_t = uint32_t; // define special datatype for register-access
constexpr uint8_t VALUE_IPL {25}; // instructions per loop, uncalibrated so far - see ./examples/debug/calibrate_by_bus_timing for an explanation