Hi!
I'm new to microcontroller electronics and I'm working on a project where I need to create a pulse counter for research purposes. I'm using an Arduino GIGA R1 WiFi board and need to count pulses with frequencies up to 10 MHz (each pulse can be as short as 100 ns).
I have successfully completed the same project on a BluePill board with an STM32F103 Nucleo, but I'm struggling to replicate it on the Arduino GIGA board. Below is my current code:
TIM_HandleTypeDef htim5;
void HAL_TIM5_Init(void);
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
HAL_Init();
HAL_TIM5_Init();
HAL_TIM_Base_MspInit(&htim5);
HAL_TIM_Base_Start(&htim5);
}
void loop() {
HAL_Delay(1);
uint32_t count = __HAL_TIM_GetCounter(&htim5);
Serial.println(count);
HAL_TIM_GenerateEvent(&htim5, TIM_EVENTSOURCE_UPDATE);
}
void HAL_TIM5_Init(void) {
htim5.Instance = TIM5;
htim5.Init.Prescaler = 0;
htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
htim5.Init.Period = 0xffff;
htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim5.Init.RepetitionCounter = 0;
htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim5) != HAL_OK) {
// Initialization Error
Error_Handler();
}
TIM_SlaveConfigTypeDef sSlaveConfig = {0};
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
sSlaveConfig.InputTrigger = TIM_TS_ETRF;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_NONINVERTED;
sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
sSlaveConfig.TriggerFilter = 0;
if (HAL_TIM_SlaveConfigSynchro(&htim5, &sSlaveConfig) != HAL_OK) {
// Synchronization Error
Error_Handler();
}
TIM_MasterConfigTypeDef sMasterConfig = {0};
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK) {
// Master Synchronization Error
Error_Handler();
}
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(htim_base->Instance == TIM5) {
/* Peripheral clock enable */
__HAL_RCC_TIM5_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM5 GPIO Configuration
PA3 ------> TIM5_ETR
*/
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
}
void Error_Handler(void) {
while(1) {
// Blink led
delay(200);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
digitalWrite(LED_BUILTIN, LOW);
}
}
Problem Description
In general, I just need to count how many pulses the Arduino receives in a specific amount of time (even less than 1 millisecond if needed) and print the count to the serial interface for debugging purposes.
The code compiles correctly, but the Arduino doesn't seem to be doing anything. The serial monitor doesn't show any output, and it appears that no pulses are being counted.
Additional Information
- The pulse signal is connected to pin PB3, which is mapped to PA3 on the STM32.
- I've tested the pulse signal separately and confirmed that it is working.
- I have also verified that the Arduino GIGA R1 WiFi board is functional and can run other sketches successfully.
- All must be done on the M7 nucleo.