Skip to content

Commit 6e0273f

Browse files
committed
codal_app: Ensure pin is in analog-out mode before changing period.
Signed-off-by: Damien George <[email protected]>
1 parent 30b86a5 commit 6e0273f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/codal_app/microbithal.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,22 @@ int microbit_hal_pin_get_pull(int pin) {
114114
}
115115

116116
int microbit_hal_pin_set_analog_period_us(int pin, int period) {
117+
// Change the audio virtual-pin period if the pin is the special mixer pin.
117118
if (pin == MICROBIT_HAL_PIN_MIXER) {
118119
uBit.audio.virtualOutputPin.setAnalogPeriodUs(period);
119120
return 0;
120121
}
122+
123+
// Calling setAnalogPeriodUs requires the pin to be in analog-out mode. So
124+
// test for this mode by first calling getAnalogPeriodUs, and if it fails then
125+
// attempt to configure the pin in analog-out mode by calling setAnalogValue.
126+
if ((ErrorCode)pin_obj[pin]->getAnalogPeriodUs() == DEVICE_NOT_SUPPORTED) {
127+
if (pin_obj[pin]->setAnalogValue(0) != DEVICE_OK) {
128+
return -1;
129+
}
130+
}
131+
132+
// Set the analog period.
121133
if (pin_obj[pin]->setAnalogPeriodUs(period) == DEVICE_OK) {
122134
return 0;
123135
} else {

0 commit comments

Comments
 (0)