Hello,
I am coding based on the ble_app_peripheral example from SDK6.0.16.1144 trying to write and read data from flash. For example I have a char with the value "00000" and would like to write into flash. I also would like to read the value of the respective ragister from flash and compare to my variable. The function I created to perform the write & read looks as follows:
uint32_t PC_FLASH_ADR = 0x20000; char default_passcode[] = "00000"; void check_nvs_available() { // Disable HW RST on P0_0 so it can be used as SPI MOSI. GPIO_Disable_HW_Reset(); // Read PC Flash --> IF PC Flash was not written in advance rewrite with default PC | ELSE do nothing int8_t ret; uint32_t bytes_read; char buffer_pc; uint8_t data_read[5] = { 0 }; ret = spi_flash_read_data(&data_read[0],PC_FLASH_ADR , sizeof(data_read), &bytes_read); while (bytes_read) { buffer_pc = (buffer_pc + bytes_read); bytes_read--; } if (buffer_pc == 0){ uint32_t pc_bytes_written; uint8_t data[5] = { default_passcode[0], default_passcode[1], default_passcode[2], default_passcode[3], default_passcode[4]}; ret = spi_flash_write_data(&data[0], PC_FLASH_ADR, sizeof(data), &pc_bytes_written); } // Reduce power consumption by putting flash into sleep mode spi_flash_power_down(); // Re-enable HW reset input (must be disabled if/when further operation on // external flash are performed) - must set as input first! GPIO_ConfigurePin(SPI_DO_PORT, SPI_DO_PIN, INPUT_PULLDOWN, PID_GPIO, false); GPIO_Enable_HW_Reset(); }
The functions should check the Flash if data was already written to PC_FLASH_ADR. IF this is the case nothting should be done. If no data was written to PC_FLASH_ADR the values of default_passcode should be written into flash starting at PC_FLASH_ADR. Are there any issues with my code?