- /******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * @brief
- * Use embedded data flash as storage to implement a USB Mass-Storage device.
- * @note
- * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
- ******************************************************************************/
- #include <stdio.h>
- #include "M451Series.h"
- #include "massstorage.h"
- /*--------------------------------------------------------------------------*/
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable Internal RC 22.1184MHz clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Waiting for Internal RC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
- /* Enable external XTAL 12MHz clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
- /* Waiting for external XTAL clock ready */
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
- /* Set Flash Access Delay */
- FMC->FTCTL |= FMC_FTCTL_FOM_Msk;
- /* Set core clock */
- CLK_SetCoreClock(72000000);
- /* Enable module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- CLK_EnableModuleClock(USBD_MODULE);
- /* Select module clock source */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
- CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV0_USB(3));
- /* Enable USB LDO33 */
- SYS->USBPHY = SYS_USBPHY_LDO33EN_Msk;
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set GPD multi-function pins for UART0 RXD and TXD, and Clock Output */
- SYS->GPD_MFPL = SYS_GPD_MFPL_PD0MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD | SYS_GPD_MFPL_PD6MFP_CLKO;
- /* Enable CLKO (PD.6) for monitor HCLK. CLKO = HCLK/8 Hz */
- CLK_EnableCKO(CLK_CLKSEL1_CLKOSEL_HCLK, 2, 0);
- }
- uint32_t u32SpiMidDid=0;
- unsigned char SrcArray[256];
- unsigned char DestArray[256];
- /*---------------------------------------------------------------------------------------------------------*/
- /* Main Function */
- /*---------------------------------------------------------------------------------------------------------*/
- int32_t main(void)
- {
- uint32_t u32ByteCount;
- /* Unlock protected registers */
- SYS_UnlockReg();
- SYS_Init();
- /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
- UART_Open(UART0, 115200);
- printf("+-------------------------------------------------------+\n");
- printf("| NuMicro USB MassStorage Sample Code |\n");
- printf("+-------------------------------------------------------+\n");
- SYS_UnlockReg();
- /* Fill the Source Data and clear Destination Data Buffer */
- for(u32ByteCount = 0; u32ByteCount < 256; u32ByteCount++)
- {
- SrcArray[u32ByteCount] = u32ByteCount;
- DestArray[u32ByteCount] = 0;
- }
- SpiInit(SPI2);
- /* read MID & DID */
- u32SpiMidDid = SpiReadMidDid();
- // SpiChipErase();
- // SpiWrite(0,256, (uint32_t)SrcArray);
- // SpiRead(0, 256, (uint32_t)DestArray);
- // while(1);
-
-
- printf("NuMicro USB MassStorage Start!\n");
- USBD_Open(&gsInfo, MSC_ClassRequest, NULL);
- USBD_SetConfigCallback(MSC_SetConfig);
- /* Endpoint configuration */
- MSC_Init();
- USBD_Start();
- NVIC_EnableIRQ(USBD_IRQn);
- while(1)
- {
- MSC_ProcessCmd();
- }
- }
- /*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/
|