Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to create a GPR project for ARM Cortex?

199 views
Skip to first unread message

Johan Söderlind Åström

unread,
Nov 11, 2017, 2:18:52 PM11/11/17
to
I build programs on my stm32l0 using this script:

arm-none-eabi-gcc -c -mcpu=cortex-m0plus -mthumb -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/main.d" -MT"build/main.d" -Wa,-a,-ad,-alms=build/main.lst src/main.c -o build/main.o
arm-none-eabi-gcc -x assembler-with-cpp -c -mcpu=cortex-m0plus -mthumb -Og -Wall -fdata-sections -ffunction-sections -g -gdwarf-2 -MMD -MP -MF"build/startup_stm32l072xx.d" -MT"build/startup_stm32l072xx.d" src/startup_stm32l072xx.s -o build/startup_stm32l072xx.o
arm-none-eabi-gcc build/startup_stm32l072xx.o build/main.o -mcpu=cortex-m0plus -mthumb -specs=nano.specs -Tsrc/STM32L072CZEx_FLASH.ld -lc -lm -lnosys -Wl,-Map=build/main.map,--cref -Wl,--gc-sections -o build/main.elf
arm-none-eabi-objcopy -O ihex build/main.elf build/main.hex
arm-none-eabi-objcopy -O binary -S build/main.elf build/main.bin
cp build/main.bin E:/


I want to convert it to a GPR project so I created this:

project Default is

for Languages use ("c");
--for Languages use ("c, s");
for Source_Dirs use ("src");
for Object_Dir use "obj";
for Exec_Dir use "bin";
for Main use ("main.c");
for Target use "arm-eabi";
for Library_Support use "none";

package Ide is
for Gnat use "arm-eabi-gnat";
for Gnatlist use "arm-eabi-gnatls";
for Debugger_Command use "arm-eabi-gdb";
end Ide;

package Builder is
for Default_Switches ("c") use ("-mcpu=cortex-m0", "-mthumb", "-Og", "-Wall", "-fdata-sections", "-ffunction-sections", "-g", "-gdwarf-2", "-MMD", "-MP", "-MF""build/main.d""", "-MT""build/main.d""", "-Wa,-a,-ad,-alms=build/main.lst");
end Builder;

package Compiler is
for Default_Switches ("c") use ("-mcpu=cortex-m0", "-mthumb", "-Og", "-Wall", "-fdata-sections", "-ffunction-sections", "-g", "-gdwarf-2", "-MMD", "-MP", "-MF""build/main.d""", "-MT""build/main.d""", "-Wa,-a,-ad,-alms=build/main.lst");
end Compiler;

package Linker is
for Linker_Options use ("-mcpu=cortex-m0plus", "-mthumb", "-specs=nano.specs", "-Tsrc/STM32L072CZEx_FLASH.ld", "-lc", "-lm", "-lnosys", "-Wl,-Map=build/main.map,--cref", "-Wl,--gc-sections");
end Linker;

end Default;


I got following result:
c:/gnat/2017/bin/../lib/gcc/arm-eabi/6.3.1/../../../../arm-eabi/bin/ld.exe: cannot find crt0.o: No such file or directory

I got following result by using assembly language s:
no compiler specified for language "s"

I do not need crt0.o, how to remove that?
Is it even possible to use ".s" or ".ld" files in a GPR project?

Simon Wright

unread,
Nov 11, 2017, 3:12:33 PM11/11/17
to
Johan Söderlind Åström <joha...@gmail.com> writes:

> arm-none-eabi-objcopy -O ihex build/main.elf build/main.hex
> arm-none-eabi-objcopy -O binary -S build/main.elf build/main.bin
> cp build/main.bin E:/

I don't think you can do these using a GPR. A Makefile would work.

> for Languages use ("c");
> --for Languages use ("c, s");

for Languages use ("C", "ASM"); -- not case-sensitive

> package Linker is
> for Linker_Options use ("-mcpu=cortex-m0plus", "-mthumb", "-specs=nano.specs", "-Tsrc/STM32L072CZEx_FLASH.ld", "-lc", "-lm", "-lnosys", "-Wl,-Map=build/main.map,--cref", "-Wl,--gc-sections");
> end Linker;

