Skip to content

Commit 08c5dbb

Browse files
committed
use return values in STM PWMOut constructor, not exceptions
1 parent 7029783 commit 08c5dbb

File tree

6 files changed

+56
-31
lines changed

6 files changed

+56
-31
lines changed

ports/esp32s2/boards/adafruit_funhouse/board.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ void board_init(void) {
5454

5555
// Debug UART
5656
#ifdef DEBUG
57-
common_hal_never_reset_pin(&pin_GPIO43);
58-
common_hal_never_reset_pin(&pin_GPIO44);
57+
common_hal_never_reset_pin(&pin_GPIO37);
58+
common_hal_never_reset_pin(&pin_GPIO38);
5959
#endif /* DEBUG */
6060

6161
busio_spi_obj_t* spi = &displays[0].fourwire_bus.inline_bus;
@@ -73,9 +73,13 @@ void board_init(void) {
7373
0, // Polarity
7474
0); // Phase
7575

76+
// workaround as board_init() is called before reset_port() in main.c
77+
pwmout_reset();
78+
7679
displayio_display_obj_t* display = &displays[0].display;
7780
display->base.type = &displayio_display_type;
78-
common_hal_displayio_display_construct(display,
81+
common_hal_displayio_display_construct(
82+
display,
7983
bus,
8084
240, // Width (after rotation)
8185
240, // Height (after rotation)

ports/esp32s2/boards/adafruit_funhouse/pins.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = {
3333

3434
{ MP_ROM_QSTR(MP_QSTR_LIGHT), MP_ROM_PTR(&pin_GPIO18) },
3535

36-
{ MP_ROM_QSTR(MP_QSTR_SPEAKER), MP_ROM_PTR(&pin_GPIO46) },
36+
// { MP_ROM_QSTR(MP_QSTR_SPEAKER), MP_ROM_PTR(&pin_GPIO46) },
3737

3838
{ MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO37) },
3939

ports/esp32s2/common-hal/pwmio/PWMOut.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
9393
}
9494
if (timer_index == INDEX_EMPTY) {
9595
// Running out of timers isn't pin related on ESP32S2 so we can't re-use error messages
96-
mp_raise_ValueError(translate("No more timers available"));
96+
return PWMOUT_ALL_TIMERS_IN_USE;
9797
}
9898

9999
// Find a viable channel
@@ -104,7 +104,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
104104
}
105105
}
106106
if (channel_index == INDEX_EMPTY) {
107-
mp_raise_ValueError(translate("No more channels available"));
107+
return PWMOUT_ALL_CHANNELS_IN_USE;
108108
}
109109

110110
// Run configuration
@@ -126,7 +126,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
126126
self->chan_handle.timer_sel = timer_index;
127127

128128
if (ledc_channel_config(&(self->chan_handle))) {
129-
mp_raise_ValueError(translate("Could not initialize channel"));
129+
return PWMOUT_INITIALIZATION_ERROR;
130130
}
131131

132132
// Make reservations

ports/stm/common-hal/pwmio/PWMOut.c

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ STATIC uint32_t timer_get_internal_duty(uint16_t duty, uint32_t period) {
4848
return (duty*period) / ((1 << 16) - 1);
4949
}
5050

51-
STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler,
51+
STATIC bool timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler,
5252
uint32_t frequency, uint32_t source_freq) {
5353
//Find the largest possible period supported by this frequency
5454
for (int i = 0; i < (1 << 16); i++) {
@@ -58,9 +58,8 @@ STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler,
5858
break;
5959
}
6060
}
61-
if (*prescaler == 0) {
62-
mp_raise_ValueError(translate("Invalid frequency supplied"));
63-
}
61+
// Return successor failure.
62+
return *prescaler != 0;
6463
}
6564

6665
void pwmout_reset(void) {
@@ -138,16 +137,14 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
138137
tim_frequencies[self->tim->tim_index - 1] = frequency;
139138
stm_peripherals_timer_reserve(TIMx);
140139
} else { //no match found
141-
if (tim_chan_taken) {
142-
mp_raise_ValueError(translate("No more timers available on this pin."));
143-
} else if (tim_taken_internal) {
144-
mp_raise_ValueError(translate("Timer was reserved for internal use - declare PWM pins earlier in the program"));
140+
if (tim_chan_taken || tim_taken_internal) {
141+
return PWMOUT_ALL_TIMERS_ON_PIN_IN_USE;
145142
} else if (tim_taken_f_mismatch) {
146-
mp_raise_ValueError(translate("Frequency must match existing PWMOut using this timer"));
143+
return PWMOUT_INVALID_FREQUENCY_ON_PIN;
147144
} else if (var_freq_mismatch) {
148-
mp_raise_ValueError(translate("Cannot vary frequency on a timer that is already in use"));
145+
return PWMOUT_VARIABLE_FREQUENCY_NOT_AVAILABLE;
149146
} else {
150-
mp_raise_ValueError(translate("Invalid pins for PWMOut"));
147+
return PWMOUT_INVALID_PIN;
151148
}
152149
}
153150

@@ -167,7 +164,9 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
167164
uint32_t prescaler = 0; //prescaler is 15 bit
168165
uint32_t period = 0; //period is 16 bit
169166
uint32_t source_freq = stm_peripherals_timer_get_source_freq(TIMx);
170-
timer_get_optimal_divisors(&period, &prescaler, frequency, source_freq);
167+
if (!timer_get_optimal_divisors(&period, &prescaler, frequency, source_freq)) {
168+
return PWMOUT_INVALID_FREQUENCY;
169+
}
171170

172171
//Timer init
173172
self->handle.Instance = TIMx;
@@ -180,7 +179,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
180179
//only run init if this is the first instance of this timer
181180
if (first_time_setup) {
182181
if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) {
183-
mp_raise_ValueError(translate("Could not initialize timer"));
182+
return PWMOUT_INITIALIZATION_ERROR;
184183
}
185184
}
186185

