Closed jLynx closed 2 years ago
` / Copyright (c) 2014 Nordic Semiconductor. All Rights Reserved.
*/
/** @file
void uart_error_handle(app_uart_evt_t * p_event) { if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR) { APP_ERROR_HANDLER(p_event->data.error_communication); } else if (p_event->evt_type == APP_UART_FIFO_ERROR) { APP_ERROR_HANDLER(p_event->data.error_code); } }
/* @brief Function for erasing a page in flash.
@param page_address Address of the first word in the page to be erased. / static void flash_page_erase(uint32_t page_address) { // Turn on flash erase enable and wait until the NVMC is ready: NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Een << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. }
// Erase page: NRF_NVMC->ERASEPAGE = (uint32_t)page_address;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. }
// Turn off flash erase enable and wait until the NVMC is ready: NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } }
/* @brief Function for filling a page in flash with a value.
@param[in] value Value to be written to flash. / static void flash_word_write(uint32_t address, uint32_t value) { // Turn on flash write enable and wait until the NVMC is ready: NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Wen << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. }
*address = value;
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. }
// Turn off flash write enable and wait until the NVMC is ready: NRF_NVMC->CONFIG = (NVMC_CONFIG_WEN_Ren << NVMC_CONFIG_WEN_Pos);
while (NRF_NVMC->READY == NVMC_READY_READY_Busy) { // Do nothing. } }
/**
@brief Function for application main entry. / int main(void) { uint32_t addr; uint8_t patwr; uint8_t patrd; uint8_t patold; uint32_t i; uint32_t pg_size; uint32_t pg_num;
uint32_t err_code; const app_uart_comm_params_t comm_params = { RX_PIN_NUMBER, TX_PIN_NUMBER, RTS_PIN_NUMBER, CTS_PIN_NUMBER, APP_UART_FLOW_CONTROL_ENABLED, false, UART_BAUDRATE_BAUDRATE_Baud38400 };
APP_UART_FIFO_INIT(&comm_params, UART_RX_BUF_SIZE, UART_TX_BUF_SIZE, uart_error_handle, APP_IRQ_PRIORITY_LOW, err_code);
APP_ERROR_CHECK(err_code);
printf("Flashwrite example\n\r"); patold = 0; pg_size = NRF_FICR->CODEPAGESIZE; pg_num = NRF_FICR->CODESIZE - 1; // Use last page in flash
while (true) { // Start address: addr = (uint32_t )(pg_size pg_num); // Erase page: flash_page_erase(addr); i = 0;
do
{
printf("Enter char to write to flash\n\r");
// Read char from uart, and write it to flash:
do
{
err_code = app_uart_get(&patwr);
}
while(err_code == NRF_ERROR_NOT_FOUND);
if (patold != patwr)
{
patold = patwr;
flash_word_write(addr, (uint32_t)patwr);
++addr;
i += 4;
printf("'%c' was write to flash\n\r", patwr);
}
// Read from flash the last written data and send it back:
patrd = (uint8_t)*(addr - 1);
printf("'%c' was read from flash\n\r\n\r", patrd);
}
while (i < pg_size);
} }
/* @} / `
found Nordic Fstorage option it worked i think tis is code ftom fstorage example
Awesome, thanks for that
why not use the Arduino library https://github.com/d00616/arduino-NVM ?
Fstorage is new. If you look into that repos issues they are talking about implementning it.
Hi, I have a nRF51822 and I have a 10 byte value that I am wanting to save so it is persistent after the device has been turned off and on again. Is there a way to implement this?