Skip to content

Commit 564a8fc

Browse files
committed
Pulled interrupts into an optional module.
1 parent fc7175d commit 564a8fc

17 files changed

+414
-341
lines changed

Makefile

+22-3
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ clean-esp32:
167167

168168
vet:
169169
$(MAKE) clean
170-
$(MAKE) all
170+
$(MAKE)
171171
$(MAKE) esp32-build
172172
$(MAKE) esp32s2-build
173173
$(MAKE) esp32c3-build
174174
$(MAKE) clean-esp32
175175
$(MAKE) add-optional
176176
$(MAKE) esp32-build
177177
$(MAKE) clean
178-
$(MAKE) all
178+
$(MAKE)
179179

180180
clean:
181181
rm -rf $(OUT)
@@ -292,7 +292,7 @@ ESP32_BOOT = $(COMMON_PHASE1) \
292292
$(COMMON_PHASE2) $(COMMON_FILETOOLS) \
293293
esp32/platform.fs \
294294
posix/httpd.fs posix/web_interface.fs esp32/web_interface.fs \
295-
esp32/registers.fs esp32/timers.fs \
295+
esp32/registers.fs \
296296
posix/telnetd.fs \
297297
esp32/optionals.fs \
298298
esp32/autoboot.fs common/fini.fs
@@ -325,6 +325,14 @@ $(GEN)/esp32_camera.h: \
325325
esp32/optional/camera/camera.fs \
326326
esp32/optional/camera/camera_server.fs >$@
327327

328+
$(GEN)/esp32_interrupts.h: \
329+
tools/source_to_string.js \
330+
esp32/optional/interrupts/interrupts.fs \
331+
esp32/optional/interrupts/timers.fs | $(GEN)
332+
$< interrupts_source $(VERSION) $(REVISION) \
333+
esp32/optional/interrupts/interrupts.fs \
334+
esp32/optional/interrupts/timers.fs >$@
335+
328336
$(GEN)/esp32_oled.h: \
329337
tools/source_to_string.js esp32/optional/oled/oled.fs | $(GEN)
330338
$< oled_source $(VERSION) $(REVISION) \
@@ -347,6 +355,7 @@ OPTIONAL_MODULES = \
347355
$(ESP32)/ESP32forth/assemblers.h \
348356
$(ESP32)/ESP32forth/camera.h \
349357
$(ESP32)/ESP32forth/oled.h \
358+
$(ESP32)/ESP32forth/interrupts.h \
350359
$(ESP32)/ESP32forth/rmt.h \
351360
$(ESP32)/ESP32forth/serial-bluetooth.h \
352361
$(ESP32)/ESP32forth/spi-flash.h
@@ -650,6 +659,15 @@ $(ESP32)/ESP32forth/optional/camera.h: \
650659
camera=@$(GEN)/esp32_camera.h \
651660
>$@
652661

662+
$(ESP32)/ESP32forth/optional/interrupts.h: \
663+
esp32/optional/interrupts/interrupts.h \
664+
$(GEN)/esp32_interrupts.h | $(ESP32)/ESP32forth/optional
665+
cat esp32/optional/interrupts/interrupts.h | tools/replace.js \
666+
VERSION=$(VERSION) \
667+
REVISION=$(REVISION) \
668+
interrupts=@$(GEN)/esp32_interrupts.h \
669+
>$@
670+
653671
$(ESP32)/ESP32forth/optional/oled.h: \
654672
esp32/optional/oled/oled.h \
655673
$(GEN)/esp32_oled.h | $(ESP32)/ESP32forth/optional
@@ -784,6 +802,7 @@ $(ESP32)/ESP32forth.zip: \
784802
$(ESP32)/ESP32forth/optional/assemblers.h \
785803
$(ESP32)/ESP32forth/optional/camera.h \
786804
$(ESP32)/ESP32forth/optional/oled.h \
805+
$(ESP32)/ESP32forth/optional/interrupts.h \
787806
$(ESP32)/ESP32forth/optional/rmt.h \
788807
$(ESP32)/ESP32forth/optional/serial-bluetooth.h \
789808
$(ESP32)/ESP32forth/optional/spi-flash.h

common/bits.h

+3
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ typedef struct {
3737
void *DOCREATE_OP;
3838
const BUILTIN_WORD *builtins;
3939
} G_SYS;
40+
41+
static G_SYS *g_sys = 0;
42+
static cell_t *forth_run(cell_t *init_rp);

common/core.h

-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ enum {
4343
#undef V
4444
};
4545

