Hi all,
I have a bizarre behaviour for a program running on a STM32F103: I’m using tone()
to generate a notification on a buzzer and it works fine when I run the program in debug
mode, but fails to generate any sounds in release
.
The platformio.ini
is this one:
[env:genericSTM32F103CB]
platform = ststm32
board = genericSTM32F103CB
framework = arduino
board_build.core = maple
lib_deps = robotis-git/Dynamixel2Arduino
upload_protocol = stlink
debug_tool = stlink
upload_flags = -c set CPUTAPID 0x2ba01477
debug_server =
/Users/Alex/.platformio/packages/tool-openocd/bin/openocd
-s /Users/Alex/.platformio/packages/tool-openocd/scripts
-f interface/stlink.cfg
-c "transport select hla_swd"
-c "set CPUTAPID 0x2ba01477"
-f target/stm32f1x.cfg
-c "reset_config none"
(I’m using a breadboarded blue pill with a Chinese clone for prototyping, hence the CPUTAPID
usage.)
The code is basically a battery voltage indicator: reads a voltage on PA6 and compares it with a number of thresholds to light 4 LEDs indicating the degree of charge. Under a certain threshold the LED 1 will blink and a buzzer will sound with a 0.5s cycle (0.5s on, 0.5s off).
the loop()
looks like this:
void loop()
{
...
// compute state
raw = analogRead(PA6);
volt_sum += raw;
num_samples++;
if (num_samples == 10) {
compute_state(volt_sum/10);
display_state();
volt_sum = 0;
num_samples = 0;
}
...
}
The compute_state()
is not that interesting, it just computes a state based on the raw voltage and the thresholds, taking into account a hysteresis effect and puts a global variable v_state
into indicating 4 LEDs, 3 LEDs, 2 LEDs, 1 LED or 1 LED blinking.
The display_state()
looks like this:
void display_state()
{
switch (v_state)
{
case STATE4:
digitalWrite(PB5, LOW);
digitalWrite(PB6, LOW);
digitalWrite(PB7, LOW);
digitalWrite(PB8, LOW);
noTone(PB9);
break;
case STATE3:
digitalWrite(PB5, HIGH);
digitalWrite(PB6, LOW);
digitalWrite(PB7, LOW);
digitalWrite(PB8, LOW);
noTone(PB9);
break;
case STATE2:
digitalWrite(PB5, HIGH);
digitalWrite(PB6, HIGH);
digitalWrite(PB7, LOW);
digitalWrite(PB8, LOW);
noTone(PB9);
break;
case STATE1:
digitalWrite(PB5, HIGH);
digitalWrite(PB6, HIGH);
digitalWrite(PB7, HIGH);
digitalWrite(PB8, LOW);
noTone(PB9);
break;
case STATE1B:
digitalWrite(PB5, HIGH);
digitalWrite(PB6, HIGH);
digitalWrite(PB7, HIGH);
if ((millis() % 1000) < 500) {
digitalWrite(PB8, LOW);
tone(PB9, 1000);
}
else {
digitalWrite(PB8, HIGH);
noTone(PB9);
}
break;
default:
break;
}
}
As I mentioned at the beginning of the note, if the program is loaded and runs in release
mode the LEDs behaviour is fine, including the blinking, but no sound is heard in the buzzer. If I run the code in debug over the stlink
I can hear the buzzer working properly.
Any idea where this might be coming from, or how it can be fixed?
Thanks.