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

how to store the sensor data in the internal flash

Hi, 

I am working on internal flash nRF52832 merged with twi sensor, my sensor function will return a string . How to assign the string to the my_data.p_data . i am getting error ,

how to resolve this ? I don't know how to write the return value from the sensor function to fds 

please help me 

#include "fds.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "nrf_drv_twi.h" //twi  added



/* TWI PINS */
#define TWI_SCL_PIN     27
#define TWI_SDA_PIN     26

/* MPU-6050 I2C-ADDRESS */
#define MPU_6050_I2C_ADDR   0x68

/* MPU-6050 Register Map */
#define WHO_AM_I            0x75
#define ACCEL_XOUT_H        0x3B
#define TEMP_OUT_H          0x41    
#define GYRO_XOUT_H         0x43         
#define PWR_MGMT_1          0x6B

/* TWI instance. */
static const nrf_drv_twi_t m_twi_mpu_6050 = NRF_DRV_TWI_INSTANCE(0); //added

char *accelerometer_value;
float alt[8];
char accel[80];
uint32_t flash=5;
/**************************Private Function Definitions**************************/

uint32_t file_id = 0x01000;
uint32_t rec_id = 0x1111;
typedef struct
{
   char accel_data[30];
   
}configuration_t_sensor;
static configuration_t_sensor my_data =
{
  .accel_data= accelerometer_value 

};
/* A record containing dummy configuration data. */
fds_record_t record =
{
  .file_id           = 0x00,
  .key               = 0x00,
  .data.p_data       = &my_data,
  /* The length of a record is always expressed in 4-byte units (words). */
  .data.length_words = 25,                  //Also change this if array size is changed.
};

/* Flag to check fds initialization. */
static bool volatile m_fds_initialized = false;;
static bool volatile m_fds_write = false;
static bool volatile m_gc_complete = false;


/**
* @brief             Event handler for FDS events
* @param[in]         p_evt              Structure with FDS data
* @retval            NONE
*/
static void fds_evt_handler(fds_evt_t const * p_evt)
{
  /*NRF_LOG_INFO("Event: %s received (%s)",
               fds_evt_str[p_evt->id],
               fds_err_str[p_evt->result]);
  */
  switch (p_evt->id)
  {
  case FDS_EVT_INIT:
    if (p_evt->result == NRF_SUCCESS)
    {
      m_fds_initialized = true;
    }
    break;
    
  case FDS_EVT_WRITE:
    {
      if (p_evt->result == NRF_SUCCESS)
      {
        m_fds_write = true;
      }
    } break;
    
  case FDS_EVT_DEL_RECORD:
    {
      if (p_evt->result == NRF_SUCCESS)
      {
        NRF_LOG_INFO("Record ID:\t0x%04x",  p_evt->del.record_id);
        NRF_LOG_INFO("File ID:\t0x%04x",    p_evt->del.file_id);
        NRF_LOG_INFO("Record key:\t0x%04x", p_evt->del.record_key);
      }
      ///m_delete_all.pending = false;
    } break;
    
  case FDS_EVT_GC:
    {
      NRF_LOG_INFO("GC Done");
      m_gc_complete = true;
    }break;
    
  default:
    break;
  }
}
/**
* @brief             Function to go to low power when waiting for FDS
* @param[in]         NONE
* @retval            NONE
*/
static void power_manage(void)
{
#ifdef SOFTDEVICE_PRESENT
  (void) sd_app_evt_wait();
#else
  __WFE();
#endif
}

/**
* @brief             The FDS takes some time for init. Waiting till it gets initialised properly.
* @param[in]         NONE
* @retval            NONE
*/
static void wait_for_fds_ready(void)
{
  while (!m_fds_initialized)
  {
    power_manage();
  }
}

static void wait_for_write(void)
{
  while (!m_fds_write)
  {
    power_manage();
  }
  m_fds_write = false;
}


void fds_write()
{
  ret_code_t rc;
  fds_record_desc_t desc = {0};
  record.file_id = file_id;

  for(int i=0;i<10;i++)
  {  
    memset(&desc, 0x00, sizeof(fds_record_desc_t)); //clear pannuthu
    record.key = rec_id;
    rc = fds_record_write(&desc, &record);
    wait_for_write();
    if(rc == NRF_SUCCESS) {
      NRF_LOG_INFO("Data written with id %d",rec_id);
    }
    else {
      NRF_LOG_INFO("Write Failed, id %d",rc);
    }
    rec_id++;
  }  
  rec_id = 0x1111;
}

void fds_read()
{
  NRF_LOG_INFO("Starting Read");
  ret_code_t rc;
   
  fds_record_desc_t desc = {0};
  fds_find_token_t  tok  = {0};
  
  for(int i=0;i<10;i++)
  {
    memset(&desc, 0x00, sizeof(fds_record_desc_t)); 
    memset(&tok, 0x00, sizeof(fds_find_token_t)); 
    
   rc = fds_record_find(file_id, rec_id, &desc, &tok);
  
    if (rc == NRF_SUCCESS)
    {
      fds_flash_record_t config = {0};
      
      /* Open the record and read its contents. */
     rc = fds_record_open(&desc, &config);
      APP_ERROR_CHECK(rc);

      NRF_LOG_INFO("File Found with id %s",record.data.p_data);
        printf("File Found with id %s",record.data.p_data);
      
      /* Close the record when done reading. */
      rc = fds_record_close(&desc);
      APP_ERROR_CHECK(rc);
    }
    rec_id++;
  }
}

