Skip to content

stm32wlex: ADC_CHSELR0_CHSELx constants off by 1 #5712

Description

@bemasher

While implementing ADC for an STM32WLE5, I discovered the CHSELx constants and helper functions for ADC_CHSELR0 are all off by one in position.

The SVD appears to have the correct definitions:
https://github.com/tinygo-org/stm32-svd/blob/main/svd/stm32wle5.svd#L1340-L1372

RM0461 Rev 10, pg 469 shows CHSEL0 starts at position 0:
Image

Running tools/gen-device-svd on stm32wle5.svd gives:

// ADC.CHSELR0: channel selection register
func (o *ADC_Type) SetCHSELR0_CHSEL0( value uint32) {
	volatile.StoreUint32(&o.CHSELR0.Reg, volatile.LoadUint32(&o.CHSELR0.Reg)&^(0x2)|value<<1)
}
func (o *ADC_Type) GetCHSELR0_CHSEL0() uint32 {
	return (volatile.LoadUint32(&o.CHSELR0.Reg)&0x2) >> 1
}
[...]

and

// CHSELR0: channel selection register
// Position of CHSEL0 field.
ADC_CHSELR0_CHSEL0_Pos = 0x1
// Bit mask of CHSEL0 field.
ADC_CHSELR0_CHSEL0_Msk = 0x2
// Bit CHSEL0.
ADC_CHSELR0_CHSEL0 = 0x2
// Input Channel is not selected for conversion
ADC_CHSELR0_CHSEL0_NotSelected = 0x0
// Input Channel is selected for conversion
ADC_CHSELR0_CHSEL0_Selected = 0x1
[...]

My best guess is that lsb and msb are both incremented before any constants are generated for field ranges, instead of after, so the first field is always at _Pos 0x1 plus some constant if it's in the middle of the register somewhere. This appears to affect all field ranges, not just those I found in ADC_CHSELR0.
https://github.com/tinygo-org/tinygo/blob/dev/tools/gen-device-svd/gen-device-svd.go#L689-L692

To test, I wrapped the lsb and msb increment in a deferred function so it happens last, and this does appear to correct the issue, but affects quite a few constants and helpers in the generated file, so I am working on verifying the changes are all correct according to the reference manual.

I'm not on the latest tinygo, but it appears this code is generated independently.

tinygo version 0.41.1 windows/amd64 (using go version go1.26.3 and LLVM version 20.1.1)

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions