static int set_data_flash_base(uint32_t u32DFBA) { uint32_t au32Config[2]; if (FMC_ReadConfig(au32Config, 2) < 0) { printf("\nRead User Config failed!\n"); return -1; } if ((!(au32Config[0] & 0x1)) && (au32Config[1] == u32DFBA)) return 0; FMC_ENABLE_CFG_UPDATE(); au32Config[0] &= ~0x1; au32Config[1] = u32DFBA; if (FMC_WriteConfig(au32Config, 2) < 0) return -1; printf("\nSet Data Flash base as 0x%x.\n", DATA_FLASH_TEST_BASE); // Perform chip reset to make new User Config take effect SYS->IPRST_CTL1 = SYS_IPRST_CTL1_CHIP_RST_Msk; return 0; }
/** * @brief Execute ISP command to write User Configuration. * @param[in] u32Config A two-word array. * u32Config[0] holds CONFIG0, while u32Config[1] holds CONFIG1. * @param[in] u32Count Avaliable word count in u32Config. * @return Success or not. * @retval 0 Success. * @retval -1 Invalid parameter. */ int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count) { FMC_ENABLE_CFG_UPDATE(); FMC_Erase(FMC_CONFIG_BASE); FMC_Write(FMC_CONFIG_BASE, u32Config[0]); FMC_Write(FMC_CONFIG_BASE+4, u32Config[1]); FMC_DISABLE_CFG_UPDATE(); return 0; }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { uint32_t au32Config[2]; SYS_Init(); UART0_Init(); printf("NuMicro USB composite device Sample.(HID Transfer and Mass storage)\n"); SYS_UnlockReg(); /* Enable FMC ISP function */ FMC_Open(); /* Check if Data Flash Size is 64K. If not, to re-define Data Flash size and to enable Data Flash function */ if (FMC_ReadConfig(au32Config, 2) < 0) return -1; if (((au32Config[0] & 0x01) == 1) || (au32Config[1] != DATA_FLASH_BASE) ) { FMC_ENABLE_CFG_UPDATE(); au32Config[0] &= ~0x1; au32Config[1] = DATA_FLASH_BASE; if (FMC_WriteConfig(au32Config, 2) < 0) return -1; FMC_ReadConfig(au32Config, 2); if (((au32Config[0] & 0x01) == 1) || (au32Config[1] != DATA_FLASH_BASE)) { printf("Error: Program Config Failed!\n"); /* Disable FMC ISP function */ FMC_Close(); SYS_LockReg(); return -1; } /* Reset Chip to reload new CONFIG value */ SYS->IPRST_CTL1 = SYS_IPRST_CTL1_CHIP_RST_Msk; } SYS_LockReg(); USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_Init(); NVIC_EnableIRQ(USBD_IRQn); USBD_Start(); while(1) { if (g_usbd_UsbConfig) MSC_ProcessCmd(); } }
/** * @brief Program config1 * * @param ulDATA: Data to be written to CONFIG1 * * @return None * * @details Program config1 */ void CONFIG1_Program(uint32_t ulDATA) { int32_t ret; FMC_ENABLE_CFG_UPDATE(); ret = DrvFMC_Write(CONFIG_START_ADDRESS + 0x04, ulDATA); if(ret < 0) { printf("Config1 Program Error\n"); while(1); } }
/** * @brief Config Erase * * @param None * * @return None * * @details Erase Config0 and Config1 */ void CONFIG_Erase(void) { int32_t ret; FMC_ENABLE_CFG_UPDATE(); ret = DrvFMC_Erase(CONFIG_START_ADDRESS); if(ret < 0) { printf("Config Erase Error\n"); while(1); } }