Skip to content

Commit ff987e7

Browse files
committed
add timer peripheral
1 parent 18e463c commit ff987e7

File tree

4 files changed

+129
-9
lines changed

4 files changed

+129
-9
lines changed

ports/esp32s2/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ SRC_C += \
188188
lib/utils/pyexec.c \
189189
lib/utils/stdout_helpers.c \
190190
lib/utils/sys_stdio_mphal.c \
191+
peripherals/timer.c \
191192
peripherals/pcnt.c \
192193
peripherals/pins.c \
193194
peripherals/rmt.c \

ports/esp32s2/peripherals/timer.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
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 "peripherals/timer.h"
28+
29+
#define TIMER_FREE 1
30+
#define TIMER_BUSY 0
31+
32+
static uint8_t timer_state[2][2];
33+
34+
void peripherals_timer_reset(void) {
35+
timer_index_t timer;
36+
for (uint8_t i = 0; i < 2; i++) {
37+
for (uint8_t j = 0; j < 2; j++) {
38+
if (timer_state[i][j] == TIMER_BUSY) {
39+
timer.idx = (timer_idx_t)j;
40+
timer.group = (timer_group_t)i;
41+
timer_state[i][j] = TIMER_FREE;
42+
peripherals_timer_deinit(&timer);
43+
}
44+
}
45+
}
46+
}
47+
48+
void peripherals_timer_init(const timer_config_t * config, timer_index_t * timer) {
49+
// get free timer
50+
for (uint8_t i = 0; i < 2; i++) {
51+
for (uint8_t j = 0; j < 2; j++) {
52+
if (timer_state[i][j] == TIMER_FREE) {
53+
timer->idx = (timer_idx_t)j;
54+
timer->group = (timer_group_t)i;
55+
timer_state[i][j] = TIMER_BUSY;
56+
goto init_timer;
57+
} else if (i == 1 && j == 1) {
58+
timer->idx = TIMER_MAX;
59+
timer->group = TIMER_GROUP_MAX;
60+
return;
61+
}
62+
}
63+
}
64+
65+
init_timer:
66+
// initialize timer module
67+
timer_init(timer->group, timer->idx, config);
68+
timer_set_counter_value(timer->group, timer->idx, 0);
69+
}
70+
71+
void peripherals_timer_deinit(timer_index_t * timer) {
72+
timer_deinit(timer->group, timer->idx);
73+
}

ports/esp32s2/peripherals/timer.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
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_PERIPHERALS_TIMER_HANDLER_H
28+
#define MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TIMER_HANDLER_H
29+
30+
#include "driver/timer.h"
31+
32+
typedef struct {
33+
timer_idx_t idx;
34+
timer_group_t group;
35+
} timer_index_t;
36+
37+
extern void peripherals_timer_init(const timer_config_t * config, timer_index_t * timer);
38+
extern void peripherals_timer_deinit(timer_index_t * timer);
39+
extern void peripherals_timer_reset(void);
40+
41+
#endif // MICROPY_INCLUDED_ESP32S2_PERIPHERALS_TIMER_HANDLER_H

ports/esp32s2/supervisor/port.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050

5151
#include "peripherals/rmt.h"
5252
#include "peripherals/pcnt.h"
53+
#include "peripherals/timer.h"
5354
#include "components/heap/include/esp_heap_caps.h"
5455
#include "components/soc/soc/esp32s2/include/soc/cache_memory.h"
5556

@@ -103,15 +104,6 @@ void reset_port(void) {
103104
analogout_reset();
104105
#endif
105106

106-
#if CIRCUITPY_PULSEIO
107-
esp32s2_peripherals_rmt_reset();
108-
pulsein_reset();
109-
#endif
110-
111-
#if CIRCUITPY_PWMIO
112-
pwmout_reset();
113-
#endif
114-
115107
#if CIRCUITPY_BUSIO
116108
i2c_reset();
117109
spi_reset();
@@ -122,6 +114,19 @@ void reset_port(void) {
122114
peripherals_pcnt_reset();
123115
#endif
124116

117+
#if CIRCUITPY_FREQUENCYIO
118+
peripherals_timer_reset();
119+
#endif
120+
121+
#if CIRCUITPY_PULSEIO
122+
esp32s2_peripherals_rmt_reset();
123+
pulsein_reset();
124+
#endif
125+
126+
#if CIRCUITPY_PWMIO
127+
pwmout_reset();
128+
#endif
129+
125130
#if CIRCUITPY_RTC
126131
rtc_reset();
127132
#endif

0 commit comments

Comments
 (0)