diff --git a/cores/nRF5/nrf52_flash.c b/cores/nRF5/nrf52_flash.c new file mode 100644 index 00000000..76fa55e5 --- /dev/null +++ b/cores/nRF5/nrf52_flash.c @@ -0,0 +1,168 @@ +/**************************************************************************/ +/*! + @file nrf52_flash.c + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include "Arduino.h" +#include "nrf52_flash.h" + +static SemaphoreHandle_t _evt_sem = NULL; +static volatile uint32_t _op_result; + +void hal_flash_event_cb(uint32_t event) +{ + _op_result = event; + xSemaphoreGive(_evt_sem); +} + +int nrf52_flash_init(void) +{ + _evt_sem = xSemaphoreCreateCounting(10, 0); + return (_evt_sem != NULL) ? 0 : 1; +} + +int nrf52_flash_erase_sector(uint32_t sector_address) +{ + uint32_t err; + +// cprintf("Flash Erase at 0x08%lX \n", sector_address); + + // delay and try again if busy + while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(sector_address/NRF52K_FLASH_SECTOR_SZ)) ) + { + delay(1); + } + VERIFY_STATUS(err); + + xSemaphoreTake(_evt_sem, portMAX_DELAY); + return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1); +} + +static int write_and_wait(uint32_t addr, uint32_t const * const src, uint32_t size) +{ + uint32_t err; + +// cprintf("Flash Write at 0x08%lX with %d bytes\n", addr, size); + + // delay and try again if busy + while ( NRF_ERROR_BUSY == (err = sd_flash_write( (uint32_t*) addr, src, size)) ) + { + delay(1); + } + VERIFY_STATUS(err); + + xSemaphoreTake(_evt_sem, portMAX_DELAY); + return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1); +} + +int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) +{ + + /* SD Flash requires + * 1. src data must be word aligned + * 2. address must be word aligned + * 2. each write is 4 bytes + * + * This cause issue with string literal. Solution is break up to 3 sequence write + * 1. First is required if address not started at word aligned + * 2. Malloc aligned data for src if needed + * 3. Trailing bytes + */ + + /*------------- non-aligned Address -------------*/ + // Address is not aligned + uint32_t miss = address & 0x03; + + if ( miss ) + { + /* Address is not aligned. Combine odd bytes data + existed data + * then update and write back + */ + + uint32_t cnt = 4 - miss; + uint32_t val; + + memcpy( &val, (uint8_t*) align4(address), 4); + memcpy( ((uint8_t*)&val)+miss, src, cnt); + + VERIFY_STATUS( write_and_wait( align4(address), &val, 1) ); + + address += cnt; + src += cnt; + num_bytes -= cnt; + } + + /*------------- Main write -------------*/ + uint32_t count = align4(num_bytes); + uint32_t* tempbuf; + + // src is not word aligned + miss = ((uint32_t)src) & 0x03; + if (miss) + { + // Malloc and copy multiple of 4 bytes from src to write to flash + tempbuf = (uint32_t*) rtos_malloc( count ); + VERIFY( tempbuf, NRF_ERROR_NO_MEM); + + memcpy(tempbuf, src, count); + }else + { + tempbuf = (uint32_t*) src; + } + + VERIFY_STATUS( write_and_wait(address, tempbuf, count / 4) ); + + if (miss) + { + free(tempbuf); + } + + address += count; + src += count; + num_bytes -= count; + + /*------------- Trailing Bytes -------------*/ + if ( num_bytes ) + { + uint32_t val; + + memcpy( &val, (uint8_t*) address, 4); + memcpy(&val, src, num_bytes); + + VERIFY_STATUS( write_and_wait(address, &val, 1) ); + } + + return 0; +} + diff --git a/cores/nRF5/nrf52_flash.h b/cores/nRF5/nrf52_flash.h new file mode 100644 index 00000000..c5d305bc --- /dev/null +++ b/cores/nRF5/nrf52_flash.h @@ -0,0 +1,55 @@ +/**************************************************************************/ +/*! + @file nrf52_flash.h + @author hathach + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2017, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ +#ifndef NRF52_FLASH_H_ +#define NRF52_FLASH_H_ + +#include + +#ifdef __cplusplus + extern "C" { +#endif + +#define NRF52K_FLASH_SECTOR_SZ 4096 + +int nrf52_flash_init(void); +int nrf52_flash_erase_sector(uint32_t sector_address); +int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes); + +#ifdef __cplusplus + } +#endif + +#endif /* NRF52_FLASH_H_ */ diff --git a/libraries/nffs/src/hw/hal/include/hal/hal_flash.h b/libraries/nffs/src/hw/hal/include/hal/hal_flash.h index 79ba79d0..76c31703 100644 --- a/libraries/nffs/src/hw/hal/include/hal/hal_flash.h +++ b/libraries/nffs/src/hw/hal/include/hal/hal_flash.h @@ -33,7 +33,7 @@ int hal_flash_write(uint8_t flash_id, uint32_t address, const void *src, int hal_flash_erase_sector(uint8_t flash_id, uint32_t sector_address); int hal_flash_erase(uint8_t flash_id, uint32_t address, uint32_t num_bytes); uint8_t hal_flash_align(uint8_t flash_id); -int hal_flash_init(void); +int nrf52_flash_init(void); struct hal_flash; const struct hal_flash *hal_bsp_flash_dev(uint8_t flash_id); diff --git a/libraries/nffs/src/hw/hal/src/hal_flash.c b/libraries/nffs/src/hw/hal/src/hal_flash.c index 115898eb..b0caae73 100644 --- a/libraries/nffs/src/hw/hal/src/hal_flash.c +++ b/libraries/nffs/src/hw/hal/src/hal_flash.c @@ -29,8 +29,7 @@ #include "common_func.h" #include "verify.h" - -#define NRF52K_FLASH_SECTOR_SZ 4096 +#include "nrf52_flash.h" int hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz); @@ -51,15 +50,6 @@ const struct hal_flash nrf52k_flash_dev = { .hf_align = 1 }; -static SemaphoreHandle_t _evt_sem = NULL; -volatile static uint32_t _op_result; - -void hal_flash_event_cb(uint32_t event) -{ - _op_result = event; - xSemaphoreGive(_evt_sem); -} - static int hal_flash_check_addr(const struct hal_flash *hf, uint32_t addr) { if (addr < hf->hf_base_addr || addr > hf->hf_base_addr + hf->hf_size) { @@ -80,103 +70,6 @@ int hal_flash_read(uint8_t id, uint32_t address, void *dst, uint32_t num_bytes) return 0; } -static int write_and_wait(uint32_t addr, uint32_t const * const src, uint32_t size) -{ - uint32_t err; - -// cprintf("Flash Write at 0x08%lX with %d bytes\n", addr, size); - - // delay and try again if busy - while ( NRF_ERROR_BUSY == (err = sd_flash_write( (uint32_t*) addr, src, size)) ) - { - delay(1); - } - VERIFY_STATUS(err); - - xSemaphoreTake(_evt_sem, portMAX_DELAY); - return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1); -} - -int nrf52_flash_write(uint32_t address, const void *src, uint32_t num_bytes) -{ - - /* SD Flash requires - * 1. src data must be word aligned - * 2. address must be word aligned - * 2. each write is 4 bytes - * - * This cause issue with string literal. Solution is break up to 3 sequence write - * 1. First is required if address not started at word aligned - * 2. Malloc aligned data for src if needed - * 3. Trailing bytes - */ - - /*------------- non-aligned Address -------------*/ - // Address is not aligned - uint32_t miss = address & 0x03; - - if ( miss ) - { - /* Address is not aligned. Combine odd bytes data + existed data - * then update and write back - */ - - uint32_t cnt = 4 - miss; - uint32_t val; - - memcpy( &val, (uint8_t*) align4(address), 4); - memcpy( ((uint8_t*)&val)+miss, src, cnt); - - VERIFY_STATUS( write_and_wait( align4(address), &val, 1) ); - - address += cnt; - src += cnt; - num_bytes -= cnt; - } - - /*------------- Main write -------------*/ - uint32_t count = align4(num_bytes); - uint32_t* tempbuf; - - // src is not word aligned - miss = ((uint32_t)src) & 0x03; - if (miss) - { - // Malloc and copy multiple of 4 bytes from src to write to flash - tempbuf = (uint32_t*) rtos_malloc( count ); - VERIFY( tempbuf, NRF_ERROR_NO_MEM); - - memcpy(tempbuf, src, count); - }else - { - tempbuf = (uint32_t*) src; - } - - VERIFY_STATUS( write_and_wait(address, tempbuf, count / 4) ); - - if (miss) - { - free(tempbuf); - } - - address += count; - src += count; - num_bytes -= count; - - /*------------- Trailing Bytes -------------*/ - if ( num_bytes ) - { - uint32_t val; - - memcpy( &val, (uint8_t*) address, 4); - memcpy(&val, src, num_bytes); - - VERIFY_STATUS( write_and_wait(address, &val, 1) ); - } - - return 0; -} - int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) { const struct hal_flash *hf; @@ -218,24 +111,6 @@ int hal_flash_erase(uint8_t id, uint32_t address, uint32_t num_bytes) return 0; } -int nrf52_flash_erase_sector(uint32_t sector_address) -{ - uint32_t err; - -// cprintf("Flash Erase at 0x08%lX \n", sector_address); - - // delay and try again if busy - while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(sector_address/NRF52K_FLASH_SECTOR_SZ)) ) - { - delay(1); - } - VERIFY_STATUS(err); - - xSemaphoreTake(_evt_sem, portMAX_DELAY); - return (_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS ) ? 0 : (-1); -} - - int hal_flash_erase_sector(uint8_t id, uint32_t sector_address) { (void) id; @@ -258,12 +133,6 @@ int hal_flash_sector_info(int idx, uint32_t *address, uint32_t *sz) return 0; } -int hal_flash_init(void) -{ - _evt_sem = xSemaphoreCreateCounting(10, 0); - return (_evt_sem != NULL) ? 0 : 1; -} - uint8_t hal_flash_align(uint8_t flash_id) { return nrf52k_flash_dev.hf_align;