Skip to content

Commit

Permalink
esp32/onewire: use GPIO
Browse files Browse the repository at this point in the history
See discussion in Moddable-OpenSource#1126 - onewire and neopixel's use of RMT conflict on
esp32-c3 even though "it should work". Needs resolving but for now
switching to GPIO with a little hack in the GPIO module works too.
  • Loading branch information
tve committed Jun 14, 2023
1 parent 68cd6b3 commit e685754
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
3 changes: 3 additions & 0 deletions modules/drivers/onewire/esp/modonewire.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@
#define MODDEF_ONEWIRE_RMT_RX_CHANNEL (RMT_CHANNEL_2)
#endif

#ifndef MODDEF_ONEWIRE_DRIVER_GPIO
#define MODDEF_ONEWIRE_DRIVER_RMT RMT
#endif

#else
#define MODDEF_ONEWIRE_DRIVER_GPIO GPIO
Expand Down Expand Up @@ -98,6 +100,7 @@ void xs_onewire(xsMachine *the)
onewire->owb = owb_gpio_initialize(&onewire->driver_info, onewire->pin);
#endif
#ifdef MODDEF_ONEWIRE_DRIVER_RMT
//xsLog("Onewire: RMT pin=%d tx=%d rx=%d\n", onewire->pin, MODDEF_ONEWIRE_RMT_TX_CHANNEL, MODDEF_ONEWIRE_RMT_RX_CHANNEL);
onewire->owb = owb_rmt_initialize(&onewire->driver_info, onewire->pin, MODDEF_ONEWIRE_RMT_TX_CHANNEL, MODDEF_ONEWIRE_RMT_RX_CHANNEL);
#endif
if (onewire->owb == NULL)
Expand Down
22 changes: 17 additions & 5 deletions modules/drivers/onewire/esp/owb_gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#if __ets__ && !ESP32 && !defined(MODDEF_ONEWIRE_DIRECTGPIO)
// esp8266 defaults to MODDEF_ONEWIRE_DIRECTGPIO
#define MODDEF_ONEWIRE_DIRECTGPIO 1
pffft!;
#endif

#if MODDEF_ONEWIRE_DIRECTGPIO
Expand All @@ -56,9 +57,6 @@
#define DIRECT_WRITE_HIGH(base, mask) (GPOS = (mask)) //GPIO_OUT_W1TS_ADDRESS
#endif

// Use for now... sort later... Need to allocate
static modGPIOConfigurationRecord config;

/// @cond ignore
struct _OneWireBus_Timing
{
Expand Down Expand Up @@ -160,16 +158,28 @@ static void _write_bit(const OneWireBus * bus, int bit)
ets_delay_us(bus->timing->A);
modGPIOWrite(config, 1); // Release the bus
modCriticalSectionEnd();
ets_delay_us(bus->timing->B);
ets_delay_us(bus->timing->C);
}
else {
ets_delay_us(bus->timing->C);
ets_delay_us(bus->timing->B);
modGPIOWrite(config, 1); // Release the bus
modCriticalSectionEnd();
ets_delay_us(bus->timing->D);
}
}

static void _drive_high(const OneWireBus * bus)
{
struct modGPIOConfigurationRecord *config = bus->config;

modGPIOWrite(config, 1); // Drive DQ high
#if MODDEF_ONEWIRE_DIRECTGPIO
DIRECT_MODE_OUTPUT(REG, PIN_TO_BITMASK(config->pin)); // FIXME
#else
modGPIOSetMode(config, kModGPIOOutput);
#endif
}

/**
* @brief Read a bit from the 1-Wire bus and return the value, with recovery time.
* @param[in] bus Initialised bus instance.
Expand Down Expand Up @@ -220,6 +230,7 @@ static owb_status _write_bits(const OneWireBus * bus, uint8_t data, int number_o
_write_bit(bus, data & 0x01);
data >>= 1;
}
_drive_high(bus);

return OWB_STATUS_OK;
}
Expand All @@ -242,6 +253,7 @@ static owb_status _read_bits(const OneWireBus * bus, uint8_t *out, int number_of
}
}
*out = result;
_drive_high(bus);

return OWB_STATUS_OK;
}
Expand Down
2 changes: 2 additions & 0 deletions modules/drivers/onewire/esp/owb_rmt.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static owb_status _reset(const OneWireBus *bus, bool *is_present)
rmt_set_rx_idle_thresh(i->rx_channel, OW_DURATION_RESET+60);

onewire_flush_rmt_rx_buf(bus);
printf("OW: reset, start RX ch %d, TX ch %d\n", i->rx_channel, i->tx_channel);
rmt_rx_start(i->rx_channel, true);
if (rmt_write_items(i->tx_channel, tx_items, 1, true) == ESP_OK)
{
Expand Down Expand Up @@ -362,6 +363,7 @@ static owb_status _init(owb_rmt_driver_info *info, uint8_t gpio_num,
{
rmt_get_ringbuf_handle(info->rx_channel, &info->rb);
status = OWB_STATUS_OK;
printf("OW: installed RMT driver on gpio %d, tx channel %d, rx channel %d\n", gpio_num, tx_channel, rx_channel);
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions modules/pins/digital/esp32/modGPIO.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ int modGPIOInit(modGPIOConfiguration config, const char *port, uint8_t pin, uint
}

config->pin = pin;
gpio_reset_pin(config->pin); // TvE
result = modGPIOSetMode(config, mode);
if (result) {
config->pin = kUninitializedPin;
Expand All @@ -56,7 +57,7 @@ void modGPIOUninit(modGPIOConfiguration config)

int modGPIOSetMode(modGPIOConfiguration config, uint32_t mode)
{
gpio_reset_pin(config->pin);
//gpio_reset_pin(config->pin); // TvE

switch (mode) {
case kModGPIOOutput:
Expand All @@ -68,7 +69,7 @@ int modGPIOSetMode(modGPIOConfiguration config, uint32_t mode)

gpio_pad_select_gpio(config->pin);
gpio_set_direction(config->pin, (kModGPIOOutputOpenDrain == mode) ? GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT);
gpio_set_level(config->pin, 0);
//gpio_set_level(config->pin, 0); // TvE
break;

case kModGPIOInput:
Expand Down

0 comments on commit e685754

Please sign in to comment.