From 5c5736ee1e28d08f0c0fa6b495e52e2e1a64edc0 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 20 Apr 2021 10:22:21 -0500 Subject: [PATCH] atmel-sam: Allow PWM to go up to cpu_frequency/2 24MHz is the nominal external clock to use with OV7670. (My testing was actually at 16MHz) --- ports/atmel-samd/common-hal/pwmio/PWMOut.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ports/atmel-samd/common-hal/pwmio/PWMOut.c b/ports/atmel-samd/common-hal/pwmio/PWMOut.c index b5142c21e1..c46145d7d7 100644 --- a/ports/atmel-samd/common-hal/pwmio/PWMOut.c +++ b/ports/atmel-samd/common-hal/pwmio/PWMOut.c @@ -156,7 +156,8 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, return PWMOUT_INVALID_PIN; } - if (frequency == 0 || frequency > 6000000) { + uint32_t system_clock = common_hal_mcu_processor_get_frequency(); + if (frequency == 0 || frequency > system_clock/2) { return PWMOUT_INVALID_FREQUENCY; } @@ -237,7 +238,6 @@ pwmout_result_t common_hal_pwmio_pwmout_construct(pwmio_pwmout_obj_t* self, resolution = tcc_sizes[timer->index]; } // First determine the divisor that gets us the highest resolution. - uint32_t system_clock = common_hal_mcu_processor_get_frequency(); uint32_t top; uint8_t divisor; for (divisor = 0; divisor < 8; divisor++) { @@ -413,7 +413,8 @@ uint16_t common_hal_pwmio_pwmout_get_duty_cycle(pwmio_pwmout_obj_t* self) { void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t* self, uint32_t frequency) { - if (frequency == 0 || frequency > 6000000) { + uint32_t system_clock = common_hal_mcu_processor_get_frequency(); + if (frequency == 0 || frequency > system_clock/2) { mp_raise_ValueError(translate("Invalid PWM frequency")); } const pin_timer_t* t = self->timer; @@ -424,7 +425,6 @@ void common_hal_pwmio_pwmout_set_frequency(pwmio_pwmout_obj_t* self, // TCC resolution varies so look it up. resolution = tcc_sizes[t->index]; } - uint32_t system_clock = common_hal_mcu_processor_get_frequency(); uint32_t new_top; uint8_t new_divisor; for (new_divisor = 0; new_divisor < 8; new_divisor++) {