This commit is contained in:
hathach 2018-06-25 16:36:50 +07:00
parent 6e9ecad3c8
commit b31eac0950
2 changed files with 94 additions and 128 deletions

View File

@ -52,33 +52,16 @@
/*------------------------------------------------------------------*/
/* MACRO TYPEDEF CONSTANT ENUM
*------------------------------------------------------------------*/
enum
{
WRITE10_IDLE,
WRITE10_ERASING,
WRITE10_ERASED,
WRITE10_WRITING,
WRITE10_WRITTEN,
WRITE10_FAILED
};
enum { FL_PAGE_SIZE = 4096 };
/*------------------------------------------------------------------*/
/* UF2
*------------------------------------------------------------------*/
void read_block(uint32_t block_no, uint8_t *data);
int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *state*/);
void ghostfat_init();
/*------------------------------------------------------------------*/
/* VARIABLES
*------------------------------------------------------------------*/
static uint8_t _page_cached[FL_PAGE_SIZE] ATTR_ALIGNED(4);
volatile static uint8_t _wr10_state;
static scsi_inquiry_data_t const mscd_inquiry_data =
{
.is_removable = 1,
@ -136,7 +119,7 @@ static inline uint32_t lba2addr(uint32_t lba)
*------------------------------------------------------------------*/
void msc_uf2_init(void)
{
ghostfat_init();
}
void msc_uf2_mount(void)
@ -232,8 +215,6 @@ int32_t tud_msc_scsi_cb (uint8_t rhport, uint8_t lun, uint8_t const scsi_cmd[16]
/*------------------------------------------------------------------*/
/* 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;

View File

@ -125,11 +125,9 @@ uint8_t flashBuf[FLASH_PAGE_SIZE] __attribute__((aligned(4)));
bool firstFlush = true;
//bool hadWrite = false;
static WriteState uf2_wr_state = { 0 };
static WriteState* state = &uf2_wr_state;
static WriteState _wr_state = { 0 };
static WriteState* state = &_wr_state;
volatile bool _is_flashing = false;
static pstorage_handle_t _fat_psh = { .module_id = 0, .block_id = USER_FLASH_END } ;
@ -156,109 +154,6 @@ static uint32_t get_flash_size(void)
return flash_sz;
}
void fat_pstorage_cb(pstorage_handle_t * p_handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len)
{
if ( result != NRF_SUCCESS )
{
TU_ASSERT(false, );
}
if ( PSTORAGE_CLEAR_OP_CODE == op_code)
{
// erase complete start writing
_fat_psh.block_id = p_handle->block_id;
TU_ASSERT( pstorage_store(&_fat_psh, flashBuf, FLASH_PAGE_SIZE, 0), );
}
else if ( PSTORAGE_STORE_OP_CODE == op_code)
{
// write completes
_is_flashing = false;
// whole uf2 file is written, inform bootloader to update setting and reset
if ( state->numWritten >= state->numBlocks )
{
dfu_update_status_t update_status;
memset(&update_status, 0, sizeof(dfu_update_status_t ));
update_status.status_code = DFU_UPDATE_APP_COMPLETE;
update_status.app_crc = 0; // skip CRC checking with uf2 upgrade
update_status.app_size = state->numBlocks*256;
bootloader_dfu_update_process(update_status);
}
}
}
void ghostfat_init(void)
{
pstorage_module_param_t fat_psp = { .cb = fat_pstorage_cb};
pstorage_register(&fat_psp, &_fat_psh);
}
void flushFlash() {
if (flashAddr == NO_CACHE)
return;
if (firstFlush) {
firstFlush = false;
#if 0
if (sdRunning) {
// disable SD - we need sync access to flash, and we might be also overwriting the SD
nrf_sdh_disable_request();
nrf_dfu_settings_init(false);
}
s_dfu_settings.write_offset = 0;
s_dfu_settings.sd_size = 0;
s_dfu_settings.bank_layout = NRF_DFU_BANK_LAYOUT_DUAL;
s_dfu_settings.bank_current = NRF_DFU_CURRENT_BANK_0;
memset(&s_dfu_settings.bank_0, 0, sizeof(s_dfu_settings.bank_0));
memset(&s_dfu_settings.bank_1, 0, sizeof(s_dfu_settings.bank_1));
nrf_dfu_settings_write(NULL);
#endif
}
// int32_t sz = flashAddr + FLASH_PAGE_SIZE;
// if (s_dfu_settings.bank_0.image_size < sz)
// s_dfu_settings.bank_0.image_size = sz;
NRF_LOG_DEBUG("Flush at %x", flashAddr);
if (memcmp(flashBuf, (void *)flashAddr, FLASH_PAGE_SIZE) != 0) {
NRF_LOG_DEBUG("Write flush at %x", flashAddr);
_is_flashing = true;
// Writing to flash will be done in erase complete callback
_fat_psh.block_id = flashAddr;
TU_ASSERT ( pstorage_clear(&_fat_psh, FLASH_PAGE_SIZE), );
}
flashAddr = NO_CACHE;
}
void flash_write(uint32_t dst, const uint8_t *src, int len) {
uint32_t newAddr = dst & ~(FLASH_PAGE_SIZE - 1);
// hadWrite = true;
if (newAddr != flashAddr) {
flushFlash();
// 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;
flashAddr = newAddr;
memcpy(flashBuf, (void *)newAddr, FLASH_PAGE_SIZE);
}
memcpy(flashBuf + (dst & (FLASH_PAGE_SIZE - 1)), src, len);
}
#if 0
void uf2_timer(void *p_context) {
UNUSED_PARAMETER(p_context);
@ -289,7 +184,7 @@ void padded_memcpy(char *dst, const char *src, int len) {
/*------------------------------------------------------------------*/
/*
/* Read
*------------------------------------------------------------------*/
void read_block(uint32_t block_no, uint8_t *data) {
memset(data, 0, 512);
@ -352,6 +247,92 @@ void read_block(uint32_t block_no, uint8_t *data) {
}
}
/*------------------------------------------------------------------*/
/* Write UF2
*------------------------------------------------------------------*/
/** inform bootloader to update setting and reset */
static void uf2_write_complete(void)
{
dfu_update_status_t update_status;
memset(&update_status, 0, sizeof(dfu_update_status_t ));
update_status.status_code = DFU_UPDATE_APP_COMPLETE;
update_status.app_crc = 0; // skip CRC checking with uf2 upgrade
update_status.app_size = state->numBlocks*256;
bootloader_dfu_update_process(update_status);
}
static void fat_pstorage_cb(pstorage_handle_t * p_handle, uint8_t op_code, uint32_t result, uint8_t * p_data, uint32_t data_len)
{
if ( result != NRF_SUCCESS )
{
TU_ASSERT(false, );
}
if ( PSTORAGE_CLEAR_OP_CODE == op_code)
{
// erase complete start writing
_fat_psh.block_id = p_handle->block_id;
TU_ASSERT( pstorage_store(&_fat_psh, flashBuf, FLASH_PAGE_SIZE, 0), );
}
else if ( PSTORAGE_STORE_OP_CODE == op_code)
{
// write completes
_is_flashing = false;
// whole uf2 file is written, complete the write op
if ( state->numWritten >= state->numBlocks )
{
uf2_write_complete();
}
}
}
void flushFlash() {
if (flashAddr == NO_CACHE)
return;
if (firstFlush) {
firstFlush = false;
// Init pstorage
pstorage_module_param_t fat_psp = { .cb = fat_pstorage_cb};
pstorage_register(&fat_psp, &_fat_psh);
}
NRF_LOG_DEBUG("Flush at %x", flashAddr);
if (memcmp(flashBuf, (void *)flashAddr, FLASH_PAGE_SIZE) != 0) {
NRF_LOG_DEBUG("Write flush at %x", flashAddr);
_is_flashing = true;
// Writing to flash will be done in erase complete callback
_fat_psh.block_id = flashAddr;
TU_ASSERT ( pstorage_clear(&_fat_psh, FLASH_PAGE_SIZE), );
}
flashAddr = NO_CACHE;
}
void flash_write(uint32_t dst, const uint8_t *src, int len) {
uint32_t newAddr = dst & ~(FLASH_PAGE_SIZE - 1);
// hadWrite = true;
if (newAddr != flashAddr) {
flushFlash();
// 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;
flashAddr = newAddr;
memcpy(flashBuf, (void *)newAddr, FLASH_PAGE_SIZE);
}
memcpy(flashBuf + (dst & (FLASH_PAGE_SIZE - 1)), src, len);
}
/** Write an block
*
* @return number of bytes processed, only 3 following values
@ -416,6 +397,10 @@ int write_block(uint32_t block_no, uint8_t *data, bool quiet/*, WriteState *stat
// flush last blocks
flushFlash();
// no flashing due to last blocks is the same to contents on the flash already
// complete the write
if (!_is_flashing) uf2_write_complete();
}
}
NRF_LOG_DEBUG("wr %d=%d (of %d)", state->numWritten, bl->blockNo, bl->numBlocks);