Skip to main content

STM32 Port configuration via structures

 How Does STM32CubeIDE

  /*Configure GPIO pin : Pd3 */

  GPIO_InitStruct.Pin = GPIO_PIN_3;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

  HAL_GPIO_Init(GPIOd, &GPIO_InitStruct);

 Now what is GPIOD and GPIO_InitStruct

GPIOD the definition is deep inside CMSIS "Drivers\CMSIS\Device\ST\STM32F4xx\Include\stm32f411xe.h"

#define GPIOD               ((GPIO_TypeDef *) GPIOD_BASE)

and  GPIOD_BASE declared in same file

#define GPIOD_BASE            (AHB1PERIPH_BASE + 0x0C00UL)


Now GPIO_InitStruct is comes from  the line GPIO_InitTypeDef GPIO_InitStruct = {0};  so the structure GPIO_InitTypeDef is as follows as defined in 

typedef struct

{

  uint32_t Pin; /*!< Specifies the GPIO pins to be configured.

    This parameter can be any value of @ref GPIO_pins_define */

  uint32_t Mode;   /*!< Specifies the operating mode for the selected pins.

   This parameter can be a value of @ref GPIO_mode_define */

  uint32_t Pull;   /*!< Specifies the Pull-up or Pull-Down activation for the selected pins.

 This parameter can be a value of @ref GPIO_pull_define */

  uint32_t Speed; /*!< Specifies the speed for the selected pins.

 This parameter can be a value of @ref GPIO_speed_define */

  uint32_t Alternate;  /*!< Peripheral to be connected to the selected pins. 

  This parameter can be a value of @ref GPIO_Alternate_function_selection */

}GPIO_InitTypeDef;



Next is stm32f4xx_hal_gpio.c where the HAL_GPIO_init() is defined

void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init) { uint32_t position; uint32_t ioposition = 0x00U; uint32_t iocurrent = 0x00U; uint32_t temp = 0x00U; /* Check the parameters */ assert_param(IS_GPIO_ALL_INSTANCE(GPIOx)); assert_param(IS_GPIO_PIN(GPIO_Init->Pin)); assert_param(IS_GPIO_MODE(GPIO_Init->Mode)); assert_param(IS_GPIO_PULL(GPIO_Init->Pull)); /* Configure the port pins */ for(position = 0U; position < GPIO_NUMBER; position++) { /* Get the IO position */ ioposition = 0x01U << position; /* Get the current IO position */ iocurrent = (uint32_t)(GPIO_Init->Pin) & ioposition; if(iocurrent == ioposition) { /*--------------------- GPIO Mode Configuration ------------------------*/ /* In case of Output or Alternate function mode selection */ if((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) { /* Check the Speed parameter */ assert_param(IS_GPIO_SPEED(GPIO_Init->Speed)); /* Configure the IO Speed */ temp = GPIOx->OSPEEDR; temp &= ~(GPIO_OSPEEDER_OSPEEDR0 << (position * 2U)); temp |= (GPIO_Init->Speed << (position * 2U)); GPIOx->OSPEEDR = temp; /* Configure the IO Output Type */ temp = GPIOx->OTYPER; temp &= ~(GPIO_OTYPER_OT_0 << position) ; temp |= (((GPIO_Init->Mode & GPIO_OUTPUT_TYPE) >> 4U) << position); GPIOx->OTYPER = temp; } /* Activate the Pull-up or Pull down resistor for the current IO */ temp = GPIOx->PUPDR; temp &= ~(GPIO_PUPDR_PUPDR0 << (position * 2U)); temp |= ((GPIO_Init->Pull) << (position * 2U)); GPIOx->PUPDR = temp; /* In case of Alternate function mode selection */ if((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) { /* Check the Alternate function parameter */ assert_param(IS_GPIO_AF(GPIO_Init->Alternate)); /* Configure Alternate function mapped with the current IO */ temp = GPIOx->AFR[position >> 3U]; temp &= ~(0xFU << ((uint32_t)(position & 0x07U) * 4U)) ; temp |= ((uint32_t)(GPIO_Init->Alternate) << (((uint32_t)position & 0x07U) * 4U)); GPIOx->AFR[position >> 3U] = temp; } /* Configure IO Direction mode (Input, Output, Alternate or Analog) */ temp = GPIOx->MODER; temp &= ~(GPIO_MODER_MODER0 << (position * 2U)); temp |= ((GPIO_Init->Mode & GPIO_MODE) << (position * 2U)); GPIOx->MODER = temp; /*--------------------- EXTI Mode Configuration ------------------------*/ /* Configure the External Interrupt or event for the current IO */ if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE) { /* Enable SYSCFG Clock */ __HAL_RCC_SYSCFG_CLK_ENABLE(); temp = SYSCFG->EXTICR[position >> 2U]; temp &= ~(0x0FU << (4U * (position & 0x03U))); temp |= ((uint32_t)(GPIO_GET_INDEX(GPIOx)) << (4U * (position & 0x03U))); SYSCFG->EXTICR[position >> 2U] = temp; /* Clear EXTI line configuration */ temp = EXTI->IMR; temp &= ~((uint32_t)iocurrent); if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT) { temp |= iocurrent; } EXTI->IMR = temp; temp = EXTI->EMR; temp &= ~((uint32_t)iocurrent); if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT) { temp |= iocurrent; } EXTI->EMR = temp; /* Clear Rising Falling edge configuration */ temp = EXTI->RTSR; temp &= ~((uint32_t)iocurrent); if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE) { temp |= iocurrent; } EXTI->RTSR = temp; temp = EXTI->FTSR; temp &= ~((uint32_t)iocurrent); if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE) { temp |= iocurrent; } EXTI->FTSR = temp; } } } }

