Skip to content

Create microsecond timer #239

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions devices/board_config_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@
#ifndef NUM_VBAT_DESKTOP_FILE
#define NUM_VBAT_DESKTOP_FILE 0
#endif // NUM_VBAT_DESKTOP_FILE
#ifndef NUM_MHZ_TIMER
#define NUM MHZ_TIMER 0
#endif // NUM_MHZ_TIMER

/* Accelerometer */

Expand Down
42 changes: 42 additions & 0 deletions devices/timer_microsecond.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* timer_microsecond.c
*
* Created on: Mar 21, 2023
* Author: matth
*/

#include <board_config.h>
#include HAL_HEADER
#include "hal_callbacks.h"
#include "timer_microsecond.h"

#define MICROS_PER_MILLI 1000

static void timerMicrosecond_callback(void *pData) {
TimerMicrosecondCtrl_s *ctrl = (TimerMicrosecondCtrl_s *)pData;
ctrl->offset += 3600000000;
}

void timerMicrosecond_init(TimerMicrosecondCtrl_s *ctrl) {
// TODO so I have by default configured the IOC to count up at 1MHz, and
// overrun every 1 hour
ctrl->pTimer->Init.Prescaler = 240 - 1;
ctrl->pTimer->Init.Period = 3600000000;

ctrl->offset = 0;

// Ensure prescaler and period are set
TIM_Base_SetConfig(ctrl->pTimer->Instance, &(ctrl->pTimer->Init));

halCallbacks_registerTimPeriodElapsedCallback(
ctrl->pTimer, timerMicrosecond_callback, ctrl);
HAL_TIM_Base_Start_IT(ctrl->pTimer);
}

uint64_t timerMicrosecond_getTimestampMicros(TimerMicrosecondCtrl_s *ctrl) {
return ctrl->pTimer->Instance->CNT + ctrl->offset;
}

uint32_t timerMicrosecond_getTimestampMillis(TimerMicrosecondCtrl_s *ctrl) {
return timerMicrosecond_getTimestampMicros(ctrl) / MICROS_PER_MILLI;
}
23 changes: 23 additions & 0 deletions devices/timer_microsecond.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* timer_microsecond.c
*
* Created on: Mar 21, 2023
* Author: matth
*/

#include <board_config.h>
#include HAL_HEADER
#include "hal_callbacks.h"

#define MICROS_PER_MILLI 1000

typedef struct {
TIM_HandleTypeDef *pTimer;
volatile uint64_t offset;
} TimerMicrosecondCtrl_s;

void timerMicrosecond_init(TimerMicrosecondCtrl_s *ctrl);

uint64_t timerMicrosecond_getTimestampMicros(TimerMicrosecondCtrl_s *ctrl);

uint32_t timerMicrosecond_getTimestampMillis(TimerMicrosecondCtrl_s *ctrl);
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/adc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 2 additions & 0 deletions fcb_v0/Core/Inc/board_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
#define RADIO_CLI_ID 0
#define USB_CLI_ID 0

#define NUM_MHZ_TIMER 1

#define RADIO_TI_TYPE RADIO_TI_TYPE_CC1120

#endif // FCB_V0_CORE_INC_BOARD_CONFIG_H_
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/dma.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/iwdg.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
1 change: 1 addition & 0 deletions fcb_v0/Core/Inc/stm32h7xx_it.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ void DMA1_Stream6_IRQHandler(void);
void TIM2_IRQHandler(void);
void SPI1_IRQHandler(void);
void DMA1_Stream7_IRQHandler(void);
void TIM5_IRQHandler(void);
void SPI3_IRQHandler(void);
void UART5_IRQHandler(void);
void OTG_FS_IRQHandler(void);
Expand Down
4 changes: 3 additions & 1 deletion fcb_v0/Core/Inc/tim.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand All @@ -33,6 +33,7 @@ extern "C" {

extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim5;
extern TIM_HandleTypeDef htim8;

/* USER CODE BEGIN Private defines */
Expand All @@ -41,6 +42,7 @@ extern TIM_HandleTypeDef htim8;

void MX_TIM1_Init(void);
void MX_TIM2_Init(void);
void MX_TIM5_Init(void);
void MX_TIM8_Init(void);

void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Inc/usart.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
4 changes: 4 additions & 0 deletions fcb_v0/Core/Src/board_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,10 @@ uint8_t vbatAdcCurrentRank[NUM_VBAT_ADC] = {1};
I2C_HandleTypeDef* vbatIna226Hi2c[NUM_VBAT_INA226];
#endif // HAS_DEV(VBAT_INA226)

#if HAS_DEV(MHZ_TIMER)
TIM_HandleTypeDef timerMicrosecondHtim[NUM_MHZ_TIMER] = {&htim5};
#endif // HAS_DEV(MHZ_TIMER)

/* Watchdogs */

#if HAS_DEV(WATCHDOG_INTERNAL)
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/dma.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/iwdg.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
2 changes: 1 addition & 1 deletion fcb_v0/Core/Src/spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
Expand Down
15 changes: 15 additions & 0 deletions fcb_v0/Core/Src/stm32h7xx_it.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ extern DMA_HandleTypeDef hdma_spi3_rx;
extern SPI_HandleTypeDef hspi1;
extern SPI_HandleTypeDef hspi3;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim5;
extern DMA_HandleTypeDef hdma_uart5_rx;
extern UART_HandleTypeDef huart5;
/* USER CODE BEGIN EV */
Expand Down Expand Up @@ -349,6 +350,20 @@ void DMA1_Stream7_IRQHandler(void)
/* USER CODE END DMA1_Stream7_IRQn 1 */
}

/**
* @brief This function handles TIM5 global interrupt.
*/
void TIM5_IRQHandler(void)
{
/* USER CODE BEGIN TIM5_IRQn 0 */

/* USER CODE END TIM5_IRQn 0 */
HAL_TIM_IRQHandler(&htim5);
/* USER CODE BEGIN TIM5_IRQn 1 */

/* USER CODE END TIM5_IRQn 1 */
}

/**
* @brief This function handles SPI3 global interrupt.
*/
Expand Down
Loading