From b19f3330c8f2d55825757ffea8d75361f86f1eba Mon Sep 17 00:00:00 2001 From: Michal Gorecki Date: Fri, 21 Jun 2024 14:57:39 +0200 Subject: [PATCH] hw/mcu/nordic: Add common flash HAL --- hw/bsp/ada_feather_nrf52/src/hal_bsp.c | 2 +- hw/bsp/arduino_primo_nrf52/src/hal_bsp.c | 2 +- hw/bsp/bbc_microbit/src/hal_bsp.c | 2 +- hw/bsp/ble400/src/hal_bsp.c | 2 +- hw/bsp/bmd200/src/hal_bsp.c | 2 +- hw/bsp/bmd300eval/src/hal_bsp.c | 2 +- hw/bsp/calliope_mini/src/hal_bsp.c | 2 +- hw/bsp/dwm1001-dev/src/hal_bsp.c | 2 +- hw/bsp/fanstel-ev-bt840/src/hal_bsp.c | 2 +- hw/bsp/nina-b1/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10028-16k/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10028/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10040/src/hal_bsp.c | 4 +- hw/bsp/nordic_pca10056/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10059/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10090/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10095/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10095_net/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10121/net/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca10121/src/hal_bsp.c | 2 +- hw/bsp/nordic_pca20020/src/hal_bsp.c | 2 +- hw/bsp/nordic_thingy53/src/hal_bsp.c | 2 +- hw/bsp/nrf51-arduino_101/src/hal_bsp.c | 2 +- hw/bsp/nrf51-blenano/src/hal_bsp.c | 2 +- hw/bsp/pinetime/src/hal_bsp.c | 2 +- hw/bsp/puckjs/src/hal_bsp.c | 2 +- hw/bsp/rb-blend2/src/hal_bsp.c | 2 +- hw/bsp/rb-nano2/src/hal_bsp.c | 2 +- hw/bsp/reel_board/src/hal_bsp.c | 2 +- hw/bsp/ruuvitag_rev_b/src/hal_bsp.c | 2 +- hw/bsp/telee02/src/hal_bsp.c | 2 +- hw/bsp/ublox_bmd_345/src/hal_bsp.c | 2 +- hw/bsp/vbluno51/src/hal_bsp.c | 2 +- hw/bsp/vbluno52/src/hal_bsp.c | 2 +- .../ipc_nrf5340_flash/src/ipc_nrf5340_flash.c | 2 +- .../nordic/nrf51xxx/include/mcu/nrf51_hal.h | 4 +- .../nrf51xxx/include/nrfx_config_nrf51.h | 1 + hw/mcu/nordic/nrf51xxx/src/hal_flash.c | 193 -------------- .../nordic/nrf52xxx/include/mcu/nrf52_hal.h | 3 +- .../nrf52xxx/include/nrfx_config_nrf52810.h | 2 +- .../nrf52xxx/include/nrfx_config_nrf52811.h | 2 +- .../nrf52xxx/include/nrfx_config_nrf52832.h | 2 +- .../nrf52xxx/include/nrfx_config_nrf52840.h | 2 +- hw/mcu/nordic/nrf52xxx/src/hal_flash.c | 221 ---------------- .../nordic/nrf5340/include/mcu/nrf5340_hal.h | 2 - .../include/nrfx_config_nrf5340_application.h | 2 +- hw/mcu/nordic/nrf5340/src/hal_flash.c | 221 ---------------- .../nrf5340_net/include/mcu/nrf5340_net_hal.h | 1 - .../include/nrfx_config_nrf5340_network.h | 2 +- hw/mcu/nordic/nrf5340_net/src/hal_flash.c | 205 --------------- hw/mcu/nordic/nrf5340_net/src/hal_vflash.c | 2 +- hw/mcu/nordic/nrf91xx/include/mcu/nrf91_hal.h | 3 +- .../nrf91xx/include/nrfx_config_nrf91.h | 2 +- hw/mcu/nordic/nrf91xx/src/hal_flash.c | 237 ------------------ hw/mcu/nordic/nrf_common/include/nrf_hal.h | 3 + hw/mcu/nordic/nrf_common/src/hal_flash.c | 114 +++++++++ 56 files changed, 165 insertions(+), 1131 deletions(-) delete mode 100644 hw/mcu/nordic/nrf51xxx/src/hal_flash.c delete mode 100644 hw/mcu/nordic/nrf52xxx/src/hal_flash.c delete mode 100644 hw/mcu/nordic/nrf5340/src/hal_flash.c delete mode 100644 hw/mcu/nordic/nrf5340_net/src/hal_flash.c delete mode 100644 hw/mcu/nordic/nrf91xx/src/hal_flash.c create mode 100644 hw/mcu/nordic/nrf_common/src/hal_flash.c diff --git a/hw/bsp/ada_feather_nrf52/src/hal_bsp.c b/hw/bsp/ada_feather_nrf52/src/hal_bsp.c index 41e6d1f661..c255ec14d9 100644 --- a/hw/bsp/ada_feather_nrf52/src/hal_bsp.c +++ b/hw/bsp/ada_feather_nrf52/src/hal_bsp.c @@ -48,7 +48,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } return NULL; diff --git a/hw/bsp/arduino_primo_nrf52/src/hal_bsp.c b/hw/bsp/arduino_primo_nrf52/src/hal_bsp.c index 459ae7f557..1282f6dde2 100644 --- a/hw/bsp/arduino_primo_nrf52/src/hal_bsp.c +++ b/hw/bsp/arduino_primo_nrf52/src/hal_bsp.c @@ -50,7 +50,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/bbc_microbit/src/hal_bsp.c b/hw/bsp/bbc_microbit/src/hal_bsp.c index 52802ae415..c88b0b31fb 100644 --- a/hw/bsp/bbc_microbit/src/hal_bsp.c +++ b/hw/bsp/bbc_microbit/src/hal_bsp.c @@ -114,7 +114,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/ble400/src/hal_bsp.c b/hw/bsp/ble400/src/hal_bsp.c index 12405cde7a..0038f60fce 100644 --- a/hw/bsp/ble400/src/hal_bsp.c +++ b/hw/bsp/ble400/src/hal_bsp.c @@ -115,7 +115,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/bmd200/src/hal_bsp.c b/hw/bsp/bmd200/src/hal_bsp.c index 12405cde7a..0038f60fce 100644 --- a/hw/bsp/bmd200/src/hal_bsp.c +++ b/hw/bsp/bmd200/src/hal_bsp.c @@ -115,7 +115,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/bmd300eval/src/hal_bsp.c b/hw/bsp/bmd300eval/src/hal_bsp.c index 022729629c..c36aa19012 100644 --- a/hw/bsp/bmd300eval/src/hal_bsp.c +++ b/hw/bsp/bmd300eval/src/hal_bsp.c @@ -51,7 +51,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/calliope_mini/src/hal_bsp.c b/hw/bsp/calliope_mini/src/hal_bsp.c index a39b6136b0..16d272bf37 100644 --- a/hw/bsp/calliope_mini/src/hal_bsp.c +++ b/hw/bsp/calliope_mini/src/hal_bsp.c @@ -116,7 +116,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/dwm1001-dev/src/hal_bsp.c b/hw/bsp/dwm1001-dev/src/hal_bsp.c index 3bb0c712cd..09672b9192 100644 --- a/hw/bsp/dwm1001-dev/src/hal_bsp.c +++ b/hw/bsp/dwm1001-dev/src/hal_bsp.c @@ -52,7 +52,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/fanstel-ev-bt840/src/hal_bsp.c b/hw/bsp/fanstel-ev-bt840/src/hal_bsp.c index 419466728c..fd1405cbee 100644 --- a/hw/bsp/fanstel-ev-bt840/src/hal_bsp.c +++ b/hw/bsp/fanstel-ev-bt840/src/hal_bsp.c @@ -42,7 +42,7 @@ const struct hal_flash * hal_bsp_flash_dev(uint8_t id) { if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } return NULL; diff --git a/hw/bsp/nina-b1/src/hal_bsp.c b/hw/bsp/nina-b1/src/hal_bsp.c index 3bb0c712cd..09672b9192 100644 --- a/hw/bsp/nina-b1/src/hal_bsp.c +++ b/hw/bsp/nina-b1/src/hal_bsp.c @@ -52,7 +52,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/nordic_pca10028-16k/src/hal_bsp.c b/hw/bsp/nordic_pca10028-16k/src/hal_bsp.c index 0042519e6c..bcefce440e 100644 --- a/hw/bsp/nordic_pca10028-16k/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10028-16k/src/hal_bsp.c @@ -115,7 +115,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/nordic_pca10028/src/hal_bsp.c b/hw/bsp/nordic_pca10028/src/hal_bsp.c index 943df05336..f21d8d4eea 100644 --- a/hw/bsp/nordic_pca10028/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10028/src/hal_bsp.c @@ -117,7 +117,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/nordic_pca10040/src/hal_bsp.c b/hw/bsp/nordic_pca10040/src/hal_bsp.c index b33572c17e..f87a1b2d1a 100644 --- a/hw/bsp/nordic_pca10040/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10040/src/hal_bsp.c @@ -50,7 +50,7 @@ static sec_data_secret struct eflash_nrf5x_dev enc_flash_dev0 = { .efd_hal = { .hf_itf = &enc_flash_funcs, }, - .efd_hwdev = &nrf52k_flash_dev + .efd_hwdev = &nrf_flash_dev } }; @@ -63,7 +63,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(ENC_FLASH_DEV) if (id == 1) { diff --git a/hw/bsp/nordic_pca10056/src/hal_bsp.c b/hw/bsp/nordic_pca10056/src/hal_bsp.c index f62e287d74..c594858c2f 100644 --- a/hw/bsp/nordic_pca10056/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10056/src/hal_bsp.c @@ -47,7 +47,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(QSPI_ENABLE) if (id == 1) { diff --git a/hw/bsp/nordic_pca10059/src/hal_bsp.c b/hw/bsp/nordic_pca10059/src/hal_bsp.c index f62e287d74..c594858c2f 100644 --- a/hw/bsp/nordic_pca10059/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10059/src/hal_bsp.c @@ -47,7 +47,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(QSPI_ENABLE) if (id == 1) { diff --git a/hw/bsp/nordic_pca10090/src/hal_bsp.c b/hw/bsp/nordic_pca10090/src/hal_bsp.c index 1d88dca424..40296ad93f 100644 --- a/hw/bsp/nordic_pca10090/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10090/src/hal_bsp.c @@ -48,7 +48,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf91k_flash_dev; + return &nrf_flash_dev; } return NULL; diff --git a/hw/bsp/nordic_pca10095/src/hal_bsp.c b/hw/bsp/nordic_pca10095/src/hal_bsp.c index c491b9684e..f0ceef03b5 100644 --- a/hw/bsp/nordic_pca10095/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10095/src/hal_bsp.c @@ -53,7 +53,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf5340_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(QSPI_ENABLE) if (id == 1) { diff --git a/hw/bsp/nordic_pca10095_net/src/hal_bsp.c b/hw/bsp/nordic_pca10095_net/src/hal_bsp.c index d26bce94ad..214103db97 100644 --- a/hw/bsp/nordic_pca10095_net/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10095_net/src/hal_bsp.c @@ -54,7 +54,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf5340_flash_dev; + return &nrf_flash_dev; } #if MCUBOOT_MYNEWT if (id == 1) { diff --git a/hw/bsp/nordic_pca10121/net/src/hal_bsp.c b/hw/bsp/nordic_pca10121/net/src/hal_bsp.c index d26bce94ad..214103db97 100644 --- a/hw/bsp/nordic_pca10121/net/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10121/net/src/hal_bsp.c @@ -54,7 +54,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf5340_flash_dev; + return &nrf_flash_dev; } #if MCUBOOT_MYNEWT if (id == 1) { diff --git a/hw/bsp/nordic_pca10121/src/hal_bsp.c b/hw/bsp/nordic_pca10121/src/hal_bsp.c index 68a0054172..e4487e59d2 100644 --- a/hw/bsp/nordic_pca10121/src/hal_bsp.c +++ b/hw/bsp/nordic_pca10121/src/hal_bsp.c @@ -54,7 +54,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf5340_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(QSPI_ENABLE) if (id == 1) { diff --git a/hw/bsp/nordic_pca20020/src/hal_bsp.c b/hw/bsp/nordic_pca20020/src/hal_bsp.c index 2ab8a3e4c1..ba24689519 100644 --- a/hw/bsp/nordic_pca20020/src/hal_bsp.c +++ b/hw/bsp/nordic_pca20020/src/hal_bsp.c @@ -61,7 +61,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } return NULL; diff --git a/hw/bsp/nordic_thingy53/src/hal_bsp.c b/hw/bsp/nordic_thingy53/src/hal_bsp.c index c491b9684e..f0ceef03b5 100644 --- a/hw/bsp/nordic_thingy53/src/hal_bsp.c +++ b/hw/bsp/nordic_thingy53/src/hal_bsp.c @@ -53,7 +53,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf5340_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(QSPI_ENABLE) if (id == 1) { diff --git a/hw/bsp/nrf51-arduino_101/src/hal_bsp.c b/hw/bsp/nrf51-arduino_101/src/hal_bsp.c index 26076d6504..5531cf0241 100644 --- a/hw/bsp/nrf51-arduino_101/src/hal_bsp.c +++ b/hw/bsp/nrf51-arduino_101/src/hal_bsp.c @@ -118,7 +118,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/nrf51-blenano/src/hal_bsp.c b/hw/bsp/nrf51-blenano/src/hal_bsp.c index 5dc79ab387..f11c1a35c8 100644 --- a/hw/bsp/nrf51-blenano/src/hal_bsp.c +++ b/hw/bsp/nrf51-blenano/src/hal_bsp.c @@ -117,7 +117,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/pinetime/src/hal_bsp.c b/hw/bsp/pinetime/src/hal_bsp.c index 723916cb78..ff6d57fe2f 100644 --- a/hw/bsp/pinetime/src/hal_bsp.c +++ b/hw/bsp/pinetime/src/hal_bsp.c @@ -70,7 +70,7 @@ struct bus_spi_node_cfg flash_spi_cfg = { static const struct hal_flash *flash_devs[] = { /* MCU internal flash. */ - [0] = &nrf52k_flash_dev, + [0] = &nrf_flash_dev, #if MYNEWT_VAL(SPIFLASH) /* External SPI Flash. */ [1] = &spiflash_dev.hal, diff --git a/hw/bsp/puckjs/src/hal_bsp.c b/hw/bsp/puckjs/src/hal_bsp.c index 3bb0c712cd..09672b9192 100644 --- a/hw/bsp/puckjs/src/hal_bsp.c +++ b/hw/bsp/puckjs/src/hal_bsp.c @@ -52,7 +52,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/rb-blend2/src/hal_bsp.c b/hw/bsp/rb-blend2/src/hal_bsp.c index a2a8ea8e03..5a02bc625e 100644 --- a/hw/bsp/rb-blend2/src/hal_bsp.c +++ b/hw/bsp/rb-blend2/src/hal_bsp.c @@ -51,7 +51,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/rb-nano2/src/hal_bsp.c b/hw/bsp/rb-nano2/src/hal_bsp.c index 00c026e281..e459198fdd 100644 --- a/hw/bsp/rb-nano2/src/hal_bsp.c +++ b/hw/bsp/rb-nano2/src/hal_bsp.c @@ -50,7 +50,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/reel_board/src/hal_bsp.c b/hw/bsp/reel_board/src/hal_bsp.c index 51c56b3200..5515d2f2dc 100644 --- a/hw/bsp/reel_board/src/hal_bsp.c +++ b/hw/bsp/reel_board/src/hal_bsp.c @@ -43,7 +43,7 @@ const struct hal_flash * hal_bsp_flash_dev(uint8_t id) { if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } return NULL; diff --git a/hw/bsp/ruuvitag_rev_b/src/hal_bsp.c b/hw/bsp/ruuvitag_rev_b/src/hal_bsp.c index 837f0559ef..fe76f949e3 100644 --- a/hw/bsp/ruuvitag_rev_b/src/hal_bsp.c +++ b/hw/bsp/ruuvitag_rev_b/src/hal_bsp.c @@ -75,7 +75,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } return NULL; diff --git a/hw/bsp/telee02/src/hal_bsp.c b/hw/bsp/telee02/src/hal_bsp.c index 4c32cda3ef..c07167f6de 100644 --- a/hw/bsp/telee02/src/hal_bsp.c +++ b/hw/bsp/telee02/src/hal_bsp.c @@ -53,7 +53,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/ublox_bmd_345/src/hal_bsp.c b/hw/bsp/ublox_bmd_345/src/hal_bsp.c index 26bcf49811..b364a9af3a 100644 --- a/hw/bsp/ublox_bmd_345/src/hal_bsp.c +++ b/hw/bsp/ublox_bmd_345/src/hal_bsp.c @@ -48,7 +48,7 @@ hal_bsp_flash_dev(uint8_t id) * Internal flash mapped to id 0. */ if (id == 0) { - return &nrf52k_flash_dev; + return &nrf_flash_dev; } #if MYNEWT_VAL(QSPI_ENABLE) if (id == 1) { diff --git a/hw/bsp/vbluno51/src/hal_bsp.c b/hw/bsp/vbluno51/src/hal_bsp.c index 7a2eee7bc9..f2fce8a97e 100644 --- a/hw/bsp/vbluno51/src/hal_bsp.c +++ b/hw/bsp/vbluno51/src/hal_bsp.c @@ -115,7 +115,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf51_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/bsp/vbluno52/src/hal_bsp.c b/hw/bsp/vbluno52/src/hal_bsp.c index 3bb0c712cd..09672b9192 100644 --- a/hw/bsp/vbluno52/src/hal_bsp.c +++ b/hw/bsp/vbluno52/src/hal_bsp.c @@ -52,7 +52,7 @@ hal_bsp_flash_dev(uint8_t id) if (id != 0) { return NULL; } - return &nrf52k_flash_dev; + return &nrf_flash_dev; } const struct hal_bsp_mem_dump * diff --git a/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c b/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c index 35db78d381..8bf594e861 100644 --- a/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c +++ b/hw/drivers/flash/ipc_nrf5340_flash/src/ipc_nrf5340_flash.c @@ -439,7 +439,7 @@ nrf5340_ipc_flash_std_resp(struct ipc_msg *req, int rc, void *data, uint32_t dat static int nrf5340_ipc_flash_info_resp(struct ipc_msg *req) { - return nrf5340_ipc_flash_std_resp(req, 0, (void *)&nrf5340_flash_dev, sizeof(nrf5340_flash_dev)); + return nrf5340_ipc_flash_std_resp(req, 0, (void *)&nrf_flash_dev, sizeof(nrf_flash_dev)); } static int diff --git a/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_hal.h b/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_hal.h index 50b4abeb74..eb6225ea55 100644 --- a/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_hal.h +++ b/hw/mcu/nordic/nrf51xxx/include/mcu/nrf51_hal.h @@ -32,6 +32,7 @@ struct nrf51_uart_cfg { int8_t suc_pin_rts; int8_t suc_pin_cts; }; + const struct nrf51_uart_cfg *bsp_uart_config(void); struct nrf51_hal_i2c_cfg { @@ -48,9 +49,6 @@ struct nrf51_hal_spi_cfg { uint8_t ss_pin; }; -struct hal_flash; -extern const struct hal_flash nrf51_flash_dev; - #ifdef __cplusplus } #endif diff --git a/hw/mcu/nordic/nrf51xxx/include/nrfx_config_nrf51.h b/hw/mcu/nordic/nrf51xxx/include/nrfx_config_nrf51.h index 88f0f8e063..2c136cb6ad 100644 --- a/hw/mcu/nordic/nrf51xxx/include/nrfx_config_nrf51.h +++ b/hw/mcu/nordic/nrf51xxx/include/nrfx_config_nrf51.h @@ -55,6 +55,7 @@ #define NRFX_UART_ENABLED 0 #define NRFX_UART0_ENABLED 0 #define NRFX_WDT_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #if MYNEWT_VAL(ADC_0) #define NRFX_ADC_ENABLED 1 diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_flash.c b/hw/mcu/nordic/nrf51xxx/src/hal_flash.c deleted file mode 100644 index 6ac0ceef9e..0000000000 --- a/hw/mcu/nordic/nrf51xxx/src/hal_flash.c +++ /dev/null @@ -1,193 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include -#include - -#include "nrf.h" -#include "mcu/nrf51_hal.h" - -#include - -#define NRF51_FLASH_SECTOR_SZ 1024 - -static int nrf51_flash_read(const struct hal_flash *dev, uint32_t address, - void *dst, uint32_t num_bytes); -static int nrf51_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes); -static int nrf51_flash_erase_sector(const struct hal_flash *dev, - uint32_t sector_address); -static int nrf51_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz); -static int nrf51_flash_init(const struct hal_flash *dev); - -static const struct hal_flash_funcs nrf51_flash_funcs = { - .hff_read = nrf51_flash_read, - .hff_write = nrf51_flash_write, - .hff_erase_sector = nrf51_flash_erase_sector, - .hff_sector_info = nrf51_flash_sector_info, - .hff_init = nrf51_flash_init -}; - -const struct hal_flash nrf51_flash_dev = { - .hf_itf = &nrf51_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 256 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 256, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; - -#define NRF51_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready) - -static int -nrf51_flash_wait_ready(void) -{ - int i; - - for (i = 0; i < 100000; i++) { - if (NRF_NVMC->READY == NVMC_READY_READY_Ready) { - return 0; - } - } - return -1; -} - -static int -nrf51_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, - uint32_t num_bytes) -{ - memcpy(dst, (void *)address, num_bytes); - return 0; -} - -/* - * Flash write is done by writing 4 bytes at a time at a word boundary. - */ -static int -nrf51_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes) -{ - int sr; - int rc = -1; - uint32_t val; - int cnt; - uint32_t tmp; - - if (nrf51_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; /* Enable erase OP */ - tmp = address & 0x3; - if (tmp) { - if (nrf51_flash_wait_ready()) { - goto out; - } - /* - * Starts at a non-word boundary. Read 4 bytes which were there - * before, update with new data, and write back. - */ - val = *(uint32_t *)(address & ~0x3); - cnt = 4 - tmp; - if (cnt > num_bytes) { - cnt = num_bytes; - } - memcpy((uint8_t *)&val + tmp, src, cnt); - *(uint32_t *)(address & ~0x3) = val; - address += cnt; - num_bytes -= cnt; - src += cnt; - } - - while (num_bytes >= sizeof(uint32_t)) { - /* - * Write data 4 bytes at a time. - */ - if (nrf51_flash_wait_ready()) { - goto out; - } - memcpy(&val, src, sizeof(uint32_t)); - *(uint32_t *)address = val; - address += sizeof(uint32_t); - src += sizeof(uint32_t); - num_bytes -= sizeof(uint32_t); - } - if (num_bytes) { - /* - * Deal with the trailing bytes. - */ - val = *(uint32_t *)address; - memcpy(&val, src, num_bytes); - if (nrf51_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = val; - } - rc = 0; - if (nrf51_flash_wait_ready()) { - rc = -1; - } -out: - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf51_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) -{ - int sr; - int rc = -1; - - if (nrf51_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een; /* Enable erase OP */ - if (nrf51_flash_wait_ready()) { - goto out; - } - - NRF_NVMC->ERASEPAGE = sector_address; - if (nrf51_flash_wait_ready()) { - goto out; - } - rc = 0; -out: - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; /* Disable erase OP */ - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf51_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz) -{ - assert(idx < nrf51_flash_dev.hf_sector_cnt); - *address = idx * NRF51_FLASH_SECTOR_SZ; - *sz = NRF51_FLASH_SECTOR_SZ; - return 0; -} - -static int -nrf51_flash_init(const struct hal_flash *dev) -{ - return 0; -} diff --git a/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_hal.h b/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_hal.h index e820214cf2..f66501f632 100644 --- a/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_hal.h +++ b/hw/mcu/nordic/nrf52xxx/include/mcu/nrf52_hal.h @@ -39,8 +39,7 @@ struct nrf52_hal_i2c_cfg { int sda_pin; uint32_t i2c_frequency; }; -struct hal_flash; -extern const struct hal_flash nrf52k_flash_dev; + extern const struct hal_flash nrf52k_qspi_dev; /* SPI configuration (used for both master and slave) */ diff --git a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52810.h b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52810.h index 30aada12da..db3ba11296 100644 --- a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52810.h +++ b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52810.h @@ -343,7 +343,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52811.h b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52811.h index 61eb59c2b1..d00e3aaf91 100644 --- a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52811.h +++ b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52811.h @@ -343,7 +343,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52832.h b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52832.h index 3564108da3..f8bed7efd5 100644 --- a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52832.h +++ b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52832.h @@ -526,7 +526,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52840.h b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52840.h index fb4895268b..9d8f6fb816 100644 --- a/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52840.h +++ b/hw/mcu/nordic/nrf52xxx/include/nrfx_config_nrf52840.h @@ -482,7 +482,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_flash.c b/hw/mcu/nordic/nrf52xxx/src/hal_flash.c deleted file mode 100644 index c1d520ad47..0000000000 --- a/hw/mcu/nordic/nrf52xxx/src/hal_flash.c +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include -#include -#include "nrf.h" -#include "mcu/nrf52_hal.h" -#include - -#define NRF52K_FLASH_SECTOR_SZ 4096 - -static int nrf52k_flash_read(const struct hal_flash *dev, uint32_t address, - void *dst, uint32_t num_bytes); -static int nrf52k_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes); -static int nrf52k_flash_erase_sector(const struct hal_flash *dev, - uint32_t sector_address); -static int nrf52k_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz); -static int nrf52k_flash_init(const struct hal_flash *dev); - -static const struct hal_flash_funcs nrf52k_flash_funcs = { - .hff_read = nrf52k_flash_read, - .hff_write = nrf52k_flash_write, - .hff_erase_sector = nrf52k_flash_erase_sector, - .hff_sector_info = nrf52k_flash_sector_info, - .hff_init = nrf52k_flash_init -}; - -#ifdef NRF52840_XXAA -const struct hal_flash nrf52k_flash_dev = { - .hf_itf = &nrf52k_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 1024 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 256, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; -#elif defined(NRF52810_XXAA) -const struct hal_flash nrf52k_flash_dev = { - .hf_itf = &nrf52k_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 192 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 48, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; -#elif defined(NRF52811_XXAA) -const struct hal_flash nrf52k_flash_dev = { - .hf_itf = &nrf52k_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 192 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 48, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; -#elif defined(NRF52832_XXAA) -const struct hal_flash nrf52k_flash_dev = { - .hf_itf = &nrf52k_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 512 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 128, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; -#else -#error "Must define hal_flash struct for NRF52 type" -#endif - -#define NRF52K_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready) - -static int -nrf52k_flash_wait_ready(void) -{ - int i; - - for (i = 0; i < 100000; i++) { - if (NRF_NVMC->READY == NVMC_READY_READY_Ready) { - return 0; - } - } - return -1; -} - -static int -nrf52k_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, - uint32_t num_bytes) -{ - memcpy(dst, (void *)address, num_bytes); - return 0; -} - -/* - * Flash write is done by writing 4 bytes at a time at a word boundary. - */ -static int -nrf52k_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes) -{ - int sr; - int rc = -1; - uint32_t val; - int cnt; - uint32_t tmp; - - if (nrf52k_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; /* Enable erase OP */ - tmp = address & 0x3; - if (tmp) { - if (nrf52k_flash_wait_ready()) { - goto out; - } - /* - * Starts at a non-word boundary. Read 4 bytes which were there - * before, update with new data, and write back. - */ - val = *(uint32_t *)(address & ~0x3); - cnt = 4 - tmp; - if (cnt > num_bytes) { - cnt = num_bytes; - } - memcpy((uint8_t *)&val + tmp, src, cnt); - *(uint32_t *)(address & ~0x3) = val; - address += cnt; - num_bytes -= cnt; - src += cnt; - } - - while (num_bytes >= sizeof(uint32_t)) { - /* - * Write data 4 bytes at a time. - */ - if (nrf52k_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = *(uint32_t *)src; - address += sizeof(uint32_t); - src += sizeof(uint32_t); - num_bytes -= sizeof(uint32_t); - } - if (num_bytes) { - /* - * Deal with the trailing bytes. - */ - val = *(uint32_t *)address; - memcpy(&val, src, num_bytes); - if (nrf52k_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = val; - } - rc = 0; - if (nrf52k_flash_wait_ready()) { - rc = -1; - } -out: - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf52k_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) -{ - int sr; - int rc = -1; - - if (nrf52k_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een; /* Enable erase OP */ - if (nrf52k_flash_wait_ready()) { - goto out; - } - - NRF_NVMC->ERASEPAGE = sector_address; - if (nrf52k_flash_wait_ready()) { - goto out; - } - rc = 0; -out: - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; /* Disable erase OP */ - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf52k_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz) -{ - assert(idx < nrf52k_flash_dev.hf_sector_cnt); - *address = idx * NRF52K_FLASH_SECTOR_SZ; - *sz = NRF52K_FLASH_SECTOR_SZ; - return 0; -} - -static int -nrf52k_flash_init(const struct hal_flash *dev) -{ - return 0; -} diff --git a/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h b/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h index ed41693515..0f459743f3 100644 --- a/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h +++ b/hw/mcu/nordic/nrf5340/include/mcu/nrf5340_hal.h @@ -35,8 +35,6 @@ struct nrf5340_uart_cfg { }; const struct nrf5340_uart_cfg *bsp_uart_config(void); -struct hal_flash; -extern const struct hal_flash nrf5340_flash_dev; extern const struct hal_flash nrf5340_qspi_dev; extern const struct hal_flash *ipc_flash(void); diff --git a/hw/mcu/nordic/nrf5340/include/nrfx_config_nrf5340_application.h b/hw/mcu/nordic/nrf5340/include/nrfx_config_nrf5340_application.h index 7e4208581c..5eda3f0c90 100644 --- a/hw/mcu/nordic/nrf5340/include/nrfx_config_nrf5340_application.h +++ b/hw/mcu/nordic/nrf5340/include/nrfx_config_nrf5340_application.h @@ -704,7 +704,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf5340/src/hal_flash.c b/hw/mcu/nordic/nrf5340/src/hal_flash.c deleted file mode 100644 index 9e4fe5bbb5..0000000000 --- a/hw/mcu/nordic/nrf5340/src/hal_flash.c +++ /dev/null @@ -1,221 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include -#include -#include -#include -#include - -#define NRF5340_FLASH_SECTOR_SZ 4096 - -#define NRF5340_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready) - -static int -nrf5340_flash_wait_ready(void) -{ - int i; - - for (i = 0; i < 100000; i++) { - if (NRF_NVMC->READY == NVMC_READY_READY_Ready) { - return 0; - } - } - return -1; -} - -static int -nrf5340_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, - uint32_t num_bytes) -{ - memcpy(dst, (void *)address, num_bytes); - return 0; -} - -/* - * Flash write is done by writing 4 bytes at a time at a word boundary. - */ -static int -nrf5340_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes) -{ - int sr; - int rc = -1; - uint32_t val; - int cnt; - uint32_t tmp; - - if (nrf5340_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); -#if MYNEWT_VAL(BOOT_LOADER) || MYNEWT_VAL(MCU_APP_SECURE) - NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Wen; /* Enable erase OP */ -#else - NRF_NVMC_NS->CONFIGNS = NVMC_CONFIGNS_WEN_Wen; /* Enable erase OP */ -#endif - tmp = address & 0x3; - if (tmp) { - if (nrf5340_flash_wait_ready()) { - goto out; - } - /* - * Starts at a non-word boundary. Read 4 bytes which were there - * before, update with new data, and write back. - */ - val = *(uint32_t *)(address & ~0x3); - cnt = 4 - tmp; - if (cnt > num_bytes) { - cnt = num_bytes; - } - memcpy((uint8_t *)&val + tmp, src, cnt); - *(uint32_t *)(address & ~0x3) = val; - address += cnt; - num_bytes -= cnt; - src += cnt; - } - - while (num_bytes >= sizeof(uint32_t)) { - /* - * Write data 4 bytes at a time. - */ - if (nrf5340_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = *(uint32_t *)src; - address += sizeof(uint32_t); - src += sizeof(uint32_t); - num_bytes -= sizeof(uint32_t); - } - if (num_bytes) { - /* - * Deal with the trailing bytes. - */ - val = *(uint32_t *)address; - memcpy(&val, src, num_bytes); - if (nrf5340_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = val; - } - - rc = nrf5340_flash_wait_ready(); -out: -#if MYNEWT_VAL(BOOT_LOADER) || MYNEWT_VAL(MCU_APP_SECURE) - NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Ren; -#else - NRF_NVMC_NS->CONFIGNS = NVMC_CONFIGNS_WEN_Ren; -#endif - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf5340_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) -{ - int sr; - int rc; - - sector_address &= ~(NRF5340_FLASH_SECTOR_SZ - 1); - - if (nrf5340_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - -#if MYNEWT_VAL(BOOT_LOADER) || MYNEWT_VAL(MCU_APP_SECURE) - NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Een; /* Enable erase OP */ -#else - NRF_NVMC_NS->CONFIGNS = NVMC_CONFIGNS_WEN_Een; /* Enable erase OP */ -#endif - *(uint32_t *)sector_address = 0xFFFFFFFF; - - rc = nrf5340_flash_wait_ready(); - -#if MYNEWT_VAL(BOOT_LOADER) || MYNEWT_VAL(MCU_APP_SECURE) - NRF_NVMC_S->CONFIG = NVMC_CONFIG_WEN_Ren; -#else - NRF_NVMC_NS->CONFIGNS = NVMC_CONFIGNS_WEN_Ren; -#endif - __HAL_ENABLE_INTERRUPTS(sr); - - return rc; -} - -static int -nrf5340_flash_erase(const struct hal_flash *dev, uint32_t address, - uint32_t num_bytes) -{ - uint32_t sector_address; - - if (address + num_bytes < dev->hf_base_addr || - address > dev->hf_base_addr + dev->hf_size) { - return -1; - } - - sector_address = address & ~(NRF5340_FLASH_SECTOR_SZ - 1); - num_bytes += address - sector_address; - num_bytes = (num_bytes + NRF5340_FLASH_SECTOR_SZ - 1) & ~(NRF5340_FLASH_SECTOR_SZ - 1); - if (sector_address < dev->hf_base_addr) { - num_bytes -= dev->hf_base_addr - sector_address; - sector_address = dev->hf_base_addr; - } - - while (num_bytes > 0 && sector_address < dev->hf_base_addr + dev->hf_size) { - nrf5340_flash_erase_sector(dev, sector_address); - num_bytes -= NRF5340_FLASH_SECTOR_SZ; - sector_address += NRF5340_FLASH_SECTOR_SZ; - } - - return 0; -} - -static int -nrf5340_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz) -{ - assert(idx < nrf5340_flash_dev.hf_sector_cnt); - *address = idx * NRF5340_FLASH_SECTOR_SZ; - *sz = NRF5340_FLASH_SECTOR_SZ; - return 0; -} - -static int -nrf5340_flash_init(const struct hal_flash *dev) -{ - return 0; -} - -static const struct hal_flash_funcs nrf5340_flash_funcs = { - .hff_read = nrf5340_flash_read, - .hff_write = nrf5340_flash_write, - .hff_erase_sector = nrf5340_flash_erase_sector, - .hff_sector_info = nrf5340_flash_sector_info, - .hff_init = nrf5340_flash_init, - .hff_erase = nrf5340_flash_erase, -}; - -const struct hal_flash nrf5340_flash_dev = { - .hf_itf = &nrf5340_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 1024 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 256, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; diff --git a/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h b/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h index d7f07ad922..3f21bca423 100644 --- a/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h +++ b/hw/mcu/nordic/nrf5340_net/include/mcu/nrf5340_net_hal.h @@ -43,7 +43,6 @@ struct nrf5340_vflash { }; extern struct nrf5340_vflash nrf5340_net_vflash_dev; -extern const struct hal_flash nrf5340_flash_dev; extern const struct hal_flash *ipc_flash(void); /* SPI configuration (used for both master and slave) */ diff --git a/hw/mcu/nordic/nrf5340_net/include/nrfx_config_nrf5340_network.h b/hw/mcu/nordic/nrf5340_net/include/nrfx_config_nrf5340_network.h index 0699d5ea34..a6e71c73f7 100644 --- a/hw/mcu/nordic/nrf5340_net/include/nrfx_config_nrf5340_network.h +++ b/hw/mcu/nordic/nrf5340_net/include/nrfx_config_nrf5340_network.h @@ -295,7 +295,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf5340_net/src/hal_flash.c b/hw/mcu/nordic/nrf5340_net/src/hal_flash.c deleted file mode 100644 index 0a42555e1a..0000000000 --- a/hw/mcu/nordic/nrf5340_net/src/hal_flash.c +++ /dev/null @@ -1,205 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include -#include -#include -#include -#include - -#define NRF5340_NET_FLASH_SECTOR_SZ 2048 - -#define NRF5340_NET_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready) - -static int -nrf5340_net_flash_wait_ready(void) -{ - int i; - - for (i = 0; i < 100000; i++) { - if (NRF_NVMC_NS->READY == NVMC_READY_READY_Ready) { - return 0; - } - } - return -1; -} - -static int -nrf5340_net_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, - uint32_t num_bytes) -{ - memcpy(dst, (void *)address, num_bytes); - return 0; -} - -/* - * Flash write is done by writing 4 bytes at a time at a word boundary. - */ -static int -nrf5340_net_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes) -{ - int sr; - int rc = -1; - uint32_t val; - int cnt; - uint32_t tmp; - - if (nrf5340_net_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - NRF_NVMC_NS->CONFIG = NVMC_CONFIG_WEN_Wen; /* Enable erase OP */ - tmp = address & 0x3; - if (tmp) { - if (nrf5340_net_flash_wait_ready()) { - goto out; - } - /* - * Starts at a non-word boundary. Read 4 bytes which were there - * before, update with new data, and write back. - */ - val = *(uint32_t *)(address & ~0x3); - cnt = 4 - tmp; - if (cnt > num_bytes) { - cnt = num_bytes; - } - memcpy((uint8_t *)&val + tmp, src, cnt); - *(uint32_t *)(address & ~0x3) = val; - address += cnt; - num_bytes -= cnt; - src += cnt; - } - - while (num_bytes >= sizeof(uint32_t)) { - /* - * Write data 4 bytes at a time. - */ - if (nrf5340_net_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = *(uint32_t *)src; - address += sizeof(uint32_t); - src += sizeof(uint32_t); - num_bytes -= sizeof(uint32_t); - } - if (num_bytes) { - /* - * Deal with the trailing bytes. - */ - val = *(uint32_t *)address; - memcpy(&val, src, num_bytes); - if (nrf5340_net_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = val; - } - - rc = nrf5340_net_flash_wait_ready(); -out: - NRF_NVMC_NS->CONFIG = NVMC_CONFIG_WEN_Ren; - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf5340_net_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) -{ - int sr; - int rc; - - sector_address &= ~(NRF5340_NET_FLASH_SECTOR_SZ - 1); - - if (nrf5340_net_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - - NRF_NVMC_NS->CONFIG = NVMC_CONFIG_WEN_Een; /* Enable erase OP */ - *(uint32_t *)sector_address = 0xFFFFFFFF; - - rc = nrf5340_net_flash_wait_ready(); - - NRF_NVMC_NS->CONFIG = NVMC_CONFIG_WEN_Ren; - __HAL_ENABLE_INTERRUPTS(sr); - - return rc; -} - -static int -nrf5340_net_flash_erase(const struct hal_flash *dev, uint32_t address, - uint32_t num_bytes) -{ - uint32_t sector_address; - - if (address + num_bytes < dev->hf_base_addr || - address > dev->hf_base_addr + dev->hf_size) { - return -1; - } - - sector_address = address & ~(NRF5340_NET_FLASH_SECTOR_SZ - 1); - num_bytes += address - sector_address; - num_bytes = (num_bytes + NRF5340_NET_FLASH_SECTOR_SZ - 1) & ~(NRF5340_NET_FLASH_SECTOR_SZ - 1); - if (sector_address < dev->hf_base_addr) { - num_bytes -= dev->hf_base_addr - sector_address; - sector_address = dev->hf_base_addr; - } - - while (num_bytes > 0 && sector_address < dev->hf_base_addr + dev->hf_size) { - nrf5340_net_flash_erase_sector(dev, sector_address); - num_bytes -= NRF5340_NET_FLASH_SECTOR_SZ; - sector_address += NRF5340_NET_FLASH_SECTOR_SZ; - } - - return 0; -} - -static int -nrf5340_net_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz) -{ - assert(idx < nrf5340_flash_dev.hf_sector_cnt); - *address = dev->hf_base_addr + idx * NRF5340_NET_FLASH_SECTOR_SZ; - *sz = NRF5340_NET_FLASH_SECTOR_SZ; - return 0; -} - -static int -nrf5340_net_flash_init(const struct hal_flash *dev) -{ - return 0; -} - -static const struct hal_flash_funcs nrf5340_net_flash_funcs = { - .hff_read = nrf5340_net_flash_read, - .hff_write = nrf5340_net_flash_write, - .hff_erase_sector = nrf5340_net_flash_erase_sector, - .hff_sector_info = nrf5340_net_flash_sector_info, - .hff_init = nrf5340_net_flash_init, - .hff_erase = nrf5340_net_flash_erase, -}; - -const struct hal_flash nrf5340_flash_dev = { - .hf_itf = &nrf5340_net_flash_funcs, - .hf_base_addr = 0x01000000, - .hf_size = 256 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 128, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; diff --git a/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c b/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c index 8024ce4517..918da87c51 100644 --- a/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c +++ b/hw/mcu/nordic/nrf5340_net/src/hal_vflash.c @@ -204,7 +204,7 @@ nrf5340_net_vflash_sector_info(const struct hal_flash *dev, int idx, { (void)dev; - assert(idx < nrf5340_flash_dev.hf_sector_cnt); + assert(idx < nrf_flash_dev.hf_sector_cnt); *address = idx * NRF5340_NET_VFLASH_SECTOR_SZ; *sz = NRF5340_NET_VFLASH_SECTOR_SZ; diff --git a/hw/mcu/nordic/nrf91xx/include/mcu/nrf91_hal.h b/hw/mcu/nordic/nrf91xx/include/mcu/nrf91_hal.h index b3de9146a0..b3f985a7d3 100644 --- a/hw/mcu/nordic/nrf91xx/include/mcu/nrf91_hal.h +++ b/hw/mcu/nordic/nrf91xx/include/mcu/nrf91_hal.h @@ -39,8 +39,7 @@ struct nrf91_hal_i2c_cfg { int sda_pin; uint32_t i2c_frequency; }; -struct hal_flash; -extern const struct hal_flash nrf91k_flash_dev; + extern const struct hal_flash nrf91k_qspi_dev; /* SPI configuration (used for both master and slave) */ diff --git a/hw/mcu/nordic/nrf91xx/include/nrfx_config_nrf91.h b/hw/mcu/nordic/nrf91xx/include/nrfx_config_nrf91.h index 35afd45f69..d2a42ce05d 100644 --- a/hw/mcu/nordic/nrf91xx/include/nrfx_config_nrf91.h +++ b/hw/mcu/nordic/nrf91xx/include/nrfx_config_nrf91.h @@ -420,7 +420,7 @@ * Boolean. Accepted values: 0 and 1. */ #ifndef NRFX_NVMC_ENABLED -#define NRFX_NVMC_ENABLED 0 +#define NRFX_NVMC_ENABLED 1 #endif /** diff --git a/hw/mcu/nordic/nrf91xx/src/hal_flash.c b/hw/mcu/nordic/nrf91xx/src/hal_flash.c deleted file mode 100644 index 00cbda63d7..0000000000 --- a/hw/mcu/nordic/nrf91xx/src/hal_flash.c +++ /dev/null @@ -1,237 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#include -#include -#include "nrf.h" -#include "mcu/nrf91_hal.h" -#include -#include "nrfx_config.h" -#include "nrf_nvmc.h" - -#define NRF91K_FLASH_SECTOR_SZ 4096 - -static int nrf91k_flash_read(const struct hal_flash *dev, uint32_t address, - void *dst, uint32_t num_bytes); -static int nrf91k_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes); -static int nrf91k_flash_erase_sector(const struct hal_flash *dev, - uint32_t sector_address); -static int nrf91k_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz); -static int nrf91k_flash_init(const struct hal_flash *dev); - -static const struct hal_flash_funcs nrf91k_flash_funcs = { - .hff_read = nrf91k_flash_read, - .hff_write = nrf91k_flash_write, - .hff_erase_sector = nrf91k_flash_erase_sector, - .hff_sector_info = nrf91k_flash_sector_info, - .hff_init = nrf91k_flash_init -}; - -#ifdef NRF9160_XXAA -const struct hal_flash nrf91k_flash_dev = { - .hf_itf = &nrf91k_flash_funcs, - .hf_base_addr = 0x00000000, - .hf_size = 1024 * 1024, /* XXX read from factory info? */ - .hf_sector_cnt = 256, /* XXX read from factory info? */ - .hf_align = 1, - .hf_erased_val = 0xff, -}; -#else -#error "Must define hal_flash struct for NRF91 type" -#endif - -#define NRF91K_FLASH_READY() (NRF_NVMC->READY == NVMC_READY_READY_Ready) - -static void -hal_flash_readonly_mode_set(void) -{ -#if defined(NVMC_CONFIGNS_WEN_Msk) - nrf_nvmc_nonsecure_mode_set(NRF_NVMC, NRF_NVMC_NS_MODE_READONLY); -#endif -#if !defined(NRF_TRUSTZONE_NONSECURE) - nrf_nvmc_mode_set(NRF_NVMC, NRF_NVMC_MODE_READONLY); -#endif -} - -static void -hal_flash_write_mode_set(void) -{ -#if defined(NVMC_CONFIGNS_WEN_Msk) - nrf_nvmc_nonsecure_mode_set(NRF_NVMC, NRF_NVMC_NS_MODE_WRITE); -#endif -#if !defined(NRF_TRUSTZONE_NONSECURE) - nrf_nvmc_mode_set(NRF_NVMC, NRF_NVMC_MODE_WRITE); -#endif -} - -static void -hal_flash_erase_mode_set(void) -{ -#if defined(NVMC_CONFIGNS_WEN_Msk) - nrf_nvmc_nonsecure_mode_set(NRF_NVMC, NRF_NVMC_NS_MODE_ERASE); -#endif -#if !defined(NRF_TRUSTZONE_NONSECURE) - nrf_nvmc_mode_set(NRF_NVMC, NRF_NVMC_MODE_ERASE); -#endif -} - -static int -nrf91k_flash_wait_ready(void) -{ - int i; - - for (i = 0; i < 100000; i++) { - if (NRF_NVMC->READY == NVMC_READY_READY_Ready) { - return 0; - } - } - return -1; -} - -static int -nrf91k_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, - uint32_t num_bytes) -{ - int sr; - __HAL_DISABLE_INTERRUPTS(sr); - hal_flash_readonly_mode_set(); - __HAL_ENABLE_INTERRUPTS(sr); - memcpy(dst, (void *)address, num_bytes); - return 0; -} - -/* - * Flash write is done by writing 4 bytes at a time at a word boundary. - */ -static int -nrf91k_flash_write(const struct hal_flash *dev, uint32_t address, - const void *src, uint32_t num_bytes) -{ - int sr; - int rc = -1; - uint32_t val; - int cnt; - uint32_t tmp; - - if (nrf91k_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - hal_flash_write_mode_set(); - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; /* Enable erase OP */ - tmp = address & 0x3; - if (tmp) { - if (nrf91k_flash_wait_ready()) { - goto out; - } - /* - * Starts at a non-word boundary. Read 4 bytes which were there - * before, update with new data, and write back. - */ - val = *(uint32_t *)(address & ~0x3); - cnt = 4 - tmp; - if (cnt > num_bytes) { - cnt = num_bytes; - } - memcpy((uint8_t *)&val + tmp, src, cnt); - *(uint32_t *)(address & ~0x3) = val; - address += cnt; - num_bytes -= cnt; - src += cnt; - } - - while (num_bytes >= sizeof(uint32_t)) { - /* - * Write data 4 bytes at a time. - */ - if (nrf91k_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = *(uint32_t *)src; - address += sizeof(uint32_t); - src += sizeof(uint32_t); - num_bytes -= sizeof(uint32_t); - } - if (num_bytes) { - /* - * Deal with the trailing bytes. - */ - val = *(uint32_t *)address; - memcpy(&val, src, num_bytes); - if (nrf91k_flash_wait_ready()) { - goto out; - } - *(uint32_t *)address = val; - } - rc = 0; - if (nrf91k_flash_wait_ready()) { - rc = -1; - } -out: - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; - hal_flash_readonly_mode_set(); - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf91k_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) -{ - int sr; - int rc = -1; - - if (nrf91k_flash_wait_ready()) { - return -1; - } - __HAL_DISABLE_INTERRUPTS(sr); - hal_flash_erase_mode_set(); - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een; /* Enable erase OP */ - if (nrf91k_flash_wait_ready()) { - goto out; - } - - NRF_NVMC->ERASEPAGEPARTIALCFG = sector_address; - if (nrf91k_flash_wait_ready()) { - goto out; - } - rc = 0; -out: - NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; /* Disable erase OP */ - hal_flash_readonly_mode_set(); - __HAL_ENABLE_INTERRUPTS(sr); - return rc; -} - -static int -nrf91k_flash_sector_info(const struct hal_flash *dev, int idx, - uint32_t *address, uint32_t *sz) -{ - assert(idx < nrf91k_flash_dev.hf_sector_cnt); - *address = idx * NRF91K_FLASH_SECTOR_SZ; - *sz = NRF91K_FLASH_SECTOR_SZ; - return 0; -} - -static int -nrf91k_flash_init(const struct hal_flash *dev) -{ - return 0; -} diff --git a/hw/mcu/nordic/nrf_common/include/nrf_hal.h b/hw/mcu/nordic/nrf_common/include/nrf_hal.h index b86322bd9f..57656c7795 100644 --- a/hw/mcu/nordic/nrf_common/include/nrf_hal.h +++ b/hw/mcu/nordic/nrf_common/include/nrf_hal.h @@ -38,6 +38,9 @@ } \ } while(0); +struct hal_flash; +extern const struct hal_flash nrf_flash_dev; + #ifdef __cplusplus } #endif diff --git a/hw/mcu/nordic/nrf_common/src/hal_flash.c b/hw/mcu/nordic/nrf_common/src/hal_flash.c new file mode 100644 index 0000000000..ced9823114 --- /dev/null +++ b/hw/mcu/nordic/nrf_common/src/hal_flash.c @@ -0,0 +1,114 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include +#include +#include +#include + +#ifdef NRF51 +#define NRF_FLASH_SECTOR_SZ 1024 +#else +#define NRF_FLASH_SECTOR_SZ 4096 +#endif + +#define NRF_FLASH_SECTOR_CNT (NRF_MEMORY_FLASH_SIZE / NRF_FLASH_SECTOR_SZ) + +static int nrf_flash_read(const struct hal_flash *dev, uint32_t address, + void *dst, uint32_t num_bytes); +static int nrf_flash_write(const struct hal_flash *dev, uint32_t address, + const void *src, uint32_t num_bytes); +static int nrf_flash_erase_sector(const struct hal_flash *dev, + uint32_t sector_address); +static int nrf_flash_sector_info(const struct hal_flash *dev, int idx, + uint32_t *address, uint32_t *sz); +static int nrf_flash_init(const struct hal_flash *dev); + +static const struct hal_flash_funcs nrf_flash_funcs = { + .hff_read = nrf_flash_read, + .hff_write = nrf_flash_write, + .hff_erase_sector = nrf_flash_erase_sector, + .hff_sector_info = nrf_flash_sector_info, + .hff_init = nrf_flash_init +}; + +const struct hal_flash nrf_flash_dev = { + .hf_itf = &nrf_flash_funcs, + .hf_base_addr = NRF_MEMORY_FLASH_BASE, + .hf_size = NRF_MEMORY_FLASH_SIZE, + .hf_sector_cnt = NRF_FLASH_SECTOR_CNT, + .hf_align = MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE), + .hf_erased_val = 0xff, +}; + +static int +nrf_flash_read(const struct hal_flash *dev, uint32_t address, void *dst, + uint32_t num_bytes) +{ + memcpy(dst, (void *)address, num_bytes); + return 0; +} + +static int +nrf_flash_write(const struct hal_flash *dev, uint32_t address, + const void *src, uint32_t num_bytes) +{ + int sr; + + __HAL_DISABLE_INTERRUPTS(sr); + nrfx_nvmc_bytes_write(address, src, num_bytes); + __HAL_ENABLE_INTERRUPTS(sr); + + return 0; +} + +static int +nrf_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address) +{ + int sr; + int rc; + + __HAL_DISABLE_INTERRUPTS(sr); + rc = nrfx_nvmc_page_erase(sector_address); + __HAL_ENABLE_INTERRUPTS(sr); + + if (rc != NRFX_SUCCESS) { + return -1; + } else { + return 0; + } +} + +static int +nrf_flash_sector_info(const struct hal_flash *dev, int idx, + uint32_t *address, uint32_t *sz) +{ + assert(idx < nrf_flash_dev.hf_sector_cnt); + *address = dev->hf_base_addr + idx * NRF_FLASH_SECTOR_SZ; + *sz = NRF_FLASH_SECTOR_SZ; + return 0; +} + +static int +nrf_flash_init(const struct hal_flash *dev) +{ + return 0; +}