46-
static G_SYS *g_sys = 0;
47-
4846
static cell_t convert(const char *pos, cell_t n, cell_t base, cell_t *ret) {
4947
*ret = 0;
5048
cell_t negate = 0;

common/forth_namespace_tests.fs

-3
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,6 @@ e: test-esp32-forth-voclist
623623
out: streams
624624
out: tasks
625625
out: rtos
626-
out: interrupts
627626
out: sockets
628627
out: Serial
629628
out: ledc
@@ -682,7 +681,6 @@ e: check-esp32-builtins
682681

683682
e: check-esp32-bindings
684683
out: rtos
685-
out: interrupts
686684
out: sockets
687685
out: Serial
688686
out: ledc
@@ -699,7 +697,6 @@ e: test-esp32-forth-namespace
699697
' forth list-from
700698
out: FORTH
701699
out: telnetd
702-
out: timers
703700
out: registers
704701
out: webui
705702
out: login

esp32/ESP32forth.ino

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
{{tier2_opcodes}}
2727
{{floats}}
2828
{{calls}}
29+
{{bits}}
2930
{{builtins.h}}
3031
{{builtins.cpp}}
3132
{{main.cpp}}

esp32/bindings.fs

-24
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,6 @@ transfer sockets-builtins
7979
: ip. ( n -- ) ip# ip# ip# 255 and n. ;
8080
forth definitions
8181

82-
vocabulary interrupts interrupts definitions
83-
transfer interrupts-builtins
84-
DEFINED? gpio_config [IF]
85-
0 constant ESP_INTR_FLAG_DEFAULT
86-
: ESP_INTR_FLAG_LEVELn ( n=1-6 -- n ) 1 swap lshift ;
87-
1 7 lshift constant ESP_INTR_FLAG_NMI
88-
1 8 lshift constant ESP_INTR_FLAG_SHARED
89-
1 9 lshift constant ESP_INTR_FLAG_EDGE
90-
1 10 lshift constant ESP_INTR_FLAG_IRAM
91-
1 11 lshift constant ESP_INTR_FLAG_INTRDISABLED
92-
( Prefix these with # because GPIO_INTR_DISABLE conflicts with a function. )
93-
0 constant #GPIO_INTR_DISABLE
94-
1 constant #GPIO_INTR_POSEDGE
95-
2 constant #GPIO_INTR_NEGEDGE
96-
3 constant #GPIO_INTR_ANYEDGE
97-
4 constant #GPIO_INTR_LOW_LEVEL
98-
5 constant #GPIO_INTR_HIGH_LEVEL
99-
( Easy word to trigger on any change to a pin )
100-
ESP_INTR_FLAG_DEFAULT gpio_install_isr_service drop
101-
: pinchange ( xt pin ) dup #GPIO_INTR_ANYEDGE gpio_set_intr_type throw
102-
swap 0 gpio_isr_handler_add throw ;
103-
[THEN]
104-
forth definitions
105-
10682
vocabulary rtos rtos definitions
10783
transfer rtos-builtins
10884
forth definitions

esp32/builtins.cpp

-61
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
static char filename[PATH_MAX];
1616
static char filename2[PATH_MAX];
1717

18-
{{bits}}
1918
{{core}}
2019
{{faults}}
2120
{{calling}}
@@ -52,63 +51,3 @@ static cell_t ResizeFile(cell_t fd, cell_t size) {
5251
if (t < 0) { return errno; }
5352
return 0;
5453
}
55-
56-
#ifdef ENABLE_INTERRUPTS_SUPPORT
57-
struct handle_interrupt_args {
58-
cell_t xt;
59-
cell_t arg;
60-
};
61-
62-
static void IRAM_ATTR HandleInterrupt(void *arg) {
63-
struct handle_interrupt_args *args = (struct handle_interrupt_args *) arg;
64-
cell_t code[2];
65-
code[0] = args->xt;
66-
code[1] = g_sys->YIELD_XT;
67-
cell_t fstack[INTERRUPT_STACK_CELLS];
68-
cell_t rstack[INTERRUPT_STACK_CELLS];
69-
cell_t stack[INTERRUPT_STACK_CELLS];
70-
stack[0] = args->arg;
71-
cell_t *rp = rstack;
72-
*++rp = (cell_t) code;
73-
*++rp = (cell_t) (fstack + 1);
74-
*++rp = (cell_t) (stack + 1);
75-
forth_run(rp);
76-
}
77-
78-
static bool IRAM_ATTR HandleInterruptAndRet(void *arg) {
79-
HandleInterrupt(arg);
80-
return true;
81-
}
82-
83-
static cell_t EspIntrAlloc(cell_t source, cell_t flags, cell_t xt, cell_t arg, void *ret) {
84-
// NOTE: Leaks memory.
85-
struct handle_interrupt_args *args = (struct handle_interrupt_args *) malloc(sizeof(struct handle_interrupt_args));
86-
args->xt = xt;
87-
args->arg = arg;
88-
return esp_intr_alloc(source, flags, HandleInterrupt, args, (intr_handle_t *) ret);
89-
}
90-
91-
static cell_t GpioIsrHandlerAdd(cell_t pin, cell_t xt, cell_t arg) {
92-
// NOTE: Leaks memory.
93-
struct handle_interrupt_args *args = (struct handle_interrupt_args *) malloc(sizeof(struct handle_interrupt_args));
94-
args->xt = xt;
95-
args->arg = arg;
96-
return gpio_isr_handler_add((gpio_num_t) pin, HandleInterrupt, args);
97-
}
98-
99-
static void TimerInitNull(cell_t group, cell_t timer) {
100-
// Seems to be required starting in the 2.0 IDE.
101-
timer_config_t config;
102-
memset(&config, 0, sizeof(config));
103-
config.divider = 2;
104-
timer_init((timer_group_t) group, (timer_idx_t) timer, &config);
105-
}
106-
107-
static cell_t TimerIsrCallbackAdd(cell_t group, cell_t timer, cell_t xt, cell_t arg, cell_t flags) {
108-
// NOTE: Leaks memory.
109-
struct handle_interrupt_args *args = (struct handle_interrupt_args *) malloc(sizeof(struct handle_interrupt_args));
110-
args->xt = xt;
111-
args->arg = arg;
112-
return timer_isr_callback_add((timer_group_t) group, (timer_idx_t) timer, HandleInterruptAndRet, args, flags);
113-
}
114-
#endif

esp32/builtins.h

+9-80
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@
3636
# define OPTIONAL_ASSEMBLERS_SUPPORT
3737
# endif
3838

39+
// Hook to pull in optional interrupts and timers support.
40+
# if __has_include("interrupts.h")
41+
# include "interrupts.h"
42+
# else
43+
# define OPTIONAL_INTERRUPTS_VOCABULARIES
44+
# define OPTIONAL_INTERRUPTS_SUPPORT
45+
# endif
46+
3947
// Hook to pull in optional Oled support.
4048
# if __has_include("oled.h")
4149
# include "oled.h"
@@ -101,13 +109,13 @@ static cell_t ResizeFile(cell_t fd, cell_t size);
101109
OPTIONAL_I2C_SUPPORT \
102110
OPTIONAL_SOCKETS_SUPPORT \
103111
OPTIONAL_FREERTOS_SUPPORT \
104-
OPTIONAL_INTERRUPTS_SUPPORT \
105112
CALLING_OPCODE_LIST \
106113
FLOATING_POINT_LIST
107114

108115
#define EXTERNAL_OPTIONAL_MODULE_SUPPORT \
109116
OPTIONAL_ASSEMBLERS_SUPPORT \
110117
OPTIONAL_CAMERA_SUPPORT \
118+
OPTIONAL_INTERRUPTS_SUPPORT \
111119
OPTIONAL_OLED_SUPPORT \
112120
OPTIONAL_RMT_SUPPORT \
113121
OPTIONAL_SERIAL_BLUETOOTH_SUPPORT \
@@ -278,85 +286,6 @@ static cell_t ResizeFile(cell_t fd, cell_t size);
278286
YV(rtos, xPortGetCoreID, PUSH xPortGetCoreID())
279287
#endif
280288

281-
#ifndef ENABLE_INTERRUPTS_SUPPORT
282-
# define OPTIONAL_INTERRUPTS_SUPPORT
283-
#else
284-
# ifndef SIM_PRINT_ONLY
285-
# include "esp_intr_alloc.h"
286-
# include "driver/timer.h"
287-
# include "driver/gpio.h"
288-
static cell_t EspIntrAlloc(cell_t source, cell_t flags, cell_t xt, cell_t arg, void *ret);
289-
static cell_t GpioIsrHandlerAdd(cell_t pin, cell_t xt, cell_t arg);
290-
static cell_t TimerIsrCallbackAdd(cell_t group, cell_t timer, cell_t xt, cell_t arg, cell_t flags);
291-
static void TimerInitNull(cell_t group, cell_t timer);
292-
# endif
293-
# define OPTIONAL_INTERRUPTS_SUPPORT \
294-
YV(interrupts, gpio_config, n0 = gpio_config((const gpio_config_t *) a0)) \
295-
YV(interrupts, gpio_reset_pin, n0 = gpio_reset_pin((gpio_num_t) n0)) \
296-
YV(interrupts, gpio_set_intr_type, n0 = gpio_set_intr_type((gpio_num_t) n1, (gpio_int_type_t) n0); NIP) \
297-
YV(interrupts, gpio_intr_enable, n0 = gpio_intr_enable((gpio_num_t) n0)) \
298-
YV(interrupts, gpio_intr_disable, n0 = gpio_intr_disable((gpio_num_t) n0)) \
299-
YV(interrupts, gpio_set_level, n0 = gpio_set_level((gpio_num_t) n1, n0); NIP) \
300-
YV(interrupts, gpio_get_level, n0 = gpio_get_level((gpio_num_t) n0)) \
301-
YV(interrupts, gpio_set_direction, n0 = gpio_set_direction((gpio_num_t) n1, (gpio_mode_t) n0); NIP) \
302-
YV(interrupts, gpio_set_pull_mode, n0 = gpio_set_pull_mode((gpio_num_t) n1, (gpio_pull_mode_t) n0); NIP) \
303-
YV(interrupts, gpio_wakeup_enable, n0 = gpio_wakeup_enable((gpio_num_t) n1, (gpio_int_type_t) n0); NIP) \
304-
YV(interrupts, gpio_wakeup_disable, n0 = gpio_wakeup_disable((gpio_num_t) n0)) \
305-
YV(interrupts, gpio_pullup_en, n0 = gpio_pullup_en((gpio_num_t) n0)) \
306-
YV(interrupts, gpio_pullup_dis, n0 = gpio_pullup_dis((gpio_num_t) n0)) \
307-
YV(interrupts, gpio_pulldown_en, n0 = gpio_pulldown_en((gpio_num_t) n0)) \
308-
YV(interrupts, gpio_pulldown_dis, n0 = gpio_pulldown_dis((gpio_num_t) n0)) \
309-
YV(interrupts, gpio_hold_en, n0 = gpio_hold_en((gpio_num_t) n0)) \
310-
YV(interrupts, gpio_hold_dis, n0 = gpio_hold_dis((gpio_num_t) n0)) \
311-
YV(interrupts, gpio_deep_sleep_hold_en, gpio_deep_sleep_hold_en()) \
312-
YV(interrupts, gpio_deep_sleep_hold_dis, gpio_deep_sleep_hold_dis()) \
313-
YV(interrupts, gpio_install_isr_service, n0 = gpio_install_isr_service(n0)) \
314-
YV(interrupts, gpio_uninstall_isr_service, gpio_uninstall_isr_service()) \
315-
YV(interrupts, gpio_isr_handler_add, n0 = GpioIsrHandlerAdd(n2, n1, n0); NIPn(2)) \
316-
YV(interrupts, gpio_isr_handler_remove, n0 = gpio_isr_handler_remove((gpio_num_t) n0)) \
317-
YV(interrupts, gpio_set_drive_capability, n0 = gpio_set_drive_capability((gpio_num_t) n1, (gpio_drive_cap_t) n0); NIP) \
318-
YV(interrupts, gpio_get_drive_capability, n0 = gpio_get_drive_capability((gpio_num_t) n1, (gpio_drive_cap_t *) a0); NIP) \
319-
YV(interrupts, esp_intr_alloc, n0 = EspIntrAlloc(n4, n3, n2, n1, a0); NIPn(4)) \
320-
YV(interrupts, esp_intr_free, n0 = esp_intr_free((intr_handle_t) n0)) \
321-
YV(timers, timer_isr_callback_add, n0 = TimerIsrCallbackAdd(n4, n3, n2, n1, n0); NIPn(4)) \
322-
YV(timers, timer_init_null, TimerInitNull(n1, n0); DROPn(2)) \
323-
YV(timers, timer_get_counter_value, \
324-
n0 = timer_get_counter_value((timer_group_t) n2, (timer_idx_t) n1, \
325-
(uint64_t *) a0); NIPn(2)) \
326-
YV(timers, timer_set_counter_value, \
327-
uint64_t val = *(uint64_t *) a0; \
328-
n0 = timer_set_counter_value((timer_group_t) n2, (timer_idx_t) n1, val); NIPn(2)) \
329-
YV(timers, timer_start, \
330-
n0 = timer_start((timer_group_t) n1, (timer_idx_t) n0); NIP) \
331-
YV(timers, timer_pause, \
332-
n0 = timer_pause((timer_group_t) n1, (timer_idx_t) n0); NIP) \
333-
YV(timers, timer_set_counter_mode, \
334-
n0 = timer_set_counter_mode((timer_group_t) n2, (timer_idx_t) n1, \
335-
(timer_count_dir_t) n0); NIPn(2)) \
336-
YV(timers, timer_set_auto_reload, \
337-
n0 = timer_set_auto_reload((timer_group_t) n2, (timer_idx_t) n1, \
338-
(timer_autoreload_t) n0); NIPn(2)) \
339-
YV(timers, timer_set_divider, \
340-
n0 = timer_set_divider((timer_group_t) n2, (timer_idx_t) n1, n0); NIPn(2)) \
341-
YV(timers, timer_set_alarm_value, uint64_t val = *(uint64_t *) a0; \
342-
n0 = timer_set_alarm_value((timer_group_t) n2, (timer_idx_t) n1, val); NIPn(2)) \
343-
YV(timers, timer_get_alarm_value, \
344-
n0 = timer_get_alarm_value((timer_group_t) n2, (timer_idx_t) n1, \
345-
(uint64_t *) a0); NIPn(2)) \
346-
YV(timers, timer_set_alarm, \
347-
n0 = timer_set_alarm((timer_group_t) n2, (timer_idx_t) n1, \
348-
(timer_alarm_t) n0); NIPn(2)) \
349-
YV(timers, timer_group_intr_enable, \
350-
n0 = timer_group_intr_enable((timer_group_t) n1, (timer_intr_t) n0); NIP) \
351-
YV(timers, timer_group_intr_disable, \
352-
n0 = timer_group_intr_disable((timer_group_t) n1, (timer_intr_t) n0); NIP) \
353-
YV(timers, timer_enable_intr, \
354-
n0 = timer_enable_intr((timer_group_t) n1, (timer_idx_t) n0); NIP) \
355-
YV(timers, timer_disable_intr, \
356-
n0 = timer_disable_intr((timer_group_t) n1, (timer_idx_t) n0); NIP)
357-
358-
#endif
359-
360289
#ifndef ENABLE_SOCKETS_SUPPORT
361290
# define OPTIONAL_SOCKETS_SUPPORT
362291
#else
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
\ Copyright 2023 Bradley D. Nelson
2+
\
3+
\ Licensed under the Apache License, Version 2.0 (the "License");
4+
\ you may not use this file except in compliance with the License.
5+
\ You may obtain a copy of the License at
6+
\
7+
\ http://www.apache.org/licenses/LICENSE-2.0
8+
\
9+
\ Unless required by applicable law or agreed to in writing, software
10+
\ distributed under the License is distributed on an "AS IS" BASIS,
11+
\ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
\ See the License for the specific language governing permissions and
13+
\ limitations under the License.
14+
15+
vocabulary interrupts interrupts definitions
16+
transfer interrupts-builtins
17+
DEFINED? gpio_config [IF]
18+
0 constant ESP_INTR_FLAG_DEFAULT
19+
: ESP_INTR_FLAG_LEVELn ( n=1-6 -- n ) 1 swap lshift ;
20+
1 7 lshift constant ESP_INTR_FLAG_NMI
21+
1 8 lshift constant ESP_INTR_FLAG_SHARED
22+
1 9 lshift constant ESP_INTR_FLAG_EDGE
23+
1 10 lshift constant ESP_INTR_FLAG_IRAM
24+
1 11 lshift constant ESP_INTR_FLAG_INTRDISABLED
25+
( Prefix these with # because GPIO_INTR_DISABLE conflicts with a function. )
26+
0 constant #GPIO_INTR_DISABLE
27+
1 constant #GPIO_INTR_POSEDGE
28+
2 constant #GPIO_INTR_NEGEDGE
29+
3 constant #GPIO_INTR_ANYEDGE
30+
4 constant #GPIO_INTR_LOW_LEVEL
31+
5 constant #GPIO_INTR_HIGH_LEVEL
32+
( Easy word to trigger on any change to a pin )
33+
ESP_INTR_FLAG_DEFAULT gpio_install_isr_service drop
34+
: pinchange ( xt pin ) dup #GPIO_INTR_ANYEDGE gpio_set_intr_type throw
35+
swap 0 gpio_isr_handler_add throw ;
36+
[THEN]
37+
forth definitions

0 commit comments

Comments
 (0)