TimeSync -> ClockSync
This commit is contained in:
parent
29e15fc0f7
commit
2f267110a9
@ -17,7 +17,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#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 */
|
||||
|
||||
|
17
Core/Inc/ClockSync.h
Normal file
17
Core/Inc/ClockSync.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef INC_CLOCK_SYNC_H_
|
||||
#define INC_CLOCK_SYNC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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_
|
@ -1,17 +0,0 @@
|
||||
#ifndef INC_TIME_SYNC_H_
|
||||
#define INC_TIME_SYNC_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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_
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,33 @@
|
||||
#include "TimeSync.h"
|
||||
#include "ClockSync.h"
|
||||
|
||||
#include "stm32f412rx.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
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.
|
@ -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();
|
||||
}
|
||||
|
@ -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 \
|
||||
|
Loading…
x
Reference in New Issue
Block a user