mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 11:37:46 +00:00
machine: Rewrite most of the GPIO functionality
Split across device types (nrf, dummy) and use registers directly instead of the HAL.
This commit is contained in:
+8
-16
@@ -1,26 +1,18 @@
|
||||
|
||||
package machine
|
||||
|
||||
// #include "../runtime/runtime_nrf.h"
|
||||
import "C"
|
||||
type GPIOConfig struct {
|
||||
Mode GPIOMode
|
||||
}
|
||||
|
||||
type GPIO struct {
|
||||
Pin uint32
|
||||
Pin uint8
|
||||
}
|
||||
|
||||
type GPIOConfig struct {
|
||||
Mode uint8
|
||||
func (p GPIO) High() {
|
||||
p.Set(true)
|
||||
}
|
||||
|
||||
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))
|
||||
func (p GPIO) Low() {
|
||||
p.Set(false)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
|
||||
// +build !avr,!nrf
|
||||
|
||||
package machine
|
||||
|
||||
// Dummy machine package, filled with no-ops.
|
||||
|
||||
type GPIOMode uint8
|
||||
|
||||
const (
|
||||
GPIO_INPUT = iota
|
||||
GPIO_OUTPUT
|
||||
)
|
||||
|
||||
const LED = 0
|
||||
|
||||
func (p GPIO) Configure(config GPIOConfig) {
|
||||
}
|
||||
|
||||
func (p GPIO) Set(value bool) {
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
|
||||
// +build nrf
|
||||
|
||||
package machine
|
||||
|
||||
// #include "../runtime/runtime_nrf.h"
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"device/nrf"
|
||||
)
|
||||
|
||||
type GPIOMode uint8
|
||||
|
||||
const (
|
||||
GPIO_INPUT = nrf.P0_PIN_CNF_DIR_Input
|
||||
GPIO_OUTPUT = nrf.P0_PIN_CNF_DIR_Output
|
||||
)
|
||||
|
||||
// LEDs on the PCA10040 (nRF52832 dev board)
|
||||
const (
|
||||
LED = LED1
|
||||
LED1 = 17
|
||||
LED2 = 18
|
||||
LED3 = 19
|
||||
LED4 = 20
|
||||
)
|
||||
|
||||
func (p GPIO) Configure(config GPIOConfig) {
|
||||
//C.gpio_cfg(C.uint(p.Pin), C.gpio_mode_t(config.Mode))
|
||||
cfg := config.Mode | nrf.P0_PIN_CNF_PULL_Disabled | nrf.P0_PIN_CNF_DRIVE_S0S1 | nrf.P0_PIN_CNF_SENSE_Disabled
|
||||
if config.Mode == GPIO_INPUT {
|
||||
cfg |= nrf.P0_PIN_CNF_INPUT_Connect
|
||||
} else {
|
||||
cfg |= nrf.P0_PIN_CNF_INPUT_Disconnect
|
||||
}
|
||||
nrf.P0.PIN_CNF[p.Pin] = nrf.RegValue(cfg)
|
||||
}
|
||||
|
||||
func (p GPIO) Set(high bool) {
|
||||
//C.gpio_set(C.uint(p.Pin), C.bool(value))
|
||||
// TODO: compiler limitation: both operands must be the same LLVM type
|
||||
if high {
|
||||
nrf.P0.OUTSET = 1 << uint32(p.Pin)
|
||||
} else {
|
||||
nrf.P0.OUTCLR = 1 << uint32(p.Pin)
|
||||
}
|
||||
}
|
||||
@@ -48,20 +48,6 @@ 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();
|
||||
}
|
||||
|
||||
@@ -9,11 +9,3 @@ 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);
|
||||
|
||||
Reference in New Issue
Block a user