Skip to main content
Yadav
Associate III
June 9, 2018
Question

STM32F4DISCOVERY Register Level Programming

  • June 9, 2018
  • 10 replies
  • 4067 views
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
This topic has been closed for replies.

10 replies

Jan Waclawek
Visitor II
June 9, 2018
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

Yadav
YadavAuthor
Associate III
June 9, 2018
Posted on June 09, 2018 at 12:23

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

Tesla DeLorean
Guru
June 9, 2018
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 VenmoUp vote any posts that you find helpful, it shows what's working..
Bogdan Golab
Lead
June 9, 2018
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

 
Yadav
YadavAuthor
Associate III
June 10, 2018
Posted on June 10, 2018 at 06:10

Thanks

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

all register-level code: 

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

 
Yuktheshwar Bhat
Associate II
June 9, 2018
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.

Yadav
YadavAuthor
Associate III
June 10, 2018
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

}

turboscrew
Senior III
June 9, 2018
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.)

Yadav
YadavAuthor
Associate III
June 10, 2018
Posted on June 10, 2018 at 06:14

 ,

 ,

We need to provide active high to the led.

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

 ,

}

waclawek.jan
Super User
June 10, 2018
Posted on June 10, 2018 at 09:09

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

It most probably was not. Try to comment out that one line, compile and run again.

Your delay may also be the culprit. With any complier optimisation switched on,  it will eliminate those loops completely. Make the loop variable volatile.

i wanna learn how to debug and inspect my code.

There is no single advice for this, and I know of no good single source reading. You need to develop skills in many different ways - including understanding your own errors, reading other's code, utilizing various debugging methods including and excluding using debuggers, reading and understanding disassembled output from the compiler, observing register content in the debugger and simultaneously understanding how that may be intrusive for the code execution etc.

JW

Yadav
YadavAuthor
Associate III
June 13, 2018
Posted on June 13, 2018 at 05:34

Hi Everyone,

    I have successfully got the output for GPIO. Now i am started working on ADC interface. But i couldn't get the output for my code. I had given 3V to PA1 pin but still i couldn't get the proportional value in DR register. Kindly help me out to solve this issue.

#include 'stm32f4xx.h'

void GPIO_Init(void);

void adc_init(void);

uint16_t adc_read();

uint16_t adc_value1=0,adc_value2=0;

int main(void)

{

GPIO_Init();

adc_init();

while (1)

{

adc_value2=adc_read();

if(adc_value2>=0X000C)

{

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

}

else

{

GPIOD->ODR|=(1<<15);

}

//

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

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

// GPIOD->ODR|=(1<<15);

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

//

// GPIOD->ODR&=~(1<<15);

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

}

}

void GPIO_Init(void)

{

RCC->AHB1ENR |=(1<<1); // clock for gpioA

GPIOA->MODER |= 0X03; // PA1 analog inputs

}

void adc_init(void)

{

RCC->APB2ENR|=(1<<8);

ADC1->CR1|=(1<<24)|(1<<0); // 1st CHANNEL // 10BIT RESOLUTION

ADC1->CR2|=(1<<10);// ADC1->CR2

ADC1->SMPR2 |= 1; //15 cycles

ADC1->SQR1 &= ~(1<<20); // 1 conversion

ADC1->SQR3 = 0x00;

}

uint16_t adc_read()

{

ADC1->CR2|=(1<<0);// ADC ON

ADC1->CR2|=(1<<30);// START CONVERSION

// if((ADC1->SR&(1<<1))) // wait untill EOC bit clear

//{

ADC1->SR = 0X00;

adc_value1=ADC1->DR;

//}

ADC1->CR2&=~(1<<0);// ADC OFF

return(adc_value1);

}
turboscrew
Senior III
June 13, 2018
Posted on June 13, 2018 at 07:13

Check the documents. I recall there needs to be a delay ('Tstab' in data sheets) between ADC_ON and START_CONVERSION to get valid values. And you should wait until the conversion is done (the out-commented part). In my document (407 reference) the conversion is done when EOC gets set, not cleared.

Yadav
YadavAuthor
Associate III
June 13, 2018
Posted on June 13, 2018 at 18:49

I have made few changes in my code. Now i am getting random values in Data register. Kindly help me out to get exact values.

#include 'stm32f4xx.h'

void GPIO_Init(void);

void adc_init(void);

uint16_t adc_read();

uint16_t adc_value1=0,adc_value2=0;

int main(void)

