Send heartbeat packets every 100 ms
This commit is contained in:
parent
bd9d86b168
commit
5ad45cf53d
@ -22,6 +22,7 @@
|
||||
#define SLAVE_CMD_BASE_ADDRESS 0x500 //
|
||||
#define SLAVE_EMERGENCY_ADDRESS 0x001 // Emergency Frame
|
||||
#define CLOCK_SYNC_ADDRESS 0x002
|
||||
#define MASTER_HEARTBEAT_ADDRESS 0x010
|
||||
|
||||
typedef struct {
|
||||
int16_t FrameID;
|
||||
|
@ -1,11 +1,34 @@
|
||||
#ifndef INC_CLOCK_SYNC_H_
|
||||
#define INC_CLOCK_SYNC_H_
|
||||
|
||||
/**
|
||||
* @file Clock_Sync.h
|
||||
* @author Jasper v. Blanckenburg (j.blanckenburg@fasttube.de)
|
||||
* @brief Clock synchronization mechanism -- master side
|
||||
* @version 0.1
|
||||
* @date 2022-08-01
|
||||
*
|
||||
* @copyright Copyright (c) 2022
|
||||
*
|
||||
* The slaves' clocks are fairly inaccurate -- too inaccurate for reliable CAN
|
||||
* communication. In order to synchronize the clock frequencies, the slaves need
|
||||
* an external clock source. Since the HSE doesn't work on all slaves, we use
|
||||
* the master as the external clock source. For more detail, take a look at the
|
||||
* clock sync section in the slave code.
|
||||
*
|
||||
* This file handles the master part of the clock sync mechanism: It sends
|
||||
* CLOCK_SYNC frames every 1000 ms and MASTER_HEARTBEAT frames every 100 ms. In
|
||||
* order to keep the time between packets accurate, they are sent from timer
|
||||
* interrupts.
|
||||
*/
|
||||
|
||||
#include "stm32g4xx_hal.h"
|
||||
#include "stm32g4xx_hal_fdcan.h"
|
||||
#include "stm32g4xx_hal_tim.h"
|
||||
|
||||
void clock_sync_init(FDCAN_HandleTypeDef* can, TIM_HandleTypeDef* timer);
|
||||
void clock_sync_init(FDCAN_HandleTypeDef* can, TIM_HandleTypeDef* sync_timer,
|
||||
TIM_HandleTypeDef* heartbeat_timer);
|
||||
|
||||
void clock_sync_handle_timer_complete();
|
||||
void clock_sync_handle_timer_complete(TIM_HandleTypeDef* timer);
|
||||
|
||||
#endif // INC_CLOCK_SYNC_H_
|
||||
|
@ -2,19 +2,31 @@
|
||||
|
||||
#include "CAN_Communication.h"
|
||||
|
||||
#include "stm32g4xx_hal.h"
|
||||
#include "stm32g4xx_hal_fdcan.h"
|
||||
#include "stm32g4xx_hal_tim.h"
|
||||
|
||||
static FDCAN_HandleTypeDef* can;
|
||||
static TIM_HandleTypeDef* timer;
|
||||
static TIM_HandleTypeDef* sync_timer;
|
||||
static TIM_HandleTypeDef* heartbeat_timer;
|
||||
|
||||
void clock_sync_init(FDCAN_HandleTypeDef* can_handle,
|
||||
TIM_HandleTypeDef* timer_handle) {
|
||||
TIM_HandleTypeDef* sync_timer_handle,
|
||||
TIM_HandleTypeDef* heartbeat_timer_handle) {
|
||||
can = can_handle;
|
||||
timer = timer_handle;
|
||||
HAL_TIM_Base_Start_IT(timer);
|
||||
sync_timer = sync_timer_handle;
|
||||
heartbeat_timer = heartbeat_timer_handle;
|
||||
HAL_TIM_Base_Start_IT(heartbeat_timer);
|
||||
// Delay between starting the timers so the interrupts don't fire at the same
|
||||
// time
|
||||
HAL_Delay(50);
|
||||
HAL_TIM_Base_Start_IT(sync_timer);
|
||||
}
|
||||
|
||||
void clock_sync_handle_timer_complete() {
|
||||
CAN_Transmit(can, CLOCK_SYNC_ADDRESS, NULL, 0);
|
||||
void clock_sync_handle_timer_complete(TIM_HandleTypeDef* timer) {
|
||||
if (timer == sync_timer) {
|
||||
CAN_Transmit(can, CLOCK_SYNC_ADDRESS, NULL, 0);
|
||||
} else if (timer == heartbeat_timer) {
|
||||
CAN_Transmit(can, MASTER_HEARTBEAT_ADDRESS, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
@ -56,6 +56,7 @@ FDCAN_HandleTypeDef hfdcan1;
|
||||
SPI_HandleTypeDef hspi1;
|
||||
|
||||
TIM_HandleTypeDef htim1;
|
||||
TIM_HandleTypeDef htim8;
|
||||
|
||||
/* USER CODE BEGIN PV */
|
||||
|
||||
@ -68,6 +69,7 @@ static void MX_FDCAN1_Init(void);
|
||||
static void MX_SPI1_Init(void);
|
||||
static void MX_CRC_Init(void);
|
||||
static void MX_TIM1_Init(void);
|
||||
static void MX_TIM8_Init(void);
|
||||
/* USER CODE BEGIN PFP */
|
||||
void setAMSError(void);
|
||||
/* USER CODE END PFP */
|
||||
@ -75,8 +77,8 @@ void setAMSError(void);
|
||||
/* Private user code ---------------------------------------------------------*/
|
||||
/* USER CODE BEGIN 0 */
|
||||
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) {
|
||||
if (htim == &htim1) {
|
||||
clock_sync_handle_timer_complete();
|
||||
if (htim == &htim1 || htim == &htim8) {
|
||||
clock_sync_handle_timer_complete(htim);
|
||||
}
|
||||
}
|
||||
|
||||
@ -117,13 +119,14 @@ int main(void) {
|
||||
MX_SPI1_Init();
|
||||
MX_CRC_Init();
|
||||
MX_TIM1_Init();
|
||||
MX_TIM8_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
airstates = init_AIR_State_Maschine();
|
||||
set_SPI_errorInfo(&defaulterrorhandle);
|
||||
spi_communication_init(&hspi1, &airstates);
|
||||
CAN_Init(&hfdcan1);
|
||||
clock_sync_init(&hfdcan1, &htim1);
|
||||
clock_sync_init(&hfdcan1, &htim1, &htim8);
|
||||
initSlaves();
|
||||
|
||||
HAL_GPIO_WritePin(Status_LED_GPIO_Port, Status_LED_Pin, GPIO_PIN_SET);
|
||||
@ -340,6 +343,48 @@ static void MX_TIM1_Init(void) {
|
||||
/* USER CODE END TIM1_Init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief TIM8 Initialization Function
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
static void MX_TIM8_Init(void) {
|
||||
|
||||
/* USER CODE BEGIN TIM8_Init 0 */
|
||||
|
||||
/* USER CODE END TIM8_Init 0 */
|
||||
|
||||
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
|
||||
/* USER CODE BEGIN TIM8_Init 1 */
|
||||
|
||||
/* USER CODE END TIM8_Init 1 */
|
||||
htim8.Instance = TIM8;
|
||||
htim8.Init.Prescaler = 1599;
|
||||
htim8.Init.CounterMode = TIM_COUNTERMODE_UP;
|
||||
htim8.Init.Period = 999;
|
||||
htim8.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
||||
htim8.Init.RepetitionCounter = 0;
|
||||
htim8.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
||||
if (HAL_TIM_Base_Init(&htim8) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
|
||||
if (HAL_TIM_ConfigClockSource(&htim8, &sClockSourceConfig) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim8, &sMasterConfig) != HAL_OK) {
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM8_Init 2 */
|
||||
|
||||
/* USER CODE END TIM8_Init 2 */
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief GPIO Initialization Function
|
||||
* @param None
|
||||
|
@ -315,6 +315,17 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
|
||||
|
||||
/* USER CODE END TIM1_MspInit 1 */
|
||||
}
|
||||
else if(htim_base->Instance==TIM8)
|
||||
{
|
||||
/* USER CODE BEGIN TIM8_MspInit 0 */
|
||||
|
||||
/* USER CODE END TIM8_MspInit 0 */
|
||||
/* Peripheral clock enable */
|
||||
__HAL_RCC_TIM8_CLK_ENABLE();
|
||||
/* USER CODE BEGIN TIM8_MspInit 1 */
|
||||
|
||||
/* USER CODE END TIM8_MspInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -340,6 +351,17 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
|
||||
|
||||
/* USER CODE END TIM1_MspDeInit 1 */
|
||||
}
|
||||
else if(htim_base->Instance==TIM8)
|
||||
{
|
||||
/* USER CODE BEGIN TIM8_MspDeInit 0 */
|
||||
|
||||
/* USER CODE END TIM8_MspDeInit 0 */
|
||||
/* Peripheral clock disable */
|
||||
__HAL_RCC_TIM8_CLK_DISABLE();
|
||||
/* USER CODE BEGIN TIM8_MspDeInit 1 */
|
||||
|
||||
/* USER CODE END TIM8_MspDeInit 1 */
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
##########################################################################################################################
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Jul 30 22:35:37 CEST 2022]
|
||||
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Mon Aug 01 08:27:49 CEST 2022]
|
||||
##########################################################################################################################
|
||||
|
||||
# ------------------------------------------------
|
||||
|
@ -19,7 +19,8 @@ Mcu.IP3=RCC
|
||||
Mcu.IP4=SPI1
|
||||
Mcu.IP5=SYS
|
||||
Mcu.IP6=TIM1
|
||||
Mcu.IPNb=7
|
||||
Mcu.IP7=TIM8
|
||||
Mcu.IPNb=8
|
||||
Mcu.Name=STM32G441CBTx
|
||||
Mcu.Package=LQFP48
|
||||
Mcu.Pin0=PC13
|
||||
@ -36,6 +37,7 @@ Mcu.Pin18=VP_SYS_VS_Systick
|
||||
Mcu.Pin19=VP_SYS_VS_DBSignals
|
||||
Mcu.Pin2=PC15-OSC32_OUT
|
||||
Mcu.Pin20=VP_TIM1_VS_ClockSourceINT
|
||||
Mcu.Pin21=VP_TIM8_VS_ClockSourceINT
|
||||
Mcu.Pin3=PA5
|
||||
Mcu.Pin4=PA6
|
||||
Mcu.Pin5=PA7
|
||||
@ -43,7 +45,7 @@ Mcu.Pin6=PB0
|
||||
Mcu.Pin7=PA8
|
||||
Mcu.Pin8=PA9
|
||||
Mcu.Pin9=PA10
|
||||
Mcu.PinsNb=21
|
||||
Mcu.PinsNb=22
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32G441CBTx
|
||||
@ -149,7 +151,7 @@ ProjectManager.StackSize=0x400
|
||||
ProjectManager.TargetToolchain=Makefile
|
||||
ProjectManager.ToolChainLocation=
|
||||
ProjectManager.UnderRoot=false
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_FDCAN1_Init-FDCAN1-false-HAL-true,4-MX_SPI1_Init-SPI1-false-HAL-true,5-MX_CRC_Init-CRC-false-HAL-true
|
||||
ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_FDCAN1_Init-FDCAN1-false-HAL-true,4-MX_SPI1_Init-SPI1-false-HAL-true,5-MX_CRC_Init-CRC-false-HAL-true,6-MX_TIM1_Init-TIM1-false-HAL-true
|
||||
RCC.ADC12Freq_Value=16000000
|
||||
RCC.AHBFreq_Value=16000000
|
||||
RCC.APB1Freq_Value=16000000
|
||||
@ -204,6 +206,9 @@ TIM1.CounterMode=TIM_COUNTERMODE_UP
|
||||
TIM1.IPParameters=Prescaler,CounterMode,PeriodNoDither
|
||||
TIM1.PeriodNoDither=9999
|
||||
TIM1.Prescaler=1599
|
||||
TIM8.IPParameters=Prescaler,PeriodNoDither
|
||||
TIM8.PeriodNoDither=999
|
||||
TIM8.Prescaler=1599
|
||||
VP_CRC_VS_CRC.Mode=CRC_Activate
|
||||
VP_CRC_VS_CRC.Signal=CRC_VS_CRC
|
||||
VP_SYS_VS_DBSignals.Mode=DisableDeadBatterySignals
|
||||
@ -212,4 +217,6 @@ VP_SYS_VS_Systick.Mode=SysTick
|
||||
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
|
||||
VP_TIM1_VS_ClockSourceINT.Mode=Internal
|
||||
VP_TIM1_VS_ClockSourceINT.Signal=TIM1_VS_ClockSourceINT
|
||||
VP_TIM8_VS_ClockSourceINT.Mode=Internal
|
||||
VP_TIM8_VS_ClockSourceINT.Signal=TIM8_VS_ClockSourceINT
|
||||
board=custom
|
||||
|
Loading…
x
Reference in New Issue
Block a user