From 3a4663150e9dd1792ca8a91fa4e76c4549ab4210 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Fri, 27 Apr 2018 01:29:13 +0200 Subject: [PATCH] runtime: Implement GPIO output Now we can actually blink a LED! --- src/examples/blinky/blinky.go | 15 +++++++++++---- src/machine/machine.go | 26 ++++++++++++++++++++++++++ src/runtime/nrfx_config.h | 4 ++++ src/runtime/runtime_nrf.c | 16 ++++++++++++++++ src/runtime/runtime_nrf.h | 9 +++++++++ 5 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 src/machine/machine.go diff --git a/src/examples/blinky/blinky.go b/src/examples/blinky/blinky.go index efda2708..10aaeb79 100644 --- a/src/examples/blinky/blinky.go +++ b/src/examples/blinky/blinky.go @@ -1,14 +1,21 @@ package main -import "runtime" +import ( + "machine" + "runtime" +) func main() { + led := machine.GPIO{17} // LED 1 on the PCA10040 + led.Configure(machine.GPIOConfig{Mode: machine.GPIO_OUTPUT}) for { - // TODO: enable/disable actual LED println("LED on") - runtime.Sleep(runtime.Millisecond * 250) + led.Set(false) + runtime.Sleep(runtime.Millisecond * 500) + println("LED off") - runtime.Sleep(runtime.Millisecond * 250) + led.Set(true) + runtime.Sleep(runtime.Millisecond * 500) } } diff --git a/src/machine/machine.go b/src/machine/machine.go new file mode 100644 index 00000000..952addf5 --- /dev/null +++ b/src/machine/machine.go @@ -0,0 +1,26 @@ + +package machine + +// #include "../runtime/runtime_nrf.h" +import "C" + +type GPIO struct { + Pin uint32 +} + +type GPIOConfig struct { + Mode uint8 +} + +const ( + GPIO_INPUT = iota + GPIO_OUTPUT +) + +func (p GPIO) Configure(config GPIOConfig) { + C.gpio_cfg(C.uint(p.Pin), C.gpio_mode_t(config.Mode)) +} + +func (p GPIO) Set(value bool) { + C.gpio_set(C.uint(p.Pin), C.bool(value)) +} diff --git a/src/runtime/nrfx_config.h b/src/runtime/nrfx_config.h index e69de29b..f2086eb4 100644 --- a/src/runtime/nrfx_config.h +++ b/src/runtime/nrfx_config.h @@ -0,0 +1,4 @@ + +#pragma once + +#define NRFX_ASSERT(expression) do { bool res = expression; (void)res; } while (0) diff --git a/src/runtime/runtime_nrf.c b/src/runtime/runtime_nrf.c index b0be67b5..b60e43ae 100644 --- a/src/runtime/runtime_nrf.c +++ b/src/runtime/runtime_nrf.c @@ -1,7 +1,9 @@ +#include "hal/nrf_gpio.h" #include "hal/nrf_uart.h" #include "nrf.h" #include "runtime.h" +#include "runtime_nrf.h" #include void uart_init(uint32_t pin_tx) { @@ -46,6 +48,20 @@ void RTC0_IRQHandler() { rtc_wakeup = true; } +void gpio_cfg(uint32_t pin, gpio_mode_t mode) { + nrf_gpio_cfg( + pin, + mode == GPIO_INPUT ? NRF_GPIO_PIN_DIR_INPUT : NRF_GPIO_PIN_DIR_OUTPUT, + mode == GPIO_INPUT ? NRF_GPIO_PIN_INPUT_CONNECT : NRF_GPIO_PIN_INPUT_DISCONNECT, + NRF_GPIO_PIN_NOPULL, + NRF_GPIO_PIN_S0S1, + NRF_GPIO_PIN_NOSENSE); +} + +void gpio_set(uint32_t pin, bool high) { + nrf_gpio_pin_write(pin, high); +} + void _start() { main(); } diff --git a/src/runtime/runtime_nrf.h b/src/runtime/runtime_nrf.h index 61acc3f5..945518f5 100644 --- a/src/runtime/runtime_nrf.h +++ b/src/runtime/runtime_nrf.h @@ -2,9 +2,18 @@ #pragma once #include +#include void uart_init(uint32_t pin_tx); void uart_send(uint8_t c); void rtc_init(); void rtc_sleep(uint32_t ticks); + +typedef enum { + GPIO_INPUT, + GPIO_OUTPUT, +} gpio_mode_t; + +void gpio_cfg(uint32_t pin, gpio_mode_t mode); +void gpio_set(uint32_t pin, bool high);