【iCore4 双核心板_ARM】例程十四:FATFS实验——文件操作

实验现象:

核心代码:

int main(void)
{    
    int i,j;
    FIL file;
    FATFS fatfs;
    static FRESULT res;
    unsigned char write_buffer[512];                                      
    unsigned char read_buffer[512];                                       
    unsigned int counter;
    
    system_clock.initialize();                                                                                        
    led.initialize();                                                                                                
    usart6.initialize(115200);                                                                                  
    usart6.printf("\x0c");                                                             
    usart6.printf("\033[1;32;40m");                                                 
    usart6.printf("\r\nHello, I am iCore4!\r\n\r\n");                                     
    if(BSP_SD_Init() != 0){
        usart6.printf("SD ERROR!\r\n");
        while(1){
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
            HAL_Delay(500);
        }
    }else{
        usart6.printf("SD Initialize successful!\r\n");
    }

    HAL_Delay(500);
    res = f_mount(&fatfs,"0:",1);
    if(res != RES_OK){
        usart6.printf("f_mount error!\r\n");
        while(1){
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
            HAL_Delay(500);            
        }
    }else{
        usart6.printf("f_mount successful!\r\n");
    }

    res = f_open(&file,"0:/SD¿¨²âÊÔ.txt",FA_READ | FA_WRITE | FA_OPEN_ALWAYS);//´ò¿ªÇý¶¯Æ÷0ÉϵÄÔ´Îļþ
    if(res != RES_OK){
        usart6.printf("f_open error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }        
    }else{
        usart6.printf("f_open successful!\r\n");
    }
    
    res = f_lseek(&file,0);                                               
    if(res != RES_OK){
        usart6.printf("f_lseek error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }                
    }else{
        usart6.printf("f_lseek successful!\r\n");
    }
    
    for(i = 0;i < 512;i++){
        write_buffer[i] = i % 256;
    }    

    res = f_write(&file,write_buffer,512,&counter);                       //½«»º³åÆ÷ÖеÄÄÚÈÝдÈëÔ´Îļþ
    if(res != RES_OK || counter != 512){
        usart6.printf("f_write error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }    
    }else{
        usart6.printf("f_write successful!\r\n");    
    }    

    res = f_lseek(&file,0);                                                  
    if(res != RES_OK){
        usart6.printf("f_lseek error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }                
    }else{
        usart6.printf("f_lseek successful!\r\n");
    }

    res = f_read(&file,read_buffer,512,&counter);
    if(res != RES_OK || counter != 512){
        usart6.printf("f_read error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_ON;
            HAL_Delay(500);
            LED_RED_OFF;
        }    
    }else{
        usart6.printf("f_read successful!\r\n");
    }
    f_close(&file);    
  
    usart6.printf("read data:\r\n");
    for(i = 0;i < 32;i++){
        for(j = 0; j < 16; j ++)
            usart6.printf(" %02X",read_buffer[i*16+j]);
        usart6.printf("\r\n");
    }    
    
    while(1){
        LED_GREEN_ON;
        HAL_Delay(500);
        LED_GREEN_OFF;
        HAL_Delay(500);            
    }    
}

源代码下载链接:

链接:https://pan.baidu.com/s/1jJDh134 密码:3pyh

iCore4链接:

posted @ 2017-08-30 08:47  XiaomaGee  阅读(688)  评论(0编辑  收藏  举报