From bd9d86b168ad0c431cf074a741798ab3f5183b34 Mon Sep 17 00:00:00 2001 From: jazzpi Date: Mon, 1 Aug 2022 08:17:43 +0200 Subject: [PATCH] Move clock sync logic to separate files --- Core/Inc/CAN_Communication.h | 2 +- Core/Inc/Clock_Sync.h | 11 +++++++++++ Core/Src/Clock_Sync.c | 20 ++++++++++++++++++++ Core/Src/main.c | 7 ++++--- STM32Make.make | 1 + 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 Core/Inc/Clock_Sync.h create mode 100644 Core/Src/Clock_Sync.c diff --git a/Core/Inc/CAN_Communication.h b/Core/Inc/CAN_Communication.h index 01989fc..67102d3 100644 --- a/Core/Inc/CAN_Communication.h +++ b/Core/Inc/CAN_Communication.h @@ -21,7 +21,7 @@ #define SLAVE_STATUS_BASE_ADDRESS 0x600 #define SLAVE_CMD_BASE_ADDRESS 0x500 // #define SLAVE_EMERGENCY_ADDRESS 0x001 // Emergency Frame -#define TIME_SYNC_ADDRESS 0x002 +#define CLOCK_SYNC_ADDRESS 0x002 typedef struct { int16_t FrameID; diff --git a/Core/Inc/Clock_Sync.h b/Core/Inc/Clock_Sync.h new file mode 100644 index 0000000..ba00769 --- /dev/null +++ b/Core/Inc/Clock_Sync.h @@ -0,0 +1,11 @@ +#ifndef INC_CLOCK_SYNC_H_ +#define INC_CLOCK_SYNC_H_ + +#include "stm32g4xx_hal.h" +#include "stm32g4xx_hal_fdcan.h" + +void clock_sync_init(FDCAN_HandleTypeDef* can, TIM_HandleTypeDef* timer); + +void clock_sync_handle_timer_complete(); + +#endif // INC_CLOCK_SYNC_H_ diff --git a/Core/Src/Clock_Sync.c b/Core/Src/Clock_Sync.c new file mode 100644 index 0000000..ceabb9f --- /dev/null +++ b/Core/Src/Clock_Sync.c @@ -0,0 +1,20 @@ +#include "Clock_Sync.h" + +#include "CAN_Communication.h" + +#include "stm32g4xx_hal_fdcan.h" +#include "stm32g4xx_hal_tim.h" + +static FDCAN_HandleTypeDef* can; +static TIM_HandleTypeDef* timer; + +void clock_sync_init(FDCAN_HandleTypeDef* can_handle, + TIM_HandleTypeDef* timer_handle) { + can = can_handle; + timer = timer_handle; + HAL_TIM_Base_Start_IT(timer); +} + +void clock_sync_handle_timer_complete() { + CAN_Transmit(can, CLOCK_SYNC_ADDRESS, NULL, 0); +} diff --git a/Core/Src/main.c b/Core/Src/main.c index f696d1c..aa91e83 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -25,6 +25,7 @@ #include "AIR_State_Maschine.h" #include "AMS_Errorcodes.h" #include "CAN_Communication.h" +#include "Clock_Sync.h" #include "Error_Check.h" #include "SPI_Slave_Communication.h" #include "Slave_Monitoring.h" @@ -75,7 +76,7 @@ void setAMSError(void); /* USER CODE BEGIN 0 */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) { if (htim == &htim1) { - CAN_Transmit(&hfdcan1, TIME_SYNC_ADDRESS, NULL, 0); + clock_sync_handle_timer_complete(); } } @@ -119,11 +120,11 @@ int main(void) { /* USER CODE BEGIN 2 */ airstates = init_AIR_State_Maschine(); - initSlaves(); set_SPI_errorInfo(&defaulterrorhandle); spi_communication_init(&hspi1, &airstates); CAN_Init(&hfdcan1); - HAL_TIM_Base_Start_IT(&htim1); + clock_sync_init(&hfdcan1, &htim1); + initSlaves(); HAL_GPIO_WritePin(Status_LED_GPIO_Port, Status_LED_Pin, GPIO_PIN_SET); // setAMSError(); diff --git a/STM32Make.make b/STM32Make.make index 5ed0916..3f01046 100644 --- a/STM32Make.make +++ b/STM32Make.make @@ -38,6 +38,7 @@ BUILD_DIR = build C_SOURCES = \ Core/Src/AIR_State_Maschine.c \ Core/Src/CAN_Communication.c \ +Core/Src/Clock_Sync.c \ Core/Src/Error_Check.c \ Core/Src/SPI_Slave_Communication.c \ Core/Src/Slave_Monitoring.c \