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

ADC is in not working on bluepill #4691

Open
grudzinski opened this issue Jan 8, 2025 · 2 comments
Open

ADC is in not working on bluepill #4691

grudzinski opened this issue Jan 8, 2025 · 2 comments
Labels

Comments

@grudzinski
Copy link

Hi!

I have a simple program that reads a value from ADC:

package main

import (
	"machine"
)

func main() {
	machine.InitADC()
	led := machine.LED
	led.Configure(machine.PinConfig{Mode: machine.PinOutput})
	sensor := machine.ADC{machine.ADC2}
	sensor.Configure(machine.ADCConfig{})
	led.High()
	val := sensor.Get()
	led.Low()
	_ = val
	for ;; {
	}
}

But it gets stuck on "wait for conversion to complete":

(gdb) backtrace 
#0  0x080014ca in runtime/volatile.LoadUint32 (addr=<optimized out>) at /usr/local/lib/tinygo/src/runtime/volatile/volatile.go:25
#1  0x080014ec in (*runtime/volatile.Register32).Get (r=0x0) at /usr/local/lib/tinygo/src/runtime/volatile/register.go:142
#2  0x080014fc in (*runtime/volatile.Register32).HasBits (r=0x0, value=2) at /usr/local/lib/tinygo/src/runtime/volatile/register.go:181
#3  0x08004146 in (machine.ADC).Get (a=...) at /usr/local/lib/tinygo/src/machine/machine_stm32_adc_f1.go:67
#4  0x0800c8aa in main.main () at /home/roman/Source/github.com/grudzinski/learn-tinygo-stm32/main.go:14

My env:

roman@alienware:~$ tinygo version
tinygo version 0.35.0 linux/amd64 (using go version go1.23.4 and LLVM version 18.1.2)
@grudzinski grudzinski changed the title ADC in not working on bluepill ADC is in not working on bluepill Jan 9, 2025
@grudzinski
Copy link
Author

Trying to fix this myself, seems to have found some issues but it still doesn't fix it:

  • InitADC is calling to enableAltFuncClock but there is no case for stm32.RCC.SetAPB2ENR_ADC1EN(stm32.RCC_APB2ENR_ADC1EN_Enabled) in the func.
  • Looks like ADC prescaler is not set, APB2 prescaler is set to 2, it means ADC works on 18Mhz, but max allowed is 14Mhz. stm32.RCC.SetCFGR_ADCPRE(stm32.RCC_CFGR_ADCPRE_Div4)

@grudzinski
Copy link
Author

Fixed it https://github.com/tinygo-org/tinygo/pull/4702/files, please CR it. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants