/** * @brief Main entry point * @return Nothing */ int main(void) { SPIFIobj *obj = &spifi_obj; uint32_t spifi_clk_mhz; SPIFIopers opers; int ret; spifi_rom_init(spifi); /* Initialize the board & LEDs for error indication */ Board_Init(); /* Since this code runs from SPIFI no special initialization required here */ prepare_write_data(data_buffer, sizeof(data_buffer)); spifi_clk_mhz = Chip_Clock_GetRate(CLK_MX_SPIFI) / 1000000; /* Typical time tCS is 20 ns min, we give 200 ns to be on safer side */ if (spifi_init(obj, spifi_clk_mhz / 5, S_RCVCLK | S_FULLCLK, spifi_clk_mhz)) { DEBUGSTR("Error initializing SPIFI interface!\r\n"); Board_LED_Set(1, 1); goto end_prog; } /* Prepare the operations structure */ memset(&opers, 0, sizeof(SPIFIopers)); opers.dest = (char *) SPIFI_WRITE_SECTOR_OFFSET; opers.length = sizeof(data_buffer); /* opers.options = S_VERIFY_PROG; */ /* NOTE: All interrupts must be disabled before calling program as * any triggered interrupts might attempt to run a code from SPIFI area */ ret = spifi_program(obj, (char *) data_buffer, &opers); if (ret) { DEBUGOUT("Error 0x%x: Programming of data buffer to SPIFI Failed!\r\n", ret); Board_LED_Set(1, 1); goto end_prog; } DEBUGSTR("SPIFI Programming successful!\r\n"); if (verify_spifi_data((uint8_t *) SPIFI_WRITE_SECTOR_ADDRESS, sizeof(data_buffer))) { DEBUGSTR("Error verifying the SPIFI data\r\n"); Board_LED_Set(1, 1); goto end_prog; } Board_LED_Set(0, 1); DEBUGSTR("SPIFI Data verified!\r\n"); end_prog: while(1) {__WFI();} }
int32_t flash_program(uint32_t addr, const uint8_t *data, uint32_t len) { SPIFIopers spifi; // add offset if (addr>=FLASH_BEGIN && addr<=FLASH_END) addr += FLASH_OFFSET; memset((void *)&spifi, 0, sizeof(spifi)); spifi.dest = (char *)addr; spifi.length = len; spifi.scratch = NULL; spifi.options = S_VERIFY_PROG; if (spifi_program(&g_spifi, (char *)data, &spifi)) return -1; return 0; }