Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] rp2040: fix timer wrap deadlock in ws2812 vendor driver #19652

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions platforms/chibios/drivers/vendor/RP/RP2040/ws2812_vendor.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
// Copyright 2022 Stefan Kerkmann (@KarlK90)
// SPDX-License-Identifier: GPL-2.0-or-later

#include "quantum.h"
#include "ws2812.h"
#include "hardware/pio.h"
#include "hardware/timer.h"
#include "hardware/clocks.h"
// Keep this exact include order otherwise we run into naming conflicts between
// pico-sdk and rp2040.h which we don't control.
#include "quantum.h"
#include "hardware/pio.h"

#if !defined(MCU_RP)
# error PIO Driver is only available for Raspberry Pi 2040 MCUs!
Expand Down Expand Up @@ -132,7 +135,7 @@ static uint32_t RP_DMA_MODE_WS2812;
static int STATE_MACHINE = -1;

static SEMAPHORE_DECL(TRANSFER_COUNTER, 1);
static rtcnt_t LAST_TRANSFER;
static absolute_time_t LAST_TRANSFER;

/**
* @brief Convert RGBW value into WS2812 compatible 32-bit data word.
Expand Down Expand Up @@ -161,7 +164,7 @@ static void ws2812_dma_callback(void* p, uint32_t ct) {
// Convert from ns to us
time_to_completion /= 1000;

LAST_TRANSFER = chSysGetRealtimeCounterX() + time_to_completion + WS2812_TRST_US;
update_us_since_boot(&LAST_TRANSFER, time_us_64() + time_to_completion + WS2812_TRST_US);

osalSysLockFromISR();
chSemSignalI(&TRANSFER_COUNTER);
Expand Down Expand Up @@ -256,8 +259,7 @@ static inline void sync_ws2812_transfer(void) {
}

// Busy wait until last transfer has finished
while (unlikely(!timer_expired32(chSysGetRealtimeCounterX(), LAST_TRANSFER))) {
}
busy_wait_until(LAST_TRANSFER);
}

void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) {
Expand Down
2 changes: 2 additions & 0 deletions platforms/chibios/vendors/RP/RP2040.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ PICOSDKROOT := $(TOP_DIR)/lib/pico-sdk
PICOSDKSRC = $(PICOSDKROOT)/src/rp2_common/hardware_clocks/clocks.c \
$(PICOSDKROOT)/src/rp2_common/hardware_pll/pll.c \
$(PICOSDKROOT)/src/rp2_common/hardware_pio/pio.c \
$(PICOSDKROOT)/src/rp2_common/hardware_timer/timer.c \
$(PICOSDKROOT)/src/rp2_common/hardware_flash/flash.c \
$(PICOSDKROOT)/src/rp2_common/hardware_gpio/gpio.c \
$(PICOSDKROOT)/src/rp2_common/hardware_claim/claim.c \
Expand All @@ -44,6 +45,7 @@ PICOSDKINC = $(CHIBIOS)//os/various/pico_bindings/dumb/include \
$(PICOSDKROOT)/src/rp2_common/hardware_pll/include \
$(PICOSDKROOT)/src/rp2_common/hardware_pio/include \
$(PICOSDKROOT)/src/rp2_common/hardware_sync/include \
$(PICOSDKROOT)/src/rp2_common/hardware_timer/include \
$(PICOSDKROOT)/src/rp2_common/hardware_resets/include \
$(PICOSDKROOT)/src/rp2_common/hardware_watchdog/include \
$(PICOSDKROOT)/src/rp2_common/hardware_xosc/include \
Expand Down