Peter Kapteyn
Peter Kapteyn

Reputation: 405

How to change #define value from another file

I have a source and header file:

//boot.h

#define BOOT_DATA_ADDRESS EMBEDDED_ADDRESS

void BootDataReadTable(bootData_t * bootData);

//boot.c

void BootDataReadTable(bootData_t * bootData)
{
    uint32_t *userPageBootDataTable = (uint32_t *) (BOOT_DATA_ADDRESS );
    uint32_t copiedDataTable[BOOT_DATA_WORDS] = {0U};
    for (uint32_t i = 0; i < BOOT_DATA_WORDS; i++)
    {
        copiedDataTable[i] = userPageBootDataTable[i];
    }
    unpackBootData(copiedDataTable, bootData);
}

and a test file:

//test_boot.c

#include "bootdata.h"

#ifdef BOOT_DATA_ADDRESS
#undef BOOT_DATA_ADDRESS
#define BOOT_DATA_ADDRESS (stackpointer)
#endif 

static uint32_t stack[0x2000] = {0};
static uint32_t * stackpointer = &stack[0];

void test_BootDataReadTable(void)
{
    bootData_t lBootData = {0};

    BootDataReadTable(&lBootData, (uint32_t *)addr);

    //test lBootData
}

This code runs on an embedded platform. The memory access in BootDataReadTable accesses internal flash on the MCU. For unit testing purposes, I want to run this on my host machine. In test_boot.c I want to change the BOOT_DATA_ADDRESS to point to my stack area on my host machine so that BootDataReadTable just accesses some dummy data instead of trying to access internal flash.

I'm compiling/running the unit test with ceedling.

The method I'm using above does not work. What am I doing wrong/how can I accomplish changing the value of BOOT_DATA_ADDRESS from another file?

Upvotes: 1

Views: 210

Answers (1)

root
root

Reputation: 6048

Since you're doing this for unit testing, I assume you have no problem compiling specifically for unit tests (probably not even for the target architecture).

env.h:

#ifdef UNIT_TESTING

extern int stack[0x2000];
#define BOOT_DATA_ADDRESS (stack)

#endif

boot.h:

#include "env.h"

#ifndef BOOT_DATA_ADDRESS
#define BOOT_DATA_ADDRESS ((void *)0xdeadbeef)
#endif

void BootDataReadTable(void);

boot.c:

#include <stdio.h>

#include "boot.h"

void BootDataReadTable(void)
{
    printf("BOOT_DATA_ADDRESS = %p\n", BOOT_DATA_ADDRESS);
}

main.c:

#include "boot.h"

int main(int argc, char **argv)
{
    BootDataReadTable();
}

test_boot.c:

#include "boot.h"

int stack[0x2000] = {0};

void test_BootDataReadTable(void)
{
    BootDataReadTable();
}

int main(int argc, char **argv)
{
    test_BootDataReadTable();
}

Makefile:

demo: main test_boot
    ./main
    ./test_boot

main::
    cc -o main main.c boot.c

test_boot::
    cc -DUNIT_TESTING -o test_boot test_boot.c boot.c

output:

cc -o main main.c boot.c
cc -DUNIT_TESTING -o test_boot test_boot.c boot.c
./main
BOOT_DATA_ADDRESS = 0xdeadbeef
./test_boot
BOOT_DATA_ADDRESS = 0x601060

Upvotes: 1

Related Questions