fordGuy112
fordGuy112

Reputation: 23

What do two consecutive parameter definitions mean for a function in C?

I am going through the source code at work and I keep seeing this function definition style:

FUNC(Std_ReturnType, SMTN_CODE)SendSignalArray(uint8* NUMPTR, int size) {
}

What does it mean/do? Thanks

Upvotes: 0

Views: 2080

Answers (1)

kesselhaus
kesselhaus

Reputation: 1496

This is from AUTOSAR CompilerAbstraction FUNC(rettype, memclass) is defined usually in Compiler.h and Compiler_Cfg.h. Include hierarchy is:

file.c
    Std_Types.h
        Compiler.h
            Compiler_Cfg.h

Compiler_Cfg.h is usually configured in the AUTOSAR stack configuration tool, and then generated. Some compilers / architectures still might require the usage of @far and @near for data access, or some compilers supporting #pragma section ..., while other compilers only __attribute__(( ... )).

There is another code part you did not mention here, which comes from AUTOSAR MemoryMapping:

#define XXX_START_SEC_...
#include "XXX_MemMap.h"

...

#define XXX_STOP_SEC_...
#include "XXX_MemMap.h"

Here is an example for the usage, consider a module XXX:

  1. XXX.c - impl
  2. XXX.h - external interface provided by XXX
  3. XXX_Cfg.h - PRECOMPILE configuration header
  4. XXX_LCfg.h - LINKTIME configuration header
  5. XXX_LCfg.c - LINKTIME configuration impl (e.g. callouts)
  6. XXX_PBCfg.h - POSTBUILD configuration header
  7. XXX_PBCfg.c - POSTBUILD configuration impl
  8. XXX_MemMap.h - Memory Mapping for module XXX

The files 1 and 2 are static code, maybe with optional code wrapped by compiler/feature switches. The rest is usually generated by a configuration tool.

XXX.h:

#include "Std_Types.h"
#include "XXX_Cfg.h"
#if (XXX_LCFG_SUPPORT == STD_ON)
#include "XXX_LCfg.h"
#endif
#if (XXX_POSTBUILD_SUPPORT == STD_ON)
#include "XXX_PBCfg.h"
#endif

#define XXX_FOO_DISABLED 0u
#define XXX_FOO_ENABLED  1u

// --- Functions
#define XXX_START_SEC_CODE
#include "XXX_MemMap.h"

FUNC(void, XXX_CODE) XXX_Init(P2CONST(XXX_ConfigType, AUTOMATIC, XXX_CONFIG_DATA) ConfigPtr);
FUNC(Std_ReturnType, XXX_CODE)  XXX_IsDetectionEnabled(void);

#define XXX_STOP_SEC_CODE
#include "XXX_MemMap.h"

XXX_PBCfg.h:

typedef struct {
    VAR(uint8, TYPEDEF) NumChn;
    VAR(uint8, TYPEDEF) FooInitStatus;
    P2CONST(uint8, TYPEDEF, TYPEDEF)  ChannelCfgPtr;
} XXX_ConfigType;

#define XXX_START_SEC_CONFIG_DATA
#include "XXX_MemMap.h"

EXTERN CONST(XXX_ConfigType, XXX_CONFIG_DATA) XXX_Config;

#define XXX_STOP_SEC_CONFIG_DATA
#include "XXX_MemMap.h"

XXX_PBCfg.c:

#include "XXX.h"
#define XXX_START_SEC_CONFIG_DATA
#include "XXX_MemMap.h"

STATIC CONST(uint8, XXX_CONFIG_DATA) XXX_InitVals[] = {
    10,
    20,
    30,
};

EXTERN CONST(XXX_ConfigType, XXX_CONFIG_DATA) XXX_FooInit = {
    sizeof(XXX_InitVals)/sizeof(XXX_InitVals[0]),
    XXX_FOO_ENABLED,
    XXX_InitVals,
};

#define XXX_STOP_SEC_CONFIG_DATA
#include "XXX_MemMap.h"

XXX.c

// --- Variables
#define XXX_START_SEC_VAR_INIT_ASILB_8
#include "XXX_MemMap.h"

VAR(uint8, XXX_DATA) XXX_DetectEnable = 0;
P2CONST(XXX_Config, XXX_DATA, XXX_CONFIG_DATA) XXX_ConfigPtr = NULL_PTR;

