Skip to main content
NHube.2
Associate
April 29, 2021
Solved

Why does my code only work on STM32 L476RG and not G474RE

  • April 29, 2021
  • 2 replies
  • 809 views

Hi,

I want to use the ETR to count pulses and set a second timer, so every ... seconds the value from the ETR counter is red and renewed like discribed in this Example

STM32 Frequency Counter – LAB10 

https://deepbluembedded.com/stm32-counter-mode-example-frequency-counter-timer-in-counter-mode/

The code is

#include "main.h"
uint32_t gu32_CounterTicks = 0x00;
uint32_t gu32_Freq = 0x00;
uint8_t gu8_MSG[40] = {'\0'};
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim5;
UART_HandleTypeDef huart1;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_TIM5_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
 HAL_Init();
 SystemClock_Config();
 MX_GPIO_Init();
 MX_TIM2_Init();
 MX_TIM5_Init();
 MX_USART1_UART_Init();
 HAL_TIM_Base_Start(&htim2);
 HAL_TIM_Base_Start_IT(&htim5);
 while (1)
 {
 }
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim)
{
 if(htim->Instance == TIM5)
 {
 	gu32_CounterTicks = TIM2->CNT;
 	gu32_Freq = gu32_CounterTicks * 20;
 	sprintf(gu8_MSG, "Frequency = %d Hz\n\r", gu32_Freq);
 	HAL_UART_Transmit(&huart1, gu8_MSG, sizeof(gu8_MSG), 100);
 	TIM5->CNT = 0;
 	TIM2->CNT = 0;
 }
}

while the example works fine on the L476RG I cant get it working on the G474RE.

Both have an ETR2 read mode at timer 2 and timer 5 is a normal timer that can be set to run a specific time.

I had a look on the datasheets of both Nucleo Devboards, but I couldnt find a difference that explains why the code only works on the L476RG and I need it to work on the G474RE too.

Thanks in advance

This topic has been closed for replies.
Best answer by NHube.2

Hi and thank you for the response!

Yesterday I solved the problem by changing the lines

gu32_CounterTicks = TIM2->CNT;

and

TIM5->CNT = 0;

to

gu32_CounterTicks = __HAL_TIM_GET_COUNTER(&htim2);

and

__HAL_TIM_SET_COUNTER(&htim2,0);

but I have no idea why the access without HAL wont work on my G474RE while it works on the 476RG. (Clock and Pinout was exactly the same and the code flashing returned without errors.)

2 replies

Technical Moderator
May 5, 2021

Hello @NHube.2​  and welcome to the STM32 Community :)

Did you checked the pin and clock configuration ?

What kind of issue do you observed?  Do you get an error message ?

Try to debug and localize where the code hangs/stop and the kind of message gives.

Imen

In order to give better visibility on the answered topics, please click on 'Best answer' on the reply which solved your issue or answered your question. Thanks
NHube.2
NHube.2AuthorBest answer
Associate
May 5, 2021

Hi and thank you for the response!

Yesterday I solved the problem by changing the lines

gu32_CounterTicks = TIM2->CNT;

and

TIM5->CNT = 0;

to

gu32_CounterTicks = __HAL_TIM_GET_COUNTER(&htim2);

and

__HAL_TIM_SET_COUNTER(&htim2,0);

but I have no idea why the access without HAL wont work on my G474RE while it works on the 476RG. (Clock and Pinout was exactly the same and the code flashing returned without errors.)