Accessing XIAO BLE Sense (nRF52840)'s onboard QSPI flash through non-mbed board

I wrote a small lib that might help (not fully independant from the rest of the program), to manage writting in Flash with Seeed nRf52840 Sense, without mbed. It’s in the github repo here in the file 'dataStorage.h: GitHub - midibody/Sleep_monitor: Sleep-monitoring wearable for breathing, apnea, and snoring detection with vibration alerts

The heart of Flash write is this section of code to write blocks of 4 bytes (the minimum write size), and write must be multiple of 4, plus they must be aligned with a multiple of 4:

  __disable_irq();

  // Autorize to write on flash
  NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
  __DSB();
  __ISB();
  nvmc_wait_ready();

  // Writes each word
  for (size_t i = 0; i < words; i++) {
    ((volatile uint32_t *)dst_addr)[i] = data[i];
    __DSB();
    __ISB();
    nvmc_wait_ready();
  }

  // Move back to read only
  NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
  __DSB();
  __ISB();
  nvmc_wait_ready();

  __enable_irq();

//***************************************
static inline void nvmc_wait_ready(void) {
  while (NRF_NVMC->READY == NVMC_READY_READY_Busy) {
    __NOP();
  }
}

I still struggle to be sure about the space authorized for user hat looks surper small: 7 × 4096= 28672 bytes :frowning: I use this information bellow, but I’m not sure it is fully correct. If experts have more accurate information,I’m happy to take it:

  • Flash total size : 1 OR 2 MBytes?
  • Flash Adressing : 0x0000_0000 → 0x000F_FFFF
  • write by word of 32bits ONLY
  • to write 1 word of 32b in a page you need first to erase it completely!
  • erase par page of 4k only
  • you read on the flash seemlessly, as if it was standard RAM, that’s the cool part

Page # | start of pages addresses (hex) free for USER PROGRAM:

0 | 0x000ED000
1 | 0x000EE000
2 | 0x000EF000
3 | 0x000F0000
4 | 0x000F1000
5 | 0x000F2000
6 | 0x000F3000
End of free user zone : 0x000F4000 (excluded)
! ! !WARNING: 0xF4000 seems used (by bootloader?). Dump:
0x000F4000: 00 00 04 20 19 AE 0F 00 41 AE 0F 00 43 AE 0F 00
0x000F4010: 45 AE 0F 00 47 AE 0F 00 49 AE 0F 00 00 00 00 00

By mistake I wrote at this address and then my board was dead, no more boot loader. I had to re-flash it all … big mess

Enjoy