2016-11-03 18:50:59 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-03 18:50:59 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2017-06-06 16:16:34 -04:00
|
|
|
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
|
2016-11-03 18:50:59 -04:00
|
|
|
* Copyright (c) 2016 Damien P. George
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/runtime.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "common-hal/pulseio/PWMOut.h"
|
|
|
|
#include "shared-bindings/pulseio/PWMOut.h"
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2016-12-01 16:47:18 -05:00
|
|
|
#include "samd21_pins.h"
|
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
#undef ENABLE
|
|
|
|
|
2017-06-09 18:29:02 -04:00
|
|
|
# define _TCC_SIZE(n,unused) TPASTE3(TCC,n,_SIZE),
|
|
|
|
# define TCC_SIZES { MREPEAT(TCC_INST_NUM, _TCC_SIZE, 0) }
|
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
uint32_t target_timer_frequencies[TC_INST_NUM + TCC_INST_NUM];
|
2017-06-12 18:37:09 -04:00
|
|
|
static uint32_t timer_periods[TC_INST_NUM + TCC_INST_NUM];
|
2017-01-26 21:05:46 -05:00
|
|
|
uint8_t timer_refcount[TC_INST_NUM + TCC_INST_NUM];
|
|
|
|
const uint16_t prescaler[8] = {1, 2, 4, 8, 16, 64, 256, 1024};
|
|
|
|
|
|
|
|
// This bitmask keeps track of which channels of a TCC are currently claimed.
|
|
|
|
uint8_t tcc_channels[3] = {0xf0, 0xfc, 0xfc};
|
|
|
|
|
|
|
|
void pwmout_reset(void) {
|
|
|
|
// Reset all but TC5
|
|
|
|
for (int i = 0; i < TC_INST_NUM + TCC_INST_NUM; i++) {
|
|
|
|
if (i == 5) {
|
|
|
|
target_timer_frequencies[i] = 1000;
|
|
|
|
timer_refcount[i] = 1;
|
|
|
|
} else {
|
|
|
|
target_timer_frequencies[i] = 0;
|
|
|
|
timer_refcount[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Tcc *tccs[TCC_INST_NUM] = TCC_INSTS;
|
|
|
|
for (int i = 0; i < TCC_INST_NUM; i++) {
|
2017-06-09 18:29:02 -04:00
|
|
|
// Disable the module before resetting it.
|
|
|
|
if (tccs[i]->CTRLA.bit.ENABLE == 1) {
|
|
|
|
tccs[i]->CTRLA.bit.ENABLE = 0;
|
|
|
|
while (tccs[i]->SYNCBUSY.bit.ENABLE == 1) {
|
|
|
|
}
|
|
|
|
}
|
2017-06-09 20:15:31 -04:00
|
|
|
// TODO(tannewt): Make this depend on the CMSIS.
|
|
|
|
if (i == 0) {
|
|
|
|
tcc_channels[i] = 0xf0;
|
|
|
|
} else {
|
|
|
|
tcc_channels[i] = 0xfc;
|
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
tccs[i]->CTRLA.bit.SWRST = 1;
|
|
|
|
}
|
|
|
|
Tc *tcs[TC_INST_NUM] = TC_INSTS;
|
|
|
|
for (int i = 0; i < TC_INST_NUM; i++) {
|
|
|
|
if (tcs[i] == TC5) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tcs[i]->COUNT16.CTRLA.bit.SWRST = 1;
|
2017-06-09 18:29:02 -04:00
|
|
|
while (tcs[i]->COUNT16.CTRLA.bit.SWRST == 1) {
|
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool channel_ok(const pin_timer_t* t, uint8_t index) {
|
|
|
|
return (!t->is_tc && (tcc_channels[index] & (1 << t->channel)) == 0) ||
|
|
|
|
t->is_tc;
|
|
|
|
}
|
|
|
|
|
2017-06-12 18:37:09 -04:00
|
|
|
static uint8_t timer_index(uint32_t base_timer_address) {
|
|
|
|
return (base_timer_address - ((uint32_t) TCC0)) / 0x400;
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
|
2017-01-26 21:05:46 -05:00
|
|
|
const mcu_pin_obj_t* pin,
|
|
|
|
uint16_t duty,
|
|
|
|
uint32_t frequency,
|
|
|
|
bool variable_frequency) {
|
2016-11-03 18:50:59 -04:00
|
|
|
self->pin = pin;
|
2017-01-26 21:05:46 -05:00
|
|
|
self->variable_frequency = variable_frequency;
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2016-12-01 16:47:18 -05:00
|
|
|
if (pin->primary_timer.tc == 0 && pin->secondary_timer.tc == 0) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_ValueError("Invalid pin");
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
if (frequency == 0 || frequency > 6000000) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_ValueError("Invalid PWM frequency");
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
uint16_t primary_timer_index = 0xff;
|
|
|
|
uint16_t secondary_timer_index = 0xff;
|
|
|
|
if (pin->primary_timer.tc != NULL) {
|
2017-06-12 18:37:09 -04:00
|
|
|
primary_timer_index = timer_index((uint32_t) pin->primary_timer.tcc);
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
|
|
|
if (pin->secondary_timer.tc != NULL) {
|
2017-06-12 18:37:09 -04:00
|
|
|
secondary_timer_index = timer_index((uint32_t) pin->secondary_timer.tcc);
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
// Figure out which timer we are using.
|
2016-11-03 18:50:59 -04:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
// First see if a timer is already going with the frequency we want and our
|
|
|
|
// channel is unused.
|
|
|
|
// NOTE(shawcroft): The enable bit is in the same position for TC and TCC so
|
|
|
|
// we treat them all as TCC for checking ENABLE.
|
|
|
|
const pin_timer_t* t = NULL;
|
|
|
|
uint8_t index = 0;
|
|
|
|
if (!variable_frequency &&
|
2017-06-06 16:16:34 -04:00
|
|
|
primary_timer_index != 0xff &&
|
|
|
|
target_timer_frequencies[primary_timer_index] == frequency &&
|
|
|
|
pin->primary_timer.tcc->CTRLA.bit.ENABLE == 1 &&
|
|
|
|
channel_ok(&pin->primary_timer, primary_timer_index)) {
|
2017-01-26 21:05:46 -05:00
|
|
|
t = &pin->primary_timer;
|
|
|
|
index = primary_timer_index;
|
2017-06-09 20:16:33 -04:00
|
|
|
self->tcc_instance.hw = t->tcc;
|
|
|
|
self->tcc_instance.double_buffering_enabled = true;
|
2017-01-26 21:05:46 -05:00
|
|
|
} else if (!variable_frequency &&
|
|
|
|
secondary_timer_index != 0xff &&
|
|
|
|
target_timer_frequencies[secondary_timer_index] == frequency &&
|
|
|
|
pin->secondary_timer.tcc->CTRLA.bit.ENABLE == 1 &&
|
|
|
|
channel_ok(&pin->secondary_timer, secondary_timer_index)) {
|
|
|
|
t = &pin->secondary_timer;
|
|
|
|
index = secondary_timer_index;
|
2017-06-09 20:16:33 -04:00
|
|
|
self->tcc_instance.hw = t->tcc;
|
|
|
|
self->tcc_instance.double_buffering_enabled = true;
|
2016-12-01 16:47:18 -05:00
|
|
|
} else {
|
2017-01-26 21:05:46 -05:00
|
|
|
// Pick an unused timer if available.
|
|
|
|
|
|
|
|
// Check the secondary timer first since its always a nicer TCC (when it
|
|
|
|
// exists)
|
|
|
|
if (pin->secondary_timer.tc != 0 &&
|
|
|
|
timer_refcount[secondary_timer_index] == 0 &&
|
|
|
|
pin->secondary_timer.tcc->CTRLA.bit.ENABLE == 0) {
|
|
|
|
t = &pin->secondary_timer;
|
|
|
|
index = secondary_timer_index;
|
|
|
|
} else if (pin->primary_timer.tc != 0 &&
|
|
|
|
(!pin->primary_timer.is_tc || pin->primary_timer.channel == 1) &&
|
|
|
|
timer_refcount[primary_timer_index] == 0) {
|
|
|
|
t = &pin->primary_timer;
|
|
|
|
index = primary_timer_index;
|
|
|
|
}
|
|
|
|
if (t == NULL) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_RuntimeError("All timers in use");
|
2017-01-26 21:05:46 -05:00
|
|
|
return;
|
|
|
|
}
|
2017-06-12 18:37:09 -04:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
uint8_t resolution = 0;
|
|
|
|
if (t->is_tc) {
|
|
|
|
resolution = 16;
|
|
|
|
} else {
|
2017-06-09 18:29:02 -04:00
|
|
|
// TCC resolution varies so look it up.
|
|
|
|
const uint8_t _tcc_sizes[TCC_INST_NUM] = TCC_SIZES;
|
|
|
|
resolution = _tcc_sizes[index];
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
|
|
|
// First determine the divisor that gets us the highest resolution.
|
|
|
|
uint32_t system_clock = system_cpu_clock_get_hz();
|
|
|
|
uint32_t top;
|
|
|
|
uint8_t divisor;
|
|
|
|
for (divisor = 0; divisor < 8; divisor++) {
|
|
|
|
top = (system_clock / prescaler[divisor] / frequency) - 1;
|
|
|
|
if (top < (1u << resolution)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 18:37:09 -04:00
|
|
|
timer_periods[index] = top;
|
2017-01-26 21:05:46 -05:00
|
|
|
if (t->is_tc) {
|
|
|
|
struct tc_config config_tc;
|
|
|
|
tc_get_config_defaults(&config_tc);
|
2016-12-01 16:47:18 -05:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
config_tc.counter_size = TC_COUNTER_SIZE_16BIT;
|
|
|
|
config_tc.clock_prescaler = TC_CTRLA_PRESCALER(divisor);
|
|
|
|
config_tc.wave_generation = TC_WAVE_GENERATION_MATCH_PWM;
|
|
|
|
config_tc.counter_16_bit.compare_capture_channel[0] = top;
|
2016-12-01 16:47:18 -05:00
|
|
|
|
2017-06-09 18:29:02 -04:00
|
|
|
enum status_code status = tc_init(&self->tc_instance, t->tc, &config_tc);
|
|
|
|
if (status != STATUS_OK) {
|
|
|
|
mp_raise_RuntimeError("Failed to init timer");
|
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
tc_enable(&self->tc_instance);
|
|
|
|
} else {
|
|
|
|
struct tcc_config config_tcc;
|
|
|
|
tcc_get_config_defaults(&config_tcc, t->tcc);
|
2016-12-01 16:47:18 -05:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
config_tcc.counter.clock_prescaler = divisor;
|
|
|
|
config_tcc.counter.period = top;
|
|
|
|
config_tcc.compare.wave_generation = TCC_WAVE_GENERATION_SINGLE_SLOPE_PWM;
|
|
|
|
|
2017-06-09 18:29:02 -04:00
|
|
|
enum status_code status = tcc_init(&self->tcc_instance, t->tcc, &config_tcc);
|
|
|
|
if (status != STATUS_OK) {
|
|
|
|
mp_raise_RuntimeError("Failed to init timer");
|
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
tcc_enable(&self->tcc_instance);
|
|
|
|
}
|
|
|
|
|
|
|
|
target_timer_frequencies[index] = frequency;
|
|
|
|
timer_refcount[index]++;
|
|
|
|
}
|
2016-12-01 16:47:18 -05:00
|
|
|
|
2017-01-26 21:05:46 -05:00
|
|
|
if (!t->is_tc) {
|
|
|
|
if (variable_frequency) {
|
|
|
|
// We're changing frequency so claim all of the channels.
|
|
|
|
tcc_channels[index] = 0xff;
|
|
|
|
} else {
|
|
|
|
tcc_channels[index] |= (1 << t->channel);
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
|
|
|
|
self->timer = t;
|
|
|
|
|
|
|
|
// Connect the wave output to the outside world.
|
|
|
|
struct system_pinmux_config pin_config;
|
|
|
|
system_pinmux_get_config_defaults(&pin_config);
|
|
|
|
pin_config.mux_position = &self->pin->primary_timer == t ? MUX_E : MUX_F;
|
|
|
|
pin_config.direction = SYSTEM_PINMUX_PIN_DIR_OUTPUT;
|
|
|
|
system_pinmux_pin_set_config(pin->pin, &pin_config);
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(self, duty);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-10-02 20:49:40 -04:00
|
|
|
bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
|
|
|
|
return self->pin == mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
|
|
|
|
if (common_hal_pulseio_pwmout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
const pin_timer_t* t = self->timer;
|
|
|
|
uint8_t index = (((uint32_t) t->tcc) - ((uint32_t) TCC0)) / 0x400;
|
|
|
|
timer_refcount[index]--;
|
|
|
|
if (!t->is_tc) {
|
|
|
|
tcc_channels[index] &= ~(1 << t->channel);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
if (timer_refcount[index] == 0) {
|
|
|
|
target_timer_frequencies[index] = 0;
|
|
|
|
if (t->is_tc) {
|
|
|
|
tc_disable(&self->tc_instance);
|
|
|
|
} else {
|
|
|
|
if (t->tcc == TCC0) {
|
|
|
|
tcc_channels[index] = 0xf0;
|
|
|
|
} else {
|
|
|
|
tcc_channels[index] = 0xfc;
|
|
|
|
}
|
|
|
|
tcc_disable(&self->tcc_instance);
|
|
|
|
tcc_reset(&self->tcc_instance);
|
|
|
|
}
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2016-12-06 13:31:38 -05:00
|
|
|
reset_pin(self->pin->pin);
|
2017-10-02 20:49:40 -04:00
|
|
|
self->pin = mp_const_none;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
extern void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {
|
2017-01-26 21:05:46 -05:00
|
|
|
const pin_timer_t* t = self->timer;
|
2017-06-12 18:37:09 -04:00
|
|
|
uint8_t index;
|
2017-01-26 21:05:46 -05:00
|
|
|
if (t->is_tc) {
|
2017-06-12 18:37:09 -04:00
|
|
|
index = timer_index((uint32_t) self->timer->tc);
|
|
|
|
uint16_t adjusted_duty = timer_periods[index] * duty / 0xffff;
|
2017-01-26 21:05:46 -05:00
|
|
|
tc_set_compare_value(&self->tc_instance, t->channel, adjusted_duty);
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2017-06-12 18:37:09 -04:00
|
|
|
index = timer_index((uint32_t) self->timer->tcc);
|
|
|
|
uint32_t adjusted_duty = ((uint64_t) timer_periods[index]) * duty / 0xffff;
|
2017-01-26 21:05:46 -05:00
|
|
|
tcc_set_compare_value(&self->tcc_instance, t->channel, adjusted_duty);
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
|
2017-01-26 21:05:46 -05:00
|
|
|
const pin_timer_t* t = self->timer;
|
|
|
|
if (t->is_tc) {
|
|
|
|
while (tc_is_syncing(&self->tc_instance)) {
|
|
|
|
/* Wait for sync */
|
|
|
|
}
|
|
|
|
uint16_t cv = t->tc->COUNT16.CC[t->channel].reg;
|
2017-06-12 18:37:09 -04:00
|
|
|
return cv * 0xffff / timer_periods[timer_index((uint32_t) self->timer->tc)];
|
2017-01-26 21:05:46 -05:00
|
|
|
} else {
|
|
|
|
uint32_t cv = 0;
|
|
|
|
if ((t->tcc->STATUS.vec.CCBV & (1 << t->channel)) != 0) {
|
|
|
|
cv = t->tcc->CCB[t->channel].reg;
|
|
|
|
} else {
|
|
|
|
cv = t->tcc->CC[t->channel].reg;
|
|
|
|
}
|
2017-06-06 16:16:34 -04:00
|
|
|
|
2017-06-12 18:37:09 -04:00
|
|
|
uint32_t duty_cycle = ((uint64_t) cv) * 0xffff / timer_periods[timer_index((uint32_t) self->timer->tcc)];
|
2017-06-06 16:16:34 -04:00
|
|
|
|
|
|
|
return duty_cycle;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self,
|
2017-01-26 21:05:46 -05:00
|
|
|
uint32_t frequency) {
|
|
|
|
if (frequency == 0 || frequency > 6000000) {
|
2017-02-24 09:13:07 -05:00
|
|
|
mp_raise_ValueError("Invalid PWM frequency");
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
|
|
|
const pin_timer_t* t = self->timer;
|
|
|
|
uint8_t resolution;
|
|
|
|
if (t->is_tc) {
|
|
|
|
resolution = 16;
|
|
|
|
} else {
|
|
|
|
resolution = 24;
|
|
|
|
}
|
|
|
|
uint32_t system_clock = system_cpu_clock_get_hz();
|
|
|
|
uint32_t new_top;
|
|
|
|
uint8_t new_divisor;
|
|
|
|
for (new_divisor = 0; new_divisor < 8; new_divisor++) {
|
|
|
|
new_top = (system_clock / prescaler[new_divisor] / frequency) - 1;
|
|
|
|
if (new_top < (1u << resolution)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2017-04-10 16:32:19 -04:00
|
|
|
uint16_t old_duty = common_hal_pulseio_pwmout_get_duty_cycle(self);
|
2017-01-26 21:05:46 -05:00
|
|
|
uint8_t old_divisor;
|
2017-06-12 18:37:09 -04:00
|
|
|
uint8_t index;
|
2017-01-26 21:05:46 -05:00
|
|
|
if (t->is_tc) {
|
2017-06-12 18:37:09 -04:00
|
|
|
index = timer_index((uint32_t) self->timer->tc);
|
2017-01-26 21:05:46 -05:00
|
|
|
old_divisor = t->tc->COUNT16.CTRLA.bit.PRESCALER;
|
2016-11-03 18:50:59 -04:00
|
|
|
} else {
|
2017-06-12 18:37:09 -04:00
|
|
|
index = timer_index((uint32_t) self->timer->tcc);
|
2017-01-26 21:05:46 -05:00
|
|
|
old_divisor = t->tcc->CTRLA.bit.PRESCALER;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|
2017-01-26 21:05:46 -05:00
|
|
|
if (new_divisor != old_divisor) {
|
|
|
|
if (t->is_tc) {
|
|
|
|
tc_disable(&self->tc_instance);
|
|
|
|
t->tc->COUNT16.CTRLA.bit.PRESCALER = new_divisor;
|
|
|
|
tc_enable(&self->tc_instance);
|
|
|
|
} else {
|
|
|
|
tcc_disable(&self->tcc_instance);
|
|
|
|
t->tcc->CTRLA.bit.PRESCALER = new_divisor;
|
|
|
|
tcc_enable(&self->tcc_instance);
|
|
|
|
}
|
|
|
|
}
|
2017-06-12 18:37:09 -04:00
|
|
|
timer_periods[index] = new_top;
|
2017-01-26 21:05:46 -05:00
|
|
|
if (t->is_tc) {
|
|
|
|
while (tc_is_syncing(&self->tc_instance)) {
|
|
|
|
/* Wait for sync */
|
|
|
|
}
|
|
|
|
t->tc->COUNT16.CC[0].reg = new_top;
|
|
|
|
} else {
|
|
|
|
tcc_set_top_value(&self->tcc_instance, new_top);
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_pulseio_pwmout_set_duty_cycle(self, old_duty);
|
2017-01-26 21:05:46 -05:00
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) {
|
2017-01-26 21:05:46 -05:00
|
|
|
uint32_t system_clock = system_cpu_clock_get_hz();
|
|
|
|
const pin_timer_t* t = self->timer;
|
2017-06-12 18:37:09 -04:00
|
|
|
uint8_t index;
|
2017-01-26 21:05:46 -05:00
|
|
|
uint8_t divisor;
|
|
|
|
if (t->is_tc) {
|
2017-06-12 18:37:09 -04:00
|
|
|
index = timer_index((uint32_t) self->timer->tc);
|
2017-01-26 21:05:46 -05:00
|
|
|
divisor = t->tc->COUNT16.CTRLA.bit.PRESCALER;
|
|
|
|
} else {
|
2017-06-12 18:37:09 -04:00
|
|
|
index = timer_index((uint32_t) self->timer->tcc);
|
2017-01-26 21:05:46 -05:00
|
|
|
divisor = t->tcc->CTRLA.bit.PRESCALER;
|
|
|
|
}
|
2017-06-12 18:37:09 -04:00
|
|
|
uint32_t top = timer_periods[index];
|
2017-01-26 21:05:46 -05:00
|
|
|
return (system_clock / prescaler[divisor]) / (top + 1);
|
|
|
|
}
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) {
|
2017-01-26 21:05:46 -05:00
|
|
|
return self->variable_frequency;
|
2016-11-03 18:50:59 -04:00
|
|
|
}
|