Search code examples
cembeddedstm32firmwarestm32cubeide

How to use a declared global variable in another global variable Array without any error in STM32 Cube IDE?


I am developing a firmware for the PCB which i developed. The micro controller which i used is STM32f401rct6.

I am using SWD interface and Stlink to flash the program.

I have declared a constant global variable (TOTAL_IC) but when i try to use the varible again in another global variable array (cell_asic bms_ic[TOTAL_IC];) it is throwing error but if i use it inside the any function it is not. But i dont want use it inside some particular function because that variable (TOTAL_IC) is used all over the program.

How to remove this error. i am a beginner?

code:

const uint8_t TOTAL_IC = 1; /* Global variable */

cell_asic bms_ic[TOTAL_IC]; /* this is where iam getting error */

LTC6811_init_cfg(TOTAL_IC, bms_ic);

for (uint8_t current_ic = 0; current_ic<TOTAL_IC; current_ic++)
{
    LTC6811_set_cfgr(current_ic,,REFON,ADCOPT,gpioBits_a,dccBits_a, dctoBits, UV, OV);
}

LTC6811_reset_crc_count(TOTAL_IC,bms_ic);

LTC6811_init_reg_limits(TOTAL_IC,bms_ic);

Solution

  • The dimension of an array with static storage duration needs to be an integer constant expression (or can be omitted if the array is defined with an initializer). A variable with a const qualifier does not count as a constant. It is a variable that cannot be modified. You need to use an actual integer constant such as an integer number, an enumeration constant, or an expression involving only integer constants as the array dimension. You could use a preprocessor macro that expands to one of those.

    For example:

    #define N_IC 1
    
    const uint8_t TOTAL_IC = N_IC;
    
    cell_asic bms_ic[N_IC];
    

    You might not need the TOTAL_IC variable at all in your program. You could probably just define it as a macro:

    #define TOTAL_IC 1
    
    cell_asic bms_ic[TOTAL_IC];