- /******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V2.00
- * $Revision: 3 $
- * $Date: 15/04/16 2:18p $
- * [url=home.php?mod=space&uid=247401]@brief[/url] NUC230_240 Series SPI Driver Sample Code
- *
- * @note
- * Copyright (C) 2014 Nuvoton Technology Corp. All rights reserved.
- *****************************************************************************/
- #include <stdio.h>
- #include "NUC230_240.h"
- #include "SPI_FLASH.h"
- #include "diskio.h"
- #include "ff.h"
- /* Function prototype declaration */
- void SYS_Init(void);
- unsigned int MidDid;
- volatile UINT Timer = 0; /* Performance timer (1kHz increment) */
- DWORD acc_size; /* Work register for fs command */
- WORD acc_files, acc_dirs;
- FILINFO Finfo;
- FATFS FatFs[_DRIVES]; /* File system object for logical drive */
- char Line[256]; /* Console input buffer */
- #if _USE_LFN
- char Lfname[512];
- #endif
- BYTE Buff[1024] ; /* Working buffer */
- uint8_t data[512];
- uint32_t sd_size;
-
- void put_rc (FRESULT rc)
- {
- const TCHAR *p =
- _T("OK\0DISK_ERR\0INT_ERR\0NOT_READY\0NO_FILE\0NO_PATH\0INVALID_NAME\0")
- _T("DENIED\0EXIST\0INVALID_OBJECT\0WRITE_PROTECTED\0INVALID_DRIVE\0")
- _T("NOT_ENABLED\0NO_FILE_SYSTEM\0MKFS_ABORTED\0TIMEOUT\0LOCKED\0")
- _T("NOT_ENOUGH_CORE\0TOO_MANY_OPEN_FILES\0");
- uint32_t i;
- for (i = 0; (i != (UINT)rc) && *p; i++) {
- while(*p++) ;
- }
- printf(_T("rc=%u FR_%s\n"), (UINT)rc, p);
- }
- /* ------------- */
- /* Main function */
- /* ------------- */
- int main(void)
- {
- uint32_t p1, s1, s2;
- FATFS *fs; /* Pointer to file system object */
- DIR dir; /* Directory object */
- FILINFO Finfo;
- char *ptr="\";
- FRESULT res;
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init system, peripheral clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- // SYS_LockReg();
- /* Configure UART0: 115200, 8-bit word, no parity bit, 1 stop bit. */
- UART_Open(UART0, 115200);
-
- SpiInit();
- /* Read MID & DID */
- MidDid = SpiReadMidDid();
- printf("\nMID and DID = %x\n\r", MidDid);
-
- res = (FRESULT)disk_initialize(0);
- if(res)
- {
- put_rc(res);
- printf("\n\nDon't initialize SPI flash\n");
- }
- res =f_mount(0, &FatFs[0]);
- if(res)
- {
- put_rc(res);
- printf("Don't mount file system\n");
- }
- #if 0
- res = f_mkfs(0, 0, _MAX_SS);
- if(res)
- {
- put_rc(res);
- printf("Don't format\n");
- }
- #endif
- // List direct information
- put_rc(f_opendir(&dir, ptr));
- p1 = s1 = s2 = 0;
- for(;;)
- {
- res = f_readdir(&dir, &Finfo);
- if ((res != FR_OK) || !Finfo.fname[0]) break;
- if (Finfo.fattrib & AM_DIR){
- s2++;
- } else {
- s1++; p1 += Finfo.fsize;
- }
- printf("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n",
- (Finfo.fattrib & AM_DIR) ? 'D' : '-',
- (Finfo.fattrib & AM_RDO) ? 'R' : '-',
- (Finfo.fattrib & AM_HID) ? 'H' : '-',
- (Finfo.fattrib & AM_SYS) ? 'S' : '-',
- (Finfo.fattrib & AM_ARC) ? 'A' : '-',
- (Finfo.fdate >> 9) + 1980, (Finfo.fdate >> 5) & 15, Finfo.fdate & 31,
- (Finfo.ftime >> 11), (Finfo.ftime >> 5) & 63, Finfo.fsize, Finfo.fname);
- }
- printf("%4u File(s),%10lu bytes total\n%4u Dir(s)", s1, p1, s2);
- if (f_getfree(ptr, (DWORD*)&p1, &fs) == FR_OK)
- printf(", %10lu bytes free\n", p1 * fs->csize * 512);
-
- while(1);
- }
- void SYS_Init(void)
- {
-
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable Internal RC 22.1184 MHz clock */
- CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
- /* Waiting for Internal RC clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
-
- /* Enable external 12 MHz XTAL */
- CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
- /* Waiting for clock ready */
- CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
- /* Set core clock rate as 72 MHz from PLL */
- CLK_SetCoreClock(72000000);
- /* Select HXT as the clock source of UART0 */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
- /* Enable UART peripheral clock */
- CLK_EnableModuleClock(UART0_MODULE);
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set PB multi-function pins for UART0 RXD and TXD */
- SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
- /* Update System Core Clock */
- /* User can use SystemCoreClockUpdate() to calculate SystemCoreClock and CyclesPerUs automatically. */
- SystemCoreClockUpdate();
- }
|