mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 11:37:46 +00:00
nrf: add micro:bit board
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// +build nrf,microbit
|
||||
|
||||
package machine
|
||||
|
||||
// The micro:bit does not have a 32kHz crystal on board.
|
||||
const HasLowFrequencyCrystal = false
|
||||
|
||||
// Buttons on the micro:bit (A and B)
|
||||
const (
|
||||
BUTTON = BUTTON1
|
||||
BUTTON1 = 5
|
||||
BUTTON2 = 11
|
||||
)
|
||||
|
||||
// UART pins
|
||||
const (
|
||||
UART_TX_PIN = 24
|
||||
UART_RX_PIN = 25
|
||||
)
|
||||
|
||||
// ADC pins
|
||||
const (
|
||||
ADC0 = 0
|
||||
ADC1 = 1
|
||||
ADC2 = 2
|
||||
)
|
||||
|
||||
// I2C pins
|
||||
const (
|
||||
SDA_PIN = 20
|
||||
SCL_PIN = 19
|
||||
)
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
package machine
|
||||
|
||||
// The PCA10040 has a low-frequency (32kHz) crystal oscillator on board.
|
||||
const HasLowFrequencyCrystal = true
|
||||
|
||||
// LEDs on the PCA10040 (nRF52832 dev board)
|
||||
const (
|
||||
LED = LED1
|
||||
|
||||
@@ -19,22 +19,25 @@ const (
|
||||
// Configure this pin with the given configuration.
|
||||
func (p GPIO) Configure(config GPIOConfig) {
|
||||
cfg := config.Mode | nrf.GPIO_PIN_CNF_DRIVE_S0S1 | nrf.GPIO_PIN_CNF_SENSE_Disabled
|
||||
nrf.P0.PIN_CNF[p.Pin] = nrf.RegValue(cfg)
|
||||
port, pin := p.getPortPin()
|
||||
port.PIN_CNF[pin] = nrf.RegValue(cfg)
|
||||
}
|
||||
|
||||
// Set the pin to high or low.
|
||||
// Warning: only use this on an output pin!
|
||||
func (p GPIO) Set(high bool) {
|
||||
port, pin := p.getPortPin()
|
||||
if high {
|
||||
nrf.P0.OUTSET = 1 << p.Pin
|
||||
port.OUTSET = 1 << pin
|
||||
} else {
|
||||
nrf.P0.OUTCLR = 1 << p.Pin
|
||||
port.OUTCLR = 1 << pin
|
||||
}
|
||||
}
|
||||
|
||||
// Get returns the current value of a GPIO pin.
|
||||
func (p GPIO) Get() bool {
|
||||
return (nrf.P0.IN>>p.Pin)&1 != 0
|
||||
port, pin := p.getPortPin()
|
||||
return (port.IN>>pin)&1 != 0
|
||||
}
|
||||
|
||||
// UART
|
||||
@@ -62,7 +65,7 @@ func (uart UART) Configure(config UARTConfig) {
|
||||
nrf.UART0.INTENSET = nrf.UART_INTENSET_RXDRDY_Msk
|
||||
|
||||
// Enable RX IRQ.
|
||||
arm.EnableIRQ(nrf.IRQ_UARTE0_UART0)
|
||||
arm.EnableIRQ(nrf.IRQ_UART0)
|
||||
}
|
||||
|
||||
// SetBaudRate sets the communication speed for the UART.
|
||||
@@ -89,8 +92,7 @@ func (uart UART) WriteByte(c byte) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:export UARTE0_UART0_IRQHandler
|
||||
func handleUART0() {
|
||||
func (uart UART) handleInterrupt() {
|
||||
if nrf.UART0.EVENTS_RXDRDY != 0 {
|
||||
bufferPut(byte(nrf.UART0.RXD))
|
||||
nrf.UART0.EVENTS_RXDRDY = 0x0
|
||||
@@ -128,13 +130,15 @@ func (i2c I2C) Configure(config I2CConfig) {
|
||||
}
|
||||
|
||||
// do config
|
||||
nrf.P0.PIN_CNF[config.SCL] = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
|
||||
sclPort, sclPin := GPIO{config.SCL}.getPortPin()
|
||||
sclPort.PIN_CNF[sclPin] = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_SENSE_Disabled << nrf.GPIO_PIN_CNF_SENSE_Pos)
|
||||
|
||||
nrf.P0.PIN_CNF[config.SDA] = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
|
||||
sdaPort, sdaPin := GPIO{config.SDA}.getPortPin()
|
||||
sdaPort.PIN_CNF[sdaPin] = (nrf.GPIO_PIN_CNF_DIR_Input << nrf.GPIO_PIN_CNF_DIR_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_INPUT_Connect << nrf.GPIO_PIN_CNF_INPUT_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_PULL_Pullup << nrf.GPIO_PIN_CNF_PULL_Pos) |
|
||||
(nrf.GPIO_PIN_CNF_DRIVE_S0D1 << nrf.GPIO_PIN_CNF_DRIVE_Pos) |
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
// +build nrf51
|
||||
|
||||
package machine
|
||||
|
||||
import (
|
||||
"device/nrf"
|
||||
)
|
||||
|
||||
// Get peripheral and pin number for this GPIO pin.
|
||||
func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) {
|
||||
return nrf.GPIO, p.Pin
|
||||
}
|
||||
|
||||
//go:export UART0_IRQHandler
|
||||
func handleUART0() {
|
||||
UART0.handleInterrupt()
|
||||
}
|
||||
@@ -7,6 +7,16 @@ import (
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// Get peripheral and pin number for this GPIO pin.
|
||||
func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) {
|
||||
return nrf.P0, p.Pin
|
||||
}
|
||||
|
||||
//go:export UARTE0_UART0_IRQHandler
|
||||
func handleUART0() {
|
||||
UART0.handleInterrupt()
|
||||
}
|
||||
|
||||
// InitADC initializes the registers needed for ADC.
|
||||
func InitADC() {
|
||||
return // no specific setup on nrf52 machine.
|
||||
|
||||
@@ -28,7 +28,9 @@ func init() {
|
||||
}
|
||||
|
||||
func initLFCLK() {
|
||||
nrf.CLOCK.LFCLKSRC = nrf.CLOCK_LFCLKSTAT_SRC_Xtal
|
||||
if machine.HasLowFrequencyCrystal {
|
||||
nrf.CLOCK.LFCLKSRC = nrf.CLOCK_LFCLKSTAT_SRC_Xtal
|
||||
}
|
||||
nrf.CLOCK.TASKS_LFCLKSTART = 1
|
||||
for nrf.CLOCK.EVENTS_LFCLKSTARTED == 0 {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user