{

GPIO_Init();

adc_init();

while (1)

{

adc_value2=adc_read();

if(adc_value2>=1000)

{

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

}

else

{

GPIOD->ODR|=(1<<15);

}

//

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

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

// GPIOD->ODR|=(1<<15);

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

//

// GPIOD->ODR&=~(1<<15);

// for(delay=0;delay<=4166666;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

GPIOD->MODER|=(1<<30); // pin 14 is output

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

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

RCC->AHB1ENR |=(1<<0); // clock for gpioA

//GPIOA->AFR|=(1<<0); GPIOD->OSPEEDR|=(1<<3);

GPIOA->MODER |= (1<<3); // alternate function

GPIOA->PUPDR&=~(0X00000000);

GPIOA->AFR[0]|=(0<<1)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1

GPIOA->OTYPER&=~(1<<1);//pin 1 as push pull

}

void adc_init(void)

{

RCC->APB2ENR|=(1<<8);

ADC1->CR1|=(1<<0)|(1<<23); // 1st CHANNEL // WATCHDOG ENABLE

ADC1->CR1&=~(1<<24);//

ADC1->CR1&=~(1<<25); //12BIT RESOLUTION

ADC1->CR2|=(1<<10);// EOCs is set

ADC1->SMPR2 |= (1<<3); //15 cycles

ADC1->SQR1 &= ~(1<<20); // 1 conversion

ADC1->SQR3 = 0x00;

}

uint16_t adc_read()

{

ADC1->CR2|=(1<<0);// ADC ON

ADC1->CR2|=(1<<30);// START CONVERSION

while(ADC1->SR&(1<<1)) // wait untill EOC bit set

{

ADC1->SR = 0X00;

adc_value1=ADC1->DR;

}

// ADC1->CR2&=~(1<<0);// ADC OFF

return(adc_value1);

}
turboscrew
Senior III
June 13, 2018
Posted on June 13, 2018 at 23:08

Should this: GPIOA->AFR[0]|=(0<<1)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1

be: GPIOA->AFR[0]|=(1<<0)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1 ?

I also remember vaguely, that ADC was not an alternate function.

I think the pin should be configured as analog input.

Not sure about the pin configuration.

Yadav
YadavAuthor
Associate III
June 14, 2018
Posted on June 14, 2018 at 03:51

   Sir, i have tried both the option. But still i could not get the output for ADC.

Please someone help me out to get this output. I have tried my best and still trying.

#include 'stm32f4xx.h'

void GPIO_Init(void);

void adc_init(void);

uint16_t adc_read();

uint16_t adc_value1=0,adc_value2=0;

int main(void)

{

GPIO_Init();

adc_init();

while (1)

{

adc_value2=adc_read();

if(adc_value2>=500)

{

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

}

else

{

GPIOD->ODR|=(1<<15);

}

//

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

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

// GPIOD->ODR|=(1<<15);

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

//

// GPIOD->ODR&=~(1<<15);

// for(delay=0;delay<=4166666;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

GPIOD->MODER|=(1<<30); // pin 14 is output

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

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

RCC->AHB1ENR |=(1<<0); // clock for gpioA

GPIOA->MODER |= (1<<3)|(1<<2); // Analog mode

GPIOA->PUPDR&=~(0X00000000);//No push pull

// GPIOA->AFR[0]|=(1<<0)|(1<<1)|(1<<2); // ADC FUNCTION FOR PA1

GPIOA->OTYPER&=~(1<<1);//pin 1 as push pull

GPIOA->OSPEEDR|=(1<<3);//HIGH speed

}

void adc_init(void)

{

RCC->APB2ENR|=(1<<8);

ADC1->CR1|=(1<<0)|(1<<23); // 1st CHANNEL // WATCHDOG ENABLE

ADC1->CR1&=~(1<<24);//

ADC1->CR1|=(1<<24); //10BIT RESOLUTION

ADC1->CR2|=(1<<10);// EOCs is set

ADC1->SMPR2 |= (1<<3); //15 cycles

ADC1->SQR1 &= ~(1<<20); // 1 conversion

ADC1->SQR3 = 0x00;

}

uint16_t adc_read()

{

ADC1->CR2|=(1<<0);// ADC ON

ADC1->CR2|=(1<<30);// START CONVERSION

while(ADC1->SR&(1<<1)) // wait untill EOC bit set

{

ADC1->SR = 0X00;

adc_value1=ADC1->DR;

ADC1->CR2&=~(1<<0);// ADC OFF

ADC1->CR2&=~(1<<30);// STOP CONVERSION

}

return(adc_value1);

}
turboscrew
Senior III
June 14, 2018
Posted on June 14, 2018 at 06:26

Maybe you can check here:

https://github.com/turboscrew/blue_pill_init

It's for a 'blue pill' (stm32F103-board), but the other STM32 uC:s should wotk pretty much the same way.

But please don't write programs the way I wrote that. The 'program' is rather an executable 'document' to myself.

(Everything in one file, easy to search for text.)

I have never tried the external ADC:s, but the internal temperature sensor (

ADC1_IN16

) works fine.

Some other peripherals are used as well.

The DHT-sensor handling is better done by a timer alone and not with GPIOs as it's done there.

The only code that I didn't write myself is 'stdint.h', so no libraries or frameworks.

Yadav
YadavAuthor
Associate III
June 16, 2018
Posted on June 16, 2018 at 13:51

Hi.. Now I am working on STM32F4Doscovery board timers. I want to make a timer interrupt for an application. It can be 1sec or 500ms interrupt. I have read the datasheet and written the program accordingly. But i dont know how to use interrupt in STM32. I have tried something, but nothing is happening. Can someone suggest me a solution??

#include 'stm32f4xx.h'

// Component selection

void GPIO_Init(void);

void tim10_init(void);

int main(void)

{

GPIO_Init();

tim10_init();

while (1)

{

GPIOD->ODR|=(1<<15);

}

}

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

GPIOD->MODER|=(1<<30); // pin 15 is output

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

GPIOD->OSPEEDR|=(1<<30)|(1<<31);//LOW speed

GPIOD->PUPDR&=~(1<<30)|(1<<31);//No push pull

}

void tim10_init(void)

{

RCC->APB2ENR|=(1<<17);

TIM10->CR1|=(1<<7);

TIM10->DIER|=(1<<1);

TIM10->SR|=(1<<1);

TIM10->EGR|=(1<<1);

TIM10->CCMR1|=(1<<3)|(0<<1)|(0<<0);

TIM10->CCER|=(1<<0);

//TIM10->CCR1=5;

TIM10->PSC|=4200;

TIM10->ARR|=1000;

NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);

}

void TIM10_IRQHandler(void)

{

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

}

waclawek.jan
Super User
June 16, 2018
Posted on June 16, 2018 at 14:13

https://community.st.com/0D50X00009XkWGnSAN