|
24 | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
25 | 25 | * THE SOFTWARE.
|
26 | 26 | */
|
27 |
| - |
| 27 | +#include <math.h> |
28 | 28 |
|
29 | 29 | #include "common-hal/pulseio/PWMOut.h"
|
30 | 30 | #include "shared-bindings/pulseio/PWMOut.h"
|
31 | 31 | #include "py/runtime.h"
|
32 | 32 | #include "driver/ledc.h"
|
33 | 33 |
|
34 |
| -#define LEDC_LS_TIMER LEDC_TIMER_1 |
35 |
| -#define LEDC_LS_MODE LEDC_LOW_SPEED_MODE |
36 |
| -#define LEDC_LS_CH0_GPIO (18) |
37 |
| -#define LEDC_LS_CH0_CHANNEL LEDC_CHANNEL_0 |
38 |
| -#define LEDC_LS_CH1_GPIO (19) |
39 |
| -#define LEDC_LS_CH1_CHANNEL LEDC_CHANNEL_1 |
40 |
| -#define LEDC_LS_CH2_GPIO (4) |
41 |
| -#define LEDC_LS_CH2_CHANNEL LEDC_CHANNEL_2 |
42 |
| -#define LEDC_LS_CH3_GPIO (5) |
43 |
| -#define LEDC_LS_CH3_CHANNEL LEDC_CHANNEL_3 |
44 |
| - |
45 |
| -#define LEDC_TEST_CH_NUM (4) |
46 |
| -#define LEDC_TEST_DUTY (4000) |
47 |
| -#define LEDC_TEST_FADE_TIME (3000) |
48 |
| - |
| 34 | +#define INDEX_EMPTY 0xFF |
49 | 35 |
|
| 36 | +STATIC uint32_t reserved_timer_freq[LEDC_TIMER_MAX]; |
| 37 | +STATIC uint8_t reserved_channels[LEDC_CHANNEL_MAX] = {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; |
| 38 | +STATIC bool never_reset_tim[LEDC_TIMER_MAX]; |
| 39 | +STATIC bool never_reset_chan[LEDC_CHANNEL_MAX]; |
50 | 40 |
|
51 | 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 | + } |
52 | 54 | }
|
53 | 55 |
|
54 | 56 | pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
|
55 | 57 | const mcu_pin_obj_t* pin,
|
56 | 58 | uint16_t duty,
|
57 | 59 | uint32_t frequency,
|
58 | 60 | bool variable_frequency) {
|
59 |
| - ledc_timer_config_t ledc_timer = { |
60 |
| - .duty_resolution = LEDC_TIMER_16_BIT, // resolution of PWM duty |
61 |
| - .freq_hz = frequency, // frequency of PWM signal |
62 |
| - .speed_mode = LEDC_LS_MODE, // timer mode |
63 |
| - .timer_num = LEDC_LS_TIMER, // timer index |
64 |
| - .clk_cfg = LEDC_AUTO_CLK, // Auto select the source clock |
65 |
| - }; |
66 |
| - // Set configuration of timer0 for high speed channels |
67 |
| - ledc_timer_config(&ledc_timer); |
68 |
| - |
69 |
| - ledc_channel_config_t pwm_channel = { |
70 |
| - .channel = LEDC_LS_CH0_CHANNEL, |
71 |
| - .duty = 0, |
72 |
| - .gpio_num = pin->number, |
73 |
| - .speed_mode = LEDC_LS_MODE, |
74 |
| - .hpoint = 0, |
75 |
| - .timer_sel = LEDC_LS_TIMER |
76 |
| - }; |
77 |
| - |
78 |
| - // Set LED Controller with previously prepared configuration |
79 |
| - for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { |
80 |
| - ledc_channel_config(&ledc_channel[ch]); |
| 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 | + } |
81 | 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; |
82 | 122 |
|
83 |
| - for (ch = 0; ch < LEDC_TEST_CH_NUM; ch++) { |
84 |
| - ledc_set_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel, duty*2); |
85 |
| - ledc_update_duty(ledc_channel[ch].speed_mode, ledc_channel[ch].channel); |
| 123 | + if (ledc_channel_config(&(self->chan_handle))) { |
| 124 | + mp_raise_ValueError(translate("Could not initialize channel")); |
86 | 125 | }
|
87 | 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 | + ledc_set_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, duty >> (16 - duty_bits)); |
| 139 | + ledc_update_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel); |
| 140 | + |
88 | 141 | return PWMOUT_OK;
|
89 | 142 | }
|
90 | 143 |
|
91 | 144 | void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) {
|
| 145 | + never_reset_tim[self->tim_handle.timer_num] = true; |
| 146 | + never_reset_chan[self->chan_handle.channel] = true; |
92 | 147 | }
|
93 | 148 |
|
94 | 149 | void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) {
|
| 150 | + never_reset_tim[self->tim_handle.timer_num] = false; |
| 151 | + never_reset_chan[self->chan_handle.channel] = false; |
95 | 152 | }
|
96 | 153 |
|
97 | 154 | bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
|
98 |
| - return false; |
| 155 | + return self->deinited == true; |
99 | 156 | }
|
100 | 157 |
|
101 | 158 | void common_hal_pulseio_pwmout_deinit(pulseio_pwmout_obj_t* self) {
|
| 159 | + if (common_hal_pulseio_pwmout_deinited(self)) { |
| 160 | + return; |
| 161 | + } |
| 162 | + ledc_stop(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, 0); |
| 163 | + ledc_timer_rst(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num); |
| 164 | + // Search if any other channel is using the timer |
| 165 | + bool taken = false; |
| 166 | + for (size_t i =0; i < LEDC_CHANNEL_MAX; i++) { |
| 167 | + if (reserved_channels[i] == self->tim_handle.timer_num) { |
| 168 | + taken = true; |
| 169 | + } |
| 170 | + } |
| 171 | + // Variable frequency means there's only one channel on the timer |
| 172 | + if (!taken || self->variable_frequency) { |
| 173 | + reserved_timer_freq[self->tim_handle.timer_num] = 0; |
| 174 | + } |
| 175 | + reset_pin_number(self->pin_number); |
| 176 | + reserved_channels[self->chan_handle.channel] = INDEX_EMPTY; |
| 177 | + self->deinited = true; |
102 | 178 | }
|
103 | 179 |
|
104 | 180 | void common_hal_pulseio_pwmout_set_duty_cycle(pulseio_pwmout_obj_t* self, uint16_t duty) {
|
| 181 | + ledc_set_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel, duty >> (16 - self->duty_resolution)); |
| 182 | + ledc_update_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel); |
105 | 183 | }
|
106 | 184 |
|
107 | 185 | uint16_t common_hal_pulseio_pwmout_get_duty_cycle(pulseio_pwmout_obj_t* self) {
|
108 |
| - return false; |
| 186 | + return ledc_get_duty(LEDC_LOW_SPEED_MODE, self->chan_handle.channel) << (16 - self->duty_resolution); |
109 | 187 | }
|
110 | 188 |
|
111 | 189 | void common_hal_pulseio_pwmout_set_frequency(pulseio_pwmout_obj_t* self, uint32_t frequency) {
|
| 190 | + ledc_set_freq(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num, frequency); |
112 | 191 | }
|
113 | 192 |
|
114 | 193 | uint32_t common_hal_pulseio_pwmout_get_frequency(pulseio_pwmout_obj_t* self) {
|
115 |
| - return false; |
| 194 | + return ledc_get_freq(LEDC_LOW_SPEED_MODE, self->tim_handle.timer_num); |
116 | 195 | }
|
117 | 196 |
|
118 | 197 | bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) {
|
119 |
| - return false; |
| 198 | + return self->variable_frequency; |
120 | 199 | }
|
0 commit comments