This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Fstorage read and write at SDK11

I want to use fstorage to write data to a file. The File with three records: 0x1111="Tablet1", 0x2222="data: abcdef", 0x3333="26". How to implement it? Can anyone guide me?

Parents Reply Children
  • My source code at attach file.

    print messages as follows: Writing Record ID = 1 set write_flag Writing Record ID = 2 set write_flag Writing Record ID = 3 set write_flag Start searching... Found Record ID = 1 Data = 0x6c626154 0x 317465 Found Record ID = 2 Data = 0x 8fb5 0x20002bf0 0xfffffff9 Found Record ID = 3 Data = 0x 1a

    Second data (0x2222="data: abcdef") is always error. Can you guide me what it is?ble_pstorge.c

  • It seems some wrong at the function of fds_record_write(). It cannot write special character like ASCII 0x21 to 0x2f etc. How to fix it if I say correctly?

  • You are printing it as a hex number instead of a string. If your string is null terminated, simply use %s in NRF_LOG_PRINT(). Something like this: char * p_data; p_data = flash_record.p_data; NRF_LOG_PRINTF("Data: %s\r\n", (uint32_t)p_data);

  • I print it by hexadecimal code, because it cannot print non-ASCII data. If I set the second data to 0x2222="data: abcdef". I got the message as follows. Writing Record ID = 1 set write_flag Writing Record ID = 2 set write_flag Writing Record ID = 3 set write_flag Start searching... Found Record ID = 1 Tablet1 Found Record ID = 2 Data = ?? Found Record ID = 3 Data = 0x0000001a

    If I set the second data to 0x2222="data abcdef". I got the message as follows. Writing Record ID = 1 set write_flag Writing Record ID = 2 set write_flag Writing Record ID = 3 set write_flag Start searching... Found Record ID = 1 Tablet1 Found Record ID = 2 Data = data abcdef Found Record ID = 3 Data = 0x0000001a

    The two strings are only different at a character of ':' .ble_pstorge_1.c

  • I think the problem might be with your conversion from byte to words. The string "data: abcdef" is 12 characters long, plus the implicit string terminator character '\0', that's a total of 13 bytes. When you set length_words field of the chunk like this:

    record_chunk.length_words = (sizeof(device_data)+1)/4;

    My guess is that sizeof(device_data) will be 13 + 1 (which you add manually) / 4 equals to 3.5, which gets rounded down to 3, which is wrong since the string is 12 characters long but you need the 13th character (string terminator) to print correctly.

    It works with "data abcdef" because that's one character shorter, thus 12 + 1 / 4 = 3,25 which gets rounded down to 3, which is right in this case because that's exactly the length of the string including the terminator character (implicit).

    If you want approximate you should add 3. I would try like this: length_words = (sizeof(data) + 3) / 4

Related