/**
* @brief             Initialise FDS
* @param[in]         NONE
* @retval            NONE
*/
void init_fds()
{
  ret_code_t rc;
  
  /* Register first to receive an event when initialization is complete. */
  (void) fds_register(fds_evt_handler);
  
  NRF_LOG_INFO("Initializing fds...");
  
  rc = fds_init();
  NRF_LOG_INFO("fds init %d",rc);
  APP_ERROR_CHECK(rc);
  
  /* Wait for fds to initialize. */
  wait_for_fds_ready();
}


/**
* @brief                       Function to call the power management
* @param[in]                   NONE
* @retval                      NONE
*/
void idle_state_handle()
{
  nrf_pwr_mgmt_run();
}

/**
* @brief             Initialise the peripherals
* @param[in]         NONE
* @retval            NONE
*/
void init_peripherals()
{  
  NRF_LOG_INIT(NULL);
  NRF_LOG_DEFAULT_BACKENDS_INIT();                                                      //Use RTT as default backend
  NRF_LOG_INFO("Start\r\n");
  
  init_fds();
}



// twi added




/**
 * @brief Function for initializing the TWI peripheral.
 */
 
void twi_init (void)
{
    ret_code_t err_code;
    
    const nrf_drv_twi_config_t twi_mpu_6050_config = {
        .scl            = 27 ,
        .sda            = 26 ,
        .frequency      = NRF_TWI_FREQ_400K,
        .interrupt_priority = APP_IRQ_PRIORITY_HIGH
    };
    
    err_code = nrf_drv_twi_init(&m_twi_mpu_6050, &twi_mpu_6050_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);
    
    nrf_drv_twi_enable(&m_twi_mpu_6050);
}

/**
 * @brief Function for writing to registers on the MPU-6050.
 */

ret_code_t mpu_6050_I2C_register_write(uint8_t reg_addr, uint8_t * p_tx_data, uint8_t bytes)
{
    ret_code_t ret_code;
    
    uint8_t tx_data[bytes+1];
    
    tx_data[0] = reg_addr;
    
    for(uint8_t i = 0 ; i<bytes ; i++) 
    {
        tx_data[i+1] = p_tx_data[i];
    }   
   
    ret_code = nrf_drv_twi_tx(&m_twi_mpu_6050, MPU_6050_I2C_ADDR, tx_data, sizeof(tx_data), false);
    
    return ret_code;
}

/**
 * @brief Function for reading from registers on the MPU-6050.
 */

 ret_code_t mpu_6050_I2C_register_read( uint8_t reg_addr,  uint8_t * p_rx_data, uint32_t bytes)
{   
    ret_code_t ret_code;
    
    ret_code = nrf_drv_twi_tx(&m_twi_mpu_6050,MPU_6050_I2C_ADDR, &reg_addr, 1, false);
    
    if(ret_code != NRF_SUCCESS)
    {
        return ret_code;
    }
    
    ret_code = nrf_drv_twi_rx(&m_twi_mpu_6050, MPU_6050_I2C_ADDR, p_rx_data, bytes);
    
    return ret_code;
}

void mpu_6050_init (void)
{
    ret_code_t err_code;
    
    uint8_t tx_data = 0;
    
    // Write zero to the PWR_MGMT_1 register to wake up the MPU-6050
    err_code = mpu_6050_I2C_register_write(PWR_MGMT_1, &tx_data, 1);
    
    APP_ERROR_CHECK(err_code);
}

void mpu_6050_get_device_id (uint8_t * p_dev_id)
{
    ret_code_t err_code;
    
    uint8_t rx_data;
    
    // Read the I2C Address of the MPU-6050 from the WHO_AM_I register
    err_code = mpu_6050_I2C_register_read(WHO_AM_I,&rx_data,1);
       APP_ERROR_CHECK(err_code);
    
    *p_dev_id = rx_data;
}


char *accel_data()
{
    ret_code_t err_code;
    float *p_a_val;
     float *p_b_val;
      float *p_c_val;
  

      int16_t *p_x_val;
int16_t *p_y_val;
int16_t *p_z_val;
      
    // Raw accelerometer measurements buffer
    uint8_t acc_data[6];
    
    // Read the six accelerometer data registers starting from ACCEL_XOUT_H
    mpu_6050_I2C_register_read(ACCEL_XOUT_H,acc_data,sizeof(acc_data));
    
    APP_ERROR_CHECK(err_code);
    
    /*  Combine the two 8-bit data registers to a 16-bit value
        for each axis by left shifting ACCEL_xOUT_H eight times 
        and OR it with ACCEL_xOUT_L. */

            

 * p_x_val = (acc_data[0]<<8)|acc_data[1];
    *p_y_val = (acc_data[2]<<8)|acc_data[3];
    *p_z_val =  (acc_data[4]<<8)|acc_data[5];


       *p_a_val = (*p_x_val) /16384.0; // in m/s^2
         *p_b_val = (*p_y_val) /16384.0; // in m/s^2

 *p_c_val = (*p_z_val) /16384.0; // in m/s^2
 
        
alt[0]=*p_a_val;
alt[1]=*p_b_val;
alt[2]=*p_c_val;



sprintf(accel,",%.2f,%.2f,%.2f\n",alt[0],alt[1],alt[2]);

    
return accel;

}
 

/**
* @brief             Main Function
* @param[in]         NONE
* @retval            NONE
*/
int main()
{

twi_init();
 mpu_6050_init();

  init_peripherals();
  fds_write(accel);
  fds_read();
  
  while(1)
  {
    NRF_LOG_PROCESS();
    accelerometer_value=accel_data();
    idle_state_handle();
  }
}

Related