Send heartbeat packets every 100 ms
This commit is contained in:
		@ -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 */
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user