From 2b7fa93fcb06ad4125530d0b6259f70043af1013 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Mon, 20 Feb 2023 20:47:42 +0100 Subject: [PATCH] machine: add flash start/end address that can be used for data --- src/examples/flash/main.go | 8 ++++++++ src/machine/flash.go | 23 +++++++++++++++++++++++ src/machine/machine_nrf51.go | 2 ++ src/machine/machine_nrf528xx.go | 2 ++ targets/arm.ld | 4 ++++ 5 files changed, 39 insertions(+) create mode 100644 src/examples/flash/main.go create mode 100644 src/machine/flash.go diff --git a/src/examples/flash/main.go b/src/examples/flash/main.go new file mode 100644 index 000000000..2ffd26157 --- /dev/null +++ b/src/examples/flash/main.go @@ -0,0 +1,8 @@ +package main + +import "machine" + +func main() { + println("flash data start:", machine.FlashDataStart()) + println("flash data end: ", machine.FlashDataEnd()) +} diff --git a/src/machine/flash.go b/src/machine/flash.go new file mode 100644 index 000000000..0907527ed --- /dev/null +++ b/src/machine/flash.go @@ -0,0 +1,23 @@ +//go:build nrf + +package machine + +import "unsafe" + +//go:extern __flash_data_start +var flashDataStart [0]byte + +//go:extern __flash_data_end +var flashDataEnd [0]byte + +// Return the start of the writable flash area, aligned on a page boundary. This +// is usually just after the program and static data. +func FlashDataStart() uintptr { + return (uintptr(unsafe.Pointer(&flashDataStart)) + flashPageSize - 1) &^ (flashPageSize - 1) +} + +// Return the end of the writable flash area. Usually this is the address one +// past the end of the on-chip flash. +func FlashDataEnd() uintptr { + return uintptr(unsafe.Pointer(&flashDataEnd)) +} diff --git a/src/machine/machine_nrf51.go b/src/machine/machine_nrf51.go index 0fd04f776..e0be627ac 100644 --- a/src/machine/machine_nrf51.go +++ b/src/machine/machine_nrf51.go @@ -6,6 +6,8 @@ import ( "device/nrf" ) +const flashPageSize = 1024 + // Get peripheral and pin number for this GPIO pin. func (p Pin) getPortPin() (*nrf.GPIO_Type, uint32) { return nrf.GPIO, uint32(p) diff --git a/src/machine/machine_nrf528xx.go b/src/machine/machine_nrf528xx.go index ac43c8f33..45c70ac0f 100644 --- a/src/machine/machine_nrf528xx.go +++ b/src/machine/machine_nrf528xx.go @@ -12,6 +12,8 @@ func CPUFrequency() uint32 { return 64000000 } +const flashPageSize = 4096 + // InitADC initializes the registers needed for ADC. func InitADC() { return // no specific setup on nrf52 machine. diff --git a/targets/arm.ld b/targets/arm.ld index e4155b909..ad6c541a1 100644 --- a/targets/arm.ld +++ b/targets/arm.ld @@ -69,3 +69,7 @@ _heap_start = _ebss; _heap_end = ORIGIN(RAM) + LENGTH(RAM); _globals_start = _sdata; _globals_end = _ebss; + +/* For the flash API */ +__flash_data_start = LOADADDR(.data) + SIZEOF(.data); +__flash_data_end = ORIGIN(FLASH_TEXT) + LENGTH(FLASH_TEXT);