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

How can I change the heap to flash instead of RAM in segger

Hi. 

I am working on nRF52832. I want to store a structure with 492kb I am getting heap is too large error. So how can I tell the complier to store my variable in flash instead of RAM by changing the settings in segger  please let me kindly 

Parents
  • In your makefile add on these compiler flag(ex:4K heap & stack):

    CFLAGS += -D__HEAP_SIZE=4096
    CFLAGS += -D__STACK_SIZE=4096
    ASMFLAGS += -D__HEAP_SIZE=4096
    ASMFLAGS += -D__STACK_SIZE=4096

  • Where can I find the makefile kindly guide me. 

    Thanks &Regards 

    Pspavi

  • Actually I want to store the data for the 5minutes and send over BLE . I am thought of using the flash fds example ,but I don't know how to push the timestamp and accelerometer data  in fds example . If you can help with this please . I referred many post even though i am not getting any idea

  • \nRF5_SDK_17.0.2_d674dde\examples\peripheral\flash_fds

    You may take for reference. 5 minutes report. Why not 5 seconds char. notify data?

    If the long times report, the nus rx package will be very large. It's must be some trouble-shooting over package large than 256 bytes.

  • i will send not immediately , by using delay one by one packet. I will send over BLE . I don't know to write the accelerometer data in fds example code . please look at my code

    flash_fds.zip

    #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 accelerometer_data[80];
       
    }configuration_t_sensor;
    static configuration_t_sensor my_data =
    {
      .accelerometer_data=   // here i tried to push the acceleromter 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();
      fds_read();
      
      while(1)
      {
        NRF_LOG_PROCESS();
       
        idle_state_handle();
      }
    }

  • The file is my project which I had done before. You may use this but the spi is done by gpio.

    nor_flash_drv.zip

    Usage:

    Write_FIFO(char *str);

    READ_FIFO(char *str);

    Good luck for you.

  • don't  you have the internal flash  reference code

Reply Children
Related