data of fstorage written data and reading back are not the same

I use nrf_fstorage_write() to write data to fstorage. And then I use nrf_fstorage_read() to read data. But data of fstorage written data and reading back are not the same.

Data written log messages is as follows.

0> <debug> app: addr=0x7D000, wlen=24
 0>
 0> <debug> app: pData w
 0>
 0> <debug> app: pData[0]=0xDE
 0>
 0> <debug> app: pData[1]=0xDD
 0>
 0> <debug> app: pData[2]=0x0
 0>
 0> <debug> app: pData[3]=0x0
 0>
 0> <debug> app: pData[4]=0x0
 0>
 0> <debug> app: pData[5]=0x0
 0>
 0> <debug> app: pData[6]=0x0
 0>
 0> <debug> app: pData[7]=0x0
 0>
 0> <debug> app: pData[8]=0x0
 0>
 0> <debug> app: pData[9]=0x0
 0>
 0> <debug> app: pData[10]=0x0
 0>
 0> <debug> app: pData[11]=0x0
 0>
 0> <debug> app: pData[12]=0x0
 0>
 0> <debug> app: pData[13]=0x0
 0>
 0> <debug> app: pData[14]=0x0
 0>
 0> <debug> app: pData[15]=0x0
 0>
 0> <debug> app: pData[16]=0x0
 0>
 0> <debug> app: pData[17]=0x0
 0>
 0> <debug> app: pData[18]=0x0
 0>
 0> <debug> app: pData[19]=0x0
 0>
 0> <debug> app: pData[20]=0x0
 0>
 0> <debug> app: pData[21]=0x0
 0>
 0> <debug> app: pData[22]=0x0
 0>
 0> <debug> app: pData[23]=0x0

Data reading log messages is as follows.

0> <debug> app: addr=0x7D000, len=24
 0>
 0> <debug> app: data r
 0>
 0> <debug> app: pData[0]=0x1C
 0>
 0> <debug> app: pData[1]=0x8A
 0>
 0> <debug> app: pData[2]=0x0
 0>
 0> <debug> app: pData[3]=0x20
 0>
 0> <debug> app: pData[4]=0xE9
 0>
 0> <debug> app: pData[5]=0xFF
 0>
 0> <debug> app: pData[6]=0xFF
 0>
 0> <debug> app: pData[7]=0xFF
 0>
 0> <debug> app: pData[8]=0x98
 0>
 0> <debug> app: pData[9]=0x2
 0>
 0> <debug> app: pData[10]=0x0
 0>
 0> <debug> app: pData[11]=0x0
 0>
 0> <debug> app: pData[12]=0x61
 0>
 0> <debug> app: pData[13]=0xD0
 0>
 0> <debug> app: pData[14]=0xB
 0>
 0> <debug> app: pData[15]=0xA
 0>
 0> <debug> app: pData[16]=0x0
 0>
 0> <debug> app: pData[17]=0x40
 0>
 0> <debug> app: pData[18]=0x0
 0>
 0> <debug> app: pData[19]=0x40
 0>
 0> <debug> app: pData[20]=0x4D
 0>
 0> <debug> app: pData[21]=0x2A
 0>
 0> <debug> app: pData[22]=0x3
 0>
 0> <debug> app: pData[23]=0x0
 0>

