Kaynağa Gözat

Working mk20dx256vlh7 usb flash support

bringup
Jacob Alexander 9 yıl önce
ebeveyn
işleme
d211790124
2 değiştirilmiş dosya ile 58 ekleme ve 88 silme
  1. 28
    2
      Bootloader/flash.c
  2. 30
    86
      Bootloader/main.c

+ 28
- 2
Bootloader/flash.c Dosyayı Görüntüle

// ----- Local Includes ----- // ----- Local Includes -----


#include "mchck.h" #include "mchck.h"
#include "debug.h"








int flash_erase_sector( uintptr_t addr ) int flash_erase_sector( uintptr_t addr )
{ {
#ifdef FLASH_DEBUG
// Debug
print("Erasing Sector: address(");
printHex( addr );
printNL(")");
#endif

if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 ) if ( addr < (uintptr_t)&_app_rom && flash_ALLOW_BRICKABLE_ADDRESSES != 0x00023420 )
return (-1); return (-1);
FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR; FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;


int flash_program_section_longwords( uintptr_t addr, size_t num_words ) int flash_program_section_longwords( uintptr_t addr, size_t num_words )
{ {
#ifdef FLASH_DEBUG
// Debug
print("Programming Sector: address(");
printHex( addr );
print(") longwords(");
printHex( num_words );
printNL(")");
#endif

FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION; FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
FTFL.fccob.program_section.addr = addr; FTFL.fccob.program_section.addr = addr;
FTFL.fccob.program_section.num_words = num_words; FTFL.fccob.program_section.num_words = num_words;


int flash_program_section_phrases( uintptr_t addr, size_t num_phrases ) int flash_program_section_phrases( uintptr_t addr, size_t num_phrases )
{ {
#ifdef FLASH_DEBUG
// Debug
print("Programming Sector: address(");
printHex( addr );
print(") phrases(");
printHex( num_phrases );
printNL(")");
#endif

FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION; FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
FTFL.fccob.program_section.addr = addr; FTFL.fccob.program_section.addr = addr;
FTFL.fccob.program_section.num_words = num_phrases; FTFL.fccob.program_section.num_words = num_phrases;


int flash_program_sector( uintptr_t addr, size_t len ) int flash_program_sector( uintptr_t addr, size_t len )
{ {
if ( len != FLASH_SECTOR_SIZE )
if ( len != USB_DFU_TRANSFER_SIZE )
return 1; return 1;


#if defined(_mk20dx128vlf5_) #if defined(_mk20dx128vlf5_)


void *flash_get_staging_area( uintptr_t addr, size_t len ) void *flash_get_staging_area( uintptr_t addr, size_t len )
{ {
if ( (addr & (FLASH_SECTOR_SIZE - 1)) != 0 || len != FLASH_SECTOR_SIZE )
if ( (addr & (USB_DFU_TRANSFER_SIZE - 1)) != 0 || len != USB_DFU_TRANSFER_SIZE )
return (NULL); return (NULL);
return (FlexRAM); return (FlexRAM);
} }

+ 30
- 86
Bootloader/main.c Dosyayı Görüntüle

FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits FTFL.fccob.read_1s_section.num_words = 250; // 2000 kB / 64 bits
int retval = ftfl_submit_cmd(); int retval = ftfl_submit_cmd();


#ifdef FLASH_DEBUG
print( NL ); print( NL );
print("Block "); print("Block ");
printHex( sector ); printHex( sector );
print(" Erased: "); print(" Erased: ");
printHex( retval ); printHex( retval );
print( NL ); print( NL );
#endif


// Display sector // Display sector
for ( size_t line = 0; pos < end - 24; line++ ) for ( size_t line = 0; pos < end - 24; line++ )
{ {
static int last = 0; static int last = 0;


#ifdef FLASH_DEBUG
// Debug
print("Setup Write: offset(");
printHex( off );
print(") len(");
printHex( len );
print(") last(");
printHex( last );
printNL(")");
#endif

if ( len > sizeof(staging) ) if ( len > sizeof(staging) )
return (DFU_STATUS_errADDRESS); return (DFU_STATUS_errADDRESS);


last = 0; last = 0;
if ( last && len != 0 ) if ( last && len != 0 )
return (DFU_STATUS_errADDRESS); return (DFU_STATUS_errADDRESS);
if ( len != FLASH_SECTOR_SIZE )
if ( len != USB_DFU_TRANSFER_SIZE )
{ {
last = 1; last = 1;
memset( staging, 0xff, sizeof(staging) ); memset( staging, 0xff, sizeof(staging) );
if ( len == 0 ) if ( len == 0 )
return (DFU_STATUS_OK); return (DFU_STATUS_OK);


target = flash_get_staging_area(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE);
target = flash_get_staging_area( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE );
if ( !target ) if ( !target )
return (DFU_STATUS_errADDRESS); return (DFU_STATUS_errADDRESS);
memcpy( target, buf, len ); memcpy( target, buf, len );


// Depending on the error return a different status // Depending on the error return a different status
switch ( flash_program_sector(off + (uintptr_t)&_app_rom, FLASH_SECTOR_SIZE) )
switch ( flash_program_sector( off + (uintptr_t)&_app_rom, USB_DFU_TRANSFER_SIZE ) )
{ {
/* /*
case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error case FTFL_FSTAT_RDCOLERR: // Flash Read Collision Error
uart_serial_setup(); uart_serial_setup();
printNL( NL "Bootloader DFU-Mode" ); printNL( NL "Bootloader DFU-Mode" );


// Bootloader Enter Reasons
print(" RCM_SRS0 - ");
printHex( RCM_SRS0 & 0x60 );
print( NL " RCM_SRS1 - ");
printHex( RCM_SRS1 & 0x02 );
print( NL " _app_rom - ");
printHex( (uint32_t)_app_rom );
print( NL " Soft Rst - " );
printHex( memcmp( (uint8_t*)&VBAT, sys_reset_to_loader_magic, sizeof(sys_reset_to_loader_magic) ) == 0 );
print( NL );

// XXX REMOVEME // XXX REMOVEME
/* /*
GPIOB_PDDR |= (1<<16); GPIOB_PDDR |= (1<<16);
GPIOC_PCOR |= (1<<3); GPIOC_PCOR |= (1<<3);
*/ */


/*
// Read Firmware 1 Status
FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
FTFL.fccob.read_1s_block.addr = (uintptr_t)&_app_rom;
FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;

int retval = ftfl_submit_cmd();
print("Firmware Erase Status: ");
printHex( retval );
print( NL );


// Read Bootloader 1 Status
FTFL.fccob.read_1s_block.fcmd = FTFL_FCMD_READ_1s_BLOCK;
FTFL.fccob.read_1s_block.addr = (uintptr_t)&_bootloader;
FTFL.fccob.read_1s_block.margin = FTFL_MARGIN_NORMAL;

retval = ftfl_submit_cmd();
print("Bootloader Erase Status: ");
printHex( retval );
print( NL );
*/

/*
// Program First Longword of firmware
FTFL.fccob.program_longword.fcmd = FTFL_FCMD_PROGRAM_LONGWORD;
FTFL.fccob.program_longword.addr = (uintptr_t)&_app_rom;
FTFL.fccob.program_longword.data_be[0] = 0x1;
FTFL.fccob.program_longword.data_be[1] = 0x2;
FTFL.fccob.program_longword.data_be[2] = 0x4;
FTFL.fccob.program_longword.data_be[3] = 0x8;
int retval = ftfl_submit_cmd();
print("Write Longword Status: ");
printHex( retval );
print( NL );
*/

/*
// Erase Sector
FTFL.fccob.erase.fcmd = FTFL_FCMD_ERASE_SECTOR;
FTFL.fccob.erase.addr = (uintptr_t)&_app_rom;
int retval = ftfl_submit_cmd();
print("Erase Status: ");
printHex( retval );
print( NL );

// Prepare FlexRAM
FTFL.fccob.set_flexram.fcmd = FTFL_FCMD_SET_FLEXRAM;
FTFL.fccob.set_flexram.flexram_function = FTFL_FLEXRAM_RAM;
retval = ftfl_submit_cmd();
print("Set FlexRAM Status: ");
printHex( retval );
print( NL );

// Write to FlexRAM
memset( FlexRAM, 0xB4, 1000 );
memset( &FlexRAM[1000], 0xE3, 1000 );

// Program Sector
FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom;
FTFL.fccob.program_section.num_words = 128;
//FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
retval = ftfl_submit_cmd();
print("Program Sector1 Status: ");
printHex( retval );
print( NL );

FTFL.fccob.program_section.fcmd = FTFL_FCMD_PROGRAM_SECTION;
FTFL.fccob.program_section.addr = (uintptr_t)&_app_rom + 0x400;
FTFL.fccob.program_section.num_words = 128;
//FTFL.fccob.program_section.num_words = 250; // 2000 kb / 64 bits
retval = ftfl_submit_cmd();
print("Program Sector2 Status: ");
printHex( retval );
print( NL );

for ( uint8_t sector = 0; sector < 1; sector++ )
//sector_print( &_bootloader, sector, 16 );
#ifdef FLASH_DEBUG
for ( uint8_t sector = 0; sector < 3; sector++ )
sector_print( &_app_rom, sector, 16 ); sector_print( &_app_rom, sector, 16 );
print( NL ); print( NL );
*/
#endif


flash_prepare_flashing(); flash_prepare_flashing();

//uint32_t *position = &_app_rom;
usb_init( &dfu_device ); usb_init( &dfu_device );

for (;;) for (;;)
{ {
usb_poll(); usb_poll();