The linker script I use includes -nostdlib, which I think is what
suppresses crt0.o. You might need -lgcc at some point: I have

"-nostdlib", "-lm", "-lgcc", "-lc"

(no -lnosys??)

Once you've included the options in Builder, I don't believe you need to
repeat them in the other packages; they should be inserted into the
compiler/linker options automatically, so you only need to specify the
compiler/linker-specific ones explicitly.

I don't think you want all the dependency/listing info to be written to
main.d, .lst?

You probably need 'for Default_Switches ("ASM") use ...' in Compiler.

You've reminded me to look at my use of -ffunction-sections etc, which I
see I've been careless about. Thanks!

Johan Söderlind Åström

unread,
Nov 11, 2017, 5:05:58 PM11/11/17
to
Thanks it is working now.

project Default is

for Languages use ("c", "asm");
for Source_Dirs use ("src");
for Object_Dir use "obj";
for Exec_Dir use "bin";
for Main use ("main.c");
for Target use "arm-eabi";

package Naming is
for Spec_Suffix ("c") use ".h";
for Body_Suffix ("c") use ".c";
for Body_Suffix ("asm") use ".s";
end Naming;

package Ide is
for Gnat use "arm-eabi-gnat";
for Gnatlist use "arm-eabi-gnatls";
for Debugger_Command use "arm-eabi-gdb";
end Ide;

package Builder is
for Default_Switches ("c") use
(
"-lm",
"-lgcc",
"-lc",
"-mcpu=cortex-m0",
"-mthumb",
"-Og",
"-Wall",
"-fdata-sections",
"-ffunction-sections",
"-g",
"-gdwarf-2",
"-MMD",
"-MP",
"-f",
"-MF""build/main.d""",
"-MT""build/main.d""",
--"-Wa,-a,-ad,-alms=""build/main.lst""",
""
);
for Default_Switches ("asm") use
(
"-x",
"-lnosys",
"-lm",
"-lgcc",
"-lc",
"-mcpu=cortex-m0",
"-mthumb",
"-Og",
"-Wall",
"-fdata-sections",
"-ffunction-sections",
"-g",
"-gdwarf-2",
"-MMD",
"-MP",
"-f"
);
end Builder;

package Compiler is
end Compiler;

package Linker is
for Default_Switches ("c") use
(
"-nostartfiles",
"-nodefaultlibs",
"-TC:/Users/Johan/Documents/B-L072Z-LRWAN1_Ada/src/STM32L072CZEx_FLASH.ld",
--"-Tsrc/STM32L072CZEx_FLASH.ld",
--"-Wl,-Map=build/main.map,--cref",
"-Wl,--gc-sections",
--"-o main.elf",
""
);
end Linker;

end Default;


I have this shell script:
set -x #echo on
gprbuild default.gpr
arm-eabi-objcopy -O binary -S bin/main bin/main.bin
cp bin/main.bin E:/


My board can run a simple blink program.


"-nostartfiles" removes "crt0.o".
"-Tsrc/STM32L072CZEx_FLASH.ld" relative path does not work.
"-Wl,-Map=build/main.map,--cref" does not work.
"-Wa,-a,-ad,-alms=""build/main.lst""" does not work.
"-o main.elf" does not work.

I have not tried gdb debugger or language Ada yet.

Simon Wright

unread,
Nov 12, 2017, 3:06:04 AM11/12/17
to
Johan Söderlind Åström <joha...@gmail.com> writes:

> "-Tsrc/STM32L072CZEx_FLASH.ld" relative path does not work.
> "-Wl,-Map=build/main.map,--cref" does not work.
> "-Wa,-a,-ad,-alms=""build/main.lst""" does not work.

The linker runs in the Object_Dir, which those paths are not relative
to. You can say e.g.

"-T" & Project'Project_Dir & "src/STM32L072CZEx_FLASH.ld"

> "-o main.elf" does not work.

Perhaps it does work, but leaves main.elf in the Object_Dir?

Simon Wright

unread,
Nov 12, 2017, 3:54:26 AM11/12/17
to
Simon Wright <si...@pushface.org> writes:

> You've reminded me to look at my use of -ffunction-sections etc, which
> I see I've been careless about. Thanks!

