cancel
Showing results for 
Search instead for 
Did you mean: 

How can I pass a variable via a Makefile into a .s file?

DHase.1
Associate III

I wish to pass a value from a script into the .s start-up file, similar to the following with a C program. The script 'mm' exports the CAN id, 0xB0A00000--

./mm B0A00000

and when 'make' executes, the 'Makefile' includes the following, which passes the value to a C program via C_DEFS--

C_DEFS += -DI_AM_CANID=$(I_AM_CANID)

and the C program has--

canwinch_ldrproto_init(IAMUNITNUMBER);

which uses the B0A00000 from the command line.

I would like to do the following with 'startup_stm32l431xx.s', e.g.--

...
  .section   .text.Reset_Handler
 
   .weak   Reset_Handler
   .type   Reset_Handler, %function
   .word   I_AM_CANID
   .word   _begin_flash
   .word  _end_prog
Reset_Handler:
 ldr  sp, =_estack   /* Set stack pointer */
...

With the following added to Makefile--

AS_DEFS = -DI_AM_CANID=$(I_AM_CANID)

But this results in--

build/startup_stm32l431xx.o:(.text.Reset_Handler+0x0): undefined reference to `I_AM_CANID'

I have tried a number of variations, with no success. There must be some detail I'm missing.

4 REPLIES 4

Pretty sure GAS ignores -D

Can you put this data in a variable on the C side, and read that?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DHase.1
Associate III

Thanks for the response. It looks like you are correct that the assembler options do not include the -D. As you suggested, putting it in C works--

In the C program define a constant--

const uint32_t i_am_canid = I_AM_CANID;

along with the following in Makefile--

-DI_AM_CANID=$(I_AM_CANID)

and doing an 'export' of the CAN id before running 'make'--

export I_AM_CANID=0x$1

which picks up the CAN id from the command that starts the script.

The assembly program then has the following--

   .word   i_am_canid /* Points to word with CAN ID */

   .word   _begin_flash

   .word  _end_prog

Reset_Handler:

 ldr  sp, =_estack   /* Set stack pointer */

If the above is done for the application program, the loader program can then find the CAN id (as well as start and end addresses of the app) that the app was compiled to use, by using the second work of the vector to get the Reset_Handler address and picking up the pointer relative to it. Upon reset the CAN loader can check to see if some bozo programmer (me most likely) used the SWD to flash the app, by-passing the loading-over-CAN, and specified the wrong CAN id for that unit.

I seem to remember that a .S file gets sent through the preprocessor but a .s file does not.

Try renaming from .s to .S

Andrei

DHase.1
Associate III

Andrei,

Thanks. Your recollection was good. It works!

It requires changing the .s to .S a few places in Makefile. There is one less level of indirection than the scheme I showed above, and even better it doesn't require anything in the .c app program.

Given the key that it has to do with the preprocessor, I did a search and turned up essentially the same recommendation--

https://stackoverflow.com/questions/34098596/assembly-files-difference-between-a-s-asm

Don