8000 Add PWMOut · ansonhex/circuitpython@61a2e4f · GitHub
[go: up one dir, main page]

Skip to content

Commit 61a2e4f

Browse files
committed
Add PWMOut
1 parent 207369e commit 61a2e4f

File tree

4 files changed

+135
-50
lines changed

4 files changed

+135
-50
lines changed

ports/esp32s2/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ all: $(BUILD)/firmware.bin $(BUILD)/firmware.uf2
262262

263263
$(BUILD)/firmware.elf: $(OBJ) | $(ESP_IDF_COMPONENTS_EXPANDED) $(ESP_AUTOGEN_LD)
264264
$(STEPECHO) "LINK $@"
265-
$(Q)$(CC) -o $@ $(LDFLAGS) $^ $(ESP_IDF_COMPONENTS_EXPANDED) $(BINARY_BLOBS) build-$(BOARD)/esp-idf/esp-idf/newlib/libnewlib.a -u newlib_include_pthread_impl
265+
$(Q)$(CC) -o $@ $(LDFLAGS) $^ $(ESP_IDF_COMPONENTS_EXPANDED) $(BINARY_BLOBS) build-$(BOARD)/esp-idf/esp-idf/newlib/libnewlib.a -u newlib_include_pthread_impl -Wl,--start-group $(LIBS) -Wl,--end-group
266266
# $(Q)$(SIZE) $@ | $(PYTHON3) $(TOP)/tools/build_memory_info.py $(BUILD)/esp-idf/esp-idf/esp32s2/esp32s2_out.ld
267267

268268
$(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf

ports/esp32s2/common-hal/pulseio/PWMOut.c

Lines changed: 124 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,97 +24,176 @@
2424
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2525
* THE SOFTWARE.
2626
*/
27-
27+
#include <math.h>
2828

2929
#include "common-hal/pulseio/PWMOut.h"
3030
#include "shared-bindings/pulseio/PWMOut.h"
3131
#include "py/runtime.h"
3232
#include "driver/ledc.h"
3333

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
4935

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];
5040

5141
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+
}
5254
}
5355

5456
pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self,
5557
const mcu_pin_obj_t* pin,
5658
uint16_t duty,
5759
uint32_t frequency,
5860
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+
}
81100
}
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;
82122

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"));
86125
}
87126

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+
88141
return PWMOUT_OK;
89142
}
90143

91144
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;
92147
}
93148

94149
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;
95152
}
96153

97154
bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) {
98-
return false;
155+
return self->deinited == true;
99156
}
100157

101158
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;
102178
}
103179

104180
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);
105183
}
106184

107185
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);
109187
}
110188

111189
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);
112191
}
113192

114193
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);
116195
}
117196

118197
bool common_hal_pulseio_pwmout_get_variable_frequency(pulseio_pwmout_obj_t* self) {
119-
return false;
198+
return self->variable_frequency;
120199
}

ports/esp32s2/common-hal/pulseio/PWMOut.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PWMOUT_H
2929

3030
#include "common-hal/microcontroller/Pin.h"
31+
#include "driver/ledc.h"
3132

3233
typedef struct {
3334
mp_obj_base_t base;
34-
uint8_t channel;
35+
ledc_timer_config_t tim_handle;
36+
ledc_channel_config_t chan_handle;
37+
uint16_t pin_number;
38+
uint8_t duty_resolution;
3539
bool variable_frequency: 1;
36-
uint16_t duty_cycle;
37-
uint32_t frequency;
38-
uint32_t period;
40+
bool deinited: 1;
3941
} pulseio_pwmout_obj_t;
4042

4143
void pwmout_reset(void);

ports/esp32s2/supervisor/port.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "common-hal/busio/I2C.h"
3939
#include "common-hal/busio/SPI.h"
4040
#include "common-hal/busio/UART.h"
41+
#include "common-hal/pulseio/PWMOut.h"
4142
#include "supervisor/memory.h"
4243
#include "supervisor/shared/tick.h"
4344

@@ -64,6 +65,9 @@ void reset_port(void) {
6465
// A larger delay so the idle task can run and do any IDF cleanup needed.
6566
vTaskDelay(4);
6667

68+
#if CIRCUITPY_PULSEIO
69+
pwmout_reset();
70+
#endif
6771
#if CIRCUITPY_BUSIO
6872
i2c_reset();
6973
spi_reset();

0 commit comments

Comments
 (0)
0