Home > Net >  Clarification on arrays within define declarations
Clarification on arrays within define declarations

Time:09-30

Working with SI Labs radios, a header file is produced from the provided software in order to initialize the radio. i have been helped tremendously already with the C program here on Stack Overflow and am hopeful this is another simple oversite on my side. in the header file, there are definitions like so:

#define RF_POWER_UP 0x02, 0x01, 0x01, 0x01, 0xC9, 0xC3, 0x80
#define RF_GPIO_PIN_CFG 0x13, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00

which i have placed within arrays for calling within a loop to send over SPI:

uint8_t rf_power_up[] = { RF_POWER_UP };
uint8_t rf_gpio_pin_cfg[] = { RF_GPIO_PIN_CFG };
...
   
uint8_t *theProgram[] = { rf_power_up, rf_gpio_pin_cfg, rf_global_xo_tune_1,
...
HAL_SPI_Transmit(&hspi1, theProgram[i], radio_array[i], 50);

this works but if i make changes to the config in the future the header file can completely change. that makes building manual arrays for the file tedious, however this array below is always included.

    #define RADIO_CONFIGURATION_DATA_ARRAY { \
        0x07, RF_POWER_UP, \
        0x08, RF_GPIO_PIN_CFG, \
        ....

this declaration has size of each definition followed by the name of the definition previously declared earlier in the header with a final value of 0x00. i cant seem to figure out how to use this in order to simplify future modifications as to not have to custom rewrite the header file every time i make changed to the prototype.

i was thinking something along the lines of:

uint8_t *config_array[] = {RADIO_CONFIGURATION_DATA_ARRAY};
int ix = 0;
while(config_array[ix] != 0x00){
HAL_SPI_Transmit(&hspi1, config_array[ix 1], config_array[ix], 50);
ix  ;
}

any help is much appreciated

CodePudding user response:

It seems that macro contains all commands you are required to send via SPI.

That could be done like this:

uint8_t config_array[] = RADIO_CONFIGURATION_DATA_ARRAY;

size_t pos = 0;
while (pos < sizeof config_array / sizeof *config_array)
{
  uint8 len = config_array[pos];
  HAL_SPI_Transmit(&hspi1, &config_array[pos 1], len, 50);
  pos  = len  1;
}

The created array will look like this:

uint8_t config_array[] = {
 0x07, 0x02, 0x01, 0x01, 0x01, 0xC9, 0xC3, 0x80,
 0x08, 0x13, 0x11, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00,
...
};

This time we cannot directly address some message based on index because the length varies. Instead we must walk through the array and jump from message to message. But as we want to send each message, we need to do that anyway.

  • Related