Cleaned up clock calibration code and integrated into the main radio loop. Also added APRS telemetry for calibration.

pull/62/head
Mike Hojnowski 2023-10-11 23:30:21 -04:00
rodzic 0901b02de4
commit a34c9b9e1f
6 zmienionych plików z 52 dodań i 17 usunięć

Wyświetl plik

@ -30,7 +30,7 @@ size_t aprs_generate_position(uint8_t *payload, size_t length, telemetry_data *d
return snprintf((char *) payload,
length,
("%s%02d%02d.%02u%c%c%03d%02u.%02u%c%c%03d/%03d/A=%06d/P%dS%dT%02dV%04dC%02d%s"),
("%s%02d%02d.%02u%c%c%03d%02u.%02u%c%c%03d/%03d/A=%06d/P%dS%dT%02dV%04dC%02dR%02dU%02d%s"),
timestamp,
abs(la_degrees), la_minutes, la_h_minutes,
la_degrees > 0 ? 'N' : 'S',
@ -46,6 +46,8 @@ size_t aprs_generate_position(uint8_t *payload, size_t length, telemetry_data *d
(int) data->internal_temperature_celsius_100 / 100,
data->battery_voltage_millivolts,
(int16_t) ((float) data->gps.climb_cm_per_second / 100.0f),
data->clock_calibration,
data->clock_calibration_count,
comment
);
}

Wyświetl plik

@ -4,6 +4,7 @@
#include "stm32f10x_rcc.h"
#include "misc.h"
#include "config.h"
#include "system.h"
#include "millis.h"
#include "timepulse.h"
@ -26,11 +27,35 @@ by the timepulse IRQ and can be used at any time it's convenient to adjust the c
*/
int calib_suggestion = 16; // Default, but we will check it in the init routine below.
int calib_current = 16; // Default, but we will check it in the init routine below.
uint32_t old_millis = 0;
volatile int timepulsed = 0;
volatile uint32_t d_millis = 0;
bool yellowLEDstate = true;
uint16_t calib_change_count = 0;
uint8_t get_clock_calibration(void)
{
return(CURRENT_TRIM);
}
uint16_t get_calib_change_count(void)
{
return(calib_change_count);
}
void adjust_clock_calibration(void)
{
if (calib_suggestion != calib_current) {
RCC_AdjustHSICalibrationValue(calib_suggestion);
calib_current = calib_suggestion;
yellowLEDstate = !yellowLEDstate;
system_set_yellow_led(yellowLEDstate);
calib_change_count++;
}
}
void timepulse_init(void)
{
// Initialize pin PB8 as floating input
@ -60,7 +85,11 @@ void timepulse_init(void)
NVIC_Init(&NVIC_InitStruct);
// Pull the current calibration to start
calib_suggestion = CURRENT_TRIM;
calib_current = CURRENT_TRIM;
calib_suggestion = calib_current;
// Set the yellow LED to help identify calibration changes
system_set_yellow_led(yellowLEDstate);
}
// This handler is (at present) only being used for the Timepulse interrupt, so we shouldn't need
@ -76,8 +105,9 @@ void EXTI9_5_IRQHandler(void)
if (old_millis == 0) {
old_millis = m; // First timepulse. Just store millis.
} else {
d_millis = m - old_millis;
delta = (int) (1000 - d_millis) / 5;
d_millis = m - old_millis; // mS since last timepulse. Ideally there were 1000.
delta = (int) (1000 - d_millis) / 5; // If too few clicks, speed up clock. If too many, slow down.
// Don't allow calibration suggestion to go out of range
if (((delta + calib_suggestion) >= 0) &&
((delta + calib_suggestion <= 31)) ) {
// If the delta makes sense, apply to the suggestion. Otherwise, skip.

Wyświetl plik

@ -1,9 +1,11 @@
#ifndef __TIMEPULSE_H
#define __TIMEPULSE_H
extern uint8_t get_clock_calibration(void);
extern uint16_t get_calib_change_count(void);
extern void timepulse_init(void);
extern int calib_suggestion;
extern void adjust_clock_calibration(void);
extern volatile int timepulsed;
extern volatile uint32_t d_millis;
#endif // __TIMEPULSE_H

Wyświetl plik

@ -92,9 +92,6 @@ void set_yellow_led(bool enabled)
int main(void)
{
bool success;
bool yellowLEDstate = false;
int old_calib_suggestion = 16;
int calib_changes = 0;
// Set up interrupt handlers
system_handle_timer_tick = handle_timer_tick;
@ -136,19 +133,15 @@ int main(void)
#ifdef DFM17
log_info("Timepulse init\n");
timepulse_init();
/*
while (1) {
if (timepulsed != 0) {
log_info("Time Pulse. Calib: %d, Delta: %d\n", calib_suggestion, (int) d_millis);
log_info("Time Pulse. Calib: %d\n", get_clock_calibration());
timepulsed = 0;
if (calib_suggestion != old_calib_suggestion) {
old_calib_suggestion = calib_suggestion;
RCC_AdjustHSICalibrationValue(calib_suggestion);
yellowLEDstate = !yellowLEDstate;
set_yellow_led(yellowLEDstate);
calib_changes++;
adjust_clock_calibration();
}
}
}
*/
#endif //DFM17
#if defined(RS41)
@ -211,6 +204,9 @@ int main(void)
while (true) {
radio_handle_main_loop();
#ifdef DFM17
adjust_clock_calibration();
#endif //DFM17
//NVIC_SystemLPConfig(NVIC_LP_SEVONPEND, DISABLE);
//__WFI();
}

Wyświetl plik

@ -7,6 +7,7 @@
#include "locator.h"
#include "config.h"
#include "log.h"
#include "hal/timepulse.h"
#ifdef RS41
#include "drivers/si4032/si4032.h"
@ -70,6 +71,8 @@ void telemetry_collect(telemetry_data *data)
data->gps.heading_degrees_100000 = 0;
data->gps.climb_cm_per_second = 0;
}
data->clock_calibration = get_clock_calibration();
data->clock_calibration_count = get_calib_change_count();
locator_from_lonlat(data->gps.longitude_degrees_1000000, data->gps.latitude_degrees_1000000,
LOCATOR_PAIR_COUNT_FULL, data->locator);

Wyświetl plik

@ -21,6 +21,8 @@ typedef struct _telemetry_data {
gps_data gps;
char locator[LOCATOR_PAIR_COUNT_FULL * 2 + 1];
int clock_calibration;
uint16_t clock_calibration_count;
} telemetry_data;
void telemetry_collect(telemetry_data *data);