Skip to content

Commit 6a46fd5

Browse files
authored
Merge pull request #3185 from hierophect/esp32-pulseio
ESP32-S2: PWMOut
2 parents bca89c2 + cf0a4d2 commit 6a46fd5

File tree

10 files changed

+480
-2
lines changed

10 files changed

+480
-2
lines changed

ports/esp32s2/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ esp-idf-stamp: $(BUILD)/esp-idf/config/sdkconfig.h
270270

271271
$(BUILD)/firmware.elf: $(OBJ) | esp-idf-stamp
272272
$(STEPECHO) "LINK $@"
273-
$(Q)$(CC) -o $@ $(LDFLAGS) $^ $(ESP_IDF_COMPONENTS_EXPANDED) $(BINARY_BLOBS) build-$(BOARD)/esp-idf/esp-idf/newlib/libnewlib.a -u newlib_include_pthread_impl
273+
$(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
274274
# $(Q)$(SIZE) $@ | $(PYTHON3) $(TOP)/tools/build_memory_info.py $(BUILD)/esp-idf/esp-idf/esp32s2/esp32s2_out.ld
275275

276276
$(BUILD)/circuitpython-firmware.bin: $(BUILD)/firmware.elf
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
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+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PWMOUT_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PWMOUT_H
29+
30+
#include "common-hal/microcontroller/Pin.h"
31+
#include "driver/ledc.h"
32+
33+
typedef struct {
34+
mp_obj_base_t base;
35+
ledc_timer_config_t tim_handle;
36+
ledc_channel_config_t chan_handle;
37+
uint16_t pin_number;
38+
uint8_t duty_resolution;
39+
bool variable_frequency: 1;
40+
bool deinited: 1;
41+
} pulseio_pwmout_obj_t;
42+
43+
void pwmout_reset(void);
44+
45+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PWMOUT_H
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Lucian Copeland for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "common-hal/pulseio/PulseIn.h"
28+
#include "py/runtime.h"
29+
30+
// STATIC void pulsein_handler(uint8_t num) {
31+
// }
32+
33+
void pulsein_reset(void) {
34+
}
35+
36+
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin,
37+
uint16_t maxlen, bool idle_state) {
38+
mp_raise_NotImplementedError(translate("PulseIn not supported on this chip"));
39+
}
40+
41+
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
42+
return false;
43+
}
44+
45+
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
46+
}
47+
48+
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
49+
}
50+
51+
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) {
52+
}
53+
54+
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
55+
}
56+
57+
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) {
58+
return false;
59+
}
60+
61+
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
62+
return false;
63+
}
64+
65+
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
66+
return false;
67+
}
68+
69+
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
70+
return false;
71+
}
72+
73+
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
74+
return false;
75+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PULSEIN_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PULSEIN_H
29+
30+
#include "common-hal/microcontroller/Pin.h"
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
37+
const mcu_pin_obj_t* pin;
38+
bool idle_state;
39+
bool paused;
40+
volatile bool first_edge;
41+
42+
uint16_t* buffer;
43+
uint16_t maxlen;
44+
45+
volatile uint16_t start;
46+
volatile uint16_t len;
47+
volatile uint32_t last_overflow;
48+
volatile uint16_t last_count;
49+
} pulseio_pulsein_obj_t;
50+
51+
void pulsein_reset(void);
52+
53+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PULSEIN_H

0 commit comments

Comments
 (0)