#define XXX_STOP_SEC_VAR_ASILB_8
#include "XXX_MemMap.h"

// --- Functions
#define XXX_START_SEC_CODE
#include "XXX_MemMap.h"

FUNC(void, XXX_CODE) XXX_Init(P2CONST(XXX_ConfigType, AUTOMATIC, XXX_CONFIG_DATA) ConfigPtr)
{
    if (ConfigPtr != NULL)
    {
        XXX_ConfigPtr = ConfigPtr;
        XXX_DetectEnable = ConfigPtr->FooInitValue;
    }
}
FUNC(Std_ReturnType, XXX_CODE)  XXX_IsDetectionEnabled(void)
{
    return XXX_DetectEnable;
}

#define XXX_STOP_SEC_CODE
#include "XXX_MemMap.h"

XXX_MemMap.h is now generated depending on the configuration, e.g for a DIAB or TASKING compiler like this:

#if defined(XXX_START_SEC_CODE)
#undef XXX_START_SEC_CODE
#pragma section CODE ".text_ASILB"

#elif defined(XXX_STOP_SEC_CODE)
#undef XXX_STOP_SEC_CODE
#pragma section CODE /* default section e.g. .text */

#elif defined(XXX_START_SEC_VAR_INIT_ASILB_8)
#undef XXX_START_SEC_VAR_INIT_ASILB_8
#pragma section DATA ".bss_asilb" ".data_asilb"

#elif defined(XXX_STOP_SEC_VAR_INIT_ASILB_8)
#undef XXX_STOP_SEC_VAR_INIT_ASILB_8
#pragma section DATA /* default section e.g. .data */

#elif defined(XXX_START_SEC_CONFIG_DATA)
#undef XXX_START_SEC_CONFIG_DATA
#pragma section CONST ".rodata_pbconfig"

#elif defined(XXX_STOP_SEC_CONFIG_DATA)
#undef XXX_STOP_SEC_CONFIG_DATA
#pragma section CONST

#else
    #error "MemClass not defined"
#endif

the Compiler_Cfg.h should configure the FUNC(), VAR(), CONST() as:

#define FUNC(rettype, memclass)  rettype
#define VAR(t, memclass)  t
#define CONST(t, memclass)  const t

or with a compiler not understanding #pragma section like gcc using the __attribute__((section *section-name*)):

#if defined(XXX_START_SEC_CODE)
#undef XXX_START_SEC_CODE
#define XXX_CODE ".text_ASILB"

#elif defined(XXX_STOP_SEC_CODE)
#undef XXX_STOP_SEC_CODE
/* attribute is only on a single entity */

#elif defined(XXX_START_SEC_VAR_INIT_ASILB_8)
#undef XXX_START_SEC_VAR_INIT_ASILB_8
#define XXX_DATA ".data_asilb"

#elif defined(XXX_STOP_SEC_VAR_INIT_ASILB_8)
#undef XXX_STOP_SEC_VAR_INIT_ASILB_8
/* attribute is only on a single entity */

#elif defined(XXX_START_SEC_CONFIG_DATA)
#undef XXX_START_SEC_CONFIG_DATA
#define XXX_CONFIG_DATA ".rodata_pbconfig"

#elif defined(XXX_STOP_SEC_CONFIG_DATA)
#undef XXX_STOP_SEC_CONFIG_DATA
/* attribute is only on a single entity */

#else
    #error "MemClass not defined"
#endif

Therefore, the Compiler_Cfg.h should define the FUNC() macros as:

#define FUNC(rettype, memclass)   __attribute__((section memclass)) rettype
#define VAR(t, memclass)          __attribute__((section memclass)) t
#define CONST(t, memclass)        __attribute__((section memclass)) const t

This might look strange in the code, but at least it does not clutter the code with:

#if __DIAB__
  #pragma section CODE ".text_asilb"
#elif __MSVC__
   /* No Mapping */
#elif __GCC__
   __attribute__((section ".text_asilb"))
#endif
  void XXX_Init(XXX_ConfigType *ConfigPtr)
  {
      ... 
  }

And the part for XXX_START/STOP_SEC_CONFIG_DATAallows also to collect and place POSTBUILD_LOADABLE configuration data in a specific memory section (e.g. FLASH Block), which can later separately be replaced by a donwload tool with different data without reflashing the whole application. Consider here a use case like a gateway which just needs a new network routing table.

Upvotes: 2

Related Questions