implement msc flash, seems to work

This commit is contained in:
hathach 2018-07-05 15:22:23 +07:00
parent 1745e20391
commit 3564e98181
3 changed files with 68 additions and 64 deletions

View File

@ -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 \

View File

@ -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)
{
}

View File

@ -37,16 +37,18 @@
#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_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,12 +141,56 @@ 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);
@ -155,9 +201,20 @@ int32_t tud_msc_read10_cb (uint8_t rhport, uint8_t lun, uint32_t lba, uint32_t o
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