Parents
  • # SEGGER J-Link RTT Viewer V6.32i Terminal Log File
    # Compiled: 15:22:50 on Jul 24 2018
    # Logging started @ 24 Jun 2022 10:51:57
     0> 
     0> <info> app_timer: RTC: initialized.
     0> <debug> app: NOR_DATA_PERIOD=819
     0> 
     0> <debug> app: NOR_RECORD_PERIOD=8192
     0> 
     0> <info> app: bsp_button_longkey_handler 0.
     0> <info> app: POWER_ON.
     0> <info> app: External devices initializing start
     0> <info> app: Reg 0 20
     0> <info> app: Reg 9 A
     0> <info> app: Reg A D
     0> <info> app: Reg 1 B
     0> <info> app: Reg 2 D
     0> <info> app: Reg D F
     0> <info> app: Reg E 13
     0> <info> app: Reg 36 10
     0> <info> app: Reg 37 13
     0> <info> app: Reg 5 11
     0> <info> app: Reg 6 13
     0> <info> app: Reg F 15
     0> <info> app: Reg 10 19
     0> <info> app: Reg 3 16
     0> <info> app: Reg 4 19
     0> <info> app: Reg 7 17
     0> <info> app: Reg 8 19
     0> <info> app: Reg 11 1B
     0> <info> app: Reg 12 1F
     0> <info> app: Reg 43 0
     0> <info> app: Reg 44 0
     0> <info> app: Reg B 1D
     0> <info> app: Reg C 1F
     0> <info> app: Reg 13 21
     0> <info> app: Reg 14 25
     0> <info> app: Reg 1D 3F
     0> <info> app: Reg 64 0
     0> <info> app: Reg 65 27
     0> <info> app: Reg 66 0
     0> <info> app: Reg 67 27
     0> <info> app: Reg 68 0
     0> <info> app: Reg 69 27
     0> <info> app: Reg 52 2C
     0> <info> app: Reg 53 2C
     0> <info> app: Reg 6A 32
     0> <info> app: Reg 6B 38
     0> <info> app: Reg 1E 101
     0> <info> app: Reg 20 3
     0> <info> app: Reg 21 2
     0> <info> app: Reg 22 802084
     0> <info> app: Reg 23 124218
     0> <info> app: Reg 4B F
     0> <info> app: Reg 50 18
     0> <info> app: Reg 31 20
     0> <info> app: Reg 39 5
     0> <info> app: Reg 4E 0
     0> <info> app: Reg 4E 8
     0> <info> app: Reg 45 8
     0> <info> app: Reg 46 13
     0> <info> app: Reg 47 14
     0> <info> app: Reg 48 1F
     0> <info> app: Reg 0 21
     0> <info> app: External devices initializing finish
     0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
     0> <info> app: Connected.
     0> <info> app: PHY update request.
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested a data length of 251 bytes.
     0> <debug> nrf_ble_gatt: Updating data length to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: Data length updated to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: max_rx_octets: 27
     0> <debug> nrf_ble_gatt: max_tx_octets: 27
     0> <debug> nrf_ble_gatt: max_rx_time: 2120
     0> <debug> nrf_ble_gatt: max_tx_time: 2120
     0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
     0> <info> app: bsp_button_longkey_handler 1.
     0> <info> app: Disconnected.
     0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
     0> <info> app: Connected.
     0> <info> app: PHY update request.
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested a data length of 251 bytes.
     0> <debug> nrf_ble_gatt: Updating data length to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: Data length updated to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: max_rx_octets: 27
     0> <debug> nrf_ble_gatt: max_tx_octets: 27
     0> <debug> nrf_ble_gatt: max_rx_time: 2120
     0> <debug> nrf_ble_gatt: max_tx_time: 2120
     0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
     0> <info> app: Disconnected.
     0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
     0> <info> app: Connected.
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested an ATT MTU of 527 bytes.
     0> <debug> nrf_ble_gatt: Updating ATT MTU to 185 bytes (desired: 185) on connection 0x0.
     0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested a data length of 251 bytes.
     0> <debug> nrf_ble_gatt: Updating data length to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: Data length updated to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: max_rx_octets: 27
     0> <debug> nrf_ble_gatt: max_tx_octets: 27
     0> <debug> nrf_ble_gatt: max_rx_time: 2120
     0> <debug> nrf_ble_gatt: max_tx_time: 2120
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  4D                     |M       
     0> <info> app: M
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x0
     0> <debug> app: data[2]=0x0
     0> <debug> app: data[3]=0x0
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  45                     |E       
     0> <info> app: E
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x0
     0> <debug> app: data[3]=0x0
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  41                     |A       
     0> <info> app: A
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x0
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  53                     |S       
     0> <info> app: S
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  55                     |U       
     0> <info> app: U
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x55
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  52                     |R       
     0> <info> app: R
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x55
     0> <debug> app: data[5]=0x52
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  45                     |E       
     0> <info> app: E
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x55
     0> <debug> app: data[5]=0x52
     0> <debug> app: data[6]=0x45
     0> <info> app: 
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=0, addr=0x7D000
     0> 
     0> <debug> app: state=0
     0> 
     0> <debug> app: START E
     0> 
     0> <debug> app: START x
     0> 
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=0, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: START E
     0> 
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x0
     0> 
     0> <debug> app: pData[4]=0x0
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x0
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x0
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: START x
     0> 
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=0, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: START E
     0> 
     0> <debug> app: START x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: addr=0x7D000, len=24
     0> 
     0> <debug> app: data r
     0> 
     0> <debug> app: pData[0]=0x1C
     0> 
     0> <debug> app: pData[1]=0x8A
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x20
     0> 
     0> <debug> app: pData[4]=0xE9
     0> 
     0> <debug> app: pData[5]=0xFF
     0> 
     0> <debug> app: pData[6]=0xFF
     0> 
     0> <debug> app: pData[7]=0xFF
     0> 
     0> <debug> app: pData[8]=0x98
     0> 
     0> <debug> app: pData[9]=0x2
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x61
     0> 
     0> <debug> app: pData[13]=0xD0
     0> 
     0> <debug> app: pData[14]=0xB
     0> 
     0> <debug> app: pData[15]=0xA
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x40
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x40
     0> 
     0> <debug> app: pData[20]=0x4D
     0> 
     0> <debug> app: pData[21]=0x2A
     0> 
     0> <debug> app: pData[22]=0x3
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: record.offsetStart=0x20E9FF
     0> <debug> app: record.offsetEnd=0xFFFF9802
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: record.offsetEnd=0x205A
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0xFF
     0> 
     0> <debug> app: pData[3]=0xFF
     0> 
     0> <debug> app: pData[4]=0xFF
     0> 
     0> <debug> app: pData[5]=0xD4
     0> 
     0> <debug> app: pData[6]=0xFF
     0> 
     0> <debug> app: pData[7]=0xFF
     0> 
     0> <debug> app: pData[8]=0xFF
     0> 
     0> <debug> app: pData[9]=0xD4
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: addr=0x7D000, len=24
     0> 
     0> <debug> app: data r
     0> 
     0> <debug> app: pData[0]=0x1C
     0> 
     0> <debug> app: pData[1]=0x8A
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x20
     0> 
     0> <debug> app: pData[4]=0x0
     0> 
     0> <debug> app: pData[5]=0x2
     0> 
     0> <debug> app: pData[6]=0x0
     0> 
     0> <debug> app: pData[7]=0x21
     0> 
     0> <debug> app: pData[8]=0x2
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0xA1
     0> 
     0> <debug> app: pData[13]=0x90
     0> 
     0> <debug> app: pData[14]=0x47
     0> 
     0> <debug> app: pData[15]=0xC
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0xAD
     0> 
     0> <debug> app: pData[21]=0x29
     0> 
     0> <debug> app: pData[22]=0x3
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: record.offsetStart=0x200002
     0> <debug> app: record.offsetEnd=0x210200
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: record.offsetEnd=0x30B4
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0x40
     0> 
     0> <debug> app: pData[3]=0x0
     0> 
     0> <debug> app: pData[4]=0x40
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x40
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x40
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: addr=0x7D000, len=24
     0> 
     0> <debug> app: data r
     0> 
     0> <debug> app: pData[0]=0x1C
     0> 
     0> <debug> app: pData[1]=0x8A
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x20
     0> 
     0> <debug> app: pData[4]=0x4
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x13
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x2
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x61
     0> 
     0> <debug> app: pData[13]=0xD0
     0> 
     0> <debug> app: pData[14]=0xB
     0> 
     0> <debug> app: pData[15]=0xA
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x40
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x40
     0> 
     0> <debug> app: pData[20]=0x4D
     0> 
     0> <debug> app: pData[21]=0x2A
     0> 
     0> <debug> app: pData[22]=0x3
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: record.offsetStart=0x200400
     0> <debug> app: record.offsetEnd=0x13000200
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: record.offsetEnd=0x41C4
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0x40
     0> 
     0> <debug> app: pData[3]=0x0
     0> 
     0> <debug> app: pData[4]=0x40
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x40
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x40
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
    
    #include "app_timer.h"
    #include "error.h"
    #include "fds_flash.h"
    #include "fstorage.h"
    #include "nrf_delay.h"
    #include "nrf_fstorage.h"
    #include "nrf_fstorage_sd.h"
    #include "nrf_fstorage_nvmc.h"
    #include "nrf_log.h"
    #include "nrf_soc.h"
    #include "spi_flash.h"
    
    #define FSTORAGE_PAGE_NUM    3
    #define FSTORAGE_WAIT_CNT    1000
    
    
    
    static bool _gFstorageInitialized = false;
    static bool _gFstorageEraseDone   = true;
    static bool _gFstorageWriteDone   = true;
    static bool _gFstoragePageDirty   = true;
    static fstorage_state_t _gFstorageState = FSTORAGE_STATE_UNKNOWN;
    
    static void _fstorageEvtHandler(nrf_fstorage_evt_t * p_evt);
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t _gFstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = _fstorageEvtHandler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
         * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
         * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
         * last page of flash available to write data. */
        .start_addr = 0x7D000,
        .end_addr   = 0x80000,
    };
    
    static void _fstorageEvtHandler(nrf_fstorage_evt_t * pEvt) {
        NRF_LOG_DEBUG("Evt e\n");
        switch (pEvt->id) {
            case NRF_FSTORAGE_EVT_WRITE_RESULT:
            {
                NRF_LOG_DEBUG("Evt w\n");
                _gFstorageWriteDone = true;
                _gFstorageState     = FSTORAGE_STATE_WRITTEN;
                if (pEvt->result != NRF_SUCCESS) {
                   NRF_LOG_WARNING("fstorage writing failed %d bytes at address 0x%x.",
                                pEvt->len, pEvt->addr);
                } else {
                    NRF_LOG_DEBUG("NRF_FSTORAGE_EVT_WRITE_RESULT ok");
                    _gFstoragePageDirty = true;
                }
            } break;
    
            case NRF_FSTORAGE_EVT_ERASE_RESULT:
            {
                NRF_LOG_DEBUG("Evt er\n");
                _gFstorageEraseDone = true;
                _gFstorageState     = FSTORAGE_STATE_ERASED;
                if (pEvt->result != NRF_SUCCESS) {
                    NRF_LOG_WARNING("fstorage erased failed %d page from address 0x%x.",
                                     pEvt->len, pEvt->addr);
                } else {
                    NRF_LOG_DEBUG("NRF_FSTORAGE_EVT_ERASE_RESULT ok");
                    _gFstoragePageDirty = false;
                }
            } break;
    
            default:
                break;
        }
         NRF_LOG_DEBUG("Evt x\n");
    }
    
    static uint32_t _fstorageGetEndAddr(void) {
        const uint32_t bootloaderAddr = BOOTLOADER_ADDRESS;
        const uint32_t pageSz         = NRF_FICR->CODEPAGESIZE;
        const uint32_t codeSz         = NRF_FICR->CODESIZE;
    
        return (bootloaderAddr != 0xFFFFFFFF ?
                bootloaderAddr : (codeSz * pageSz));
    }
    
    static uint32_t _fstorageGetStartAddr(void) {
        const uint32_t eraseUnit = _gFstorage.p_flash_info->erase_unit;
        const uint32_t endAddr = _fstorageGetEndAddr();
        const uint32_t bootloaderAddr = BOOTLOADER_ADDRESS;
        uint32_t startAddr;
        if (bootloaderAddr == 0xFFFFFFFF) {
            startAddr = endAddr - FDS_VIRTUAL_PAGES * eraseUnit; 
        } else {
            startAddr = bootloaderAddr - FSTORAGE_PAGE_NUM * eraseUnit;
        }
    
        NRF_LOG_DEBUG("endAddr=0x%x", endAddr);
        NRF_LOG_DEBUG("startAddr=0x%x", startAddr);
        return startAddr;
    }
    static bool _fstorageIsEraseNeeded(void) {
        return _gFstoragePageDirty;
    }    
    
    static uint32_t _fstorageRoundUpU32(const uint32_t len) {
        if (len % sizeof(uint32_t)) {
            return (len + sizeof(uint32_t) - (len % sizeof(uint32_t)));
        }
    
        return len;
    }
    
    static void power_manage(void)
    {
    #ifdef SOFTDEVICE_PRESENT
        (void) sd_app_evt_wait();
    #else
        __WFE();
    #endif
    }
    
    int _fstorageWait(const uint32_t timeOutCnt) {
    #if 0
        NRF_LOG_DEBUG("timeOutCnt=%u\n", timeOutCnt);
        const uint32_t startCnt   = app_timer_cnt_get();
        NRF_LOG_DEBUG("startCnt=%u\n", startCnt);
        uint32_t endCnt;
        uint32_t diffCnt;
    
        do {
            endCnt           = app_timer_cnt_get();
            diffCnt          = app_timer_cnt_diff_compute(endCnt, startCnt);
            if (diffCnt >= timeOutCnt) {
                NRF_LOG_WARNING("_fstorage wait timeout\n");
                return ERROR_FSTORAGE_WAIT_TIMEOUT;
            }
    
            if (_gFstorageEraseDone && _gFstorageWriteDone) {
                break;
            }
        } while (true);
    
        NRF_LOG_DEBUG("endCnt=%u\n", endCnt);
    
        //while (nrf_fstorage_is_busy(&_gFstorage))
        while (!_gFstorageEraseDone || !_gFstorageWriteDone) {
            NRF_LOG_DEBUG("pm e\n");
            power_manage();
            NRF_LOG_DEBUG("pm x\n");
        }
    #endif
    		return 0;
        
    }
    
    int _fstorageRead(const uint32_t addr, const uint32_t len, uint8_t* data) {
        if (!_gFstorageInitialized) {
            return ERROR_FSTORAGE_UNINITIALIZED;
        }   
    
        const ret_code_t errCode = nrf_fstorage_read(&_gFstorage, addr, data, len);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_fstorage_read() failed 0x%x\n", errCode);
            return ERROR_FSTORAGE_READ_FAIL;
        }
    
        NRF_LOG_DEBUG("addr=0x%x, len=%u\n", addr, len);
        recordPrintData("data r", data);
    
        return 0;
    }
    
    int _fstorageWrite(const uint32_t addr, const uint32_t len, void const* pData) {
        if (!_gFstorageInitialized) {
            return ERROR_FSTORAGE_UNINITIALIZED;
        }
    
        if (!_gFstorageEraseDone) {
            return ERROR_FSTORAGE_ERASE_ON_GOING;
        }
    
        if (!_gFstorageWriteDone) {
            return ERROR_FSTORAGE_WRITE_ON_GOING;
        }
    
        _gFstorageWriteDone = false;
        _gFstorageState     = FSTORAGE_STATE_WRITING;
        const uint32_t wlen = _fstorageRoundUpU32(len);
        NRF_LOG_DEBUG("addr=0x%x, wlen=%u\n", addr, wlen);
        recordPrintData("pData w", pData);
        const ret_code_t retCode = nrf_fstorage_write(&_gFstorage, addr, pData, wlen, NULL);
        if (retCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_fstorage_write() failed 0x%x", retCode);
            return ERROR_FSTORAGE_WRITE_FAIL;
        }
    
        _fstorageWait(FSTORAGE_WAIT_CNT);
    
        return 0;
    }
    
    
    int fstorageInit(void) {
        // initializing variables
        _gFstorageInitialized = false;
        _gFstorageEraseDone   = true;
        _gFstorageWriteDone   = true;
        _gFstoragePageDirty   = true;
        _gFstorageState = FSTORAGE_STATE_UNKNOWN;
    
        const ret_code_t errCode = nrf_fstorage_init(&_gFstorage, &nrf_fstorage_sd, NULL);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_fstorage_init() failed 0x%x", errCode);
            return ERROR_FSTORAGE_INITIALIZE_FAIL;
        }
    
        _gFstorageInitialized = true;
    
        return 0;
    }    
    
    int fstorageErase(const uint32_t addr, const uint32_t pagesCnt) {
        if (!_gFstorageInitialized) {
            return ERROR_FSTORAGE_UNINITIALIZED;
        }
    
        if (!_gFstorageEraseDone) {
            return ERROR_FSTORAGE_ERASE_ON_GOING;
        }
    
        if (!_gFstorageWriteDone) {
            return ERROR_FSTORAGE_WRITE_ON_GOING;
        }    
    
        _gFstorageEraseDone = false;
        _gFstorageState     = FSTORAGE_STATE_ERASED;
        const ret_code_t errCode = nrf_fstorage_erase(&_gFstorage, addr, pagesCnt, NULL);
        if (errCode != NRF_SUCCESS) { 
            NRF_LOG_WARNING("nrf_fstorage_erase() failed 0x%x", errCode);
            return ERROR_FSTORAGE_ERASE_FAIL;
        }
    
        _fstorageWait(FSTORAGE_WAIT_CNT);
        
        return 0;
    }
    
    int fstorageReadRecord(const uint32_t addr, record_t* pRecord) {
        uint8_t data[RECORD_LEN] = {0};
        if (_fstorageRead(addr, RECORD_LEN, data) != 0) {
            return ERROR_FDS_READ_FAIL;
        }
    
        data2Record(data, pRecord);
    
        return 0;
    }    
    
    int fstorageUpdateRecord(const record_data_type_t type, const record_t* pRecord) {
        const uint32_t addr = _fstorageGetStartAddr();
        NRF_LOG_DEBUG("type=%d, addr=0x%x\n", type, addr);
        const fstorage_state_t state = fstorageGetState();
        NRF_LOG_DEBUG("state=%d\n", state);
        switch (type) {
            case RECORD_DATA_TYPE_START:
            {  
                NRF_LOG_DEBUG("START E\n");
                if ((state == FSTORAGE_STATE_UNKNOWN)) {
                    fstorageErase(addr, 1);    
                } else if (state == FSTORAGE_STATE_ERASED) {    
                    record_t record;
                    record.offsetStart = 0;
                    record.offsetEnd   = 0;
                    fstorageWriteRecord(addr, &record);
                }    
                NRF_LOG_DEBUG("START x\n");
            }    
            break;
    
            case RECORD_DATA_TYPE_END:
            {
                NRF_LOG_DEBUG("END E\n");
                record_t record;
    
                if ((state == FSTORAGE_STATE_UNKNOWN) || (state == FSTORAGE_STATE_WRITTEN)) {
                    if (fstorageReadRecord(addr, &record) != 0) {
                        NRF_LOG_WARNING("fstorageReadRecord(0x%x) failed\n", addr);
                        return ERROR_FSTORAGE_READ_FAIL;
                    }
                    NRF_LOG_DEBUG("record.offsetStart=0x%x", record.offsetStart);
                    NRF_LOG_DEBUG("record.offsetEnd=0x%x",   record.offsetEnd);
                    fstorageErase(addr, 1);
                } else if (state == FSTORAGE_STATE_ERASED) {
                    record.offsetEnd = pRecord->offsetEnd;
                    NRF_LOG_DEBUG("record.offsetEnd=0x%x",   record.offsetEnd);
                    fstorageWriteRecord(addr, &record);
                }    
                NRF_LOG_DEBUG("END X");
            }   
            break;    
        }    
    
        return 0;
    }
    
    int fstorageWriteRecord(const uint32_t addr, record_t* pRecord) {
        uint8_t data[RECORD_LEN] = {0};
        record2Data(pRecord, data);
        if (_fstorageWrite(addr, RECORD_LEN, data) != 0) {
            return ERROR_FSTORAGE_WRITE_FAIL;
        }
    
        return 0;
    }
    
    bool fstorageGetInitialized(void) {
        return _gFstorageInitialized;
    }
    
    bool fstorageGetEraseDone(void) {
        return _gFstorageEraseDone;
    }
    
    bool fstorageGetWriteDone(void) {
        return _gFstorageWriteDone;
    }
    
    bool fstorageGetPageDirty(void) {
        return _gFstoragePageDirty;
    }
    
    fstorage_state_t fstorageGetState(void) {
        return _gFstorageState;
    }
    
    
    
    
    
    7851.fstorage.h

