Skip to content
This repository was archived by the owner on Mar 11, 2019. It is now read-only.

Commit debc2ce

Browse files
committed
esp32: Add machine module with GPIO object
1 parent a2cc28b commit debc2ce

File tree

6 files changed

+98
-13
lines changed

6 files changed

+98
-13
lines changed

ports/common/rules.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ $(BUILD)/os.o: $(SRC_GO_OS) $(BUILD)/syscall.o
2828
@echo "Go $@"
2929
$(Q)$(GCCGO) $(GOFLAGS) -fgo-pkgpath=os -c -o $@ $(SRC_GO_OS)
3030

31+
$(BUILD)/machine.o: $(SRC_GO_MACHINE)
32+
@echo "Go $@"
33+
$(Q)$(GCCGO) $(GOFLAGS) -fgo-pkgpath=machine -c -o $@ $(SRC_GO_MACHINE)
34+
3135
# Build libgo C sources.
3236
# TODO: this uses gnu99 to work around undefined stack_t
3337
$(BUILD)/libgo/%.o: $(GCCREPO)/libgo/runtime/%.c

ports/esp32/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ include ../common/vars.mk
88
SRC_C_PORT += main.c
99
SRC_GO_RUNTIME += runtime.go
1010
SRC_GO_OS += os.go
11+
SRC_GO_MACHINE += machine.go
12+
13+
LIBS += $(BUILD)/machine.o
1114

1215
all: mkdir $(LIBS) $(BUILD)/esp32_out.ld #$(BUILD)/bootloader.bin
1316

ports/esp32/machine.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
2+
package machine
3+
4+
func gpio_config(*gpio_config_t) uint __asm__("gpio_config")
5+
func gpio_set_direction(uint, uint) uint __asm__("gpio_set_direction")
6+
func gpio_set_level(uint, uint32) uint __asm__("gpio_set_level")
7+
func gpio_get_level(uint) int __asm__("gpio_get_level")
8+
9+
type gpio_config_t struct {
10+
pin_bit_mask uint64
11+
mode uint8
12+
pull_up_en uint8
13+
pull_down_en uint8
14+
gpio_int_type uint8
15+
}
16+
17+
const (
18+
GPIO_DISABLE = 0
19+
GPIO_INPUT = 1
20+
GPIO_OUTPUT = 2
21+
GPIO_OPEN_DRAIN = 4
22+
)
23+
24+
const (
25+
GPIO_PULLUP = iota
26+
GPIO_PULLDOWN
27+
GPIO_PULLBOTH
28+
GPIO_FLOATING
29+
)
30+
31+
type GPIO struct {
32+
pin uint
33+
}
34+
35+
type GPIOConfig struct {
36+
Mode uint8
37+
Pull uint8 // TODO
38+
}
39+
40+
func (p *GPIO) Configure(config GPIOConfig) {
41+
c := gpio_config_t{
42+
pin_bit_mask: 1 << p.pin,
43+
mode: config.Mode,
44+
}
45+
gpio_config(&c)
46+
}
47+
48+
func (p *GPIO) Set(value bool) {
49+
level := uint32(0)
50+
if value {
51+
level = 1
52+
}
53+
gpio_set_level(p.pin, level)
54+
}
55+
56+
func (p *GPIO) Get() bool {
57+
level := gpio_get_level(p.pin)
58+
if level != 0 {
59+
return true
60+
}
61+
return false
62+
}
63+
64+
// TODO: split the rest of this module into a board package.
65+
66+
var (
67+
GPIO2 = GPIO{2}
68+
GPIO4 = GPIO{4}
69+
)
70+
71+
var LED_BUILTIN = GPIO2

ports/esp32/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ esp_err_t event_handler(void *ctx, system_event_t *event)
99
return ESP_OK;
1010
}
1111

