cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4DISCOVERY Register Level Programming

Yadav
Associate II
Posted on June 09, 2018 at 04:32

Hi Everyone,

    I am new to stm32f4 series. I have done GPIO example using libraries. But i wanted to do register level programming. I have tried it but i could not get output for this. Can anyone help me out?

&sharpinclude 'main.h'

&sharpinclude 'stm32f4xx.h'

&sharpinclude 'stm32f4xx_hal.h' // Device header

void SystemClock_Config(void);

void GPIO_Init(void);

int main(void)

{

// HAL_Init();

//unsigned int delay=0;

SystemClock_Config();

GPIO_Init();

while (1)

{

GPIOD->ODR|=0X00004000;

//for(delay=0;delay<=16666667;delay++);

HAL_Delay(500);

//HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15);

GPIOD->ODR&=~0X00004000;

// for(delay=0;delay<=16666667;delay++);

HAL_Delay(500);

}

/* USER CODE END 3 */

}

/**

* @brief System Clock Configuration

* @retval None

*/

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct;

RCC_ClkInitTypeDef RCC_ClkInitStruct;

/**Configure the main internal regulator output voltage

*/

__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

RCC_OscInitStruct.HSEState = RCC_HSE_ON;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

RCC_OscInitStruct.PLL.PLLM = 8;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;

RCC_OscInitStruct.PLL.PLLQ = 7;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Initializes the CPU, AHB and APB busses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV4;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

/**Configure the Systick interrupt time

*/

HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

/**Configure the Systick

*/

HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

/* SysTick_IRQn interrupt configuration */

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/** Configure pins as

* Analog

* Input

* Output

* EVENT_OUT

* EXTI

*/

void GPIO_Init(void)

{

RCC->AHB1ENR|=RCC_AHB1ENR_GPIODEN;

GPIOD->MODER|=0X1000000;// PIN 14 as output

GPIOD->OTYPER|=0X00004000;//Output push pull

GPIOD->OSPEEDR|=0X00000000;/ low speed

GPIOD->PUPDR&=0X00000000;//no pull up and pull down

}

void _Error_Handler(char *file, int line)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

while(1)

{

}

/* USER CODE END Error_Handler_Debug */

}

&sharpifdef USE_FULL_ASSERT

/**

* @brief Reports the name of the source file and the source line number

* where the assert_param error has occurred.

* @param file: pointer to the source file name

* @param line: assert_param error line source number

* @retval None

*/

void assert_failed(uint8_t* file, uint32_t line)

{

/* USER CODE BEGIN 6 */

/* User can add his own implementation to report the file name and line number,

tex: printf('Wrong parameters value: file %s on line %d\r\n', file, line) */

/* USER CODE END 6 */

}

&sharpendif /* USE_FULL_ASSERT */

/**

* @}

*/

/**

* @}

*/

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

#stm32f4discovery #stm32f4 #stm32
23 REPLIES 23
Jan Waclawek
Senior II
Posted on June 09, 2018 at 06:42

GPIOD->MODER|=0X1000000;// PIN 14 as output

This would be for pin 12 (one zero missing for pin 14).

JW

Posted on June 09, 2018 at 10:46

More examples for low level programming you will find in the Reference Manual for STM32F0 series. You need to adapt them to the F4 MCU but you will get the general idea. They will cover GPIO, ADC, UART, etc

http://www.st.com/content/ccc/resource/technical/document/reference_manual/cf/10/a8/c4/29/fb/4c/42/DM00091010.pdf/files/DM00091010.pdf/jcr:content/translations/en.DM00091010.pdf

 
henry.dick
Senior II
Posted on June 09, 2018 at 11:51

all register-level code: 

https://github.com/dannyf00/My-MCU-Libraries---2nd-try/tree/master/STM32F4

 
Posted on June 09, 2018 at 12:23

I used that 0 in program execution but output not came.

Yuktheshwar Bhat
Associate II
Posted on June 09, 2018 at 15:56

First use Hal_GPIO_TOGGLE_PIN() in your program and use debugger to see program is executing then do regester level program.

turboscrew
Senior III
Posted on June 09, 2018 at 17:24

Which board do you have? Like it's already mentioned, it's not clear which pin you actually want to drive.

It's just that if you try to drive a LED, which pin it's connected to?

//HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_15);

GPIOD->ODR&=~0X00004000; -> This is pin 14

GPIOD->MODER|=0X1000000;// PIN 14 as output -> This is pin 12

GPIOD->OTYPER|=0X00004000;//Output push pull -> This is pin 14

(Also, LEDs are usually active low - pin needs to be set to zero to light up the LED.)

Posted on June 09, 2018 at 21:43

So use the debugger, and inspect the registers. Create an equivalent app in HAL, and then compare and contrast the behaviours and register settings.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on June 10, 2018 at 06:10

Thanks

Posted on June 10, 2018 at 06:13

 ,

 ,

i tried with the both. Now my code is working fine. ,

Problem was with speed register. i had set it high and now its working fine. ,

But my question is, why it is not working in low speed?

here is my working code.

♯ include 'stm32f4xx.h'

 ,

void GPIO_Init(void),

int main(void)

 ,

{

 ,

unsigned int delay=0,

 ,

GPIO_Init(),

while (1)

 ,

{

 ,

 ,

 ,

GPIOD->,ODR|=(1<,<,14),

 ,

for(delay=0,delay<,=16666667,delay++),

 ,

 ,

GPIOD->,ODR&,=~(1<,<,14),

 ,

for(delay=0,delay<,=16666667,delay++),

}

}

void GPIO_Init(void)

 ,

{

 ,

RCC->,AHB1ENR|=RCC_AHB1ENR_GPIODEN,

 ,

 ,

GPIOD->,MODER|=(1<,<,28), // pin 14 is output

 ,

GPIOD->,OTYPER&,=~(1<,<,14),//pin 14 as push pull

 ,

GPIOD->,OSPEEDR|=(1<,<,29)|(1<,<,28),//LOW speed

 ,

GPIOD->,PUPDR&,=~(1<,<,29)|(1<,<,28),//No push pull

}