Reply
  • # SEGGER J-Link RTT Viewer V6.32i Terminal Log File
    # Compiled: 15:22:50 on Jul 24 2018
    # Logging started @ 24 Jun 2022 10:51:57
     0> 
     0> <info> app_timer: RTC: initialized.
     0> <debug> app: NOR_DATA_PERIOD=819
     0> 
     0> <debug> app: NOR_RECORD_PERIOD=8192
     0> 
     0> <info> app: bsp_button_longkey_handler 0.
     0> <info> app: POWER_ON.
     0> <info> app: External devices initializing start
     0> <info> app: Reg 0 20
     0> <info> app: Reg 9 A
     0> <info> app: Reg A D
     0> <info> app: Reg 1 B
     0> <info> app: Reg 2 D
     0> <info> app: Reg D F
     0> <info> app: Reg E 13
     0> <info> app: Reg 36 10
     0> <info> app: Reg 37 13
     0> <info> app: Reg 5 11
     0> <info> app: Reg 6 13
     0> <info> app: Reg F 15
     0> <info> app: Reg 10 19
     0> <info> app: Reg 3 16
     0> <info> app: Reg 4 19
     0> <info> app: Reg 7 17
     0> <info> app: Reg 8 19
     0> <info> app: Reg 11 1B
     0> <info> app: Reg 12 1F
     0> <info> app: Reg 43 0
     0> <info> app: Reg 44 0
     0> <info> app: Reg B 1D
     0> <info> app: Reg C 1F
     0> <info> app: Reg 13 21
     0> <info> app: Reg 14 25
     0> <info> app: Reg 1D 3F
     0> <info> app: Reg 64 0
     0> <info> app: Reg 65 27
     0> <info> app: Reg 66 0
     0> <info> app: Reg 67 27
     0> <info> app: Reg 68 0
     0> <info> app: Reg 69 27
     0> <info> app: Reg 52 2C
     0> <info> app: Reg 53 2C
     0> <info> app: Reg 6A 32
     0> <info> app: Reg 6B 38
     0> <info> app: Reg 1E 101
     0> <info> app: Reg 20 3
     0> <info> app: Reg 21 2
     0> <info> app: Reg 22 802084
     0> <info> app: Reg 23 124218
     0> <info> app: Reg 4B F
     0> <info> app: Reg 50 18
     0> <info> app: Reg 31 20
     0> <info> app: Reg 39 5
     0> <info> app: Reg 4E 0
     0> <info> app: Reg 4E 8
     0> <info> app: Reg 45 8
     0> <info> app: Reg 46 13
     0> <info> app: Reg 47 14
     0> <info> app: Reg 48 1F
     0> <info> app: Reg 0 21
     0> <info> app: External devices initializing finish
     0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
     0> <info> app: Connected.
     0> <info> app: PHY update request.
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested a data length of 251 bytes.
     0> <debug> nrf_ble_gatt: Updating data length to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: Data length updated to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: max_rx_octets: 27
     0> <debug> nrf_ble_gatt: max_tx_octets: 27
     0> <debug> nrf_ble_gatt: max_rx_time: 2120
     0> <debug> nrf_ble_gatt: max_tx_time: 2120
     0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
     0> <info> app: bsp_button_longkey_handler 1.
     0> <info> app: Disconnected.
     0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
     0> <info> app: Connected.
     0> <info> app: PHY update request.
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested a data length of 251 bytes.
     0> <debug> nrf_ble_gatt: Updating data length to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: Data length updated to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: max_rx_octets: 27
     0> <debug> nrf_ble_gatt: max_tx_octets: 27
     0> <debug> nrf_ble_gatt: max_rx_time: 2120
     0> <debug> nrf_ble_gatt: max_tx_time: 2120
     0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
     0> <info> app: Disconnected.
     0> <debug> nrf_ble_gatt: Requesting to update ATT MTU to 185 bytes on connection 0x0.
     0> <info> app: Connected.
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested an ATT MTU of 527 bytes.
     0> <debug> nrf_ble_gatt: Updating ATT MTU to 185 bytes (desired: 185) on connection 0x0.
     0> <debug> nrf_ble_gatt: ATT MTU updated to 185 bytes on connection 0x0 (response).
     0> <debug> nrf_ble_gatt: Peer on connection 0x0 requested a data length of 251 bytes.
     0> <debug> nrf_ble_gatt: Updating data length to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: Data length updated to 27 on connection 0x0.
     0> <debug> nrf_ble_gatt: max_rx_octets: 27
     0> <debug> nrf_ble_gatt: max_tx_octets: 27
     0> <debug> nrf_ble_gatt: max_rx_time: 2120
     0> <debug> nrf_ble_gatt: max_tx_time: 2120
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  4D                     |M       
     0> <info> app: M
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x0
     0> <debug> app: data[2]=0x0
     0> <debug> app: data[3]=0x0
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  45                     |E       
     0> <info> app: E
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x0
     0> <debug> app: data[3]=0x0
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  41                     |A       
     0> <info> app: A
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x0
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  53                     |S       
     0> <info> app: S
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x0
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  55                     |U       
     0> <info> app: U
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x55
     0> <debug> app: data[5]=0x0
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  52                     |R       
     0> <info> app: R
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x55
     0> <debug> app: data[5]=0x52
     0> <debug> app: data[6]=0x0
     0> <info> app: 
     0> 
     0> <info> app: Received data from BLE NUS. Writing data on UART.
     0> <debug> app:  45                     |E       
     0> <info> app: E
     0> <debug> app: length=1
     0> <debug> app: data[0]=0x4D
     0> <debug> app: data[1]=0x45
     0> <debug> app: data[2]=0x41
     0> <debug> app: data[3]=0x53
     0> <debug> app: data[4]=0x55
     0> <debug> app: data[5]=0x52
     0> <debug> app: data[6]=0x45
     0> <info> app: 
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=0, addr=0x7D000
     0> 
     0> <debug> app: state=0
     0> 
     0> <debug> app: START E
     0> 
     0> <debug> app: START x
     0> 
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=0, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: START E
     0> 
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x0
     0> 
     0> <debug> app: pData[4]=0x0
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x0
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x0
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: START x
     0> 
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=0, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: START E
     0> 
     0> <debug> app: START x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: addr=0x7D000, len=24
     0> 
     0> <debug> app: data r
     0> 
     0> <debug> app: pData[0]=0x1C
     0> 
     0> <debug> app: pData[1]=0x8A
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x20
     0> 
     0> <debug> app: pData[4]=0xE9
     0> 
     0> <debug> app: pData[5]=0xFF
     0> 
     0> <debug> app: pData[6]=0xFF
     0> 
     0> <debug> app: pData[7]=0xFF
     0> 
     0> <debug> app: pData[8]=0x98
     0> 
     0> <debug> app: pData[9]=0x2
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x61
     0> 
     0> <debug> app: pData[13]=0xD0
     0> 
     0> <debug> app: pData[14]=0xB
     0> 
     0> <debug> app: pData[15]=0xA
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x40
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x40
     0> 
     0> <debug> app: pData[20]=0x4D
     0> 
     0> <debug> app: pData[21]=0x2A
     0> 
     0> <debug> app: pData[22]=0x3
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: record.offsetStart=0x20E9FF
     0> <debug> app: record.offsetEnd=0xFFFF9802
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: record.offsetEnd=0x205A
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0xFF
     0> 
     0> <debug> app: pData[3]=0xFF
     0> 
     0> <debug> app: pData[4]=0xFF
     0> 
     0> <debug> app: pData[5]=0xD4
     0> 
     0> <debug> app: pData[6]=0xFF
     0> 
     0> <debug> app: pData[7]=0xFF
     0> 
     0> <debug> app: pData[8]=0xFF
     0> 
     0> <debug> app: pData[9]=0xD4
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: addr=0x7D000, len=24
     0> 
     0> <debug> app: data r
     0> 
     0> <debug> app: pData[0]=0x1C
     0> 
     0> <debug> app: pData[1]=0x8A
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x20
     0> 
     0> <debug> app: pData[4]=0x0
     0> 
     0> <debug> app: pData[5]=0x2
     0> 
     0> <debug> app: pData[6]=0x0
     0> 
     0> <debug> app: pData[7]=0x21
     0> 
     0> <debug> app: pData[8]=0x2
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0xA1
     0> 
     0> <debug> app: pData[13]=0x90
     0> 
     0> <debug> app: pData[14]=0x47
     0> 
     0> <debug> app: pData[15]=0xC
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0xAD
     0> 
     0> <debug> app: pData[21]=0x29
     0> 
     0> <debug> app: pData[22]=0x3
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: record.offsetStart=0x200002
     0> <debug> app: record.offsetEnd=0x210200
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: record.offsetEnd=0x30B4
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0x40
     0> 
     0> <debug> app: pData[3]=0x0
     0> 
     0> <debug> app: pData[4]=0x40
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x40
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x40
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=4
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: addr=0x7D000, len=24
     0> 
     0> <debug> app: data r
     0> 
     0> <debug> app: pData[0]=0x1C
     0> 
     0> <debug> app: pData[1]=0x8A
     0> 
     0> <debug> app: pData[2]=0x0
     0> 
     0> <debug> app: pData[3]=0x20
     0> 
     0> <debug> app: pData[4]=0x4
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x13
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x2
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x61
     0> 
     0> <debug> app: pData[13]=0xD0
     0> 
     0> <debug> app: pData[14]=0xB
     0> 
     0> <debug> app: pData[15]=0xA
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x40
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x40
     0> 
     0> <debug> app: pData[20]=0x4D
     0> 
     0> <debug> app: pData[21]=0x2A
     0> 
     0> <debug> app: pData[22]=0x3
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: record.offsetStart=0x200400
     0> <debug> app: record.offsetEnd=0x13000200
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt er
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_ERASE_RESULT ok
     0> <debug> app: Evt x
     0> 
     0> <debug> app: endAddr=0x80000
     0> <debug> app: startAddr=0x7D000
     0> <debug> app: type=1, addr=0x7D000
     0> 
     0> <debug> app: state=2
     0> 
     0> <debug> app: END E
     0> 
     0> <debug> app: record.offsetEnd=0x41C4
     0> <debug> app: addr=0x7D000, wlen=24
     0> 
     0> <debug> app: pData w
     0> 
     0> <debug> app: pData[0]=0xDE
     0> 
     0> <debug> app: pData[1]=0xDD
     0> 
     0> <debug> app: pData[2]=0x40
     0> 
     0> <debug> app: pData[3]=0x0
     0> 
     0> <debug> app: pData[4]=0x40
     0> 
     0> <debug> app: pData[5]=0x0
     0> 
     0> <debug> app: pData[6]=0x40
     0> 
     0> <debug> app: pData[7]=0x0
     0> 
     0> <debug> app: pData[8]=0x40
     0> 
     0> <debug> app: pData[9]=0x0
     0> 
     0> <debug> app: pData[10]=0x0
     0> 
     0> <debug> app: pData[11]=0x0
     0> 
     0> <debug> app: pData[12]=0x0
     0> 
     0> <debug> app: pData[13]=0x0
     0> 
     0> <debug> app: pData[14]=0x0
     0> 
     0> <debug> app: pData[15]=0x0
     0> 
     0> <debug> app: pData[16]=0x0
     0> 
     0> <debug> app: pData[17]=0x0
     0> 
     0> <debug> app: pData[18]=0x0
     0> 
     0> <debug> app: pData[19]=0x0
     0> 
     0> <debug> app: pData[20]=0x0
     0> 
     0> <debug> app: pData[21]=0x0
     0> 
     0> <debug> app: pData[22]=0x0
     0> 
     0> <debug> app: pData[23]=0x0
     0> 
     0> <debug> app: END X
     0> <debug> app: Evt e
     0> 
     0> <debug> app: Evt w
     0> 
     0> <debug> app: NRF_FSTORAGE_EVT_WRITE_RESULT ok
     0> <debug> app: Evt x
     0> 
    
    #include "app_timer.h"
    #include "error.h"
    #include "fds_flash.h"
    #include "fstorage.h"
    #include "nrf_delay.h"
    #include "nrf_fstorage.h"
    #include "nrf_fstorage_sd.h"
    #include "nrf_fstorage_nvmc.h"
    #include "nrf_log.h"
    #include "nrf_soc.h"
    #include "spi_flash.h"
    
    #define FSTORAGE_PAGE_NUM    3
    #define FSTORAGE_WAIT_CNT    1000
    
    
    
    static bool _gFstorageInitialized = false;
    static bool _gFstorageEraseDone   = true;
    static bool _gFstorageWriteDone   = true;
    static bool _gFstoragePageDirty   = true;
    static fstorage_state_t _gFstorageState = FSTORAGE_STATE_UNKNOWN;
    
    static void _fstorageEvtHandler(nrf_fstorage_evt_t * p_evt);
    
    NRF_FSTORAGE_DEF(nrf_fstorage_t _gFstorage) =
    {
        /* Set a handler for fstorage events. */
        .evt_handler = _fstorageEvtHandler,
    
        /* These below are the boundaries of the flash space assigned to this instance of fstorage.
         * You must set these manually, even at runtime, before nrf_fstorage_init() is called.
         * The function nrf5_flash_end_addr_get() can be used to retrieve the last address on the
         * last page of flash available to write data. */
        .start_addr = 0x7D000,
        .end_addr   = 0x80000,
    };
    
    static void _fstorageEvtHandler(nrf_fstorage_evt_t * pEvt) {
        NRF_LOG_DEBUG("Evt e\n");
        switch (pEvt->id) {
            case NRF_FSTORAGE_EVT_WRITE_RESULT:
            {
                NRF_LOG_DEBUG("Evt w\n");
                _gFstorageWriteDone = true;
                _gFstorageState     = FSTORAGE_STATE_WRITTEN;
                if (pEvt->result != NRF_SUCCESS) {
                   NRF_LOG_WARNING("fstorage writing failed %d bytes at address 0x%x.",
                                pEvt->len, pEvt->addr);
                } else {
                    NRF_LOG_DEBUG("NRF_FSTORAGE_EVT_WRITE_RESULT ok");
                    _gFstoragePageDirty = true;
                }
            } break;
    
            case NRF_FSTORAGE_EVT_ERASE_RESULT:
            {
                NRF_LOG_DEBUG("Evt er\n");
                _gFstorageEraseDone = true;
                _gFstorageState     = FSTORAGE_STATE_ERASED;
                if (pEvt->result != NRF_SUCCESS) {
                    NRF_LOG_WARNING("fstorage erased failed %d page from address 0x%x.",
                                     pEvt->len, pEvt->addr);
                } else {
                    NRF_LOG_DEBUG("NRF_FSTORAGE_EVT_ERASE_RESULT ok");
                    _gFstoragePageDirty = false;
                }
            } break;
    
            default:
                break;
        }
         NRF_LOG_DEBUG("Evt x\n");
    }
    
    static uint32_t _fstorageGetEndAddr(void) {
        const uint32_t bootloaderAddr = BOOTLOADER_ADDRESS;
        const uint32_t pageSz         = NRF_FICR->CODEPAGESIZE;
        const uint32_t codeSz         = NRF_FICR->CODESIZE;
    
        return (bootloaderAddr != 0xFFFFFFFF ?
                bootloaderAddr : (codeSz * pageSz));
    }
    
    static uint32_t _fstorageGetStartAddr(void) {
        const uint32_t eraseUnit = _gFstorage.p_flash_info->erase_unit;
        const uint32_t endAddr = _fstorageGetEndAddr();
        const uint32_t bootloaderAddr = BOOTLOADER_ADDRESS;
        uint32_t startAddr;
        if (bootloaderAddr == 0xFFFFFFFF) {
            startAddr = endAddr - FDS_VIRTUAL_PAGES * eraseUnit; 
        } else {
            startAddr = bootloaderAddr - FSTORAGE_PAGE_NUM * eraseUnit;
        }
    
        NRF_LOG_DEBUG("endAddr=0x%x", endAddr);
        NRF_LOG_DEBUG("startAddr=0x%x", startAddr);
        return startAddr;
    }
    static bool _fstorageIsEraseNeeded(void) {
        return _gFstoragePageDirty;
    }    
    
    static uint32_t _fstorageRoundUpU32(const uint32_t len) {
        if (len % sizeof(uint32_t)) {
            return (len + sizeof(uint32_t) - (len % sizeof(uint32_t)));
        }
    
        return len;
    }
    
    static void power_manage(void)
    {
    #ifdef SOFTDEVICE_PRESENT
        (void) sd_app_evt_wait();
    #else
        __WFE();
    #endif
    }
    
    int _fstorageWait(const uint32_t timeOutCnt) {
    #if 0
        NRF_LOG_DEBUG("timeOutCnt=%u\n", timeOutCnt);
        const uint32_t startCnt   = app_timer_cnt_get();
        NRF_LOG_DEBUG("startCnt=%u\n", startCnt);
        uint32_t endCnt;
        uint32_t diffCnt;
    
        do {
            endCnt           = app_timer_cnt_get();
            diffCnt          = app_timer_cnt_diff_compute(endCnt, startCnt);
            if (diffCnt >= timeOutCnt) {
                NRF_LOG_WARNING("_fstorage wait timeout\n");
                return ERROR_FSTORAGE_WAIT_TIMEOUT;
            }
    
            if (_gFstorageEraseDone && _gFstorageWriteDone) {
                break;
            }
        } while (true);
    
        NRF_LOG_DEBUG("endCnt=%u\n", endCnt);
    
        //while (nrf_fstorage_is_busy(&_gFstorage))
        while (!_gFstorageEraseDone || !_gFstorageWriteDone) {
            NRF_LOG_DEBUG("pm e\n");
            power_manage();
            NRF_LOG_DEBUG("pm x\n");
        }
    #endif
    		return 0;
        
    }
    
    int _fstorageRead(const uint32_t addr, const uint32_t len, uint8_t* data) {
        if (!_gFstorageInitialized) {
            return ERROR_FSTORAGE_UNINITIALIZED;
        }   
    
        const ret_code_t errCode = nrf_fstorage_read(&_gFstorage, addr, data, len);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_fstorage_read() failed 0x%x\n", errCode);
            return ERROR_FSTORAGE_READ_FAIL;
        }
    
        NRF_LOG_DEBUG("addr=0x%x, len=%u\n", addr, len);
        recordPrintData("data r", data);
    
        return 0;
    }
    
    int _fstorageWrite(const uint32_t addr, const uint32_t len, void const* pData) {
        if (!_gFstorageInitialized) {
            return ERROR_FSTORAGE_UNINITIALIZED;
        }
    
        if (!_gFstorageEraseDone) {
            return ERROR_FSTORAGE_ERASE_ON_GOING;
        }
    
        if (!_gFstorageWriteDone) {
            return ERROR_FSTORAGE_WRITE_ON_GOING;
        }
    
        _gFstorageWriteDone = false;
        _gFstorageState     = FSTORAGE_STATE_WRITING;
        const uint32_t wlen = _fstorageRoundUpU32(len);
        NRF_LOG_DEBUG("addr=0x%x, wlen=%u\n", addr, wlen);
        recordPrintData("pData w", pData);
        const ret_code_t retCode = nrf_fstorage_write(&_gFstorage, addr, pData, wlen, NULL);
        if (retCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_fstorage_write() failed 0x%x", retCode);
            return ERROR_FSTORAGE_WRITE_FAIL;
        }
    
        _fstorageWait(FSTORAGE_WAIT_CNT);
    
        return 0;
    }
    
    
    int fstorageInit(void) {
        // initializing variables
        _gFstorageInitialized = false;
        _gFstorageEraseDone   = true;
        _gFstorageWriteDone   = true;
        _gFstoragePageDirty   = true;
        _gFstorageState = FSTORAGE_STATE_UNKNOWN;
    
        const ret_code_t errCode = nrf_fstorage_init(&_gFstorage, &nrf_fstorage_sd, NULL);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_fstorage_init() failed 0x%x", errCode);
            return ERROR_FSTORAGE_INITIALIZE_FAIL;
        }
    
        _gFstorageInitialized = true;
    
        return 0;
    }    
    
    int fstorageErase(const uint32_t addr, const uint32_t pagesCnt) {
        if (!_gFstorageInitialized) {
            return ERROR_FSTORAGE_UNINITIALIZED;
        }
    
        if (!_gFstorageEraseDone) {
            return ERROR_FSTORAGE_ERASE_ON_GOING;
        }
    
        if (!_gFstorageWriteDone) {
            return ERROR_FSTORAGE_WRITE_ON_GOING;
        }    
    
        _gFstorageEraseDone = false;
        _gFstorageState     = FSTORAGE_STATE_ERASED;
        const ret_code_t errCode = nrf_fstorage_erase(&_gFstorage, addr, pagesCnt, NULL);
        if (errCode != NRF_SUCCESS) { 
            NRF_LOG_WARNING("nrf_fstorage_erase() failed 0x%x", errCode);
            return ERROR_FSTORAGE_ERASE_FAIL;
        }
    
        _fstorageWait(FSTORAGE_WAIT_CNT);
        
        return 0;
    }
    
    int fstorageReadRecord(const uint32_t addr, record_t* pRecord) {
        uint8_t data[RECORD_LEN] = {0};
        if (_fstorageRead(addr, RECORD_LEN, data) != 0) {
            return ERROR_FDS_READ_FAIL;
        }
    
        data2Record(data, pRecord);
    
        return 0;
    }    
    
    int fstorageUpdateRecord(const record_data_type_t type, const record_t* pRecord) {
        const uint32_t addr = _fstorageGetStartAddr();
        NRF_LOG_DEBUG("type=%d, addr=0x%x\n", type, addr);
        const fstorage_state_t state = fstorageGetState();
        NRF_LOG_DEBUG("state=%d\n", state);
        switch (type) {
            case RECORD_DATA_TYPE_START:
            {  
                NRF_LOG_DEBUG("START E\n");
                if ((state == FSTORAGE_STATE_UNKNOWN)) {
                    fstorageErase(addr, 1);    
                } else if (state == FSTORAGE_STATE_ERASED) {    
                    record_t record;
                    record.offsetStart = 0;
                    record.offsetEnd   = 0;
                    fstorageWriteRecord(addr, &record);
                }    
                NRF_LOG_DEBUG("START x\n");
            }    
            break;
    
            case RECORD_DATA_TYPE_END:
            {
                NRF_LOG_DEBUG("END E\n");
                record_t record;
    
                if ((state == FSTORAGE_STATE_UNKNOWN) || (state == FSTORAGE_STATE_WRITTEN)) {
                    if (fstorageReadRecord(addr, &record) != 0) {
                        NRF_LOG_WARNING("fstorageReadRecord(0x%x) failed\n", addr);
                        return ERROR_FSTORAGE_READ_FAIL;
                    }
                    NRF_LOG_DEBUG("record.offsetStart=0x%x", record.offsetStart);
                    NRF_LOG_DEBUG("record.offsetEnd=0x%x",   record.offsetEnd);
                    fstorageErase(addr, 1);
                } else if (state == FSTORAGE_STATE_ERASED) {
                    record.offsetEnd = pRecord->offsetEnd;
                    NRF_LOG_DEBUG("record.offsetEnd=0x%x",   record.offsetEnd);
                    fstorageWriteRecord(addr, &record);
                }    
                NRF_LOG_DEBUG("END X");
            }   
            break;    
        }    
    
        return 0;
    }
    
    int fstorageWriteRecord(const uint32_t addr, record_t* pRecord) {
        uint8_t data[RECORD_LEN] = {0};
        record2Data(pRecord, data);
        if (_fstorageWrite(addr, RECORD_LEN, data) != 0) {
            return ERROR_FSTORAGE_WRITE_FAIL;
        }
    
        return 0;
    }
    
    bool fstorageGetInitialized(void) {
        return _gFstorageInitialized;
    }
    
    bool fstorageGetEraseDone(void) {
        return _gFstorageEraseDone;
    }
    
    bool fstorageGetWriteDone(void) {
        return _gFstorageWriteDone;
    }
    
    bool fstorageGetPageDirty(void) {
        return _gFstoragePageDirty;
    }
    
    fstorage_state_t fstorageGetState(void) {
        return _gFstorageState;
    }
    
    
    
    
    
    7851.fstorage.h

Children
No Data
Related