nRF52832 how to write or read to internal flash? SDK IS 15:00
nRF52832 how to write or read to internal flash? SDK IS 15:00
Flash Handling Library: http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/lib_flash.html?cp=4_0_0_3_15
Experimental: Flash Data Storage: http://infocenter.nordicsemi.com/topic/com.nordic.infocenter.sdk5.v15.0.0/lib_fds.html?cp=4_0_0_3_52
Experimental: Flash Storage: infocenter.nordicsemi.com/.../lib_fstorage.html
NRF_LOG_INFO("Writing \"%x\" to flash.", m_data);
rc = nrf_fstorage_write(&fstorage, 0x3e200, &m_data, sizeof(m_data), NULL);
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
NRF_LOG_INFO("Donelql.");
rc = nrf_fstorage_read(&fstorage, 0x3e200, read_data, sizeof(read_data));
APP_ERROR_CHECK(rc);
wait_for_flash_ready(&fstorage);
Above is my program. The first time I let m_data =0xDEADBEEF , reading data is 0xDEADBEEF, that is OK. But the second time I let m_data =0x5E8D7E4F; reading data is 0x5E8D3E4F, that is FAULT . why?.
The first time I let m_data =0xDEADBEEF , reading data is 0xDEADBEEF, that is OK. But the second time I let m_data =0x5E8D7E4F; reading data is 0x5E8D3E4F, that is FAULT . why?.
At the hardware level, an erased Flash bit is a 1.
A write can only change a 1 to a 0 - to change a 0 to a 1, you need to do an erase.
The library documentation, and the example, do not make it at all clear that the library does not do this for you.
You must "manually" do an erase before a write.
If you look at the bit patterns you are writing, you can see what is happening:
| D | E | A | D | B | E | E | F | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 1 : 1 : 0 : 1 | 1 : 1 : 1 : 0 : 1 | 0 : 1 : 0 : 1 | 1 : 0 : 1 : 1 : 0 | 1 : 1 : 1 : 1 | 1 : 0 : 1 : 1 : 1 | 0 : 1 : 1 : 1 | 1 : +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 5 | E | 8 | D | 7 | E | 4 | F | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 0 : 1 : 0 : 1 | 1 : 1 : 1 : 0 : 1 | 0 : 0 : 0 : 1 | 1 : 0 : 1 : 0 : 1 | 1 : 1 : 1 : 1 | 1 : 0 : 0 : 1 : 0 | 0 : 1 : 1 : 1 | 1 : +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 0 : 1 : 0 : 1 | 1 : 1 : 1 : 0 : 1 | 0 : 0 : 0 : 1 | 1 : 0 : 1 : 0 : 0 | 1 : 1 : 1 : 1 | 1 : 0 : 0 : 1 : 0 | 0 : 1 : 1 : 1 | 1 : +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | | | | | *** | | | | | 5 | E | 8 | D | 3 | E | 4 | F |
It is only in the case of the 'B' nibble trying to change to '7' that you try to change a 0 to a 1 - every other bit is either unchanged, or a 1 being cleared to 0
I should say that I fell into this trap myself.
As I said, it was unclear from the documentation whether an erase was needed before a write.
I tested by writing "hello" and then writing "HELLO" (with no erase between) - it worked, so I thought that was OK.
But then my application didn't work.
The reason being that going from "hello" to "HELLO" involves only clearing bits.
If I had written "HELLO" first, and then "hello" I would have seen the issue sooner.
The Flash Data Storage library says it manages this for you, but I found the descriptions of "files" and "records" very unclear and confusing.
I should say that I fell into this trap myself.
As I said, it was unclear from the documentation whether an erase was needed before a write.
I tested by writing "hello" and then writing "HELLO" (with no erase between) - it worked, so I thought that was OK.
But then my application didn't work.
The reason being that going from "hello" to "HELLO" involves only clearing bits.
If I had written "HELLO" first, and then "hello" I would have seen the issue sooner.
The Flash Data Storage library says it manages this for you, but I found the descriptions of "files" and "records" very unclear and confusing.