12+
void machine_init() __asm__("machine..import");
13+
1214
void app_main(void)
1315
{
1416
//go_init(); // TODO
17+
machine_init();
1518
go_main();
1619

1720
// sleep forever

tinygo

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,16 @@ if len(args) >= 2:
2929
if mcu == 'esp32':
3030
os.environ['GOOS'] = 'esp32'
3131
os.environ['GOARCH'] = 'xtensa'
32+
port = 'esp32'
3233
elif mcu is not None:
3334
print('unknown MCU:', mcu, file=sys.stdout)
3435
sys.exit(1)
36+
else:
37+
port = 'unix'
3538

3639
# for tinygo-gccgo
3740
os.environ['TINYGO_LTO'] = str(int(lto))
41+
os.environ['TINYGO_PORT'] = port
3842
if mcu:
3943
os.environ['TINYGO_MCU'] = mcu
4044

@@ -62,5 +66,6 @@ os.environ['PATH'] = TINYGO + '/bin:' + os.environ['PATH']
6266
os.environ['GOROOT'] = TINYGO + '/goroot' # set GOROOT to fake runtime etc. packages
6367
os.environ['GOPATH'] = os.environ.get('GOPATH', os.environ['HOME'] + '/go') # set default GOPATH
6468
os.environ['GOPATH'] += ':' + TINYGO + '/libgo' # append standard library
69+
os.environ['GOPATH'] += ':%s/ports/%s/build' % (TINYGO, port)
6570

6671
sys.exit(subprocess.run(args).returncode)

tinygo-cmd

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ TOOLCHAIN = os.environ.get('TOOLCHAIN', '')
88
TINYGO = os.path.dirname(os.path.realpath(__file__))
99
MCU = os.environ.get('TINYGO_MCU') or None
1010
LTO = bool(int(os.environ.get('TINYGO_LTO')))
11+
PORT = os.environ['TINYGO_PORT']
1112

1213
CC = TOOLCHAIN + 'gcc'
1314
GCCGO = TOOLCHAIN + 'gccgo'
@@ -36,17 +37,13 @@ def get_lib_path(name):
3637
sys.exit(cmd.returncode)
3738
return cmd.stdout.decode('utf-8').strip()
3839

39-
port = 'unix'
40-
if MCU == 'esp32':
41-
port = 'esp32'
42-
4340
def wrapActionCompileC():
4441
# Compiling C code
4542
cmd[0] = CC
4643
cmd.extend(FLAGS)
4744
cmd.append('-I%s/goroot/src/runtime' % TINYGO)
4845
cmd.append('-I%s/gcc/libgo/runtime' % TINYGO)
49-
cmd.append('-I%s/ports/%s' % (TINYGO, port))
46+
cmd.append('-I%s/ports/%s' % (TINYGO, PORT))
5047
cmd.append('-fplan9-extensions')
5148

5249
def wrapActionCompileGo():
@@ -56,7 +53,7 @@ def wrapActionCompileGo():
5653
if MCU == 'esp32':
5754
cmd.append('-mlongcalls')
5855
cmd.append('-nostdlib')
59-
cmd.append('-L%s/ports/%s/build' % (TINYGO, port))
56+
cmd.append('-L%s/ports/%s/build' % (TINYGO, PORT))
6057

6158
def wrapActionArMerge():
6259
# Merge multiple object files together
@@ -68,9 +65,11 @@ def wrapActionLink():
6865
cmd.extend(FLAGS)
6966
cmd.append('-Wl,--gc-sections')
7067
cmd.append('-Wno-lto-type-mismatch')
71-
cmd.append('%s/ports/%s/build/syscall.o' % (TINYGO, port))
72-
cmd.append('%s/ports/%s/build/os.o' % (TINYGO, port))
73-
cmd.append('%s/ports/%s/build/libruntime.a' % (TINYGO, port))
68+
cmd.append('%s/ports/%s/build/syscall.o' % (TINYGO, PORT))
69+
cmd.append('%s/ports/%s/build/os.o' % (TINYGO, PORT))
70+
cmd.append('%s/ports/%s/build/libruntime.a' % (TINYGO, PORT))
71+
if os.path.exists('%s/ports/%s/build/machine.o' % (TINYGO, PORT)):
72+
cmd.append('%s/ports/%s/build/machine.o' % (TINYGO, PORT))
7473
if MCU == 'esp32':
7574
#cmd[0] = LD
7675
SDK = os.environ['ESPIDF']
@@ -92,12 +91,12 @@ def wrapActionLink():
9291
cmd.append('-lwpa2')
9392
cmd.append(SDK + '/components/newlib/lib/libc.a')
9493
cmd.append(SDK + '/components/newlib/lib/libm.a')
95-
cmd.append(SDK + '/components/' + port + '/libhal.a')
94+
cmd.append(SDK + '/components/' + PORT + '/libhal.a')
9695
cmd.append('-Wl,--end-group')
97-
cmd.append('-L%s/components/%s/ld' % (SDK, port))
98-
cmd.append('-L%s/components/%s/lib' % (SDK, port))
96+
cmd.append('-L%s/components/%s/ld' % (SDK, PORT))
97+
cmd.append('-L%s/components/%s/lib' % (SDK, PORT))
9998
cmd.append('-T')
100-
cmd.append('%s/ports/%s/build/esp32_out.ld' % (TINYGO, port))
99+
cmd.append('%s/ports/%s/build/esp32_out.ld' % (TINYGO, PORT))
101100
cmd.append('-T')
102101
cmd.append('esp32.common.ld')
103102
cmd.append('-T')

0 commit comments

Comments
 (0)