diff --git a/Core/Inc/AMS_CAN.h b/Core/Inc/AMS_CAN.h index 5a20b43..815b94a 100644 --- a/Core/Inc/AMS_CAN.h +++ b/Core/Inc/AMS_CAN.h @@ -17,7 +17,7 @@ #include #define CAN_ID_SLAVE_ERROR 0x001 -#define CAN_ID_TIME_SYNC 0x002 +#define CAN_ID_CLOCK_SYNC 0x002 #define CAN_ID_AMS_SLAVE_HEARTBEAT_BASE 0x600 #define CAN_HEARTBEAT_TX_TIMEOUT 10 /* ms */ diff --git a/Core/Inc/ClockSync.h b/Core/Inc/ClockSync.h new file mode 100644 index 0000000..b99a59d --- /dev/null +++ b/Core/Inc/ClockSync.h @@ -0,0 +1,17 @@ +#ifndef INC_CLOCK_SYNC_H_ +#define INC_CLOCK_SYNC_H_ + +#include + +#define CLOCK_TARGET_FREQ 16000000 // Hz +#define HSI_TRIM_FREQ 48000 // Hz +#define CLOCK_SYNC_INTERVAL 1000 // ms +#define RCC_CR_HSITRIM_MAX 31 + +#define STARTUP_CLOCK_SYNC_TIME 5000 // ms + +void clock_sync_handle_frame(); +void clock_sync_startup_check(); +void trim_hsi(int32_t delta); + +#endif // INC_CLOCK_SYNC_H_ diff --git a/Core/Inc/TimeSync.h b/Core/Inc/TimeSync.h deleted file mode 100644 index 6247e43..0000000 --- a/Core/Inc/TimeSync.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef INC_TIME_SYNC_H_ -#define INC_TIME_SYNC_H_ - -#include - -#define TARGET_FREQ 16000000 // Hz -#define TRIM_FREQ 48000 // Hz -#define TIME_SYNC_INTERVAL 1000 // ms -#define RCC_CR_HSITRIM_MAX 31 - -#define STARTUP_TIME_SYNC_TIME 5000 // ms - -void time_sync_handle_frame(); -void time_sync_startup_check(); -void trim_hsi(int32_t delta); - -#endif // INC_TIME_SYNC_H_ diff --git a/Core/Src/AMS_CAN.c b/Core/Src/AMS_CAN.c index c17d693..601a621 100644 --- a/Core/Src/AMS_CAN.c +++ b/Core/Src/AMS_CAN.c @@ -8,8 +8,8 @@ #include "AMS_CAN.h" #include "BQ_Abstraction_Layer.h" +#include "ClockSync.h" #include "TMP144.h" -#include "TimeSync.h" #include "common_defs.h" #include "main.h" @@ -39,7 +39,7 @@ void ams_can_init(CAN_HandleTypeDef* ams_handle, can_filter.FilterBank = 0; can_filter.FilterFIFOAssignment = CAN_FILTER_FIFO0; /* Message ID is in the MSBs of the FilterId register */ - can_filter.FilterIdHigh = CAN_ID_TIME_SYNC << (16 - 11); + can_filter.FilterIdHigh = CAN_ID_CLOCK_SYNC << (16 - 11); can_filter.FilterIdLow = 0; /* Filter the 11 MSBs (i.e. a StdId) */ can_filter.FilterMaskIdHigh = 0xFFE0; @@ -88,8 +88,8 @@ void ams_can_handle_ams_msg(CAN_RxHeaderTypeDef* header, uint8_t* data) { } switch (header->StdId) { - case CAN_ID_TIME_SYNC: - time_sync_handle_frame(); + case CAN_ID_CLOCK_SYNC: + clock_sync_handle_frame(); break; } } diff --git a/Core/Src/TimeSync.c b/Core/Src/ClockSync.c similarity index 62% rename from Core/Src/TimeSync.c rename to Core/Src/ClockSync.c index 2b61cfa..06ebabd 100644 --- a/Core/Src/TimeSync.c +++ b/Core/Src/ClockSync.c @@ -1,33 +1,33 @@ -#include "TimeSync.h" +#include "ClockSync.h" #include "stm32f412rx.h" #include "stm32f4xx_hal.h" #include -static uint32_t last_time_sync_frame = 0; +static uint32_t last_clock_sync_frame = 0; -void time_sync_handle_frame() { - static uint32_t f_pre_trim = TARGET_FREQ; - static uint8_t trimmed_last_frame = 0; - static int32_t last_trim_delta = TRIM_FREQ; +void clock_sync_handle_frame() { + static uint32_t f_pre_trim = CLOCK_TARGET_FREQ; + static int32_t trimmed_last_frame = 0; + static int32_t last_trim_delta = HSI_TRIM_FREQ; uint32_t now = HAL_GetTick(); - if (last_time_sync_frame != 0) { - uint32_t n_measured = now - last_time_sync_frame; - uint32_t f_real = n_measured * (TARGET_FREQ / TIME_SYNC_INTERVAL); + if (last_clock_sync_frame != 0) { + uint32_t n_measured = now - last_clock_sync_frame; + uint32_t f_real = n_measured * (CLOCK_TARGET_FREQ / CLOCK_SYNC_INTERVAL); if (trimmed_last_frame) { - last_trim_delta = (f_pre_trim - f_real) / trimmed_last_frame; + last_trim_delta = ((int32_t)(f_pre_trim - f_real)) / trimmed_last_frame; if (last_trim_delta < 0) { last_trim_delta = -last_trim_delta; } trimmed_last_frame = 0; } - int32_t delta_f = TARGET_FREQ - f_real; + int32_t delta_f = CLOCK_TARGET_FREQ - f_real; int32_t delta_quants = delta_f / last_trim_delta; if (delta_quants != 0) { - int32_t trim_delta = (now < STARTUP_TIME_SYNC_TIME) ? 2 : 1; + int32_t trim_delta = (now < STARTUP_CLOCK_SYNC_TIME) ? 2 : 1; if (delta_quants < 0) { trim_delta = -trim_delta; } @@ -36,15 +36,15 @@ void time_sync_handle_frame() { trimmed_last_frame = trim_delta; } } - last_time_sync_frame = now; + last_clock_sync_frame = now; } -void time_sync_startup_check() { +void clock_sync_startup_check() { static uint32_t last_startup_trim = 0; uint32_t now = HAL_GetTick(); - if (now - last_time_sync_frame > TIME_SYNC_INTERVAL && - now - last_startup_trim > TIME_SYNC_INTERVAL) { + if (now - last_clock_sync_frame > CLOCK_SYNC_INTERVAL && + now - last_startup_trim > CLOCK_SYNC_INTERVAL) { // The slave is probably warm, which increases the clock frequency. If the // frequency is too high, we won't receive any CAN frames anymore, so let's // try trimming down a bit. diff --git a/Core/Src/main.c b/Core/Src/main.c index 7fd7fd2..4b002a1 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -23,10 +23,10 @@ /* USER CODE BEGIN Includes */ #include "AMS_CAN.h" #include "BQ_Abstraction_Layer.h" +#include "ClockSync.h" #include "EEPROM.h" #include "FanControl.h" #include "TMP144.h" -#include "TimeSync.h" #include "common_defs.h" #include "stm32f4xx_hal.h" @@ -216,10 +216,10 @@ int main(void) { } fan_ctrl_update(); // Only start sending CAN frames once the clock is somewhat synchronized - if (HAL_GetTick() > STARTUP_TIME_SYNC_TIME) { + if (HAL_GetTick() > STARTUP_CLOCK_SYNC_TIME) { ams_can_send_heartbeat(); } else { - time_sync_startup_check(); + clock_sync_startup_check(); } delay_period(); } diff --git a/STM32Make.make b/STM32Make.make index fb2c1ed..3d20d8f 100644 --- a/STM32Make.make +++ b/STM32Make.make @@ -40,11 +40,11 @@ Core/Src/AMS_CAN.c \ Core/Src/BQ_Abstraction_Layer.c \ Core/Src/BQ_Communication.c \ Core/Src/BatteryManagement.c \ +Core/Src/ClockSync.c \ Core/Src/EEPROM.c \ Core/Src/FanControl.c \ Core/Src/SoftI2C.c \ Core/Src/TMP144.c \ -Core/Src/TimeSync.c \ Core/Src/main.c \ Core/Src/stm32f4xx_hal_msp.c \ Core/Src/stm32f4xx_it.c \