diff options
Diffstat (limited to 'platforms')
26 files changed, 175 insertions, 72 deletions
diff --git a/platforms/avr/drivers/ws2812_bitbang.c b/platforms/avr/drivers/ws2812_bitbang.c index aad10d86b0..116053591f 100644 --- a/platforms/avr/drivers/ws2812_bitbang.c +++ b/platforms/avr/drivers/ws2812_bitbang.c @@ -37,13 +37,13 @@ static inline void ws2812_sendarray_mask(uint8_t *data, uint16_t datlen, uint8_t masklo, uint8_t maskhi); -void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) { +void ws2812_setleds(rgb_led_t *ledarray, uint16_t number_of_leds) { DDRx_ADDRESS(WS2812_DI_PIN) |= pinmask(WS2812_DI_PIN); uint8_t masklo = ~(pinmask(WS2812_DI_PIN)) & PORTx_ADDRESS(WS2812_DI_PIN); uint8_t maskhi = pinmask(WS2812_DI_PIN) | PORTx_ADDRESS(WS2812_DI_PIN); - ws2812_sendarray_mask((uint8_t *)ledarray, number_of_leds * sizeof(LED_TYPE), masklo, maskhi); + ws2812_sendarray_mask((uint8_t *)ledarray, number_of_leds * sizeof(rgb_led_t), masklo, maskhi); _delay_us(WS2812_TRST_US); } diff --git a/platforms/avr/drivers/ws2812_i2c.c b/platforms/avr/drivers/ws2812_i2c.c index f4a2fbe0b3..f52a037b8e 100644 --- a/platforms/avr/drivers/ws2812_i2c.c +++ b/platforms/avr/drivers/ws2812_i2c.c @@ -18,12 +18,12 @@ void ws2812_init(void) { } // Setleds for standard RGB -void ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { +void ws2812_setleds(rgb_led_t *ledarray, uint16_t leds) { static bool s_init = false; if (!s_init) { ws2812_init(); s_init = true; } - i2c_transmit(WS2812_I2C_ADDRESS, (uint8_t *)ledarray, sizeof(LED_TYPE) * leds, WS2812_I2C_TIMEOUT); + i2c_transmit(WS2812_I2C_ADDRESS, (uint8_t *)ledarray, sizeof(rgb_led_t) * leds, WS2812_I2C_TIMEOUT); } diff --git a/platforms/avr/flash.mk b/platforms/avr/flash.mk index 9c2ab72410..51731f0aa8 100644 --- a/platforms/avr/flash.mk +++ b/platforms/avr/flash.mk @@ -130,10 +130,10 @@ avrdude-split-right: $(BUILD_DIR)/$(TARGET).hex check-size cpfirmware $(call EXEC_AVRDUDE,eeprom-righthand.eep) define EXEC_USBASP - if $(AVRDUDE_PROGRAMMER) -p $(AVRDUDE_MCU) -c usbasp 2>&1 | grep -q "could not find USB device with"; then \ + if $(AVRDUDE_PROGRAMMER) -p $(AVRDUDE_MCU) -c usbasp 2>&1 | grep -q "\(could not\|cannot\) find USB device with"; then \ printf "$(MSG_BOOTLOADER_NOT_FOUND_QUICK_RETRY)" ;\ sleep $(BOOTLOADER_RETRY_TIME) ;\ - until $(AVRDUDE_PROGRAMMER) -p $(AVRDUDE_MCU) -c usbasp 2>&1 | (! grep -q "could not find USB device with"); do\ + until $(AVRDUDE_PROGRAMMER) -p $(AVRDUDE_MCU) -c usbasp 2>&1 | (! grep -q "\(could not\|cannot\) find USB device with"); do\ printf "." ;\ sleep $(BOOTLOADER_RETRY_TIME) ;\ done ;\ diff --git a/platforms/avr/platform.mk b/platforms/avr/platform.mk index 09028d80af..aef449cadf 100644 --- a/platforms/avr/platform.mk +++ b/platforms/avr/platform.mk @@ -109,6 +109,29 @@ DEBUG_HOST = localhost #============================================================================ +SIZE_MARGIN = 1024 + +check-size: + $(eval MAX_SIZE=$(shell n=`$(CC) -E -mmcu=$(MCU) -D__ASSEMBLER__ $(CFLAGS) $(OPT_DEFS) platforms/avr/bootloader_size.c 2> /dev/null | $(SED) -ne 's/\r//;/^#/n;/^AVR_SIZE:/,$${s/^AVR_SIZE: //;p;}'` && echo $$(($$n)) || echo 0)) + $(eval CURRENT_SIZE=$(shell if [ -f $(BUILD_DIR)/$(TARGET).hex ]; then $(SIZE) --target=$(FORMAT) $(BUILD_DIR)/$(TARGET).hex | $(AWK) 'NR==2 {print $$4}'; else printf 0; fi)) + $(eval FREE_SIZE=$(shell expr $(MAX_SIZE) - $(CURRENT_SIZE))) + $(eval OVER_SIZE=$(shell expr $(CURRENT_SIZE) - $(MAX_SIZE))) + $(eval PERCENT_SIZE=$(shell expr $(CURRENT_SIZE) \* 100 / $(MAX_SIZE))) + if [ $(MAX_SIZE) -gt 0 ] && [ $(CURRENT_SIZE) -gt 0 ]; then \ + $(SILENT) || printf "$(MSG_CHECK_FILESIZE)" | $(AWK_CMD); \ + if [ $(CURRENT_SIZE) -gt $(MAX_SIZE) ]; then \ + $(REMOVE) $(TARGET).$(FIRMWARE_FORMAT); \ + $(REMOVE) $(BUILD_DIR)/$(TARGET).{hex,bin,uf2}; \ + printf "\n * $(MSG_FILE_TOO_BIG)"; $(PRINT_ERROR_PLAIN); \ + else \ + if [ $(FREE_SIZE) -lt $(SIZE_MARGIN) ]; then \ + $(PRINT_WARNING_PLAIN); printf " * $(MSG_FILE_NEAR_LIMIT)"; \ + else \ + $(PRINT_OK); $(SILENT) || printf " * $(MSG_FILE_JUST_RIGHT)"; \ + fi ; \ + fi ; \ + fi + # Convert hex to bin. bin: $(BUILD_DIR)/$(TARGET).hex ifeq ($(BOOTLOADER),lufa-ms) diff --git a/platforms/chibios/boards/BONSAI_C4/configs/config.h b/platforms/chibios/boards/BONSAI_C4/configs/config.h index c5dbb25c45..7539ebed41 100644 --- a/platforms/chibios/boards/BONSAI_C4/configs/config.h +++ b/platforms/chibios/boards/BONSAI_C4/configs/config.h @@ -66,7 +66,7 @@ #endif // WS2812-style LED control on pin A10 -#ifdef WS2812_DRIVER_PWM +#ifdef WS2812_PWM # ifndef WS2812_DI_PIN # define WS2812_DI_PIN PAL_LINE(GPIOA, 10) # endif diff --git a/platforms/chibios/boards/BONSAI_C4/configs/halconf.h b/platforms/chibios/boards/BONSAI_C4/configs/halconf.h index 7887e7c9ba..6bab6fbcff 100644 --- a/platforms/chibios/boards/BONSAI_C4/configs/halconf.h +++ b/platforms/chibios/boards/BONSAI_C4/configs/halconf.h @@ -32,7 +32,7 @@ # endif #endif -#ifdef WS2812_DRIVER_PWM +#ifdef WS2812_PWM # ifndef HAL_USE_PWM # define HAL_USE_PWM TRUE # endif diff --git a/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/mcuconf.h b/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/mcuconf.h index 8621807cbb..4f39793264 100644 --- a/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/mcuconf.h +++ b/platforms/chibios/boards/GENERIC_PROMICRO_RP2040/configs/mcuconf.h @@ -54,6 +54,7 @@ #define RP_IRQ_USB0_PRIORITY 3 #define RP_IRQ_I2C0_PRIORITY 2 #define RP_IRQ_I2C1_PRIORITY 2 +#define RP_IRQ_RTC_PRIORITY 3 /* * ADC driver system settings. diff --git a/platforms/chibios/boards/GENERIC_RP_RP2040/configs/mcuconf.h b/platforms/chibios/boards/GENERIC_RP_RP2040/configs/mcuconf.h index 902f9b5005..3a10f67727 100644 --- a/platforms/chibios/boards/GENERIC_RP_RP2040/configs/mcuconf.h +++ b/platforms/chibios/boards/GENERIC_RP_RP2040/configs/mcuconf.h @@ -54,6 +54,7 @@ #define RP_IRQ_USB0_PRIORITY 3 #define RP_IRQ_I2C0_PRIORITY 2 #define RP_IRQ_I2C1_PRIORITY 2 +#define RP_IRQ_RTC_PRIORITY 3 /* * ADC driver system settings. diff --git a/platforms/chibios/boards/GENERIC_WB32_F3G71XX/board/board.c b/platforms/chibios/boards/GENERIC_WB32_F3G71XX/board/board.c index f74c9e8be7..80472b88f7 100644 --- a/platforms/chibios/boards/GENERIC_WB32_F3G71XX/board/board.c +++ b/platforms/chibios/boards/GENERIC_WB32_F3G71XX/board/board.c @@ -73,6 +73,7 @@ void __early_init(void) { wb32_clock_init(); wb32_gpio_init(); } + /** * @brief Board-specific initialization code. * @note You can add your board-specific code here. @@ -80,7 +81,3 @@ void __early_init(void) { void boardInit(void) { } - -void restart_usb_driver(USBDriver *usbp) { - // Do nothing. Restarting the USB driver on these boards breaks it. -} diff --git a/platforms/chibios/boards/GENERIC_WB32_FQ95XX/board/board.c b/platforms/chibios/boards/GENERIC_WB32_FQ95XX/board/board.c index a99537fc27..5701fd2e0d 100644 --- a/platforms/chibios/boards/GENERIC_WB32_FQ95XX/board/board.c +++ b/platforms/chibios/boards/GENERIC_WB32_FQ95XX/board/board.c @@ -73,6 +73,7 @@ void __early_init(void) { wb32_clock_init(); wb32_gpio_init(); } + /** * @brief Board-specific initialization code. * @note You can add your board-specific code here. @@ -80,7 +81,3 @@ void __early_init(void) { void boardInit(void) { } - -void restart_usb_driver(USBDriver *usbp) { - // Do nothing. Restarting the USB driver on these boards breaks it. -} diff --git a/platforms/chibios/boards/QMK_BLOK/configs/mcuconf.h b/platforms/chibios/boards/QMK_BLOK/configs/mcuconf.h index 0c2ef592d6..d5dec0fcd0 100644 --- a/platforms/chibios/boards/QMK_BLOK/configs/mcuconf.h +++ b/platforms/chibios/boards/QMK_BLOK/configs/mcuconf.h @@ -54,6 +54,7 @@ #define RP_IRQ_USB0_PRIORITY 3 #define RP_IRQ_I2C0_PRIORITY 2 #define RP_IRQ_I2C1_PRIORITY 2 +#define RP_IRQ_RTC_PRIORITY 3 /* * ADC driver system settings. diff --git a/platforms/chibios/boards/QMK_PM2040/configs/mcuconf.h b/platforms/chibios/boards/QMK_PM2040/configs/mcuconf.h index 493dcf6434..e3351deb3b 100644 --- a/platforms/chibios/boards/QMK_PM2040/configs/mcuconf.h +++ b/platforms/chibios/boards/QMK_PM2040/configs/mcuconf.h @@ -54,6 +54,7 @@ #define RP_IRQ_USB0_PRIORITY 3 #define RP_IRQ_I2C0_PRIORITY 2 #define RP_IRQ_I2C1_PRIORITY 2 +#define RP_IRQ_RTC_PRIORITY 3 /* * ADC driver system settings. diff --git a/platforms/chibios/bootloader.mk b/platforms/chibios/bootloader.mk index fc898e7699..5b6edd73ad 100644 --- a/platforms/chibios/bootloader.mk +++ b/platforms/chibios/bootloader.mk @@ -107,6 +107,7 @@ ifeq ($(strip $(BOOTLOADER)), tinyuf2) endif ifeq ($(strip $(BOOTLOADER)), uf2boot) OPT_DEFS += -DBOOTLOADER_UF2BOOT + BOARD = STM32_F103_STM32DUINO BOOTLOADER_TYPE = uf2boot FIRMWARE_FORMAT = uf2 endif diff --git a/platforms/chibios/chibios_config.h b/platforms/chibios/chibios_config.h index 1f8a7842fe..f1636f9da0 100644 --- a/platforms/chibios/chibios_config.h +++ b/platforms/chibios/chibios_config.h @@ -37,7 +37,9 @@ # define BACKLIGHT_PAL_MODE (PAL_MODE_ALTERNATE_PWM | PAL_RP_PAD_DRIVE12 | PAL_RP_GPIO_OE) # endif # define BACKLIGHT_PWM_COUNTER_FREQUENCY 1000000 -# define BACKLIGHT_PWM_PERIOD BACKLIGHT_PWM_COUNTER_FREQUENCY / 2048 +# ifndef BACKLIGHT_PWM_PERIOD +# define BACKLIGHT_PWM_PERIOD BACKLIGHT_PWM_COUNTER_FREQUENCY / 2048 +# endif # ifndef AUDIO_PWM_PAL_MODE # define AUDIO_PWM_PAL_MODE (PAL_MODE_ALTERNATE_PWM | PAL_RP_PAD_DRIVE12 | PAL_RP_GPIO_OE) diff --git a/platforms/chibios/drivers/audio_dac_additive.c b/platforms/chibios/drivers/audio_dac_additive.c index 22e4fa2608..26e044b048 100644 --- a/platforms/chibios/drivers/audio_dac_additive.c +++ b/platforms/chibios/drivers/audio_dac_additive.c @@ -84,7 +84,7 @@ static const dacsample_t dac_buffer_trapezoid[AUDIO_DAC_BUFFER_SIZE] = {0x0, 0 0xfff, 0xfdf, 0xf7f, 0xf1f, 0xebf, 0xe5f, 0xdff, 0xd9f, 0xd3f, 0xcdf, 0xc7f, 0xc1f, 0xbbf, 0xb5f, 0xaff, 0xa9f, 0xa3f, 0x9df, 0x97f, 0x91f, 0x8bf, 0x85f, 0x7ff, 0x79f, 0x73f, 0x6df, 0x67f, 0x61f, 0x5bf, 0x55f, 0x4ff, 0x49f, 0x43f, 0x3df, 0x37f, 0x31f, 0x2bf, 0x25f, 0x1ff, 0x19f, 0x13f, 0xdf, 0x7f, 0x1f, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; #endif // AUDIO_DAC_SAMPLE_WAVEFORM_TRAPEZOID -static dacsample_t dac_buffer_empty[AUDIO_DAC_BUFFER_SIZE] = {AUDIO_DAC_OFF_VALUE}; +static dacsample_t dac_buffer[AUDIO_DAC_BUFFER_SIZE]; /* keep track of the sample position for for each frequency */ static float dac_if[AUDIO_MAX_SIMULTANEOUS_TONES] = {0.0}; @@ -121,24 +121,27 @@ __attribute__((weak)) uint16_t dac_value_generate(void) { /* doing additive wave synthesis over all currently playing tones = adding up * sine-wave-samples for each frequency, scaled by the number of active tones */ - uint16_t value = 0; - float frequency = 0.0f; + uint_fast16_t value = 0; + float frequency = 0.0f; - for (uint8_t i = 0; i < active_tones_snapshot_length; i++) { + for (size_t i = 0; i < active_tones_snapshot_length; i++) { /* Note: a user implementation does not have to rely on the active_tones_snapshot, but * could directly query the active frequencies through audio_get_processed_frequency */ frequency = active_tones_snapshot[i]; - dac_if[i] = dac_if[i] + ((frequency * AUDIO_DAC_BUFFER_SIZE) / AUDIO_DAC_SAMPLE_RATE) * 2 / 3; + float new_dac_if = dac_if[i]; + new_dac_if += frequency * ((float)AUDIO_DAC_BUFFER_SIZE / AUDIO_DAC_SAMPLE_RATE * 2.0f / 3.0f); /*Note: the 2/3 are necessary to get the correct frequencies on the * DAC output (as measured with an oscilloscope), since the gpt * timer runs with 3*AUDIO_DAC_SAMPLE_RATE; and the DAC callback * is called twice per conversion.*/ - dac_if[i] = fmodf(dac_if[i], AUDIO_DAC_BUFFER_SIZE); + while (new_dac_if >= AUDIO_DAC_BUFFER_SIZE) + new_dac_if -= AUDIO_DAC_BUFFER_SIZE; + dac_if[i] = new_dac_if; // Wavetable generation/lookup - uint16_t dac_i = (uint16_t)dac_if[i]; + size_t dac_i = (size_t)new_dac_if; #if defined(AUDIO_DAC_SAMPLE_WAVEFORM_SINE) value += dac_buffer_sine[dac_i] / active_tones_snapshot_length; @@ -308,10 +311,17 @@ void audio_driver_initialize(void) { DACD1.params->dac->CR &= ~DAC_CR_BOFF1; DACD2.params->dac->CR &= ~DAC_CR_BOFF2; + /* Start the DAC output with all off values. This buffer will then get fed + * with samples from dac_end, which will play notes. + */ + for (size_t i = 0; i < AUDIO_DAC_BUFFER_SIZE; i++) { + dac_buffer[i] = AUDIO_DAC_OFF_VALUE; + } + if (AUDIO_PIN == A4) { - dacStartConversion(&DACD1, &dac_conv_cfg, dac_buffer_empty, AUDIO_DAC_BUFFER_SIZE); + dacStartConversion(&DACD1, &dac_conv_cfg, dac_buffer, AUDIO_DAC_BUFFER_SIZE); } else if (AUDIO_PIN == A5) { - dacStartConversion(&DACD2, &dac_conv_cfg, dac_buffer_empty, AUDIO_DAC_BUFFER_SIZE); + dacStartConversion(&DACD2, &dac_conv_cfg, dac_buffer, AUDIO_DAC_BUFFER_SIZE); } // no inverted/out-of-phase waveform (yet?), only pulling AUDIO_PIN_ALT to AUDIO_DAC_OFF_VALUE diff --git a/platforms/chibios/drivers/serial.c b/platforms/chibios/drivers/serial.c index f087d0c2ed..f199716a2b 100644 --- a/platforms/chibios/drivers/serial.c +++ b/platforms/chibios/drivers/serial.c @@ -10,7 +10,7 @@ #include <hal.h> // TODO: resolve/remove build warnings -#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) && defined(PROTOCOL_CHIBIOS) && defined(WS2812_DRIVER_BITBANG) +#if defined(RGBLIGHT_ENABLE) && defined(RGBLED_SPLIT) && defined(PROTOCOL_CHIBIOS) && defined(WS2812_BITBANG) # warning "RGBLED_SPLIT not supported with bitbang WS2812 driver" #endif diff --git a/platforms/chibios/drivers/spi_master.c b/platforms/chibios/drivers/spi_master.c index c3ab0623f0..481a2e422a 100644 --- a/platforms/chibios/drivers/spi_master.c +++ b/platforms/chibios/drivers/spi_master.c @@ -18,14 +18,14 @@ #include "timer.h" -static pin_t currentSlavePin = NO_PIN; +static bool spiStarted = false; -#if defined(K20x) || defined(KL2x) || defined(RP2040) -static SPIConfig spiConfig = {NULL, 0, 0, 0}; -#else -static SPIConfig spiConfig = {false, NULL, 0, 0, 0, 0}; +#if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE +static pin_t currentSlavePin; #endif +static SPIConfig spiConfig; + __attribute__((weak)) void spi_init(void) { static bool is_initialised = false; if (!is_initialised) { @@ -33,28 +33,45 @@ __attribute__((weak)) void spi_init(void) { // Try releasing special pins for a short time setPinInput(SPI_SCK_PIN); - setPinInput(SPI_MOSI_PIN); - setPinInput(SPI_MISO_PIN); + if (SPI_MOSI_PIN != NO_PIN) { + setPinInput(SPI_MOSI_PIN); + } + if (SPI_MISO_PIN != NO_PIN) { + setPinInput(SPI_MISO_PIN); + } chThdSleepMilliseconds(10); #if defined(USE_GPIOV1) palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), SPI_SCK_PAL_MODE); - palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_PAL_MODE); - palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_PAL_MODE); + if (SPI_MOSI_PIN != NO_PIN) { + palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_PAL_MODE); + } + if (SPI_MISO_PIN != NO_PIN) { + palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_PAL_MODE); + } #else palSetPadMode(PAL_PORT(SPI_SCK_PIN), PAL_PAD(SPI_SCK_PIN), SPI_SCK_FLAGS); - palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_FLAGS); - palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_FLAGS); + if (SPI_MOSI_PIN != NO_PIN) { + palSetPadMode(PAL_PORT(SPI_MOSI_PIN), PAL_PAD(SPI_MOSI_PIN), SPI_MOSI_FLAGS); + } + if (SPI_MISO_PIN != NO_PIN) { + palSetPadMode(PAL_PORT(SPI_MISO_PIN), PAL_PAD(SPI_MISO_PIN), SPI_MISO_FLAGS); + } #endif spiStop(&SPI_DRIVER); - currentSlavePin = NO_PIN; + spiStarted = false; } } bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) { - if (currentSlavePin != NO_PIN || slavePin == NO_PIN) { + if (spiStarted) { + return false; + } +#if SPI_SELECT_MODE != SPI_SELECT_MODE_NONE + if (slavePin == NO_PIN) { return false; } +#endif #if !(defined(WB32F3G71xx) || defined(WB32FQ95xx)) uint16_t roundedDivisor = 2; @@ -247,13 +264,29 @@ bool spi_start(pin_t slavePin, bool lsbFirst, uint8_t mode, uint16_t divisor) { } #endif - currentSlavePin = slavePin; + spiStarted = true; +#if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE + currentSlavePin = slavePin; +#endif +#if SPI_SELECT_MODE == SPI_SELECT_MODE_PAD spiConfig.ssport = PAL_PORT(slavePin); spiConfig.sspad = PAL_PAD(slavePin); - setPinOutput(slavePin); +#elif SPI_SELECT_MODE == SPI_SELECT_MODE_NONE + if (slavePin != NO_PIN) { + setPinOutput(slavePin); + } +#else +# error "Unsupported SPI_SELECT_MODE" +#endif + spiStart(&SPI_DRIVER, &spiConfig); spiSelect(&SPI_DRIVER); +#if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE + if (slavePin != NO_PIN) { + writePinLow(slavePin); + } +#endif return true; } @@ -283,9 +316,14 @@ spi_status_t spi_receive(uint8_t *data, uint16_t length) { } void spi_stop(void) { - if (currentSlavePin != NO_PIN) { + if (spiStarted) { +#if SPI_SELECT_MODE == SPI_SELECT_MODE_NONE + if (currentSlavePin != NO_PIN) { + writePinHigh(currentSlavePin); + } +#endif spiUnselect(&SPI_DRIVER); spiStop(&SPI_DRIVER); - currentSlavePin = NO_PIN; + spiStarted = false; } } diff --git a/platforms/chibios/drivers/vendor/RP/RP2040/ws2812_vendor.c b/platforms/chibios/drivers/vendor/RP/RP2040/ws2812_vendor.c index 8d59e13bb2..de317e269a 100644 --- a/platforms/chibios/drivers/vendor/RP/RP2040/ws2812_vendor.c +++ b/platforms/chibios/drivers/vendor/RP/RP2040/ws2812_vendor.c @@ -268,7 +268,7 @@ static inline void sync_ws2812_transfer(void) { busy_wait_until(LAST_TRANSFER); } -void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { +void ws2812_setleds(rgb_led_t* ledarray, uint16_t leds) { static bool is_initialized = false; if (unlikely(!is_initialized)) { is_initialized = ws2812_init(); diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_efl_config.h b/platforms/chibios/drivers/wear_leveling/wear_leveling_efl_config.h index e74cf85efd..0f0fa694e9 100644 --- a/platforms/chibios/drivers/wear_leveling/wear_leveling_efl_config.h +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_efl_config.h @@ -50,5 +50,5 @@ // 1kB logical EEPROM #ifndef WEAR_LEVELING_LOGICAL_SIZE -# define WEAR_LEVELING_LOGICAL_SIZE 1024 +# define WEAR_LEVELING_LOGICAL_SIZE ((WEAR_LEVELING_BACKING_SIZE) / 2) #endif // WEAR_LEVELING_LOGICAL_SIZE diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash.c b/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash.c index 640628e1e9..6624c30b5b 100644 --- a/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash.c +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash.c @@ -25,8 +25,8 @@ #define FLASHCMD_READ_STATUS 0x05 #define FLASHCMD_WRITE_ENABLE 0x06 -extern uint8_t BOOT2_ROM[256]; -static uint32_t BOOT2_ROM_RAM[64]; +extern const uint8_t BOOT2_ROM[256]; +static uint32_t BOOT2_ROM_RAM[64]; static ssi_hw_t *const ssi = (ssi_hw_t *)XIP_SSI_BASE; diff --git a/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash_config.h b/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash_config.h index 93a9aa0372..e1e2433601 100644 --- a/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash_config.h +++ b/platforms/chibios/drivers/wear_leveling/wear_leveling_rp2040_flash_config.h @@ -18,7 +18,7 @@ // 32kB logical EEPROM #ifndef WEAR_LEVELING_LOGICAL_SIZE -# define WEAR_LEVELING_LOGICAL_SIZE 4096 +# define WEAR_LEVELING_LOGICAL_SIZE ((WEAR_LEVELING_BACKING_SIZE) / 2) #endif // WEAR_LEVELING_LOGICAL_SIZE // Define how much flash space we have (defaults to lib/pico-sdk/src/boards/include/boards/***) diff --git a/platforms/chibios/drivers/ws2812_bitbang.c b/platforms/chibios/drivers/ws2812_bitbang.c index c55e0f654c..e3b735a1a6 100644 --- a/platforms/chibios/drivers/ws2812_bitbang.c +++ b/platforms/chibios/drivers/ws2812_bitbang.c @@ -72,7 +72,7 @@ void ws2812_init(void) { } // Setleds for standard RGB -void ws2812_setleds(LED_TYPE *ledarray, uint16_t leds) { +void ws2812_setleds(rgb_led_t *ledarray, uint16_t leds) { static bool s_init = false; if (!s_init) { ws2812_init(); diff --git a/platforms/chibios/drivers/ws2812_pwm.c b/platforms/chibios/drivers/ws2812_pwm.c index cfee547a82..440687bd72 100644 --- a/platforms/chibios/drivers/ws2812_pwm.c +++ b/platforms/chibios/drivers/ws2812_pwm.c @@ -379,7 +379,7 @@ void ws2812_write_led_rgbw(uint16_t led_number, uint8_t r, uint8_t g, uint8_t b, } // Setleds for standard RGB -void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { +void ws2812_setleds(rgb_led_t* ledarray, uint16_t leds) { static bool s_init = false; if (!s_init) { ws2812_init(); diff --git a/platforms/chibios/drivers/ws2812_spi.c b/platforms/chibios/drivers/ws2812_spi.c index f188576e04..01162f07f4 100644 --- a/platforms/chibios/drivers/ws2812_spi.c +++ b/platforms/chibios/drivers/ws2812_spi.c @@ -6,8 +6,8 @@ /* Adapted from https://github.com/gamazeps/ws2812b-chibios-SPIDMA/ */ // Define the spi your LEDs are plugged to here -#ifndef WS2812_SPI -# define WS2812_SPI SPID1 +#ifndef WS2812_SPI_DRIVER +# define WS2812_SPI_DRIVER SPID1 #endif #ifndef WS2812_SPI_MOSI_PAL_MODE @@ -106,7 +106,7 @@ static uint8_t get_protocol_eq(uint8_t data, int pos) { return eq; } -static void set_led_color_rgb(LED_TYPE color, int pos) { +static void set_led_color_rgb(rgb_led_t color, int pos) { uint8_t* tx_start = &txbuf[PREAMBLE_SIZE]; #if (WS2812_BYTE_ORDER == WS2812_BYTE_ORDER_GRB) @@ -179,15 +179,15 @@ void ws2812_init(void) { #endif }; - spiAcquireBus(&WS2812_SPI); /* Acquire ownership of the bus. */ - spiStart(&WS2812_SPI, &spicfg); /* Setup transfer parameters. */ - spiSelect(&WS2812_SPI); /* Slave Select assertion. */ + spiAcquireBus(&WS2812_SPI_DRIVER); /* Acquire ownership of the bus. */ + spiStart(&WS2812_SPI_DRIVER, &spicfg); /* Setup transfer parameters. */ + spiSelect(&WS2812_SPI_DRIVER); /* Slave Select assertion. */ #ifdef WS2812_SPI_USE_CIRCULAR_BUFFER - spiStartSend(&WS2812_SPI, ARRAY_SIZE(txbuf), txbuf); + spiStartSend(&WS2812_SPI_DRIVER, ARRAY_SIZE(txbuf), txbuf); #endif } -void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { +void ws2812_setleds(rgb_led_t* ledarray, uint16_t leds) { static bool s_init = false; if (!s_init) { ws2812_init(); @@ -202,9 +202,9 @@ void ws2812_setleds(LED_TYPE* ledarray, uint16_t leds) { // Instead spiSend can be used to send synchronously (or the thread logic can be added back). #ifndef WS2812_SPI_USE_CIRCULAR_BUFFER # ifdef WS2812_SPI_SYNC - spiSend(&WS2812_SPI, ARRAY_SIZE(txbuf), txbuf); + spiSend(&WS2812_SPI_DRIVER, ARRAY_SIZE(txbuf), txbuf); # else - spiStartSend(&WS2812_SPI, ARRAY_SIZE(txbuf), txbuf); + spiStartSend(&WS2812_SPI_DRIVER, ARRAY_SIZE(txbuf), txbuf); # endif #endif } diff --git a/platforms/chibios/vendors/RP/stage2_bootloaders.c b/platforms/chibios/vendors/RP/stage2_bootloaders.c index e65b0a5802..131fa0ce9e 100644 --- a/platforms/chibios/vendors/RP/stage2_bootloaders.c +++ b/platforms/chibios/vendors/RP/stage2_bootloaders.c @@ -13,7 +13,7 @@ #if defined(RP2040_FLASH_AT25SF128A) -uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { +const uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { 0x00, 0xb5, 0x31, 0x4b, 0x21, 0x20, 0x58, 0x60, 0x98, 0x68, 0x02, 0x21, 0x88, 0x43, 0x98, 0x60, 0xd8, 0x60, 0x18, 0x61, 0x58, 0x61, 0x2d, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x04, 0x21, 0x59, 0x61, 0x01, 0x21, 0xf0, 0x22, @@ -40,7 +40,7 @@ uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { #elif defined(RP2040_FLASH_GD25Q64CS) -uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { +const uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { 0x00, 0xb5, 0x31, 0x4b, 0x21, 0x20, 0x58, 0x60, 0x98, 0x68, 0x02, 0x21, 0x88, 0x43, 0x98, 0x60, 0xd8, 0x60, 0x18, 0x61, 0x58, 0x61, 0x2d, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x04, 0x21, 0x59, 0x61, 0x01, 0x21, 0xf0, 0x22, @@ -67,7 +67,7 @@ uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { #elif defined(RP2040_FLASH_W25X10CL) -uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { +const uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { 0x00, 0xb5, 0x14, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x04, 0x21, 0x59, 0x61, 0x12, 0x49, 0x19, 0x60, 0x00, 0x21, 0x59, 0x60, 0x11, 0x49, 0x12, 0x48, 0x01, 0x60, 0x01, 0x21, 0x99, 0x60, 0xbb, 0x21, 0x19, 0x66, 0x02, 0x21, @@ -94,7 +94,7 @@ uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { #elif defined(RP2040_FLASH_IS25LP080) -uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { +const uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { 0x00, 0xb5, 0x2b, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x04, 0x21, 0x59, 0x61, 0x29, 0x49, 0x19, 0x60, 0x01, 0x21, 0x99, 0x60, 0x28, 0x48, 0x00, 0xf0, 0x42, 0xf8, 0x28, 0x4a, 0x90, 0x42, 0x12, 0xd0, 0x06, 0x21, 0x19, 0x66, @@ -121,7 +121,7 @@ uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { #elif defined(RP2040_FLASH_GENERIC_03H) -uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { +const uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { 0x00, 0xb5, 0x0c, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x04, 0x21, 0x59, 0x61, 0x0a, 0x49, 0x19, 0x60, 0x0a, 0x49, 0x0b, 0x48, 0x01, 0x60, 0x00, 0x21, 0x59, 0x60, 0x01, 0x21, 0x99, 0x60, 0x01, 0xbc, 0x00, 0x28, 0x00, 0xd0, @@ -148,7 +148,7 @@ uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { #else -uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { +const uint8_t BOOTLOADER_SECTION BOOT2_ROM[256] = { 0x00, 0xb5, 0x32, 0x4b, 0x21, 0x20, 0x58, 0x60, 0x98, 0x68, 0x02, 0x21, 0x88, 0x43, 0x98, 0x60, 0xd8, 0x60, 0x18, 0x61, 0x58, 0x61, 0x2e, 0x4b, 0x00, 0x21, 0x99, 0x60, 0x04, 0x21, 0x59, 0x61, 0x01, 0x21, 0xf0, 0x22, diff --git a/platforms/test/timer.c b/platforms/test/timer.c index 320cc57782..eb929d7dac 100644 --- a/platforms/test/timer.c +++ b/platforms/test/timer.c @@ -17,34 +17,65 @@ #include "timer.h" #include <stdatomic.h> -static atomic_uint_least32_t current_time = 0; +static atomic_uint_least32_t current_time = 0; +static atomic_uint_least32_t async_tick_amount = 0; +static atomic_uint_least32_t access_counter = 0; + +void simulate_async_tick(uint32_t t) { + async_tick_amount = t; +} + +uint32_t timer_read_internal(void) { + return current_time; +} + +uint32_t current_access_counter(void) { + return access_counter; +} + +void reset_access_counter(void) { + access_counter = 0; +} void timer_init(void) { - current_time = 0; + current_time = 0; + async_tick_amount = 0; + access_counter = 0; } void timer_clear(void) { - current_time = 0; + current_time = 0; + async_tick_amount = 0; + access_counter = 0; } uint16_t timer_read(void) { - return current_time & 0xFFFF; + return (uint16_t)timer_read32(); } + uint32_t timer_read32(void) { + if (access_counter++ > 0) { + current_time += async_tick_amount; + } return current_time; } + uint16_t timer_elapsed(uint16_t last) { return TIMER_DIFF_16(timer_read(), last); } + uint32_t timer_elapsed32(uint32_t last) { return TIMER_DIFF_32(timer_read32(), last); } void set_time(uint32_t t) { - current_time = t; + current_time = t; + access_counter = 0; } + void advance_time(uint32_t ms) { current_time += ms; + access_counter = 0; } void wait_ms(uint32_t ms) { |