Comments

Popular posts from this blog

Questions

1. What is a Stub function? Ans : A function without any definition which presents no error when called. 2. Why there are two ld scripts generated by STM32Cube IDE? Ans: The CubeIDE always presents two different LDscripts on for generating the executable for Debugging which goes to RAM another for normal code upload that goes to flash memory. 3. What is Supervisory Mode/ privileged mode ? Ans : When you are using a Kernel then there are two modes user mode and privileged mode. In User mode the applications cant have system calls. 4. What is "make -j6" ? Ans : Make has an argument for number of jobs, so when you add -j6 it will create 6 different compiler instances, so that the systems could use multicore to the fullest.  Linux people generally do add "-j $(nproc)" where nproc is a command line which returns number of processors attached. 5. What is weak attribute ? Ans : Weak attribute used to denote weak symbols which helps linkers to choose one function out of mul...

#@$%

  #include  "msp430x21x2.h"     #define PWM_OUTPUT              0x03 #define DUTY_CYCLE              TA0CCR2 #define MAX_LIMIT               300   /* #define no_of_samples           1 #define duty_cycle_limit        1000 #define temp_sensor             0xA000   #define panel_selection_cutoff  250 */ #define PV_VOLTAGE              INCH_0 #define PV_CURRENT              INCH_1 #define BATT_VOLTAGE            INCH_2 #define BATT_CURRENT            INCH_3   #define UPPER_CRG_LVL           0x0105 // 0x00FF // 0x00F5 //0x023A #define LOWER_CRG_LVL   ...

Preparing for embedded systems interview

  Preparing for a technical interview in embedded systems involves a wide range of topics, as the field encompasses both hardware and software components. Here are some key areas and example questions that you might encounter in such an interview: Basic Concepts and Theory: What is an embedded system? Can you explain the difference between a microprocessor and a microcontroller? Describe the various types of memory in an embedded system. Programming and Software Design: 4. How do you write an interrupt service routine in C? 5. Explain the concept of a real-time operating system (RTOS). How does it differ from a general-purpose operating system? 6. What are the different states of a thread in an RTOS? Hardware and Interfacing : 7. How does SPI communication work? What about I2C communication? 8. Explain the concept of GPIO (General-Purpose Input/Output). How would you use it in an embedded application? 9. What are interrupts, and how are they handled in embedded systems? Low-Level P...