Writing and reading float numbers in the flash memory

I want to write numbers in my flash memory so I used this function "nrf_nvmc_write_words" but in it couldn't save float numbers in my flash memory

it is like that  in nrf_nvmc.c file:

void nrf_nvmc_write_words(uint32_t address, const float * src, uint32_t num_words)
{
uint32_t i;

// Enable write.
NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
__ISB();
__DSB();

for (i = 0; i < num_words; i++)
{
((uint32_t*)address)[i] = src[i];
wait_for_flash_ready();
}

NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
__ISB();
__DSB();
}

I did some modification and wrote float instead of uint32_t but it couldn't work so have you any suggestion please? 

Parents Reply Children
  • Hi,

    For the purpose of writing and reading bytes to and from flash, the type doesn't really matter. Casting the pointer will not change the value pointed to. You can treat the uint32_t pointer used by that flash API as a pointer to an arbitrary 4 byte data type. Casting any pointer to a 4 byte data type, such as a pointer to a float, to a pointer to uint32_t, will allow you to store the bits verbatim to flash using this API.

    Regards,
    Terje

  • I wrote this code to save gyroscope and accelerometer data but it gives me errors in the outputs

    #include "app_error.h"
    #include "app_util_platform.h"
    #include "boards.h"
    #include "math.h"
    #include "nrf_delay.h"
    #include "nrf_drv_twi.h"
    #include <stdio.h>

    #include "LSM9DS1.h"
    #include "nrf_drv_rtc.h"
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"

    #include <stdbool.h>
    #include <stdio.h>
    #include "nrf.h"
    #include "bsp.h"
    #include "app_error.h"
    #include "nrf_nvmc.h"
    #include "nordic_common.h"

    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"

    #include "app_timer.h"
    #include "nrf_drv_clock.h"

    #include "nrf_cli.h"
    #include "nrf_cli_uart.h"
    #include "nrf_delay.h"

    /**
    * @brief Function for application main entry.
    */

    // static void create_timers() {
    // ret_code_t err_code;

    // Create timers
    // err_code = app_timer_create(&m_repeated_timer_id,
    // APP_TIMER_MODE_REPEATED,
    // repeated_timer_200);
    // APP_ERROR_CHECK(err_code);
    // err_code = app_timer_create(&timer400,
    // APP_TIMER_MODE_REPEATED,
    // repeated_timer_400);
    //APP_ERROR_CHECK(err_code);
    //err_code = app_timer_create(&imutimer,
    // APP_TIMER_MODE_REPEATED,
    // imutimerhandler);
    //err_code = app_timer_create(&pressuretimer,
    // APP_TIMER_MODE_REPEATED,
    // pressuretimerhandler);
    //}

    const nrf_drv_rtc_t rtc = NRF_DRV_RTC_INSTANCE(2);

    static void rtc_handler(nrf_drv_rtc_int_type_t int_type) {
    if (int_type == NRF_DRV_RTC_INT_COMPARE0) {
    //NRF_LOG_INFO("Compare event ");
    } else if (int_type == NRF_DRV_RTC_INT_TICK) {
    //NRF_LOG_INFO(" Tick Event ");
    }
    }

    /** @brief Function starting the internal LFCLK XTAL oscillator.
    */
    static void lfclk_config(void) {
    //ret_code_t err_code = nrf_drv_clock_init();
    //APP_ERROR_CHECK(err_code);

    nrf_drv_clock_lfclk_request(NULL);
    NRF_LOG_INFO(" Low Frequency Configuration done ");
    }

    static void rtc_config(void) {
    uint32_t err_code;

    //Initialize RTC instance
    nrf_drv_rtc_config_t config = NRF_DRV_RTC_DEFAULT_CONFIG;
    config.prescaler = 4;
    err_code = nrf_drv_rtc_init(&rtc, &config, rtc_handler);
    APP_ERROR_CHECK(err_code);

    //Enable tick event & interrupt
    // nrf_drv_rtc_tick_enable(&rtc,true);

    //Set compare channel to trigger interrupt after COMPARE_COUNTERTIME seconds
    // err_code = nrf_drv_rtc_cc_set(&rtc,0,COMPARE_COUNTERTIME * 8,true);
    // APP_ERROR_CHECK(err_code);

    //Power on RTC instance
    nrf_drv_rtc_enable(&rtc);
    NRF_LOG_INFO("RTC Configuration done");
    }

    //end of timer stuff

    uint64_t micros() {
    uint64_t timer;
    timer = nrfx_rtc_counter_get(&rtc);
    return timer * 125;
    //return timer * 125; TRUE VALUE
    }

    static void log_init(void) {
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
    }

    int number=100;
    int main(void) { //me
    nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(1, 0));
    nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(1, 0));

    nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(0, 22));
    nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(0, 22));

    log_init();
    twi_init2();

    rtc_config();
    lfclk_config();
    nrf_delay_ms(10);
    begin();

    //Testing thing
    char Texttesting[] = "Baha:50 ";

    NRF_LOG_INFO("Hello");

    //ITS ON!
    //nrf_delay_ms(100);
    //yellowledoff();

    //gyroscope Calibration :
    setGyroFS(2);
    setGyroODR(5);
    setGyroOffset(-0.033844, 0.557159, -0.420441);
    setGyroSlope(1.111426, 1.188752, 1.148356);

    //magnatometer calibration :
    setMagnetFS(0);
    setMagnetODR(8);
    setMagnetOffset(55.104370, -0.712280, -7.662354);
    setMagnetSlope(1.295429, 1.251174, 1.354278);

    //accelometer calibration :
    setAccelFS(3);
    setAccelODR(5);
    setAccelOffset(-0.011773, -0.000467, -0.011638);
    setAccelSlope(1.011914, 1.000468, 0.988496);

    /* Configure board. */
    bsp_board_init(BSP_INIT_LEDS); //me

    /* Toggle LEDs. */
    //me
    float gx,gy,gz,ax,ay,az,mx,my,mz;
    uint32_t t[100];
    float r[100];
    int j=0;
    int i;

    while (j<100) {
    if (gyroAvailable()) // alias IMU.gyroscopeAvailable
    {
    readGyro(&gx, &gy, &gz);
    r[j]= gx*1000;
    j=j+1;

    r[j]= gy*1000;
    j=j+1;

    r[j]= gz*1000;
    j=j+1;

    }
    if (accelAvailable()) // alias IMU.accelerationAvailable in library version 1.01
    {
    readAccel(&ax, &ay, &az); // alias IMU.readAcceleration in library version 1.01

    r[j]= ax*1000;
    j=j+1;

    r[j]= ay*1000;
    j=j+1;

    r[j]= az*1000;
    j=j+1;
    }

    NRF_LOG_INFO("Gyroscope Information ");
    NRF_LOG_FLUSH();
    NRF_LOG_RAW_INFO(" gx: " NRF_LOG_FLOAT_MARKER , NRF_LOG_FLOAT(gx));
    NRF_LOG_RAW_INFO(" gy: " NRF_LOG_FLOAT_MARKER , NRF_LOG_FLOAT(gy));
    NRF_LOG_RAW_INFO(" gz: " NRF_LOG_FLOAT_MARKER , NRF_LOG_FLOAT(gz));
    NRF_LOG_RAW_INFO("\n\r");

    NRF_LOG_INFO("Acceleration Information ");
    NRF_LOG_FLUSH();
    NRF_LOG_RAW_INFO(" ax: " NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(ax));
    NRF_LOG_RAW_INFO(" ay: " NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(ay));
    NRF_LOG_RAW_INFO(" az: " NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(az));
    NRF_LOG_RAW_INFO("\n\r");

    NRF_LOG_INFO(" Index: %d",j);
    }

    for (int i=0; i<100;i++)
    { if(r[i]>=0)
    t[i]=r[i];
    else
    t[i]=(r[i]*(-1000));
    NRF_LOG_RAW_INFO(" r[i]: " NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(r[i]));
    NRF_LOG_RAW_INFO(" t[i]: " NRF_LOG_FLOAT_MARKER, NRF_LOG_FLOAT(t[i]));


    NRF_LOG_FLUSH();
    nrf_delay_us(1500);
    }

    //t[i]=r[i];

    float k[100];
    for(int i=0;i<number;i++)
    k[i]=t[i];

    NRF_LOG_INFO("Application started");
    uint32_t f_addr = 0x000f7000;
    uint32_t * p_addr = (uint32_t *)f_addr;
    nrf_nvmc_page_erase(f_addr);
    NRF_LOG_INFO("writing the value to the given address");
    nrf_nvmc_write_words(f_addr, k, 100);


    for(int i=0; i<100; i++)
    {
    NRF_LOG_INFO("The Data read from flash is: %d", (* p_addr+i));
    nrf_delay_us(1500);
    NRF_LOG_FLUSH();
    }
    nrf_delay_ms(100);
    }

    /**
    *@}
    **/

Related