【iCore1S 双核心板_ARM】例程十四:FATFS实验——读写文件

实验现象:

核心代码:

int main(void)
{

  /* USER CODE BEGIN 1 */
    int i;
    int j;
    FIL file;
    FATFS fatfs;                                                             //Âß¼­Çý¶¯Æ÷µÄ¹¤×÷Çø
    static FRESULT res;                                                      //FRESULTº¯Êý¹«¹²½á¹û´úÂë
    unsigned char write_buffer[512];                                         //дÎļþ»º³åÇø
    unsigned char read_buffer[512];                                          //¶ÁÎļþ»º³åÇø
    unsigned int counter;

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SDIO_SD_Init();
  MX_FATFS_Init();
  MX_USART1_UART_Init();
  SystemClock_Config();

  /* USER CODE BEGIN 2 */
    usart1.printf("\x0c");                                   //ÇåÆÁ
    usart1.printf("\033[1;32;40m");                          //½«×ÖÌåÉèÖóÉÂÌÉ«
    usart1.printf("\r\n\r\nhello! I am iCore1S!\r\n\r\n\r\n");

    HAL_Delay(100);
        res = f_mount(&fatfs,(TCHAR const*)SDPath,1);          //¹ÒÔØSD¿¨
    if(res != RES_OK){
                usart1.printf("f_mount error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_TOGGLE;
        }
    }
            else{
        usart1.printf("f_mount successful!\r\n");
    }
    res = f_open(&file,"0:/test.txt",FA_READ | FA_WRITE | FA_OPEN_ALWAYS); //´ò¿ªÇý¶¯Æ÷0ÉϵÄÔ´Îļþ
    if(res != RES_OK){
        usart1.printf("f_open error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_TOGGLE;
        }        
    }else{
        usart1.printf("f_open successful!\r\n");
    }
    
    res = f_lseek(&file,0);                                                     //Òƶ¯Ð´Ö¸Õëµ½ÎļþÊ×
    if(res != RES_OK){
        usart1.printf("f_lseek error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_TOGGLE;
        }                
    }else{
        usart1.printf("f_lseek successful!\r\n");
    }
    
    for(i = 0;i < 512;i++){
        write_buffer[i] = i % 256;
    }
    res = f_write(&file,write_buffer,sizeof(write_buffer),&counter); //½«»º³åÆ÷ÖеÄÄÚÈÝдÈëÔ´Îļþ
    if(res != RES_OK || counter != 512){
        usart1.printf("f_write error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_TOGGLE;
        }    
    }else{
        usart1.printf("f_write successful!\r\n");    
    }
    
    res = f_lseek(&file,0);                                               //Òƶ¯¶ÁÖ¸Õëµ½ÎļþÊ×
    if(res != RES_OK){
        usart1.printf("f_lseek error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_TOGGLE;
        }                
    }else{
        usart1.printf("f_lseek successful!\r\n");
    }
    
    res = f_read(&file,read_buffer,sizeof(read_buffer),&counter);
    if(res != RES_OK || counter != 512){
        usart1.printf("f_read error!\r\n");
        while(1){
            HAL_Delay(500);
            LED_RED_TOGGLE;
        }    
    }else{
        usart1.printf("f_read successful!\r\n");
    }
    f_close(&file);                                                 //¹Ø±ÕÔ´Îļþ
    

    usart1.printf("read data:\r\n");
    for(i = 0;i < 32;i++){
        for(j = 0; j < 16; j ++)
            usart1.printf(" %02X",read_buffer[i*16+j]);
            usart1.printf("\r\n");
    }

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
        HAL_Delay(500);
        LED_GREEN_TOGGLE;
  }
  /* USER CODE END 3 */

}

源代码下载链接:

链接:http://pan.baidu.com/s/1c2rkKuc 密码:k4e9

posted @ 2017-09-11 08:36  XiaomaGee  阅读(352)  评论(0编辑  收藏  举报