2019-03-27 06:38:36 +00:00
|
|
|
/*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Ha Thach for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
2018-09-28 09:27:58 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2018-10-02 09:55:55 +00:00
|
|
|
#include "nrf_sdm.h"
|
2018-09-28 09:27:58 +00:00
|
|
|
#include "flash_nrf5x.h"
|
|
|
|
#include "boards.h"
|
|
|
|
|
2018-09-30 16:42:40 +00:00
|
|
|
#define FLASH_PAGE_SIZE 4096
|
|
|
|
#define FLASH_CACHE_INVALID_ADDR 0xffffffff
|
2018-09-28 09:27:58 +00:00
|
|
|
|
|
|
|
static uint32_t _fl_addr = FLASH_CACHE_INVALID_ADDR;
|
|
|
|
static uint8_t _fl_buf[FLASH_PAGE_SIZE] __attribute__((aligned(4)));
|
|
|
|
|
2018-10-02 11:57:50 +00:00
|
|
|
void flash_nrf5x_flush (bool need_erase)
|
2018-09-28 09:27:58 +00:00
|
|
|
{
|
|
|
|
if ( _fl_addr == FLASH_CACHE_INVALID_ADDR ) return;
|
|
|
|
|
|
|
|
if ( memcmp(_fl_buf, (void *) _fl_addr, FLASH_PAGE_SIZE) != 0 )
|
|
|
|
{
|
2018-10-02 09:55:55 +00:00
|
|
|
// - nRF52832 dfu via uart can miss incoming byte when erasing because cpu is blocked for > 2ms.
|
|
|
|
// Since dfu_prepare_func_app_erase() already erase the page for us, we can skip it here.
|
2019-02-21 17:21:34 +00:00
|
|
|
// - nRF52840 dfu serial/uf2 are USB-based which are DMA and should have no problems.
|
|
|
|
//
|
|
|
|
// Note: MSC uf2 does not erase page in advance like dfu serial
|
2019-11-11 08:27:44 +00:00
|
|
|
if ( need_erase ) nrfx_nvmc_page_erase(_fl_addr);
|
2018-10-02 11:57:50 +00:00
|
|
|
|
2019-11-11 08:27:44 +00:00
|
|
|
nrfx_nvmc_words_write(_fl_addr, (uint32_t *) _fl_buf, FLASH_PAGE_SIZE / 4);
|
2018-09-28 09:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_fl_addr = FLASH_CACHE_INVALID_ADDR;
|
|
|
|
}
|
|
|
|
|
2018-10-02 11:57:50 +00:00
|
|
|
void flash_nrf5x_write (uint32_t dst, void const *src, int len, bool need_erase)
|
2018-09-28 09:27:58 +00:00
|
|
|
{
|
|
|
|
uint32_t newAddr = dst & ~(FLASH_PAGE_SIZE - 1);
|
|
|
|
|
|
|
|
if ( newAddr != _fl_addr )
|
|
|
|
{
|
2018-10-02 11:57:50 +00:00
|
|
|
flash_nrf5x_flush(need_erase);
|
2018-09-28 09:27:58 +00:00
|
|
|
_fl_addr = newAddr;
|
|
|
|
memcpy(_fl_buf, (void *) newAddr, FLASH_PAGE_SIZE);
|
|
|
|
}
|
|
|
|
memcpy(_fl_buf + (dst & (FLASH_PAGE_SIZE - 1)), src, len);
|
|
|
|
}
|
|
|
|
|