Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STM32F103 GPIO Ports

I have a STM32F103C8 MCU, and I want to control GPIO registers without Cube MX. The MCU has an embedded LED and I want control it. I'm currently using CubeMX and IAR Software, and I make the pin an output (in CubeMX) with this code:

HAL_GPIO_TogglePin(Ld2_GPIO_Port,Ld2_Pin); 
HAL_Delay(1000); 

This works, but I want to do it without Cube and HAL library; I want to edit the register files directly.

like image 571
GiGeNCo Avatar asked Oct 24 '25 11:10

GiGeNCo


1 Answers

Using GPIO using registers is very easy. You fo not have to write your own startup (as ion the @old_timer answer). Only 2 steps are needed

you will need the STM provided CMSIS headers with datatypes declarations and human readable #defines and the reference manual

  1. Enable GPIO port clock. ecample: RCC -> APB2ENR |= RCC_APB2ENR_IOPAEN;
  2. Configure the pins using CRL/CRH GPIO registers
#define GPIO_OUTPUT_2MHz (0b10)
#define GPIO_OUTPUT_PUSH_PULL (0 << 2)
  GPIOA -> CRL &= ~(GPIO_CRL_MODE0 | GPIO_CRL_CNF0);
  GPIOA -> CRL |= GPIO_OUTPUT_2MHz | GPIO_OUTPUT_PUSH_PULL; 
  1. Manipulate the output
  /* to toggle */
  GPIOA -> ODR ^= (1 << pinNummer);
  /* to set */
  GPIOA -> BSRR = (1 << pinNummer);
  /* to reset */
  GPIOA -> BRR = (1 << pinNummer);
  //or
  GPIOA -> BSRR = (1 << (pinNummer + 16));
like image 115
0___________ Avatar answered Oct 27 '25 00:10

0___________