move flash API out of core to FileSystem library
This commit is contained in:
1 parent
db6206864b
commit
e3c7a1a1ec
4 files changed
No files matched your search
@@ -0,0 +1,143 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file flash_cache.c
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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 "flash_cache.h"
|
||||
|
||||
#include "common_func.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
static inline uint32_t page_addr_of (uint32_t addr)
|
||||
{
|
||||
return addr & ~(FLASH_CACHE_SIZE - 1);
|
||||
}
|
||||
|
||||
static inline uint32_t page_offset_of (uint32_t addr)
|
||||
{
|
||||
return addr & (FLASH_CACHE_SIZE - 1);
|
||||
}
|
||||
|
||||
uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const * src, uint32_t len)
|
||||
{
|
||||
uint8_t const * src8 = (uint8_t const *) src;
|
||||
uint32_t remain = len;
|
||||
|
||||
// Program blocks up to page boundary each loop
|
||||
while ( remain )
|
||||
{
|
||||
uint32_t const page_addr = page_addr_of(dst);
|
||||
uint32_t const offset = page_offset_of(dst);
|
||||
|
||||
uint32_t wr_bytes = FLASH_CACHE_SIZE - offset;
|
||||
wr_bytes = min32(remain, wr_bytes);
|
||||
|
||||
// Page changes, flush old and update new cache
|
||||
if ( page_addr != fc->cache_addr )
|
||||
{
|
||||
flash_cache_flush(fc);
|
||||
fc->cache_addr = page_addr;
|
||||
|
||||
// read existing flash to cache except those we are writing
|
||||
if ( offset )
|
||||
{
|
||||
fc->read(fc->cache_buf, page_addr, offset);
|
||||
}
|
||||
|
||||
uint32_t const last_byte = offset + wr_bytes;
|
||||
if ( last_byte < FLASH_CACHE_SIZE )
|
||||
{
|
||||
fc->read(fc->cache_buf + last_byte, page_addr + last_byte, FLASH_CACHE_SIZE - last_byte);
|
||||
}
|
||||
}
|
||||
|
||||
memcpy(fc->cache_buf + offset, src8, wr_bytes);
|
||||
|
||||
// adjust for next run
|
||||
src8 += wr_bytes;
|
||||
remain -= wr_bytes;
|
||||
}
|
||||
|
||||
return len - remain;
|
||||
}
|
||||
|
||||
void flash_cache_flush (flash_cache_t* fc)
|
||||
{
|
||||
if ( fc->cache_addr == FLASH_CACHE_INVALID_ADDR ) return;
|
||||
|
||||
// skip erase & program if verify() exists, and memory matches
|
||||
if ( !(fc->verify && fc->verify(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE)) )
|
||||
{
|
||||
fc->erase(fc->cache_addr);
|
||||
fc->program(fc->cache_addr, fc->cache_buf, FLASH_CACHE_SIZE);
|
||||
}
|
||||
|
||||
fc->cache_addr = FLASH_CACHE_INVALID_ADDR;
|
||||
}
|
||||
|
||||
void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count)
|
||||
{
|
||||
// overwrite with cache value if available
|
||||
if ( (fc->cache_addr != FLASH_CACHE_INVALID_ADDR) &&
|
||||
!(addr < fc->cache_addr && addr + count <= fc->cache_addr) &&
|
||||
!(addr >= fc->cache_addr + FLASH_CACHE_SIZE) )
|
||||
{
|
||||
int dst_off = fc->cache_addr - addr;
|
||||
int src_off = 0;
|
||||
|
||||
if ( dst_off < 0 )
|
||||
{
|
||||
src_off = -dst_off;
|
||||
dst_off = 0;
|
||||
}
|
||||
|
||||
int cache_bytes = minof(FLASH_CACHE_SIZE-src_off, count - dst_off);
|
||||
|
||||
// start to cached
|
||||
if ( dst_off ) fc->read(dst, addr, dst_off);
|
||||
|
||||
// cached
|
||||
memcpy(dst + dst_off, fc->cache_buf + src_off, cache_bytes);
|
||||
|
||||
// cached to end
|
||||
int copied = dst_off + cache_bytes;
|
||||
if ( copied < count ) fc->read(dst + copied, addr + copied, count - copied);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(dst, (void*) addr, count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file flash_cache.h
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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 FLASH_CACHE_H_
|
||||
#define FLASH_CACHE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define FLASH_CACHE_SIZE 4096 // must be a erasable page size
|
||||
#define FLASH_CACHE_INVALID_ADDR 0xffffffff
|
||||
|
||||
typedef struct
|
||||
{
|
||||
bool (*erase) (uint32_t addr);
|
||||
uint32_t (*program) (uint32_t dst, void const * src, uint32_t len);
|
||||
uint32_t (*read) (void* dst, uint32_t src, uint32_t len);
|
||||
bool (*verify) (uint32_t addr, void const * buf, uint32_t len);
|
||||
|
||||
uint32_t cache_addr;
|
||||
uint8_t cache_buf[FLASH_CACHE_SIZE] __attribute__((aligned(4)));
|
||||
} flash_cache_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
uint32_t flash_cache_write (flash_cache_t* fc, uint32_t dst, void const *src, uint32_t count);
|
||||
void flash_cache_flush (flash_cache_t* fc);
|
||||
void flash_cache_read (flash_cache_t* fc, void* dst, uint32_t addr, uint32_t count);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* FLASH_CACHE_H_ */
|
||||
@@ -0,0 +1,162 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file flash_nrf5x.c
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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 "flash_nrf5x.h"
|
||||
#include "flash_cache.h"
|
||||
#include "nrf_sdm.h"
|
||||
#include "nrf_soc.h"
|
||||
#include "rtos.h"
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
|
||||
//--------------------------------------------------------------------+
|
||||
static SemaphoreHandle_t _sem = NULL;
|
||||
static volatile uint32_t _op_result;
|
||||
|
||||
void flash_nrf5x_event_cb (uint32_t event)
|
||||
{
|
||||
_op_result = event;
|
||||
xSemaphoreGive(_sem);
|
||||
}
|
||||
|
||||
// Flash Abstraction Layer
|
||||
static bool fal_erase (uint32_t addr);
|
||||
static uint32_t fal_program (uint32_t dst, void const * src, uint32_t len);
|
||||
static uint32_t fal_read (void* dst, uint32_t src, uint32_t len);
|
||||
static bool fal_verify (uint32_t addr, void const * buf, uint32_t len);
|
||||
|
||||
flash_cache_t _cache = {
|
||||
.erase = fal_erase,
|
||||
.program = fal_program,
|
||||
.read = fal_read,
|
||||
.verify = fal_verify,
|
||||
.cache_addr = FLASH_CACHE_INVALID_ADDR
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// Application API
|
||||
//--------------------------------------------------------------------+
|
||||
void flash_nrf5x_flush (void)
|
||||
{
|
||||
flash_cache_flush(&_cache);
|
||||
}
|
||||
|
||||
uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len)
|
||||
{
|
||||
// TODO prevent write SD + bootloader region
|
||||
return flash_cache_write(&_cache, dst, src, len);
|
||||
}
|
||||
|
||||
uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len)
|
||||
{
|
||||
// return cache value if available
|
||||
flash_cache_read(&_cache, dst, src, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
bool flash_nrf5x_erase(uint32_t addr)
|
||||
{
|
||||
return fal_erase(addr);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------+
|
||||
// HAL for caching
|
||||
//--------------------------------------------------------------------+
|
||||
bool fal_erase (uint32_t addr)
|
||||
{
|
||||
// Init semaphore for first call
|
||||
if ( !_sem )
|
||||
{
|
||||
_sem = xSemaphoreCreateBinary();
|
||||
VERIFY(_sem);
|
||||
}
|
||||
|
||||
// delay and retry if busy
|
||||
uint32_t err;
|
||||
while ( NRF_ERROR_BUSY == (err = sd_flash_page_erase(addr / FLASH_NRF52_PAGE_SIZE)) )
|
||||
{
|
||||
delay(1);
|
||||
}
|
||||
VERIFY_STATUS(err);
|
||||
|
||||
// wait for async event if SD is enabled
|
||||
uint8_t sd_en = 0;
|
||||
(void) sd_softdevice_is_enabled(&sd_en);
|
||||
|
||||
if ( sd_en )
|
||||
{
|
||||
xSemaphoreTake(_sem, portMAX_DELAY);
|
||||
VERIFY(_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static uint32_t fal_program (uint32_t dst, void const * src, uint32_t len)
|
||||
{
|
||||
// delay and try again if busy
|
||||
uint32_t err;
|
||||
while ( NRF_ERROR_BUSY == (err = sd_flash_write((uint32_t*) dst, (uint32_t const *) src, len / 4)) )
|
||||
{
|
||||
delay(1);
|
||||
}
|
||||
VERIFY_STATUS(err);
|
||||
|
||||
// wait for async event if SD is enabled
|
||||
uint8_t sd_en = 0;
|
||||
(void) sd_softdevice_is_enabled(&sd_en);
|
||||
|
||||
if ( sd_en )
|
||||
{
|
||||
xSemaphoreTake(_sem, portMAX_DELAY);
|
||||
VERIFY(_op_result == NRF_EVT_FLASH_OPERATION_SUCCESS);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static uint32_t fal_read (void* dst, uint32_t src, uint32_t len)
|
||||
{
|
||||
memcpy(dst, (void*) src, len);
|
||||
return len;
|
||||
}
|
||||
|
||||
static bool fal_verify (uint32_t addr, void const * buf, uint32_t len)
|
||||
{
|
||||
return 0 == memcmp((void*) addr, buf, len);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/**************************************************************************/
|
||||
/*!
|
||||
@file flash_nrf5x.h
|
||||
@author hathach (tinyusb.org)
|
||||
|
||||
@section LICENSE
|
||||
|
||||
Software License Agreement (BSD License)
|
||||
|
||||
Copyright (c) 2018, 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 FLASH_NRF52_H_
|
||||
#define FLASH_NRF52_H_
|
||||
|
||||
#include "common_inc.h"
|
||||
|
||||
#define FLASH_NRF52_PAGE_SIZE 4096
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void flash_nrf5x_flush (void);
|
||||
bool flash_nrf5x_erase(uint32_t addr);
|
||||
|
||||
uint32_t flash_nrf5x_write (uint32_t dst, void const * src, uint32_t len);
|
||||
uint32_t flash_nrf5x_read (void* dst, uint32_t src, uint32_t len);
|
||||
|
||||
static inline uint32_t flash_nrf5x_write8 (uint32_t dst, uint8_t num)
|
||||
{
|
||||
flash_nrf5x_write(dst, &num, sizeof(num));
|
||||
return sizeof(num);
|
||||
}
|
||||
|
||||
static inline uint32_t flash_nrf5x_write16 (uint32_t dst, uint8_t num)
|
||||
{
|
||||
flash_nrf5x_write(dst, &num, sizeof(num));
|
||||
return sizeof(num);
|
||||
}
|
||||
static inline uint32_t flash_nrf5x_write32 (uint32_t dst, uint8_t num)
|
||||
{
|
||||
flash_nrf5x_write(dst, &num, sizeof(num));
|
||||
return sizeof(num);
|
||||
}
|
||||
|
||||
static inline uint8_t flash_nrf5x_read8 (uint32_t src)
|
||||
{
|
||||
uint8_t num;
|
||||
flash_nrf5x_read(&num, src, sizeof(num));
|
||||
return num;
|
||||
}
|
||||
|
||||
static inline uint16_t flash_nrf5x_read16 (uint32_t src)
|
||||
{
|
||||
uint16_t num;
|
||||
flash_nrf5x_read(&num, src, sizeof(num));
|
||||
return num;
|
||||
}
|
||||
|
||||
static inline uint16_t flash_nrf5x_read32 (uint32_t src)
|
||||
{
|
||||
uint32_t num;
|
||||
flash_nrf5x_read(&num, src, sizeof(num));
|
||||
return num;
|
||||
}
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* FLASH_NRF52_H_ */
|
||||
Reference in new issue
Block a user