//========================================================================= //------------------------------------------------------------------------- __myevic__ void InitUSB() { USBD_SwReset(); if ( dfStatus.vcom ) { USBD_Open( &usbdVCOMDescriptors, usbdClassRequest+1, 0 ); } else if ( dfStatus.storage ) { USBD_Open( &usbdMSCDescriptors, usbdClassRequest+1, 0 ); USBD_SetConfigCallback( MSC_SetConfig ); MSC_Init(); } else { USBD_Open( &usbdDescriptors, usbdClassRequest+1, 0 ); } SetupEndpoints(); USBD_Start(); NVIC_EnableIRQ( USBD_IRQn ); if ( dfStatus.vcom ) { myputc = VCOM_Putc; } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { uint32_t u32TrimInit; uint8_t Str[9]; /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); printf("\n"); printf("+--------------------------------------------------------+\n"); printf("| NuMicro USB Micro Printer Sample Code |\n"); printf("+--------------------------------------------------------+\n"); USBD_Open(&gsInfo, PTR_ClassRequest, NULL); /* Endpoint configuration */ PTR_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); PB->PMD = 0x5000; // PB.6, PB.7 output mode 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 CLK_SysTickDelay(2000); // delay if(++Str[1] > 0x39) Str[1] = 0x30; // increase 1 to 10 than reset to 0 PB->DOUT ^= 0x40; // PB.6 } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); UART1_Init(); printf("\n\n"); printf("+------------------------------------------------------------+\n"); printf("| NuMicro USB Virtual COM Dual Port Sample Code |\n"); printf("+------------------------------------------------------------+\n"); USBD_Open(&gsInfo, VCOM_ClassRequest, NULL); /* Endpoint configuration */ VCOM_Init(); USBD_Start(); NVIC_EnableIRQ(USBD_IRQn); NVIC_EnableIRQ(UART0_IRQn); NVIC_EnableIRQ(UART1_IRQn); while(1) { VCOM_TransferData(); } }
//========================================================================= //------------------------------------------------------------------------- __myevic__ void InitUSB() { USBD_Open( &usbdDescriptors, hidClassRequest+1, 0 ); hidInit(); USBD_Start(); NVIC_EnableIRQ( USBD_IRQn ); }
/*---------------------------------------------------------------------------------------------------------*/ 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 u32TrimInit; /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); printf("\n"); printf("+--------------------------------------------------------------+\n"); printf("| NuMicro USB Virtual COM and HID Keyboard Sample Code |\n"); printf("+--------------------------------------------------------------+\n"); printf("If PB.15 = 0, just report it is key 'a'.\n"); /* Set PB.15 as Quasi-bidirectional mode */ PB->PMD = (PB->PMD & ~GPIO_PMD_PMD15_Msk) | (GPIO_PMD_QUASI << GPIO_PMD_PMD15_Pos); USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_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(); HID_UpdateKbData(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { uint32_t u32TrimInit; /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); UART0_Init(); printf("\n"); printf("+--------------------------------------------------------+\n"); printf("| NuMicro USB HID Keyboard Sample Code |\n"); printf("+--------------------------------------------------------+\n"); printf("If PB.15 = 0, just report it is key 'a'.\n"); USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_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); /* start to IN data */ g_u8EP2Ready = 1; 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 HID_UpdateKbData(); } }
/*---------------------------------------------------------------------------------------------------------*/ 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(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { SYS_Init(); UART0_Init(); printf("NuMicro USB HID Transfer via Control Transfer\n"); printf("Windows tool will Read and Write one pair of reports(periodic exchanges of reports).\n"); USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_Init(); NVIC_EnableIRQ(USBD_IRQn); USBD_Start(); while(1) { } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { /* This sample code demonstrate how to use HID interface to transfer data between PC and USB device. A demo window tool is also included in "WindowsTool" directory with this sample code. User can use it to test data transfer with this sample code. */ /* Unlock write-protected registers */ SYS_UnlockReg(); /* Init system and multi-function I/O */ SYS_Init(); /* Init UART for debug message */ UART0_Init(); printf("\n"); printf("+--------------------------------------------------------+\n"); printf("| NuMicro USB Composite Device Sample Code |\n"); printf("| (USB HID Transfer + Keyboard) |\n"); printf("+--------------------------------------------------------+\n"); printf("If PB.5 = 0, just report key 'a'.\n"); /* Set PB.5 as Quasi-bidirectional mode */ PB->MODE = (PB->MODE & ~GPIO_MODE_MODE5_Msk) | (GPIO_MODE_QUASI << GPIO_MODE_MODE5_Pos); /* Open USB controller */ USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Init Endpoint configuration for HID */ HID_Init(); /* Start USB device */ USBD_Start(); /* Enable USB device interrupt */ NVIC_EnableIRQ(USBD_IRQn); while(1) { HID_UpdateKbData(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { SYS_Init(); UART0_Init(); printf("NuMicro USB HID\n"); USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_Init(); NVIC_EnableIRQ(USBD_IRQn); USBD_Start(); while(1) { HID_UpdateMouseData(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { SYS_Init(); UART0_Init(); printf("NuMicro USB CDC One Port\n"); USBD_Open(&gsInfo, VCOM_ClassRequest, NULL); /* Endpoint configuration */ VCOM_Init(); NVIC_EnableIRQ(UART0_IRQn); NVIC_EnableIRQ(USBD_IRQn); USBD_Start(); while(1) { VCOM_TransferData(); } }
void USB_VirtualCOM_Init() { // Initialize state USB_VirtualCOM_txQueue.head = USB_VirtualCOM_txQueue.tail = NULL; USB_VirtualCOM_bulkInWaiting = 1; USB_VirtualCOM_rxBuffer.readIndex = USB_VirtualCOM_rxBuffer.writeIndex = 0; USB_VirtualCOM_rxBuffer.dataSize = 0; USB_VirtualCOM_rxCallbackPtr = NULL; USB_VirtualCOM_isAsync = 0; // Open USB USBD_Open(&USB_VirtualCOM_UsbdInfo, USB_VirtualCOM_HandleClassRequest, NULL); // Initialize setup packet buffer USBD->STBUFSEG = USB_VCOM_SETUP_BUF_BASE; // Control IN endpoint USBD_CONFIG_EP(USB_VCOM_CTRL_IN_EP, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | USB_VCOM_CTRL_IN_EP_NUM); USBD_SET_EP_BUF_ADDR(USB_VCOM_CTRL_IN_EP, USB_VCOM_CTRL_IN_BUF_BASE); // Control OUT endpoint USBD_CONFIG_EP(USB_VCOM_CTRL_OUT_EP, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | USB_VCOM_CTRL_OUT_EP_NUM); USBD_SET_EP_BUF_ADDR(USB_VCOM_CTRL_OUT_EP, USB_VCOM_CTRL_OUT_BUF_BASE); // Bulk IN endpoint USBD_CONFIG_EP(USB_VCOM_BULK_IN_EP, USBD_CFG_EPMODE_IN | USB_VCOM_BULK_IN_EP_NUM); USBD_SET_EP_BUF_ADDR(USB_VCOM_BULK_IN_EP, USB_VCOM_BULK_IN_BUF_BASE); // Bulk OUT endpoint USBD_CONFIG_EP(USB_VCOM_BULK_OUT_EP, USBD_CFG_EPMODE_OUT | USB_VCOM_BULK_OUT_EP_NUM); USBD_SET_EP_BUF_ADDR(USB_VCOM_BULK_OUT_EP, USB_VCOM_BULK_OUT_BUF_BASE); USBD_SET_PAYLOAD_LEN(USB_VCOM_BULK_OUT_EP, USB_VCOM_BULK_OUT_MAX_PKT_SIZE); // Interrupt IN endpoint USBD_CONFIG_EP(USB_VCOM_INT_IN_EP, USBD_CFG_EPMODE_IN | USB_VCOM_INT_IN_EP_NUM); USBD_SET_EP_BUF_ADDR(USB_VCOM_INT_IN_EP, USB_VCOM_INT_IN_BUF_BASE); // Start USB USBD_Start(); // Enable USB interrupt NVIC_EnableIRQ(USBD_IRQn); }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { SYS_Init(); UART0_Init(); printf("+-------------------------------------------------------+\n"); printf("| NuMicro USB MassStorage Sample Code |\n"); printf("+-------------------------------------------------------+\n"); printf("NnMicro USB MassStorage Start!\n"); USBD_Open(&gsInfo, MSC_ClassRequest, NULL); /* Endpoint configuration */ MSC_Init(); NVIC_EnableIRQ(USBD_IRQn); USBD_Start(); while(1) { if (g_usbd_UsbConfig) MSC_ProcessCmd(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { SYS_Init(); UART0_Init(); printf("NuMicro USB composite device Sample.(VCOM and HID Keyboard)\n"); printf("If PB.15 = 0, just report it is key 'a'.\n"); USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Endpoint configuration */ HID_Init(); NVIC_EnableIRQ(UART0_IRQn); NVIC_EnableIRQ(USBD_IRQn); USBD_Start(); while(1) { VCOM_TransferData(); HID_UpdateKbData(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main (void) { sysInitializeUART(); sysprintf("\n"); sysprintf("=========================\n"); sysprintf(" NUC970 USB VCOM \n"); sysprintf("=========================\n"); sysDisableCache(); sysInvalidCache(); sysSetMMUMappingMethod(MMU_DIRECT_MAPPING); sysEnableCache(CACHE_WRITE_BACK); sysInstallISR(HIGH_LEVEL_SENSITIVE|IRQ_LEVEL_1, USBD_IRQn, (PVOID)USBD_IRQHandler); /* enable CPSR I bit */ sysSetLocalInterrupt(ENABLE_IRQ); USBD_Open(&gsInfo, VCOM_ClassRequest, NULL); /* Endpoint configuration */ VCOM_Init(); sysEnableInterrupt(USBD_IRQn); /* Start transaction */ while(1) { if (USBD_IS_ATTACHED()) { USBD_Start(); break; } } while(1) { VCOM_TransferData(); } }
void main (void) { // set pll out 48Mhz, set cpu clock 48Mhz SYS_UnlockReg(); CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); CLK_SetCoreClock(48000000); CLK_EnableModuleClock(USBD_MODULE); CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV_USB(1)); DAP_Setup(); USBD_Open(&gsInfo, HID_ClassRequest, NULL); HID_Init(); balabala_hid_init(); USBD_Start(); NVIC_EnableIRQ(USBD_IRQn); while(1) { usbd_hid_process(); } }
/*---------------------------------------------------------------------------------------------------------*/ int32_t main(void) { int32_t i; /* This sample code is used to demo USB Audio Class + NAU8822. User can define PLAY_RATE in usbd_audio.h to support 48000Hz, 32000Hz, 16000Hz and 8000Hz. The audio is input from NAU8822 AUXIN. The audio is output by NAU8822 Headphone output. NAU8822 is connect with I2S1 (PA.4~PA.7) and controlled by I2C0 (PD.4, PD.5). NAU8822 clock source is also come from I2S1 (MCLK, PD.0). PC.1 is used to output clock (HCLK/8) to check HCLK frequency. */ /* Unlock Protected Regsiter */ SYS_UnlockReg(); /* Initial system & multi-function */ SYS_Init(); /* Initial UART0 for debug message */ UART0_Init(); printf("\n"); printf("+-------------------------------------------------------+\n"); printf("| NuMicro USB Audio CODEC Sample Code |\n"); printf("+-------------------------------------------------------+\n"); /* Init I2C0 to access NAU8822 */ I2C0_Init(); /* Initialize NAU8822 codec */ WAU8822_Setup(); I2S_Open(SPI1, I2S_MODE_SLAVE, PLAY_RATE, I2S_DATABIT_16, I2S_STEREO, I2S_FORMAT_I2S); /* Set MCLK and enable MCLK */ I2S_EnableMCLK(SPI1, 12000000); /* Fill dummy data to I2S TX for start I2S iteration */ for(i = 0; i < 4; i++) I2S_WRITE_TX_FIFO(SPI1, 0); /* Start I2S play iteration */ I2S_EnableInt(SPI1, I2S_FIFO_TXTH_INT_MASK | I2S_FIFO_RXTH_INT_MASK); USBD_Open(&gsInfo, UAC_ClassRequest, (SET_INTERFACE_REQ)UAC_SetInterface); /* Endpoint configuration */ UAC_Init(); USBD_Start(); NVIC_EnableIRQ(USBD_IRQn); NVIC_EnableIRQ(SPI1_IRQn); /* SPI (I2S) interrupt has higher frequency then USBD interrupt. Therefore, we need to set SPI (I2S) with higher priority to avoid SPI (I2S) interrupt pending too long time when USBD interrupt happen. */ NVIC_SetPriority(USBD_IRQn, 3); NVIC_SetPriority(SPI1_IRQn, 2); while(SYS->PDID) { uint8_t ch; uint32_t u32Reg, u32Data; extern int32_t kbhit(void); /* Adjust codec sampling rate to synch with USB. The adjustment range is +-0.005% */ AdjFreq(); /* Set audio volume according USB volume control settings */ VolumnControl(); /* User can change audio codec settings by I2C at run-time if necessary */ if(!kbhit()) { printf("\nEnter codec setting:\n"); // Get Register number ch = getchar(); u32Reg = ch - '0'; ch = getchar(); u32Reg = u32Reg * 10 + (ch - '0'); printf("%d\n", u32Reg); // Get data ch = getchar(); u32Data = (ch >= '0' && ch <= '9') ? ch - '0' : ch - 'a' + 10; ch = getchar(); u32Data = u32Data * 16 + ((ch >= '0' && ch <= '9') ? ch - '0' : ch - 'a' + 10); ch = getchar(); u32Data = u32Data * 16 + ((ch >= '0' && ch <= '9') ? ch - '0' : ch - 'a' + 10); printf("%03x\n", u32Data); I2C_WriteWAU8822(u32Reg, u32Data); } } }
int main() { CLEAR_WRITE(); SET_STOPMOTOR(); CLEAR_SCANMEDIA(); CLEAR_MEDIASET(); CLEAR_READY(); //setup led and button gpio GPIO_SetMode(LED_G_PORT, LED_G_PIN, GPIO_PMD_OUTPUT); GPIO_SetMode(LED_R_PORT, LED_R_PIN, GPIO_PMD_OUTPUT); GPIO_SetMode(SWITCH_PORT, SWITCH_PIN, GPIO_PMD_INPUT); GPIO_SetMode(IRDATA_PORT, IRDATA_PIN, GPIO_PMD_INPUT); LED_GREEN(0); LED_RED(1); detect_board_version(); /* Unlock protected registers */ SYS_UnlockReg(); SYS_Init(); /* Lock protected registers */ SYS_LockReg(); UART0_Init(); SPI_Init(); TIMER_Open(TIMER0, TIMER_CONTINUOUS_MODE, 6000000); TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, TRANSFER_RATE * 2); TIMER_Open(TIMER3, TIMER_PERIODIC_MODE, TRANSFER_RATE * 2); TIMER_EnableInt(TIMER1); TIMER_EnableInt(TIMER3); /* Open USB controller */ USBD_Open(&gsInfo, HID_ClassRequest, NULL); /* Init Endpoint configuration for HID */ HID_Init(); /* Start USB device */ USBD_Start(); /* Enable USB device interrupt */ NVIC_EnableIRQ(USBD_IRQn); LED_GREEN(1); LED_RED(0); printf("\n\nnuc123-fdsemu v%d.%02d build %d started. Compiled on "__DATE__" at "__TIME__"\n",version / 100,version % 100,BUILDNUM); printf("--CPU @ %0.3f MHz\n", (double)SystemCoreClock / 1000000.0f); printf("--SPI0 @ %0.3f MHz\n", (double)SPI_GetBusClock(SPI0) / 1000000.0f); printf("--SPI1 @ %0.3f MHz\n", (double)SPI_GetBusClock(SPI1) / 1000000.0f); printf("--Detected board version: %d (config = %d %d %d)\n", boardver,PA12,PA13,PA14); NVIC_SetPriority(USBD_IRQn,2); NVIC_SetPriority(TMR1_IRQn,1); NVIC_SetPriority(TMR2_IRQn,0); NVIC_SetPriority(TMR3_IRQn,0); NVIC_SetPriority(GPAB_IRQn,0); NVIC_SetPriority(EINT0_IRQn,0); flash_init(); sram_init(); fds_init(); print_block_info(0); while(1) { if(havepacket) { havepacket = 0; // process_send_feature(epdata,64); } console_tick(); fds_tick(); } }
/*---------------------------------------------------------------------------------------------------------*/ 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(); } }