runtime: Implement GPIO output

Now we can actually blink a LED!
Этот коммит содержится в:
Ayke van Laethem 2018-04-27 01:29:13 +02:00
родитель 5bbd41e9fb
коммит 3a4663150e
5 изменённых файлов: 66 добавлений и 4 удалений

Просмотреть файл

@ -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)
}
}

26
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))
}

Просмотреть файл

@ -0,0 +1,4 @@
#pragma once
#define NRFX_ASSERT(expression) do { bool res = expression; (void)res; } while (0)

Просмотреть файл

@ -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 <string.h>
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();
}

Просмотреть файл

@ -2,9 +2,18 @@
#pragma once
#include <stdint.h>
#include <stdbool.h>
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);