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

PStorage INVALID_ADDRES Store and Load

So I'm trying to do some simple store and load from and to my flash memory.
I'm using the nRF51822 with 256 kB flash memory.
My problem looks exactly like this one, but of course no solution was provided.

However, I also do not get to store since I get NRF_ERROR_INVALID_ADDR.
I want to store at 0x3F800. 

Below I put down my source code.
Can anyone tell why I get this error and what I'm doing wrong?

#include "mbed.h"
#include <string.h>

extern "C" {
#include "nrf_error.h"
#include "pstorage.h"
#include "crc16.h"
}

pstorage_handle_t       pstorage_handle;
uint8_t pstorage_buffer[pstorage_block_size] __attribute__ ((aligned (4)));
Serial pc(USBTX,USBRX);

// Retrieve the handle of a storage page
void PStorage_GetBlockHandle(uint8_t id, pstorage_handle_t *block_handle);

static void PStorage_Callback_Handler(pstorage_handle_t *handle, uint8_t op_code, uint32_t result, uint8_t *p_data, uint32_t data_len)
{    
    if(op_code == PSTORAGE_STORE_OP_CODE)
    {
        // Configuration stored reset the system
        //wait_ms(100);
        //NVIC_SystemReset();
        pc.printf("OP Code returned succesfull.\n\r");
    }  
}

bool PStorage_Init()
{ 
    if(pstorage_init() != NRF_SUCCESS)
        return false;
    
    pstorage_module_param_t pstorage_param;  
    pstorage_param.block_size  = 64;           
    pstorage_param.block_count = 1;                             
    pstorage_param.cb          = PStorage_Callback_Handler;     
        
    if(pstorage_register(&pstorage_param, &pstorage_handle) != NRF_SUCCESS)
        return false;
                                  
    return true;
}    

void PStorage_GetBlockHandle(uint8_t id, pstorage_handle_t *block_handle)
{
    //Get block identifiers
    uint32_t status = pstorage_block_identifier_get(&pstorage_handle, id, block_handle);
    switch(status){  
    case NRF_SUCCESS:
        pc.printf("GetBlock Success.\n\r");
        break;
        case NRF_ERROR_INVALID_STATE:
        pc.printf("GetBlock Inavalid state.\n\r");
        break;
        case NRF_ERROR_NULL:
        pc.printf("GetBlock Null Parameter.\n\r");
        break;
        case NRF_ERROR_INVALID_PARAM:
        pc.printf("GetBlock Invalid Parameter.\n\r");
        break;
        default:
        pc.printf("Load Unkown Error .\n\r");
        break;
        }       
}
    
void PStorage_Load(uint8_t id, uint8_t *dest_data)
{
    pstorage_handle_t block_handle;
    PStorage_GetBlockHandle(id, &block_handle);
    uint32_t status = pstorage_load(dest_data, &block_handle, pstorage_block_size, 0);         
switch(status){
        case NRF_SUCCESS:
        pc.printf("Load Success.\n\r");
        break;
        case NRF_ERROR_INVALID_STATE:
        pc.printf("Load Inavalid state.\n\r");
        break;
        case NRF_ERROR_NULL:
        pc.printf("Load Null Parameter.\n\r");
        break;
        case NRF_ERROR_INVALID_PARAM:
        pc.printf("Load Invalid Parameter.\n\r");
        break;
        case NRF_ERROR_INVALID_ADDR:
        pc.printf("Load Invallid Adress.\n\r");
        break;
        case NRF_ERROR_NO_MEM:
        pc.printf("Load No Memory.\n\r");
        break;
        default:
        pc.printf("Load Unkown Error .\n\r");
        break;
        }

}

void PStorage_Clear(uint8_t id)
{   
    pstorage_handle_t block_handle;
    PStorage_GetBlockHandle(id, &block_handle);    
    pstorage_clear(&block_handle, pstorage_block_size);      

}

void PStorage_Store(uint8_t id, uint8_t *dest_data)
{     
    pstorage_handle_t block_handle;
    PStorage_GetBlockHandle(id, &block_handle);  
    //pc.printf("BlockHandle.moduleID = %X \t BlockHandle.BlockID = %X\n", block_handle.module_id,block_handle.block_id);      
    uint32_t status = pstorage_store(&block_handle, dest_data, 4, 0);                  
    switch(status){
        case NRF_SUCCESS:
        pc.printf("Store Success.\n\r");
        break;
        case NRF_ERROR_INVALID_STATE:
        pc.printf("Store Inavalid state.\n\r");
        break;
        case NRF_ERROR_NULL:
        pc.printf("Store Null Parameter.\n\r");
        break;
        case NRF_ERROR_INVALID_PARAM:
        pc.printf("Store Invalid Parameter.\n\r");
        break;
        case NRF_ERROR_INVALID_ADDR:
        pc.printf("Store Invallid Adress.\n\r");
        break;
        case NRF_ERROR_NO_MEM:
        pc.printf("Store No Memory.\n\r");
        break;
        default:
        pc.printf("Store Unkown Error .\n\r");
        break;
        }
}

And my main file.

    pc.baud(115200);
    pc.printf("Starting\n");
    
    PStorage_Init();
    
    xacc[1] = 1;
    PStorage_Store(0, xacc);
    PStorage_Load(0, stored);
    pc.printf("Stored = %u \t Load_1 = %u\n", xacc[1], stored[1]);
    wait(1);
    xacc[1] = 0;
    PStorage_Store(0, xacc);
    PStorage_Load(0, stored);
    pc.printf("Stored = %u \t Load_0 = %u\n", xacc[1], stored[1]);
    wait(1);

Related