cancel
Showing results for 
Search instead for 
Did you mean: 

How to access peripheral register directly using pointer in STM32F107VC

HB
Associate II

Hi,

I am using  STM32f107vct6. I want to create generalized GPIO function which can set gpio mode. I am using pointer to access peripheral registers. 

I have tested following

1]  Configured gpio using structures which are available in library(i.e. GPIOA->ODR). Then used same method below to toggle led of gpio_odr register. It worked perfectly.

2] With same method I tried to access GPIOC->CRH & CLR but it is not updating.

This is my code.

#include "stm32f10x.h"

//Define all port addresses 
#define GPIOA_ODR_ADD *((uint32_t *)0x4001080C)
#define GPIOB_ODR_ADD *((uint32_t *)0x40010C0C)
#define GPIOC_ODR_ADD *((uint32_t *)0x4001100C)
#define GPIOD_ODR_ADD *((uint32_t *)0x4001140C)
#define GPIOE_ODR_ADD *((uint32_t *)0x4001180C)

//MODE
#define GPIOA_CRL_ADD *((uint32_t *)0x40010800)
#define GPIOB_CRL_ADD *((uint32_t *)0x40010C00)
#define GPIOC_CRL_ADD *((uint32_t *)0x40011000)
#define GPIOD_CRL_ADD *((uint32_t *)0x40011400)
#define GPIOE_CRL_ADD *((uint32_t *)0x40011800)

#define GPIOA_CRH_ADD *((uint32_t *)0x40010804)
#define GPIOB_CRH_ADD *((uint32_t *)0x40010C04)
#define GPIOC_CRH_ADD *((uint32_t *)0x40011004)
#define GPIOD_CRH_ADD *((uint32_t *)0x40011404)
#define GPIOE_CRH_ADD *((uint32_t *)0x40011804)

#define PORT_A 1
#define PORT_B 2
#define PORT_C 3
#define PORT_D 4
#define PORT_E 5

//CONFIG FOR GPIO INIT
#define CONFIG_PP 0 //PUSH PULL = OUTPUT //ANALOG MODE = INPUT
#define CONFIG_OD 1 //OPEN DRAIN = OUTPUT //FLOATING MODE = INPUT
#define CONFIG_AF_PP 2 //ANALOG MODE PUSH PULL = OUTPUT, //PULLUP/DOWN = INPUT
#define CONFIG_AF_OD 3 //ANALOG MODE OPEN DRAIN = OUTPUT

//MODE FOR GPIO INIT
#define MODE_INPUT 0 //INPUT MODE
#define MODE_OUTPUT_10Mhz 1 //OUTPUT MODE MAX 10Mhz
#define MODE_OUTPUT_2Mhz 2 //OUTPUT MODE MAX 2Mhz
#define MODE_OUTPUT_50Mhz 3 //OUTPUT MODE MAX 50Mhz

//PROTOTYPE 
void GPIO_Setup(uint8_t Port,uint8_t Pin, uint8_t Config, uint8_t Mode);

//----------------------------- MAIN START ---------------------------
int main(void)
{
//001-GPIO INIT
GPIO_Setup(PORT_C,14,CONFIG_PP,MODE_OUTPUT_50Mhz);

while(1);
}//---------------------------- MAIN END ------------------------------

void GPIO_Setup(uint8_t Port,uint8_t Pin, uint8_t Config, uint8_t Mode)
{
   uint32_t U_CRH = 0, U_CRL = 0;

    switch(Port)
    {
       //PORT A-E
       case 1: { U_CRH = GPIOA_CRH_ADD; U_CRL = GPIOA_CRL_ADD; break;}
       case 2: { U_CRH = GPIOB_CRH_ADD; U_CRL = GPIOB_CRL_ADD; break;}
       case 3: { U_CRH = GPIOC_CRH_ADD; U_CRL = GPIOC_CRL_ADD; break;}
       case 4: { U_CRH = GPIOD_CRH_ADD; U_CRL = GPIOD_CRL_ADD; break;}
       case 5: { U_CRH = GPIOE_CRH_ADD; U_CRL = GPIOE_CRL_ADD; break;}
       default: break;
     }

   if(Pin <8)
   {
       //MODE
       U_CRL &= ~(3U << (((Pin + 1) * 4)- 4)); //CLEAR
       U_CRL |= (Mode << (((Pin + 1) * 4)- 4)); //SET

       //CONFIG
       U_CRL &= ~(3U << (((Pin + 1) * 4)- 2)); //CLEAR
       U_CRL |= (Config << (((Pin + 1) * 4)- 2)); //SET
    }
    else
    {
       //MODE
        U_CRH &= ~(3U << (((Pin - 7) * 4)- 4)); //CLEAR
        U_CRH |= (Mode << (((Pin - 7) * 4)- 4)); //SET

        //CONFIG
        U_CRH &= ~(3U << (((Pin - 7) * 4)- 2)); //CLEAR
        U_CRH |= (Config << (((Pin - 7) * 4)- 2)); //SET
      }

    switch(Port)
    {
      //PORT A-E
      case 1: { GPIOA_CRH_ADD = U_CRH; GPIOA_CRL_ADD = U_CRL; break;}
      case 2: { GPIOB_CRH_ADD = U_CRH; GPIOB_CRL_ADD = U_CRL; break;}
      case 3: { GPIOC_CRH_ADD = U_CRH; GPIOC_CRL_ADD = U_CRL; break;}
      case 4: { GPIOD_CRH_ADD = U_CRH; GPIOD_CRL_ADD = U_CRL; break;}
      case 5: { GPIOE_CRH_ADD = U_CRH; GPIOE_CRL_ADD = U_CRL; break;}
      default: break;
     }
}

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> 2] With same method I tried to access GPIOC->CRH & CLR but it is not updating.

Sounds like you're not enabling the GPIO clock. It's not in your code.

HAL_RCC_GPIOC_CLK_ENABLE();

 

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

> 2] With same method I tried to access GPIOC->CRH & CLR but it is not updating.

Sounds like you're not enabling the GPIO clock. It's not in your code.

HAL_RCC_GPIOC_CLK_ENABLE();

 

If you feel a post has answered your question, please click "Accept as Solution".
HB
Associate II

Thanks you very much. I forgot to turn it on.