Skip to content

tools/gen-device-svd: Increment lsb/msb last in bitfield ranges. - #5821

Open
bemasher wants to merge 3 commits into
tinygo-org:devfrom
bemasher:gen-svd
Open

bemasher wants to merge 3 commits into
tinygo-org:devfrom
bemasher:gen-svd

Conversation

@bemasher

@bemasher bemasher commented Oct 3, 2026 •

Copy link
Copy Markdown

Fixes #5712 in gen-device-svd.

Registers with bitfield ranges were incorrectly generated. This corrects _Pos, _Msk constants, and accessors in the following device families and peripherals:

Family Peripheral
ESP32 (H2, S2, S3, C3, C6) RMT
Kendryte K210 APU, DMAC, GPIO, GPIOHS
STM32C0 ADC, DMA, DMAMUX, GPIO, TIM
STM32F0 ADC, CAN, CAN_RX, CAN_TX, DAC, DMA, GPIO, TIM, TSC
STM32F1 ADC, CAN, CAN_RX, CAN_TX, DAC, DMA, EXTI, GPIO, TIM
STM32F2 ADC, CAN, CAN_RX, CAN_TX, DAC, DCMI, EXTI, FLASH, GPIO, RTC, TIM
STM32F3 ADC, CAN, CAN_RX, CAN_TX, DAC, DMA, GPIO, HRTIM, RTC, TIM, TSC
STM32F4 ADC, CAN, CAN_RX, CAN_TX, DAC, DCMI, DFSDM, DFSDM_FLT, DSIHOST, EXTI, FLASH, GPIO, RTC, TIM
STM32F7 ADC, CAN, CAN_RX, CAN_TX, DAC, DCMI, DFSDM, DFSDM_FLT, DSI, EXTI, FLASH, GPIO, RTC, TIM
STM32G0 ADC, DAC, DMA, DMAMUX, GPIO, RTC, TIM
STM32G4 ADC, DAC, DMA, DMAMUX, FMC, GPIO, HRTIM, RTC, SAI, TIM
STM32H5 ADC, DAC, DCMI, FMC, GPDMA, GPIO, GTZC, RTC, TIM
STM32H7 ADC, BDMA, DAC, DCMI, DFSDM, DFSDM_FLT, DMAMUX, DSIHOST, GPDMA, GPIO, HPDMA, HRTIM, HSEM, MDMA, RTC, SAI, TIM
STM32L0 ADC, DAC, DMA, GPIO, RTC, TIM, TSC
STM32L1 ADC, DAC, DMA, EXTI, GPIO, RTC, TIM
STM32L4 ADC, CAN, CAN_RX, CAN_TX, DAC, DCMI, DFSDM, DFSDM_FLT, DMA, DMAMUX, DSI, FMC, GPIO, RTC, TIM, TSC
STM32L5 ADC, DAC, DFSDM, DFSDM_FLT, DMA, DMAMUX, FMC, GPIO, RTC, SAI, TIM, TSC
STM32MP DSIHOST1, GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG, GPIOH, GPIOI, GPIOJ, GPIOK, RTC
STM32N6 BSEC, GPIO
STM32U0 DMA, DMAMUX, GPIO, RTC, TIM, TSC
STM32U5 ADC, DAC, DCMI, FMC, GPDMA, GPIO, LPDMA, LPGPIO, RTC, TIM, TSC
STM32WB ADC, DMA, DMAMUX, GPDMA, GPIO, HSEM, RTC, SAI, TIM, TSC
STM32WL ADC, DMA, DMAMUX, GPIO, HSEM, RTC, TIM

@bemasher bemasher changed the title Increment lsb/msb last in bitfield ranges. tools/gen-device-svd: Increment lsb/msb last in bitfield ranges. Oct 3, 2026
@davecheney

Copy link
Copy Markdown
Contributor

@bemasher TYVM, please consider pulling this test bemasher#1

@davecheney davecheney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. thank you. Someone else must approve

@deadprogram

Copy link
Copy Markdown
Member

Thanks for tracking this down @bemasher and thanks @davecheney for the test. I regenerated the device files before and after this change and took a good look. The following is edited from an automated review:

  1. rangeElems already passes the per element offset as the second callback argument, and this is its only caller. Perhaps use it instead of mutating lsb/msb? This gives the same output for stm32 and Espressif and drops the da != nil check.
da.rangeElems(func(ia int, offset uint32) bool {
	lsb, msb := lsb+offset, msb+offset
  1. The newer Espressif SVDs also change (esp32c3, esp32c6, esp32h2, esp32s2, esp32s3), all in the RMT interrupt fields. No machine code uses those. Perhaps mention them in the description?

  2. This also fixes some machine code that used the wrong constants. In machine_stm32_moder_gpio.go the I2C pin setup used GPIO_OTYPER_OT0_Msk (was 0x2), so it cleared the open drain bit of the next pin. The F1/F4 ADC sample time code used ADC_SMPR2_SMP1_Pos (was 6, now 3). The local workarounds in machine_stm32_tim.go and the ADC_SQR1_SQ1_Pos note in machine_stm32_adc_u5.go can be removed in a follow up.

How does that sound?

@bemasher

bemasher commented Oct 9, 2026 •

Copy link
Copy Markdown
Author

Thanks for the suggestions.

I've updated the original description of the pull request to describe the families and peripherals affected from parsing git's diff for the generated device files and used the offset parameter to set lsb and msb in each call.

I'll review machine source for affected accessor and constant use to see if any need fixes in a separate pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

stm32wlex: ADC_CHSELR0_CHSELx constants off by 1

3 participants