aconno flash API for nrf52832. (Modified mbed flash API)
Diff: aconno_flash.cpp
- Revision:
- 2:e5390f8eab8c
- Parent:
- 1:954ac9003e99
diff -r 954ac9003e99 -r e5390f8eab8c aconno_flash.cpp --- a/aconno_flash.cpp Tue May 22 17:41:01 2018 +0200 +++ b/aconno_flash.cpp Tue May 22 18:32:21 2018 +0200 @@ -45,12 +45,14 @@ #if DEVICE_FLASH +#include "aconno_flash.h" #include "hal/flash_api.h" #include "hal/lp_ticker_api.h" #include "nrf_drv_common.h" #include "nrf_nvmc.h" #include "nrf_soc.h" +#include <string.h> // Max. value from datasheet: 338 us // // Constant increased by the author experimentaly @@ -76,7 +78,6 @@ #define NRF_HAL_SD_IS_ENABLED() 0 #endif - int32_t aconno_flash_init(flash_t *obj) { (void)(obj); @@ -135,6 +136,12 @@ return (result == NRF_SUCCESS) ? 0 : -1; } +int32_t aconno_flash_read(flash_t *obj, uint32_t address, uint8_t *data, uint32_t size) +{ + memcpy(data, (const void *)address, size); + return 0; +} + int32_t aconno_flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data, uint32_t size) { (void)(obj); @@ -183,7 +190,7 @@ (void)(obj); /* Test if passed address is in flash space. */ - if (address < flash_get_size(obj)) { + if (address < aconno_flash_get_size(obj)) { return NRF_FICR->CODEPAGESIZE; } @@ -206,5 +213,102 @@ return 0; } +/** + * Just leave the following function's declarations here! + * It's total mess if you try to put them somewhere else (mbed stuff...) + */ +void nrf_nvmc_page_erase(uint32_t address) +{ + // Enable erase. + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } + + // Erase the page + NRF_NVMC->ERASEPAGE = address; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } + + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } +} + +void nrf_nvmc_write_byte(uint32_t address, uint8_t value) +{ + uint32_t byte_shift = address & (uint32_t)0x03; + uint32_t address32 = address & ~byte_shift; // Address to the word this byte is in. + uint32_t value32 = (*(uint32_t*)address32 & ~((uint32_t)0xFF << (byte_shift << (uint32_t)3))); + value32 = value32 + ((uint32_t)value << (byte_shift << 3)); + + // Enable write. + NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos); + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } + + *(uint32_t*)address32 = value32; + while(NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } + + NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos); + { + } +} + +void nrf_nvmc_write_word(uint32_t address, uint32_t value) +{ + // Enable write. + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy){ + } + + *(uint32_t*)address = value; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy){ + } + + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } +} + +void nrf_nvmc_write_bytes(uint32_t address, const uint8_t * src, uint32_t num_bytes) +{ + uint32_t i; + for(i=0;i<num_bytes;i++) + { + nrf_nvmc_write_byte(address+i,src[i]); + } +} + +void nrf_nvmc_write_words(uint32_t address, const uint32_t * src, uint32_t num_words) +{ + uint32_t i; + + // Enable write. + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } + + for(i=0;i<num_words;i++) + { + ((uint32_t*)address)[i] = src[i]; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } + } + + NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren; + while (NRF_NVMC->READY == NVMC_READY_READY_Busy) + { + } +} + #endif /** @}*/