diff options
| -rw-r--r-- | ports/esp32s2/boards/adafruit_funhouse/board.c | 10 | ||||
| -rw-r--r-- | ports/esp32s2/boards/adafruit_funhouse/pins.c | 2 | ||||
| -rw-r--r-- | ports/esp32s2/common-hal/pwmio/PWMOut.c | 6 | ||||
| -rw-r--r-- | ports/stm/common-hal/pwmio/PWMOut.c | 29 | ||||
| -rw-r--r-- | shared-bindings/pwmio/PWMOut.c | 34 | ||||
| -rw-r--r-- | shared-bindings/pwmio/PWMOut.h | 6 |
6 files changed, 56 insertions, 31 deletions
diff --git a/ports/esp32s2/boards/adafruit_funhouse/board.c b/ports/esp32s2/boards/adafruit_funhouse/board.c index 1b3505378..09eb9266e 100644 --- a/ports/esp32s2/boards/adafruit_funhouse/board.c +++ b/ports/esp32s2/boards/adafruit_funhouse/board.c @@ -54,8 +54,8 @@ void board_init(void) { // Debug UART #ifdef DEBUG - common_hal_never_reset_pin(&pin_GPIO43); - common_hal_never_reset_pin(&pin_GPIO44); + common_hal_never_reset_pin(&pin_GPIO37); + common_hal_never_reset_pin(&pin_GPIO38); #endif /* DEBUG */ busio_spi_obj_t* spi = &displays[0].fourwire_bus.inline_bus; @@ -73,9 +73,13 @@ void board_init(void) { 0, // Polarity 0); // Phase + // workaround as board_init() is called before reset_port() in main.c + pwmout_reset(); + displayio_display_obj_t* display = &displays[0].display; display->base.type = &displayio_display_type; - common_hal_displayio_display_construct(display, + common_hal_displayio_display_construct( + display, bus, 240, // Width (after rotation) 240, // Height (after rotation) diff --git a/ports/esp32s2/boards/adafruit_funhouse/pins.c b/ports/esp32s2/boards/adafruit_funhouse/pins.c index 2721a301c..0ad5beb6c 100644 --- a/ports/esp32s2/boards/adafruit_funhouse/pins.c +++ b/ports/esp32s2/boards/adafruit_funhouse/pins.c @@ -33,7 +33,7 @@ STATIC const mp_rom_map_elem_t board_global_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_LIGHT), MP_ROM_PTR(&pin_GPIO18) }, - { MP_ROM_QSTR(MP_QSTR_SPEAKER), MP_ROM_PTR(&pin_GPIO46) }, +// { MP_ROM_QSTR(MP_QSTR_SPEAKER), MP_ROM_PTR(&pin_GPIO46) }, { MP_ROM_QSTR(MP_QSTR_LED), MP_ROM_PTR(&pin_GPIO37) }, diff --git a/ports/esp32s2/common-hal/pwmio/PWMOut.c b/ports/esp32s2/common-hal/pwmio/PWMOut.c index e1fdd4760..dfba42bb8 100644 --- a/ports/esp32s2/common-hal/pwmio/PWMOut.c +++ b/ports/esp32s2/common-hal/pwmio/PWMOut.c @@ -93,7 +93,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, } if (timer_index == INDEX_EMPTY) { // Running out of timers isn't pin related on ESP32S2 so we can't re-use error messages - mp_raise_ValueError(translate("No more timers available")); + return PWMOUT_ALL_TIMERS_IN_USE; } // Find a viable channel @@ -104,7 +104,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, } } if (channel_index == INDEX_EMPTY) { - mp_raise_ValueError(translate("No more channels available")); + return PWMOUT_ALL_CHANNELS_IN_USE; } // Run configuration @@ -126,7 +126,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, self->chan_handle.timer_sel = timer_index; if (ledc_channel_config(&(self->chan_handle))) { - mp_raise_ValueError(translate("Could not initialize channel")); + return PWMOUT_INITIALIZATION_ERROR; } // Make reservations diff --git a/ports/stm/common-hal/pwmio/PWMOut.c b/ports/stm/common-hal/pwmio/PWMOut.c index 85427185e..951488325 100644 --- a/ports/stm/common-hal/pwmio/PWMOut.c +++ b/ports/stm/common-hal/pwmio/PWMOut.c @@ -48,7 +48,7 @@ STATIC uint32_t timer_get_internal_duty(uint16_t duty, uint32_t period) { return (duty*period) / ((1 << 16) - 1); } -STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler, +STATIC bool timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler, uint32_t frequency, uint32_t source_freq) { //Find the largest possible period supported by this frequency for (int i = 0; i < (1 << 16); i++) { @@ -58,9 +58,8 @@ STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler, break; } } - if (*prescaler == 0) { - mp_raise_ValueError(translate("Invalid frequency supplied")); - } + // Return successor failure. + return *prescaler != 0; } void pwmout_reset(void) { @@ -138,16 +137,14 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, tim_frequencies[self->tim->tim_index - 1] = frequency; stm_peripherals_timer_reserve(TIMx); } else { //no match found - if (tim_chan_taken) { - mp_raise_ValueError(translate("No more timers available on this pin.")); - } else if (tim_taken_internal) { - mp_raise_ValueError(translate("Timer was reserved for internal use - declare PWM pins earlier in the program")); + if (tim_chan_taken || tim_taken_internal) { + return PWMOUT_ALL_TIMERS_ON_PIN_IN_USE; } else if (tim_taken_f_mismatch) { - mp_raise_ValueError(translate("Frequency must match existing PWMOut using this timer")); + return PWMOUT_INVALID_FREQUENCY_ON_PIN; } else if (var_freq_mismatch) { - mp_raise_ValueError(translate("Cannot vary frequency on a timer that is already in use")); + return PWMOUT_VARIABLE_FREQUENCY_NOT_AVAILABLE; } else { - mp_raise_ValueError(translate("Invalid pins for PWMOut")); + return PWMOUT_INVALID_PIN; } } @@ -167,7 +164,9 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, uint32_t prescaler = 0; //prescaler is 15 bit uint32_t period = 0; //period is 16 bit uint32_t source_freq = stm_peripherals_timer_get_source_freq(TIMx); - timer_get_optimal_divisors(&period, &prescaler, frequency, source_freq); + if (!timer_get_optimal_divisors(&period, &prescaler, frequency, source_freq)) { + return PWMOUT_INVALID_FREQUENCY; + } //Timer init self->handle.Instance = TIMx; @@ -180,7 +179,7 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, //only run init if this is the first instance of this timer if (first_time_setup) { if (HAL_TIM_PWM_Init(&self->handle) != HAL_OK) { - mp_raise_ValueError(translate("Could not initialize timer")); + return PWMOUT_INITIALIZATION_ERROR; } } @@ -190,10 +189,10 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, self->chan_handle.OCPolarity = TIM_OCPOLARITY_HIGH; self->chan_handle.OCFastMode = TIM_OCFAST_DISABLE; if (HAL_TIM_PWM_ConfigChannel(&self->handle, &self->chan_handle, self->channel) != HAL_OK) { - mp_raise_ValueError(translate("Could not initialize channel")); + return PWMOUT_INITIALIZATION_ERROR; } if (HAL_TIM_PWM_Start(&self->handle, self->channel) != HAL_OK) { - mp_raise_ValueError(translate("Could not start PWM")); + return PWMOUT_INITIALIZATION_ERROR; } self->variable_frequency = variable_frequency; diff --git a/shared-bindings/pwmio/PWMOut.c b/shared-bindings/pwmio/PWMOut.c index da0755592..43944977e 100644 --- a/shared-bindings/pwmio/PWMOut.c +++ b/shared-bindings/pwmio/PWMOut.c @@ -102,14 +102,32 @@ STATIC mp_obj_t pwmio_pwmout_make_new(const mp_obj_type_t *type, size_t n_args, pwmio_pwmout_obj_t *self = m_new_obj(pwmio_pwmout_obj_t); self->base.type = &pwmio_pwmout_type; pwmout_result_t result = common_hal_pwmio_pwmout_construct(self, pin, duty_cycle, frequency, variable_frequency); - if (result == PWMOUT_INVALID_PIN) { - mp_raise_ValueError(translate("Invalid pin")); - } else if (result == PWMOUT_INVALID_FREQUENCY) { - mp_raise_ValueError(translate("Invalid PWM frequency")); - } else if (result == PWMOUT_ALL_TIMERS_ON_PIN_IN_USE) { - mp_raise_ValueError(translate("All timers for this pin are in use")); - } else if (result == PWMOUT_ALL_TIMERS_IN_USE) { - mp_raise_RuntimeError(translate("All timers in use")); + switch (result) { + case PWMOUT_INVALID_PIN: + mp_raise_ValueError(translate("Invalid pin")); + break; + case PWMOUT_INVALID_FREQUENCY: + mp_raise_ValueError(translate("Invalid PWM frequency")); + break; + case PWMOUT_INVALID_FREQUENCY_ON_PIN: + mp_raise_ValueError(translate("Frequency must match existing PWMOut using this timer")); + break; + case PWMOUT_VARIABLE_FREQUENCY_NOT_AVAILABLE: + mp_raise_ValueError(translate("Cannot vary frequency on a timer that is already in use")); + break; + case PWMOUT_ALL_TIMERS_ON_PIN_IN_USE: + mp_raise_ValueError(translate("All timers for this pin are in use")); + break; + case PWMOUT_ALL_TIMERS_IN_USE: + mp_raise_RuntimeError(translate("All timers in use")); + break; + case PWMOUT_ALL_CHANNELS_IN_USE: + mp_raise_RuntimeError(translate("All channels in use")); + break; + default: + case PWMOUT_INITIALIZATION_ERROR: + mp_raise_RuntimeError(translate("Could not start PWM")); + break; } return MP_OBJ_FROM_PTR(self); diff --git a/shared-bindings/pwmio/PWMOut.h b/shared-bindings/pwmio/PWMOut.h index de2ebd1cf..69e7249e7 100644 --- a/shared-bindings/pwmio/PWMOut.h +++ b/shared-bindings/pwmio/PWMOut.h @@ -36,8 +36,12 @@ typedef enum pwmout_result_t { PWMOUT_OK, PWMOUT_INVALID_PIN, PWMOUT_INVALID_FREQUENCY, + PWMOUT_INVALID_FREQUENCY_ON_PIN, + PWMOUT_VARIABLE_FREQUENCY_NOT_AVAILABLE, PWMOUT_ALL_TIMERS_ON_PIN_IN_USE, - PWMOUT_ALL_TIMERS_IN_USE + PWMOUT_ALL_TIMERS_IN_USE, + PWMOUT_ALL_CHANNELS_IN_USE, + PWMOUT_INITIALIZATION_ERROR, } pwmout_result_t; extern pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, |
