-
Notifications
You must be signed in to change notification settings - Fork 919
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
Fix stm32f103 ADC #4702
Fix stm32f103 ADC #4702
Conversation
@@ -221,14 +221,19 @@ func (p Pin) enableClock() { | |||
|
|||
// Enable peripheral clock. Expand to include all the desired peripherals | |||
func enableAltFuncClock(bus unsafe.Pointer) { | |||
if bus == unsafe.Pointer(stm32.USART1) { | |||
switch bus { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
stm32.FLASH.ACR.SetBits(stm32.FLASH_ACR_LATENCY_WS2) // Two wait states, per datasheet | ||
stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PPRE1_Div2 << stm32.RCC_CFGR_PPRE1_Pos) // prescale PCLK1 = HCLK/2 | ||
stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_PPRE2_Div1 << stm32.RCC_CFGR_PPRE2_Pos) // prescale PCLK2 = HCLK/1 | ||
stm32.RCC.CFGR.SetBits(stm32.RCC_CFGR_ADCPRE_Div6 << stm32.RCC_CFGR_ADCPRE_Pos) // prescale ADCCLK = PCLK2/6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Max ADC clock is 14Mhz, set it to 12MHz
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_SPI1EN) | ||
case unsafe.Pointer(stm32.ADC1): | ||
stm32.RCC.APB2ENR.SetBits(stm32.RCC_APB2ENR_ADC1EN) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enable clock for ADC1
@@ -23,6 +23,9 @@ func InitADC() { | |||
// Enable ADC clock | |||
enableAltFuncClock(unsafe.Pointer(stm32.ADC1)) | |||
|
|||
// enable | |||
stm32.ADC1.CR2.SetBits(stm32.ADC_CR2_ADON) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First switch ON the ADC, only now we can touch the ADC registers
@@ -61,7 +52,7 @@ func (a ADC) Get() uint16 { | |||
stm32.ADC1.SQR3.SetBits(ch) | |||
|
|||
// start conversion | |||
stm32.ADC1.CR2.SetBits(stm32.ADC_CR2_SWSTART) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It work only if EXTTRIG is enabled and SWSTART is selected in EXTSEL.
@@ -70,12 +61,6 @@ func (a ADC) Get() uint16 { | |||
// read result as 16 bit value | |||
result := uint16(stm32.ADC1.DR.Get()) << 4 | |||
|
|||
// clear flag | |||
stm32.ADC1.SR.ClearBits(stm32.ADC_SR_EOC) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
EOC is cleared by reading the ADC_DR.
@@ -23,15 +23,6 @@ func InitADC() { | |||
// Enable ADC clock | |||
enableAltFuncClock(unsafe.Pointer(stm32.ADC1)) | |||
|
|||
// set scan mode |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the chip reset the ADC is already configured for "Single conversion mode". Scan mode is relevant when DMA is used.
stm32.ADC1.SR.ClearBits(stm32.ADC_SR_EOC) | ||
|
||
// clear rank | ||
stm32.ADC1.SQR3.ClearBits(ch) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant. It sets it to 0, means 0 channel.
Thank you for the fixes @grudzinski now squash/merging. |
This PR fixes the issue ADC is in not working on bluepill #4691