Before proper use of section switches:
lockheed:certyflie simon$ arm-eabi-size cflie.elf--
text data bss dec hex filename
522972 4104 42160 569236 8af94 cflie.elf--

After:
lockheed:certyflie simon$ arm-eabi-size cflie.elf
text data bss dec hex filename
289912 3568 42144 335624 51f08 cflie.elf

I was wondering why it was so big, but there were worse problems!

Johan Söderlind Åström

unread,
Nov 12, 2017, 5:18:03 AM11/12/17
to
> > "-o main.elf" does not work.
>
> Perhaps it does work, but leaves main.elf in the Object_Dir?

It has no effect.


I will use Executable_Suffix instead.
https://docs.adacore.com/gprbuild-docs/html/gprbuild_ug/gnat_project_manager.html#executable-file-names

package Builder is
for Executable_Suffix use ".elf";

Simon Wright

unread,
Nov 12, 2017, 6:39:25 AM11/12/17
to
Johan Söderlind Åström <joha...@gmail.com> writes:

>> > "-o main.elf" does not work.
>>
>> Perhaps it does work, but leaves main.elf in the Object_Dir?
>
> It has no effect.

I had another look. My main program is in main.adb, but I have

package Builder is
...
for Executable ("main.adb") use project'Project_Dir & "cflie.elf";
...
end Builder;

Johan Söderlind Åström

unread,
Nov 12, 2017, 9:29:59 AM11/12/17
to
Debugger is working to a moderate extent.

package Ide is
for Gnat use "arm-eabi-gnat";
for Gnatlist use "arm-eabi-gnatls";
for Debugger_Command use "arm-eabi-gdb";
--for Connection_Config_File use "stm32l0.cfg";
for Program_Host use "localhost:4242";
for Communication_Protocol use "remote";
--for Connection_Tool use "openocd";
for Connection_Tool use "st-util";
end Ide;


Using openocd: crashes randomly with "JTAG failure -4"

Using st-util: everything is working except I can not set breakpoints, I get following: "Could not execute "debug set line breakpoint'"

Flash to board button in GNAT Programming Studio does not work. It writes to the board but the program does not execute correct. I think the load address might be wrong. I get following:
gprbuild --target=arm-eabi -d -PC:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\default.gpr C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\src\main.c -largs -Wl,-Map=map.txt
Compile
[c] main.c
[asm] startup_stm32l072xx.s
Link
[archive] libdefault.a
[index] libdefault.a
[link] main.c
Memory region Used Size Region Size %age Used
RAM: 1544 B 20 KB 7.54%
FLASH: 1800 B 192 KB 0.92%
[2017-11-12 15:28:07] process terminated successfully, elapsed time: 00.68s
Retrieving the load address.
arm-eabi-objdump C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\bin\main.elf -h
Load address is: 0x080000c0
Creating the binary (flashable) image.
arm-eabi-objcopy -O binary C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\bin\main.elf C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\bin\main.elf.bin
Flashing image to board.
Could not flash the executable.
[workflow stopped]

Simon Wright

unread,
Nov 13, 2017, 5:54:21 AM11/13/17
to
Johan Söderlind Åström <joha...@gmail.com> writes:

Groping around here ...

> Retrieving the load address.
> arm-eabi-objdump C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\bin\main.elf -h
> Load address is: 0x080000c0

My arm-eabi-objdump doesn't produce that output with -h.

And shouldn't the load address be 0x08000000?

> Creating the binary (flashable) image.
> arm-eabi-objcopy -O binary C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\bin\main.elf C:\Users\Johan\Documents\B-L072Z-LRWAN1_Ada\bin\main.elf.bin
> Flashing image to board.
> Could not flash the executable.

I think you need to run st-flash by hand to find out what's going on
here.

What board are you using?

Johan Söderlind Åström

unread,
Nov 13, 2017, 2:08:09 PM11/13/17
to
> I think you need to run st-flash by hand to find out what's going on
> here.

Program can be executed just by moving the .bin file to E:/.
cp build/main.bin E:/

> What board are you using?

http://www.st.com/en/evaluation-tools/b-l072z-lrwan1.html
0 new messages