From 3564e981812ae9d4d91da72805dba5fad1c166f9 Mon Sep 17 00:00:00 2001 From: hathach Date: Thu, 5 Jul 2018 15:22:23 +0700 Subject: [PATCH] implement msc flash, seems to work --- ports/nrf/Makefile | 2 +- ports/nrf/usb/usb_cdc.c | 53 ------------------------ ports/nrf/usb/usb_msc_flash.c | 77 ++++++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 64 deletions(-) delete mode 100644 ports/nrf/usb/usb_cdc.c diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 745357695b..ab02e12197 100644 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -115,6 +115,7 @@ SRC_C += \ drivers/bluetooth/ble_uart.c \ boards/$(BOARD)/board.c \ nrfx/mdk/system_$(MCU_SUB_VARIANT).c \ + nrfx/hal/nrf_nvmc.c \ device/$(MCU_VARIANT)/startup_$(MCU_SUB_VARIANT).c \ background.c \ lib/oofatfs/ff.c \ @@ -135,7 +136,6 @@ ifeq ($(MCU_SUB_VARIANT),nrf52840) SRC_C += \ usb/tusb_descriptors.c \ usb/usb_msc_flash.c \ - usb/usb_cdc.c \ lib/tinyusb/src/portable/nordic/nrf5x/dcd_nrf5x.c \ lib/tinyusb/src/portable/nordic/nrf5x/hal_nrf5x.c \ lib/tinyusb/src/common/tusb_fifo.c \ diff --git a/ports/nrf/usb/usb_cdc.c b/ports/nrf/usb/usb_cdc.c deleted file mode 100644 index 1f4600d526..0000000000 --- a/ports/nrf/usb/usb_cdc.c +++ /dev/null @@ -1,53 +0,0 @@ -/**************************************************************************/ -/*! - @file usb_cdc.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 "tusb.h" - -/*------------------------------------------------------------------*/ -/* MACRO TYPEDEF CONSTANT ENUM - *------------------------------------------------------------------*/ - -/*------------------------------------------------------------------*/ -/* VARIABLE DECLARATION - *------------------------------------------------------------------*/ - -/*------------------------------------------------------------------*/ -/* FUNCTION DECLARATION - *------------------------------------------------------------------*/ - -void tud_cdc_rx_cb(uint8_t port) -{ -} diff --git a/ports/nrf/usb/usb_msc_flash.c b/ports/nrf/usb/usb_msc_flash.c index b9e0c86106..22cc689885 100644 --- a/ports/nrf/usb/usb_msc_flash.c +++ b/ports/nrf/usb/usb_msc_flash.c @@ -37,15 +37,17 @@ #ifdef NRF52840_XXAA #include "tusb.h" - -#if CFG_TUD_MSC +#include "nrf_nvmc.h" /*------------------------------------------------------------------*/ /* MACRO TYPEDEF CONSTANT ENUM *------------------------------------------------------------------*/ -#define MSC_FLASH_ADDR_START 0xAD000 -#define MSC_FLASH_SIZE (256*1024) -#define MSC_FLASH_BLOCK_SIZE 512 +#define MSC_FLASH_ADDR_END 0xED000 +#define MSC_FLASH_SIZE (256*1024) +#define MSC_FLASH_ADDR_START (MSC_FLASH_ADDR_END-MSC_FLASH_SIZE) +#define MSC_FLASH_BLOCK_SIZE 512 + +#define FL_PAGE_SZ 4096 /*------------------------------------------------------------------*/ /* VARIABLES @@ -139,25 +141,80 @@ int32_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t const scsi_cmd[16] return len; } +/*------------------------------------------------------------------*/ +/* Internal Flash + *------------------------------------------------------------------*/ +#define NO_CACHE 0xffffffff + +uint8_t _fl_cache[FL_PAGE_SZ] ATTR_ALIGNED(4); +uint32_t _fl_addr = NO_CACHE; + + +static void fl_flush() { + if (_fl_addr == NO_CACHE) return; + + // Skip if data is the same + if (memcmp(_fl_cache, (void *)_fl_addr, FL_PAGE_SZ) != 0) { +// _is_flashing = true; + nrf_nvmc_page_erase(_fl_addr); + nrf_nvmc_write_words(_fl_addr, (uint32_t *)_fl_cache, FL_PAGE_SZ / sizeof(uint32_t)); + } + + _fl_addr = NO_CACHE; +} + +static bool fl_write(uint32_t addr, uint8_t* buf, uint16_t bufsize) +{ + uint32_t new_addr = addr & ~(FL_PAGE_SZ - 1); + + if (new_addr != _fl_addr) { + fl_flush(); + + // writing previous cached data, skip current data until flashing is done + // tinyusb stack will invoke write_block() with the same parameters later on +// if ( _is_flashing ) return; + + _fl_addr = new_addr; + memcpy(_fl_cache, (void *)new_addr, FL_PAGE_SZ); + } + + memcpy(_fl_cache + (addr & (FL_PAGE_SZ - 1)), buf, bufsize); + + return true; +} + + /*------------------------------------------------------------------*/ /* Tinyusb Flash READ10 & WRITE10 *------------------------------------------------------------------*/ int32_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { - (void) rhport; (void) lun; + (void) rhport; + (void) lun; - uint32_t addr = lba2addr(lba) + offset; - memcpy(buffer, (uint8_t*) addr, bufsize); + uint32_t addr = lba2addr(lba) + offset; + memcpy(buffer, (uint8_t*) addr, bufsize); - return bufsize; + return bufsize; } int32_t tud_msc_write10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize) { (void) rhport; (void) lun; + uint32_t addr = lba2addr(lba) + offset; + + // bufsize <= CFG_TUD_MSC_BUFSIZE (4096) + fl_write(addr, buffer, bufsize); return bufsize; } -#endif +void tud_msc_write10_complete_cb(uint8_t rhport, uint8_t lun) +{ + (void) rhport; (void) lun; + + // flush pending cache when write10 is complete + fl_flush(); +} + #endif