From daf92226d835d9eac6360087b7853499ea871af5 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 6 Oct 2018 13:53:58 +0200 Subject: [PATCH] nrf: add nrf52840-mdk board With the help of Chillance on GitHub. --- .travis.yml | 15 ++++++++------- src/machine/board_nrf52840-mdk.go | 25 +++++++++++++++++++++++++ src/machine/machine_nrf.go | 6 ++---- src/machine/machine_nrf51.go | 10 ++++++++++ src/machine/machine_nrf52.go | 10 ++++++++++ src/machine/machine_nrf52840.go | 31 +++++++++++++++++++++++++++++++ targets/nrf52840-mdk.json | 25 +++++++++++++++++++++++++ targets/nrf52840.ld | 10 ++++++++++ 8 files changed, 121 insertions(+), 11 deletions(-) create mode 100644 src/machine/board_nrf52840-mdk.go create mode 100644 src/machine/machine_nrf52840.go create mode 100644 targets/nrf52840-mdk.json create mode 100644 targets/nrf52840.ld diff --git a/.travis.yml b/.travis.yml index 21305376..c95a34fd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -19,10 +19,11 @@ script: - go install github.com/aykevl/tinygo - go test -v . - make gen-device - - tinygo build -o blinky1.nrf.elf -target=pca10040 examples/blinky1 - - tinygo build -o blinky2.nrf.elf -target=pca10040 examples/blinky2 - - tinygo build -o blinky2 examples/blinky2 - - tinygo build -o test.nrf.elf -target=pca10040 examples/test - - tinygo build -o blinky1.nrf51.elf -target=microbit examples/echo - - tinygo build -o blinky1.stm32.elf -target=bluepill examples/blinky1 - - tinygo build -o blinky1.avr.o -target=arduino examples/blinky1 # TODO: avr-as/avr-gcc doesn't work + - tinygo build -o blinky1.nrf.elf -target=pca10040 examples/blinky1 + - tinygo build -o blinky2.nrf.elf -target=pca10040 examples/blinky2 + - tinygo build -o blinky2 examples/blinky2 + - tinygo build -o test.nrf.elf -target=pca10040 examples/test + - tinygo build -o blinky1.nrf51.elf -target=microbit examples/echo + - tinygo build -o test.nrf.elf -target=nrf52840-mdk examples/blinky1 + - tinygo build -o blinky1.stm32.elf -target=bluepill examples/blinky1 + - tinygo build -o blinky1.avr.o -target=arduino examples/blinky1 # TODO: avr-as/avr-gcc doesn't work diff --git a/src/machine/board_nrf52840-mdk.go b/src/machine/board_nrf52840-mdk.go new file mode 100644 index 00000000..6e875fdd --- /dev/null +++ b/src/machine/board_nrf52840-mdk.go @@ -0,0 +1,25 @@ +// +build nrf52840_mdk + +package machine + +const HasLowFrequencyCrystal = true + +// LEDs on the nrf52840-mdk (nRF52840 dev board) +const ( + LED = LED_GREEN + LED_GREEN = 22 + LED_RED = 23 + LED_BLUE = 24 +) + +// UART pins +const ( + UART_TX_PIN = 20 + UART_RX_PIN = 19 +) + +// I2C pins (unused) +const ( + SDA_PIN = 0xff + SCL_PIN = 0xff +) diff --git a/src/machine/machine_nrf.go b/src/machine/machine_nrf.go index 627c70b0..8af896eb 100644 --- a/src/machine/machine_nrf.go +++ b/src/machine/machine_nrf.go @@ -70,8 +70,7 @@ func (uart UART) Configure(config UARTConfig) { uart.SetBaudRate(config.BaudRate) // Set TX and RX pins from board. - nrf.UART0.PSELTXD = UART_TX_PIN - nrf.UART0.PSELRXD = UART_RX_PIN + uart.setPins(UART_TX_PIN, UART_RX_PIN) nrf.UART0.ENABLE = nrf.UART_ENABLE_ENABLE_Enabled nrf.UART0.TASKS_STARTTX = 1 @@ -165,8 +164,7 @@ func (i2c I2C) Configure(config I2CConfig) { } i2c.Bus.ENABLE = nrf.TWI_ENABLE_ENABLE_Enabled - i2c.Bus.PSELSCL = nrf.RegValue(config.SCL) - i2c.Bus.PSELSDA = nrf.RegValue(config.SDA) + i2c.setPins(config.SCL, config.SDA) } // WriteTo writes a slice of data bytes to a peripheral with a specific address. diff --git a/src/machine/machine_nrf51.go b/src/machine/machine_nrf51.go index 29abf989..a24a3710 100644 --- a/src/machine/machine_nrf51.go +++ b/src/machine/machine_nrf51.go @@ -11,7 +11,17 @@ func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) { return nrf.GPIO, p.Pin } +func (uart UART) setPins(tx, rx uint32) { + nrf.UART0.PSELTXD = nrf.RegValue(tx) + nrf.UART0.PSELRXD = nrf.RegValue(rx) +} + //go:export UART0_IRQHandler func handleUART0() { UART0.handleInterrupt() } + +func (i2c I2C) setPins(scl, sda uint8) { + i2c.Bus.PSELSCL = nrf.RegValue(scl) + i2c.Bus.PSELSDA = nrf.RegValue(sda) +} diff --git a/src/machine/machine_nrf52.go b/src/machine/machine_nrf52.go index 1022df58..810bc678 100644 --- a/src/machine/machine_nrf52.go +++ b/src/machine/machine_nrf52.go @@ -12,11 +12,21 @@ func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) { return nrf.P0, p.Pin } +func (uart UART) setPins(tx, rx uint32) { + nrf.UART0.PSELTXD = nrf.RegValue(tx) + nrf.UART0.PSELRXD = nrf.RegValue(rx) +} + //go:export UARTE0_UART0_IRQHandler func handleUART0() { UART0.handleInterrupt() } +func (i2c I2C) setPins(scl, sda uint8) { + i2c.Bus.PSELSCL = nrf.RegValue(scl) + i2c.Bus.PSELSDA = nrf.RegValue(sda) +} + // InitADC initializes the registers needed for ADC. func InitADC() { return // no specific setup on nrf52 machine. diff --git a/src/machine/machine_nrf52840.go b/src/machine/machine_nrf52840.go new file mode 100644 index 00000000..2f0e5b48 --- /dev/null +++ b/src/machine/machine_nrf52840.go @@ -0,0 +1,31 @@ +// +build nrf52840 + +package machine + +import ( + "device/nrf" +) + +// Get peripheral and pin number for this GPIO pin. +func (p GPIO) getPortPin() (*nrf.GPIO_Type, uint8) { + if p.Pin >= 32 { + return nrf.P1, p.Pin - 32 + } else { + return nrf.P0, p.Pin + } +} + +func (uart UART) setPins(tx, rx uint32) { + nrf.UART0.PSEL.TXD = nrf.RegValue(tx) + nrf.UART0.PSEL.RXD = nrf.RegValue(rx) +} + +//go:export UARTE0_UART0_IRQHandler +func handleUART0() { + UART0.handleInterrupt() +} + +func (i2c I2C) setPins(scl, sda uint8) { + i2c.Bus.PSEL.SCL = nrf.RegValue(scl) + i2c.Bus.PSEL.SDA = nrf.RegValue(sda) +} diff --git a/targets/nrf52840-mdk.json b/targets/nrf52840-mdk.json new file mode 100644 index 00000000..15424c51 --- /dev/null +++ b/targets/nrf52840-mdk.json @@ -0,0 +1,25 @@ +{ + "llvm-target": "armv7em-none-eabi", + "build-tags": ["nrf52840_mdk", "nrf52840", "nrf", "arm", "js", "wasm"], + "linker": "arm-none-eabi-gcc", + "pre-link-args": [ + "-nostdlib", + "-nostartfiles", + "-mcpu=cortex-m4", + "-mthumb", + "-T", "targets/nrf52840.ld", + "-Wl,--gc-sections", + "-fno-exceptions", "-fno-unwind-tables", + "-ffunction-sections", "-fdata-sections", + "-Os", + "-DNRF52840_XXAA", + "-Ilib/CMSIS/CMSIS/Include", + "lib/nrfx/mdk/system_nrf52840.c", + "src/device/nrf/nrf52840.s" + ], + "objcopy": "arm-none-eabi-objcopy", + "flash": "openocd -f interface/cmsis-dap.cfg -f target/nrf51.cfg -c 'program {hex} reset exit'", + "ocd-daemon": ["openocd", "-f", "interface/cmsis-dap.cfg", "-f", "target/nrf51.cfg"], + "gdb": "arm-none-eabi-gdb", + "gdb-initial-cmds": ["target remote :3333", "monitor halt", "load", "monitor reset", "c"] +} diff --git a/targets/nrf52840.ld b/targets/nrf52840.ld new file mode 100644 index 00000000..3f766a91 --- /dev/null +++ b/targets/nrf52840.ld @@ -0,0 +1,10 @@ + +MEMORY +{ + FLASH_TEXT (rw) : ORIGIN = 0x00000000, LENGTH = 1M + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 256K +} + +_stack_size = 4K; + +INCLUDE "targets/arm.ld"