|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 Lucian Copeland for Adafruit Industries |
| 7 | + * Uses code from Micropython, Copyright (c) 2013-2016 Damien P. George |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + */ |
| 27 | +#include <math.h> |
| 28 | + |
| 29 | +#include "common-hal/pulseio/PWMOut.h" |
| 30 | +#include "shared-bindings/pulseio/PWMOut.h" |
| 31 | +#include "py/runtime.h" |
| 32 | +#include "driver/ledc.h" |
| 33 | + |
| 34 | +#define INDEX_EMPTY 0xFF |
| 35 | + |
| 36 | +STATIC uint32_t reserved_timer_freq[LEDC_TIMER_MAX]; |
| 37 | +STATIC uint8_t reserved_channels[LEDC_CHANNEL_MAX]; |
| 38 | +STATIC bool never_reset_tim[LEDC_TIMER_MAX]; |
| 39 | +STATIC bool never_reset_chan[LEDC_CHANNEL_MAX]; |
| 40 | + |
| 41 | +void pwmout_reset(void) { |
| 42 | + for (size_t i = 0; i < LEDC_CHANNEL_MAX; i++ ) { |
| 43 | + ledc_stop(LEDC_LOW_SPEED_MODE, i, 0); |
| 44 | + if (!never_reset_chan[i]) { |
| 45 | + reserved_channels[i] = INDEX_EMPTY; |
| 46 | + } |
| 47 | + } |
| 48 | + for (size_t i = 0; i < LEDC_TIMER_MAX; i++ ) { |
| 49 | + ledc_timer_rst(LEDC_LOW_SPEED_MODE, i); |
| 50 | + if (!never_reset_tim[i]) { |
| 51 | + reserved_timer_freq[i] = 0; |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, |
| 57 | + const mcu_pin_obj_t* pin, |
| 58 | + uint16_t duty, |
| 59 | + uint32_t frequency, |
| 60 | + bool variable_frequency) { |
| 61 | + // Calculate duty cycle |
| 62 | + uint32_t duty_bits = 0; |
| 63 | + uint32_t interval = LEDC_APB_CLK_HZ/frequency; |
| 64 | + for (size_t i = 0; i < 32; i++) { |
| 65 | + if(!(interval >> i)) { |
| 66 | + duty_bits = i - 1; |
| 67 | + break; |
| 68 | + } |
| 69 | + } |
| 70 | + if (duty_bits < 1) { |
| 71 | + mp_raise_ValueError(translate("Invalid frequency")); |
| 72 | + } else if (duty_bits >= LEDC_TIMER_14_BIT) { |
| 73 | + duty_bits = LEDC_TIMER_13_BIT; |
| 74 | + } |
| 75 | + |
| 76 | + // Find a viable timer |
| 77 | + size_t timer_index = INDEX_EMPTY; |
| 78 | + size_t channel_index = INDEX_EMPTY; |
| 79 | + for (size_t i = 0; i < LEDC_TIMER_MAX; i++) { |
| 80 | + if ((reserved_timer_freq[i] == frequency) && !variable_frequency) { |
| 81 | + //prioritize matched frequencies so we don't needlessly take slots |
| 82 | + timer_index = i; |
| 83 | + break; |
| 84 | + } else if (reserved_timer_freq[i] == 0) { |
| 85 | + timer_index = i; |
| 86 | + break; |
| 87 | + } |
| 88 | + } |
| 89 | + if (timer_index == INDEX_EMPTY) { |
| 90 | + // Running out of timers isn't pin related on ESP32S2 so we can't re-use error messages |
| 91 | + mp_raise_ValueError(translate("No more timers available")); |
| 92 | + } |
| 93 | + |
| 94 | + // Find a viable channel |
| 95 | + for (size_t i = 0; i < LEDC_CHANNEL_MAX; i++) { |
| 96 | + if (reserved_channels[i] == INDEX_EMPTY) { |
| 97 | + channel_index = i; |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + if (channel_index == INDEX_EMPTY) { |
| 102 | + mp_raise_ValueError(translate("No more channels available")); |
| 103 | + } |
| 104 | + |
| 105 | + // Run configuration |
| 106 | + self->tim_handle.timer_num = timer_index; |
| 107 | + self->tim_handle.duty_resolution = duty_bits; |
| 108 | + self->tim_handle.freq_hz = frequency; |
| 109 | + self->tim_handle.speed_mode = LEDC_LOW_SPEED_MODE; |
| 110 | + self->tim_handle.clk_cfg = LEDC_AUTO_CLK; |
| 111 | + |
| 112 | + if (ledc_timer_config(&(self->tim_handle)) != ESP_OK) { |
| 113 | + mp_raise_ValueError(translate("Could not initialize timer")); |
| 114 | + } |
| 115 | + |
| 116 | + self->chan_handle.channel = channel_index; |
| 117 | + self->chan_handle.duty = duty >> (16 - duty_bits); |
| 118 | + self->chan_handle.gpio_num = pin->number; |
| 119 | + self->chan_handle.speed_mode = LEDC_LOW_SPEED_MODE; // Only LS is allowed on ESP32-S2 |
| 120 | + self->chan_handle.hpoint = 0; |
| 121 | + self->chan_handle.timer_sel = timer_index; |
| 122 | + |
| 123 | + if (ledc_channel_config(&(self->chan_handle))) { |
| 124 | + mp_raise_ValueError(translate("Could not initialize channel")); |
| 125 | + } |
| 126 | + |
| 127 | + // Make reservations |
| 128 | + reserved_timer_freq[timer_index] = frequency; |
| 129 | + reserved_channels[channel_index] = timer_index; |
| 130 | + |
| 131 | + self->variable_frequency = variable_frequency; |
| 132 | + self->pin_number = pin->number; |
| 133 | + self->deinited = false; |
| 134 | + self->duty_resolution = duty_bits; |
| 135 | + claim_pin(pin); |
| 136 | + |
| 137 | + // Set initial duty |
| 138 | + common_hal_pulseio_pwmout_set_duty_cycle(self, duty); |
| 139 | + |
| 140 | + return PWMOUT_OK; |
| 141 | +} |
| 142 | + |
| 143 | +void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { |
| 144 | + never_reset_tim[self->tim_handle.timer_num] = true; |
| 145 | + never_reset_chan[self->chan_handle.channel] = true; |
| 146 | +} |
| 147 | + |
| 148 | +void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { |
| 149 | + never_reset_tim[self->tim_handle.timer_num] = false; |
| 150 | + never_reset_chan[self->chan_handle.channel] = false; |
| 151 | +} |
| 152 | + |
| 153 | +bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) { |
| 154 | + return self->deinited == true; |
| 155 | +} |
| 156 | + |
| 157 | +void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) { |
| 158 | + if (common_hal_pulseio_pwmout_deinited(self)) { |
| 159 | + return; |
| 160 | + } |
| 161 | + ledc_stop(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, 0); |
| 162 | + // Search if any other channel is using the timer |
| 163 | + bool taken = false; |
| 164 | + for (size_t i =0; i < LEDC_CHANNEL_MAX; i++) { |
| 165 | + if (reserved_channels[i] == self->tim_handle.timer_num) { |
| 166 | + taken = true; |
| 167 | + } |
| 168 | + } |
| 169 | + // Variable frequency means there's only one channel on the timer |
| 170 | + if (!taken || self->variable_frequency) { |
| 171 | + ledc_timer_rst(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num); |
| 172 | + reserved_timer_freq[self->tim_handle.timer_num] = 0; |
| 173 | + } |
| 174 | + reset_pin_number(self->pin_number); |
| 175 | + reserved_channels[self->chan_handle.channel] = INDEX_EMPTY; |
| 176 | + self->deinited = true; |
| 177 | +} |
| 178 | + |
| 179 | +void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) { |
| 180 | + ledc_set_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, duty >> (16 - self->duty_resolution)); |
| 181 | + ledc_update_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel); |
| 182 | +} |
| 183 | + |
| 184 | +uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) { |
| 185 | + return ledc_get_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel) << (16 - self->duty_resolution); |
| 186 | +} |
| 187 | + |
| 188 | +void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) { |
| 189 | + ledc_set_freq(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num, frequency); |
| 190 | +} |
| 191 | + |
| 192 | +uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) { |
| 193 | + return ledc_get_freq(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num); |
| 194 | +} |
| 195 | + |
| 196 | +bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) { |
| 197 | + return self->variable_frequency; |
| 198 | +} |
0 commit comments