This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52 PM_EVT_ERROR_UNEXPECTED because of dirty FDS pages

Peer manager worked fine for bonding on my pca10040 board. However, when I flashed the code to my prototype, built with a SparkFun nrf52832 prototype board, I received PM_EVT_ERROR_UNEXPECTED messages during bonding, causing an assert-reset.

I dug down, down the stack, and found that pages_init() was returning an unexpected value in fds.c:fds_init(), which caused the switch statement to drop through to the default "Should not happen" case. Specifically, pages_init was encountering 1 swap page (at the second index, #1) and two unerased, uninitialized garbage pages (#0 and #2) that were bypassed as unusable; the unexpected return value was FDS_PAGE_SWAP. I haven't analyzed the code thoroughly enough to ascertain where the responsibility lies (between fds and fstorage) for erasing pages before the first use, but it didn't seem to go according to plan on my device. I combed through the HR monitor peripheral example and didn't observe that I had missed a flash storage initialization step, beyond calling fds_init() directly.

Once you're in this state, there seems to be no easy way to break out. I re-flashed the softdevice and the program to no avail. I worked around the problem by adding a single-use modification to fds.c, immediately and synchronously erasing any page that triggered the FDS_PAGE_UNDEFINED case in pages_init(). I'm sure there's a better way to solve this in the general case.

Could this be a bug in the SDK, failing to handle first-time initialization properly when fstorage provides unerased pages?

Development environment: armgcc 6.2 2016q4, s132 5.0.0-1.alpha, SDK nRF5_SDK_13.0.0-1.alpha

Related