Skip to main content
Explorer
June 16, 2024
Solved

GPIO not working by baremetal programming

  • June 16, 2024
  • 1 reply
  • 1906 views

Greetings, 

I am trying to blink LED using baremetal programming. I am just starting bare metal programming. My LED on Nucleo - F070 board is not being turned ON. 

I checked register values on data sheet and user manual and it seems to be correct. RCC and GPIOA are in different bus and I was confused a bit. I have mentioned the code below. 

Kindly, help me getting through this. Thank you so much in advance. 

 

 

// Where is the LED connected
// Port - A
// Pin - 5

#define PERIPH_BASE (0x40000000UL) // Base addr. of peripheral in memory
#define OFFSET_AHB2PERIPH (0x08000000UL) // In bus AHB2 0x4800 0000 is the address of GPIO port A & this offset + base value = GPIOA PORT A in memory
#define AHB2_PERIPHBASE (PERIPH_BASE+OFFSET_AHB2PERIPH) // 0x4800 0000 is starting point of AHB2
#define GPIOA_OFFSET (0x00000000UL)
#define GPIOA_BASE (AHB2_PERIPHBASE+GPIOA_OFFSET)

#define AHB1_PERIPHBASE (0x40020000UL)
#define RCC_OFFSET (0x00001000UL) //0x4002 1000 is the starting addr.
#define RCC_BASE (*(volatile unsigned int *)(AHB1_PERIPHBASE+RCC_OFFSET)) // starting addr. of RCC

#define AHBEN_REG_OFFSET (0x14UL) // offset to be added in RCC base according to Ref. Manual 7.4.6
#define RCC_AHBEN_REG (*(volatile unsigned int *)(RCC_BASE+AHBEN_REG_OFFSET))

#define GPIOA_EN (1UL<<17) // As per Ref. Manual 7.4.6 moving 1 to bit 17 to enable GPIOA
/*1<<0 = 0000 0000 0000 0000 0000 0000 0000 0001
 * 1<< 4 = 0000 0000 0000 0000 0000 0000 0001 0000 */

#define GPIOA_MODE_REG_OFFSET (0x0000UL)
#define GPIOA_MODE_REG (*(volatile unsigned int *)(GPIOA_BASE+GPIOA_MODE_REG_OFFSET))
/*As we are using PA5 - we will be using MODER[1:0] - bit 11 & bit 10*/
/*
 *
 * 00 -> INPUT MODE
 * 01 -> General Purpose output mode
 * 10 -> Alternate function mode
 * 11 -> Analog mode
 *
 * */
/*To set bit 10 to 1 = (1UL<<10)
 * To set bit 11 to 0 &=~(1UL<<11)*/ // according to Ref. MAnual

#define GPIOA_OD_R (0x14UL) // Offset for output data register
#define GPIOA_OD_REG (GPIOA_BASE+GPIOA_OD_R) // Using OUTPUT register as we have set GPIOA-5 as an output pin

#define PIN5 (1UL<<5) // To toggle pin 5 we need to toggle 5th bit in GPIOA_ODR register
#define LED_PIN PIN5


int main(void)
{
	//1. Enable clock access to GPIO

	RCC_AHBEN_REG != GPIOA_EN; // "!=" operation is done to prevent any other bit to change as "=" operation may disrupt other bits for the operation

	//2. Set PA5 as an output pin

	GPIOA_MODE_REG != (1UL<<10);
	GPIOA_MODE_REG &=~(1UL<<11);
	while(1)
	{
		GPIOA_OD_REG != PIN5;

	} // End of while loop

} // end of the main function

 

 

 

    This topic has been closed for replies.
    Best answer by jsoda.1

    After inserting break point I found that program is not getting to the second line and there is some issue in below mentioned line. 

    RCC_AHBEN_REG |= GPIOA_EN;

    1 reply

    Graduate II
    June 16, 2024

    Use the OR EQUAL not NOT EQUAL, ie  |= not  !=

    jsoda.1Author
    Explorer
    June 16, 2024

    I AM REALLY SORRY for this dumb question. 

    After correcting this, my program is being stuck at this infinite loop par.

    jsoda1_0-1718541113383.png