K210 Introduction-Bare Metal Development (8:30) File System Supplement

Development board: K210 AIRV R3 version widora

Development environment: kendryte IDE official

Required reference documents: Standalone SDK Programming Guide v0.5.0, and Widora schematic

(1) In this episode, try the file system to open your own files

First read a.txt and get the length inside v_fileinfo.fsize

Then print out what you read

Comment out the following code first and test this separately 

Go one-stop

It reports an error. v_fileinfo.fsize can’t be initialized. I don’t want to use mem allocation, so I just use the fixed constant 128 (it is recommended to use dynamic allocation of memory, I don’t know how to use the fixed one)

The effect is good, I read it all out

Continue reading the next document

I just read it here and I don’t get information

The serial port does not print out, it proves that the Chinese cannot be recognized. I don’t know how to solve the problem of Chinese recognition. 

So can't use Chinese

(2) Then let's test and read mp3

I can read it too, but I don't know if the content is read.

Write to a.txt below (write directly without reading, wait for the reader to read)

Not bad this time, writing in Chinese is okay

Change the file names to all English

Read the picture below 

Can't read the content, the reason should be that the obtained is 8-bit, but the picture is not 8-bit but 32-bit, so it won't

 

Just went to learn a wave of dynamic memory calloc, remember to free to release the array after use

 

File system learning is complete, see you next time

 

Summary

Create folder

  char *dir = "jwdeng1995";
  ret = f_mkdir(dir);

Get the file size (other information can be displayed in FILINFO, only .fsize is used here)

  char *path = "jwdeng1995/test.txt";
  printf("/*******************sd read write test*******************/\n");
  uint32_t v_ret_len = 0;

  FILINFO v_fileinfo;
  if ((ret = f_stat(path, &v_fileinfo)) == FR_OK) {
    printf("%s length is %lld\n", path, v_fileinfo.fsize);
  } else {
    printf("%s fstat err [%d]\n", path, ret);
  }

Open the document and output the content


  if ((ret = f_open(&file, path, FA_READ)) == FR_OK) {
    char v_buf[128] = {0};
    ret = f_read(&file, (void *)v_buf, 128, &v_ret_len);
    if (ret != FR_OK) {
      printf("Read %s err[%d]\n", path, ret);
    } else {
      printf("Read :> %s %d bytes lenth\n", v_buf, v_ret_len);
    }
    f_close(&file);
  }

 Write documentation

  if ((ret = f_open(&file, path, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) {
    printf("open file %s err[%d]\n", path, ret);
    return ret;
  } else {
    printf("Open %s ok\n", path);
  }
  uint8_t hello[1024]= "阿斯打扫打扫打扫打扫打扫";
  ret = f_write(&file, hello, sizeof(hello), &v_ret_len);
  if (ret != FR_OK) {
    printf("Write %s err[%d]\n", path, ret);
  } else {
    printf("Write %d bytes to %s ok\n", v_ret_len, path);
  }
  f_close(&file);

 

Dynamic memory allocation space for array printing

  char *v_buf = calloc(v_fileinfo.fsize, sizeof(char));

//用完free掉
  free(v_buf);
  v_buf = NULL;

 

There is no code or engineering provided in this section.

Guess you like

Origin blog.csdn.net/jwdeng1995/article/details/108063316