@@ -190,10 +189,10 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,
190189
self->chan_handle.OCPolarity = TIM_OCPOLARITY_HIGH;
191190
self->chan_handle.OCFastMode = TIM_OCFAST_DISABLE;
192191
if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) {
193-
mp_raise_ValueError(translate("Could not initialize channel"));
192+
return PWMOUT_INITIALIZATION_ERROR;
194193
}
195194
if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) {
196-
mp_raise_ValueError(translate("Could not start PWM"));
195+
return PWMOUT_INITIALIZATION_ERROR;
197196
}
198197

199198
self->variable_frequency = variable_frequency;

shared-bindings/pwmio/PWMOut.c

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,32 @@ STATIC mp_obj_t pwmio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args,
102102
pwmio_pwmout_obj_t *self = m_new_obj(pwmio_pwmout_obj_t);
103103
self->base.type = &pwmio_pwmout_type;
104104
pwmout_result_t result = common_hal_pwmio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency);
105-
if (result == PWMOUT_INVALID_PIN) {
106-
mp_raise_ValueError(translate("Invalid pin"));
107-
} else if (result == PWMOUT_INVALID_FREQUENCY) {
108-
mp_raise_ValueError(translate("Invalid PWM frequency"));
109-
} else if (result == PWMOUT_ALL_TIMERS_ON_PIN_IN_USE) {
110-
mp_raise_ValueError(translate("All timers for this pin are in use"));
111-
} else if (result == PWMOUT_ALL_TIMERS_IN_USE) {
112-
mp_raise_RuntimeError(translate("All timers in use"));
105+
switch (result) {
106+
case PWMOUT_INVALID_PIN:
107+
mp_raise_ValueError(translate("Invalid pin"));
108+
break;
109+
case PWMOUT_INVALID_FREQUENCY:
110+
mp_raise_ValueError(translate("Invalid PWM frequency"));
111+
break;
112+
case PWMOUT_INVALID_FREQUENCY_ON_PIN:
113+
mp_raise_ValueError(translate("Frequency must match existing PWMOut using this timer"));
114+
break;
115+
case PWMOUT_VARIABLE_FREQUENCY_NOT_AVAILABLE:
116+
mp_raise_ValueError(translate("Cannot vary frequency on a timer that is already in use"));
117+
break;
118+
case PWMOUT_ALL_TIMERS_ON_PIN_IN_USE:
119+
mp_raise_ValueError(translate("All timers for this pin are in use"));
120+
break;
121+
case PWMOUT_ALL_TIMERS_IN_USE:
122+
mp_raise_RuntimeError(translate("All timers in use"));
123+
break;
124+
case PWMOUT_ALL_CHANNELS_IN_USE:
125+
mp_raise_RuntimeError(translate("All channels in use"));
126+
break;
127+
default:
128+
case PWMOUT_INITIALIZATION_ERROR:
129+
mp_raise_RuntimeError(translate("Could not start PWM"));
130+
break;
113131
}
114132

115133
return MP_OBJ_FROM_PTR(self);

shared-bindings/pwmio/PWMOut.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ typedef enum pwmout_result_t {
3636
PWMOUT_OK,
3737
PWMOUT_INVALID_PIN,
3838
PWMOUT_INVALID_FREQUENCY,
39+
PWMOUT_INVALID_FREQUENCY_ON_PIN,
40+
PWMOUT_VARIABLE_FREQUENCY_NOT_AVAILABLE,
3941
PWMOUT_ALL_TIMERS_ON_PIN_IN_USE,
40-
PWMOUT_ALL_TIMERS_IN_USE
42+
PWMOUT_ALL_TIMERS_IN_USE,
43+
PWMOUT_ALL_CHANNELS_IN_USE,
44+
PWMOUT_INITIALIZATION_ERROR,
4145
} pwmout_result_t;
4246

4347
extern pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self,

0 commit comments

Comments
 (0)