static int SetIAPBoot(void) { uint32_t au32Config[2]; uint32_t u32CBS; /* Read current boot mode */ u32CBS = (FMC->ISPSTA & FMC_ISPSTA_CBS_Msk) >> FMC_ISPSTA_CBS_Pos; if(u32CBS & 1) { /* Modify User Configuration when it is not in IAP mode */ FMC_ReadConfig(au32Config, 2); if(au32Config[0] & 0x40) { FMC_EnableConfigUpdate(); au32Config[0] &= ~0x40; FMC_Erase(FMC_CONFIG_BASE); FMC_WriteConfig(au32Config, 2); // Perform chip reset to make new User Config take effect SYS_ResetChip(); } } return 0; }
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; }
static int SetDataFlashBase(uint32_t u32DFBA) { uint32_t au32Config[2]; /* Read current User Configuration */ FMC_ReadConfig(au32Config, 1); /* Just return when Data Flash has been enabled */ if(!(au32Config[0] & 0x1)) return 0; /* Enable User Configuration Update */ FMC_EnableConfigUpdate(); /* Erase User Configuration */ FMC_Erase(FMC_CONFIG_BASE); /* Write User Configuration to Enable Data Flash */ au32Config[0] &= ~0x1; au32Config[1] = u32DFBA; if(FMC_WriteConfig(au32Config, 2)) return -1; printf("\nSet Data Flash base as 0x%x.\n", FMC_ReadDataFlashBaseAddr()); /* Perform chip reset to make new User Config take effect */ SYS->IPRST0 |= SYS_IPRST0_CHIPRST_Msk; return 0; }
static int SetDataFlashBase(uint32_t u32DFBA) { uint32_t au32Config[2]; /* Read current User Configuration */ FMC_ReadConfig(au32Config, 1); /* Just return when Data Flash has been enabled */ if(!(au32Config[0] & 0x1)) return 0; /* Enable User Configuration Update */ FMC_EnableConfigUpdate(); /* Erase User Configuration */ FMC_Erase(FMC_CONFIG_BASE); /* Write User Configuration to Enable Data Flash */ /* Note: DFVSEN = 1, DATA Flash Size is 4K bytes DFVSEN = 0, DATA Flash Size is based on CONFIG1 */ au32Config[0] &= ~(CONFIG0_DFEN | CONFIG0_DFVSEN); au32Config[1] = u32DFBA; if(FMC_WriteConfig(au32Config, 2)) return -1; printf("\nSet Data Flash base as 0x%x.\n", FMC_ReadDataFlashBaseAddr()); /* Perform chip reset to make new User Config take effect */ SYS->IPRSTC1 |= SYS_IPRSTC1_CHIP_RST_Msk; return 0; }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { uint32_t au32Config[2]; /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); printf("\n\n"); printf("+-------------------------------------------------------------+\n"); printf("| NuMicro USB Virtual COM and MassStorage Sample Code |\n"); printf("+-------------------------------------------------------------+\n"); /* Enable FMC ISP function */ FMC_Open(); /* Check User Configuration. If not match, to re-define Data Flash size and to enable Data Flash function. */ FMC_ReadConfig(au32Config, 2); if(((au32Config[0] & 0x01) == 1) || (au32Config[1] != DATA_FLASH_BASE)) { FMC_EnableConfigUpdate(); FMC_Erase(FMC_CONFIG0_ADDR); 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(); return -1; } /* Reset Chip to reload new CONFIG value */ SYS->IPRST0 = SYS_IPRST0_CHIPRST_Msk; } printf("NuMicro USB MassStorage Start!\n"); USBD_Open(&gsInfo, VCOM_MSC_ClassRequest, NULL); USBD_SetConfigCallback(MSC_SetConfig); /* Endpoint configuration */ VCOM_MSC_Init(); USBD_Start(); NVIC_EnableIRQ(USBD_IRQn); NVIC_EnableIRQ(UART0_IRQn); while(1) { VCOM_TransferData(); MSC_ProcessCmd(); } }
/*---------------------------------------------------------------------------------------------------------*/ 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(); } }
bool flash_io_driver_initialize(void) { uint32_t desiredConfig0 = 0xF8FFFF7E; uint32_t desiredConfig1 = APPLICATION_DATA_START; uint32_t config0; uint32_t config1; bool fail = false; critical_section_enter(); UNLOCKREG(); FMC_Init(); FMC_EnableAPUpdate(); LOCKREG(); if(FMC_Read(CONFIG0, &config0) != E_FMC_OK) { fail = true; } if(FMC_Read(CONFIG1, &config1) != E_FMC_OK) { fail = true; } if(fail || (desiredConfig0 != config0) || (desiredConfig1 != config1)) { FMC_EnableConfigUpdate(); if(FMC_WriteConfig(config0, config1) != E_FMC_OK) { fail = true; } else { fail = false; } FMC_DisableConfigUpdate(); } critical_section_exit(); return fail == false; }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { uint32_t u32TrimInit; uint32_t au32Config[2]; /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); printf("\n"); printf("+-------------------------------------------------------------+\n"); printf("| NuMicro USB Virtual COM and MassStorage Sample Code |\n"); printf("+-------------------------------------------------------------+\n"); /* 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_EnableConfigUpdate(); au32Config[0] &= ~0x1; au32Config[1] = DATA_FLASH_BASE; FMC_Erase(CONFIG_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(); return -1; } /* Reset Chip to reload new CONFIG value */ SYS->IPRSTC1 = SYS_IPRSTC1_CHIP_RST_Msk; } printf("NuMicro USB MassStorage Start!\n"); /* Open USB controller */ USBD_Open(&gsInfo, VCOM_MSC_ClassRequest, NULL); USBD_SetConfigCallback(MSC_SetConfig); /* Endpoint configuration */ VCOM_MSC_Init(); USBD_Start(); #if CRYSTAL_LESS /* Backup init trim */ u32TrimInit = M32(TRIM_INIT); /* Enable USB crystal-less */ SYS->HIRCTCTL = 0x201 | (31 << SYS_HIRCTCTL_BOUNDARY_Pos); #endif NVIC_EnableIRQ(USBD_IRQn); NVIC_EnableIRQ(UART02_IRQn); while(1) { #if CRYSTAL_LESS /* Re-start crystal-less when any error found */ if (SYS->HIRCTSTS & (SYS_HIRCTSTS_CLKERIF_Msk | SYS_HIRCTSTS_TFAILIF_Msk)) { SYS->HIRCTSTS = SYS_HIRCTSTS_CLKERIF_Msk | SYS_HIRCTSTS_TFAILIF_Msk; if((u32TrimInit < 0x1E6) || (u32TrimInit > 0x253)) /* Re-enable crystal-less */ SYS->HIRCTCTL = 0x201 | (1 << SYS_HIRCTCTL_BOUNDARY_Pos); else /* Re-enable crystal-less */ SYS->HIRCTCTL = 0x201 | (31 << SYS_HIRCTCTL_BOUNDARY_Pos); //printf("USB trim fail. Just retry. SYS->HIRCTSTS = 0x%x, SYS->HIRCTCTL = 0x%x\n", SYS->HIRCTSTS, SYS->HIRCTCTL); } #endif VCOM_TransferData(); MSC_ProcessCmd(); } }