This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

LED not toggling in bare-metal C

Hello there, i tried writing a program that blinks an LED, but for a reason it turns on and stays on, doesn't toggle. I'm using the Discovery board with STM32F407VG MCU.

And this is the code:

Note: I'm connecting pin PA5 with PD12(which is connected to the GREEN LED).

I took this code from a course, where the author uses STM32F411VET mcu, which is a disco board as well. And one strange thing, which he doesn't explain is: he says that "the led" is connected to PA5. I checked the datasheet of the board and don't see any LED connected to that pin.

And btw, he develops this from scratch, write his own structures and registers.

#include <stdint.h>

#define  PERIPH_BASE       0x40000000U
#define  AHB1PERIPH_BASE   (PERIPH_BASE + 0x00020000U)
#define  RCC_BASE          (AHB1PERIPH_BASE + 0x00003800U)

#define GPIOA_BASE          AHB1PERIPH_BASE + 0x00U
#define __IO     volatile  //Typecasting: __IO will be used for "volatile"
typedef struct{
	__IO  uint32_t MODER; 
	__IO  uint32_t OTYPER; 
	__IO  uint32_t OSPEEDR;
	__IO  uint32_t PUPDR;
	__IO  uint32_t IDR;
	__IO  uint32_t ODR;
	__IO  uint32_t BSSR;
	__IO  uint32_t LCKR;
	__IO  uint32_t AFR[2]; //Instead of creating separate members for AFRL and AFRH we create AFR array with size 2
/*This is the list of registers that belong to the GPIO port, regardless of the port 
you are working with(A...H)*/

/*Next a RCC structure is required, through which we enable AHB or APB*/
typedef struct{
	__IO  uint32_t CR;
	__IO  uint32_t PLLCFGR;
	__IO  uint32_t CFGR;
	__IO  uint32_t CIR;
	__IO  uint32_t AHB1RSTR;
	__IO  uint32_t AHB2RSTR;
	__IO  uint32_t AHB3RSTR;
	      uint32_t GAP1;    //This is 4 bytes long
	__IO  uint32_t APB1RSTR;
	__IO  uint32_t APB2RSTR;
	      uint32_t GAP2[2]; //This is 2 x 4 bytes long => 8 bytes long
	__IO  uint32_t AHB1ENR;
	__IO  uint32_t AHB2ENR;
	__IO  uint32_t AHB3ENR;
	      uint32_t GAP3;   //Padding by creating an empty register to reach a certain offset for another register
	__IO  uint32_t APB1ENR;
	__IO  uint32_t APB2ENR;
	      uint32_t GAP4[2];
	__IO  uint32_t AHB1LPENR;
	__IO  uint32_t AHB2LPENR;
	__IO  uint32_t AHB3LPENR;
	      uint32_t GAP5;
	__IO  uint32_t APB1LPENR;
	__IO  uint32_t APB2LPENR;
	      uint32_t GAP6[2];
	__IO  uint32_t BDCR;
	__IO  uint32_t CSR;
	      uint32_t GAP7[2];
	__IO  uint32_t SSCGR;
	__IO  uint32_t PLLI2SCFGR;
	__IO  uint32_t PLLSAICFGR;
	__IO  uint32_t DCKCFGR;

#define   RCC   ((RCC_TypeDef *)RCC_BASE)
#define  GPIOA  ((GPIO_TypeDef *)GPIOA_BASE)

#define   MODER_5_OUT   1<<10 //Set bit 10 in MODER to 1 to set it as output
#define   LED_PIN       1<<5

int main(void){
	RCC->AHB1ENR |= 0x01; //Set bit 3 to 1 
		for(int i=0; i<900000; i++){}  //Run a few cycles doing nothing