cancel
Showing results for 
Search instead for 
Did you mean: 

Hello All, I am new to this community as well as STM32F407 Discovery board . I am trying to write a program to turn ON LED without using any library. I wrote the below code, But I am not able to compile it .

JSuda
Associate II

  #define RCC_AHB1ENR (*(volatile unsigned long *)0x40023830)

#define GPIOD_MODER (*(volatile unsigned long *)0x40020C00)

#define GPIOD_OTYPER (*(volatile unsigned long *)0x40020C04)

#define GPIOD_OSPEEDR (*(volatile unsigned long *)0x40020C08)

#define GPIOD_PUPDR (*(volatile unsigned long *)0x40020C0C)

#define GPIOD_ODR (*(volatile unsigned long *)0x40020C14)

/* Init Starts here */

main()

{

RCC_AHB1ENR = 0x00000008 ;

GPIOD_MODER = 0x40000000 ;

GPIO_OTYPER = 0x00000000 ;

GPIOD_OSPEEDR = 0x00000000 ;

GPIOD_PUPDR = 0x40000000 ;

/* Init Ends here */

/* LED ON */

GPIO_ODR = 0x10000000 ;

}

Please help me to understand where I am going wrong with the code.

Many Thanks in advance.

11 REPLIES 11

So tell us what error the compiler or linker shows when it fails? What tool chain?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
dc.c:17:1: warning: return type defaults to 'int' [enabled by default]
 main()
 ^
dc.c:17:1: warning: function declaration isn't a prototype [-Wstrict-prototypes]
dc.c: In function 'main':
dc.c:29:1: error: 'GPIO_OTYPER' undeclared (first use in this function)
 GPIO_OTYPER = 0x00000000 ;
 ^
dc.c:29:1: note: each undeclared identifier is reported only once for each function it appears in
dc.c:47:1: error: 'GPIO_ODR' undeclared (first use in this function)
 GPIO_ODR = 0x10000000 ;
 ^
make.exe: *** [objdir/dc.o] Error 1

To me, this is self-explanatory: you've defined GPIOD_OTYPER and then used GPIO_OTYPER (without the D letter); the same with ODR.

You might want to consider using the CMSIS-mandated device headers, i.e. what's in [CubeF4]\Drivers\CMSIS\Device\ST\STM32F4xx\Include

In 21th century, you also should use the C99-style function headers, i.e.

void main(void) {

}

You also should end main() with a infinite loop, never returning from it (not even implicitly).

JW

Hello Clive,

Many Thanks for your reply.

I am compiling the above code with MDK Lite 5.27.1.0.

I am getting only one error now. The compilation error says :

.\Objects\LED_blink.axf: Error: L6320W: Ignoring --entry command. Cannot find argument 'Reset_Handler'.

I am not sure what all initialization or entry code need to be added in the code , to make things work. Please guide how to proceed with this.

Thanks and Best Regards,

Jaspreet

Hello waclawek,

Thanks for your inputs. I apologize , I typed those things incorrectly here, but they were fine in the code.

I am getting the compilation error :

.\Objects\LED_blink.axf: Error: L6320W: Ignoring --entry command. Cannot find argument 'Reset_Handler'.

Also, I am not using the libraries as my purpose is to understand how libraries are working at the root level.

Thanks a lot for your time and inputs.

Best Regards,

Jaspreet

Ah, I am not familiar with Keil. But generally, while the C program's entry point is indeed main(), the translation environment has to honor the way how the processor starts up, and provide the essential initializations (including the global and static variables initialization as mandated by the C standard). Usually, the environment requires to have a piece of code for that. That piece of code is called startup code, and is usually written in assembly language. You might have removed it while striving for "library-less" setup.

Also, IMO, the CMSIS-mandated headers don't really count as "library". It's not much more than the defines you've written at the beginning of your program, except that they are provided by the manufacturer (ST) and cover all IO registers of the chip.

JW

You would need startup_stm32f4xx.s (or equivalent) startup file to provide a vector table, and C runtime initialization code.

Perhaps look at Keil templates provided in SPL or HAL releases, you can decide not to use the libraries, but you should at least establish what the micro-controller needs when it starts.

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

When you don't use any libraries, you can't use initialized globals, because the initialization of globals is done by cinit (C runtime).

Uninitialized globals are OK as well as initialized locals. What you need, however is reset vector, and make your main to loop forever. Returning from it may do whatever.

The execution doesn't stop even if program ends.

turboscrew
Senior III

Have a look here: https://github.com/turboscrew/blue_pill_init

Especially start.S

Thanks a lot Clive for the inputs. I added startup code and a while (1) loop , and everything is working fine now,

Many Thanks to you.