kopia lustrzana https://github.com/mikaelnousiainen/RS41ng
Added a timepulse routine for DFM17 and also a millis() routine. Working toward clock calibration.
rodzic
7818133f52
commit
324c3bf83b
|
@ -34,8 +34,8 @@ void data_timer_init(uint32_t baud_rate)
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM2_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
||||
|
@ -48,8 +48,8 @@ void data_timer_uninit()
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM2_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelCmd = DISABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ void delay_init()
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM3_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
#include <stm32f10x_rcc.h>
|
||||
#include <stm32f10x_tim.h>
|
||||
#include <misc.h>
|
||||
|
||||
#include "src/hal/millis.h"
|
||||
|
||||
static uint32_t millis_counter;
|
||||
|
||||
void millis_timer_init(void)
|
||||
{
|
||||
TIM_DeInit(TIM7);
|
||||
|
||||
TIM_TimeBaseInitTypeDef tim_init;
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, ENABLE);
|
||||
RCC_APB2PeriphResetCmd(RCC_APB1Periph_TIM7, DISABLE);
|
||||
|
||||
// The data timer assumes a 24 MHz clock source
|
||||
tim_init.TIM_Prescaler = 24 - 1; // tick every 1/1000000 s
|
||||
tim_init.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
tim_init.TIM_Period = (uint16_t) (1000 - 1); // Timer pop 1/millisec
|
||||
tim_init.TIM_ClockDivision = TIM_CKD_DIV1;
|
||||
tim_init.TIM_RepetitionCounter = 0;
|
||||
|
||||
TIM_TimeBaseInit(TIM7, &tim_init);
|
||||
|
||||
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
|
||||
TIM_ITConfig(TIM7, TIM_IT_Update, ENABLE);
|
||||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM7_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
||||
TIM_Cmd(TIM7, ENABLE);
|
||||
}
|
||||
|
||||
void millis_timer_uninit()
|
||||
{
|
||||
TIM_Cmd(TIM7, DISABLE);
|
||||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM7_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelCmd = DISABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
||||
TIM_ITConfig(TIM7, TIM_IT_Update, DISABLE);
|
||||
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
|
||||
}
|
||||
|
||||
void TIM7_IRQHandler(void)
|
||||
{
|
||||
if (TIM_GetITStatus(TIM7, TIM_IT_Update) != RESET) {
|
||||
TIM_ClearITPendingBit(TIM7, TIM_IT_Update);
|
||||
|
||||
millis_counter++;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t millis(void)
|
||||
{
|
||||
return millis_counter;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
#ifndef __MILLIS_H
|
||||
#define __MILLIS_H
|
||||
|
||||
|
||||
extern void millis_timer_init(void);
|
||||
extern void millis_timer_uninit();
|
||||
|
||||
extern uint32_t millis();
|
||||
|
||||
#endif // __MILLIS_H
|
|
@ -43,8 +43,8 @@ void pwm_data_timer_init()
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM2_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
*/
|
||||
|
@ -117,7 +117,7 @@ void pwm_timer_init(uint32_t frequency_hz_100)
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = TIM1_BRK_TIM15_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
@ -164,8 +164,8 @@ void pwm_dma_init()
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = DMA1_Channel2_IRQn;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 1;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 0;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
}
|
||||
|
@ -246,6 +246,7 @@ inline void pwm_timer_set_frequency(uint32_t pwm_period)
|
|||
// TIM_CtrlPWMOutputs(TIM15, DISABLE);
|
||||
// TIM_Cmd(TIM15, DISABLE);
|
||||
|
||||
// TIM_SetAutoreload(TIM15, pwm_period);
|
||||
TIM_SetAutoreload(TIM15, pwm_period);
|
||||
// TIM_SetCompare2(TIM15, pwm_period / 2);
|
||||
|
||||
|
|
|
@ -360,7 +360,7 @@ void system_init()
|
|||
gpio_init();
|
||||
dma_adc_init();
|
||||
delay_init();
|
||||
|
||||
millis_timer_init();
|
||||
system_scheduler_timer_init();
|
||||
|
||||
RCC_ClocksTypeDef RCC_Clocks;
|
||||
|
|
|
@ -0,0 +1,88 @@
|
|||
#include "stm32f10x_exti.h"
|
||||
#include "stm32f10x_gpio.h"
|
||||
#include "stm32f10x.h"
|
||||
#include "stm32f10x_rcc.h"
|
||||
#include "misc.h"
|
||||
#include "config.h"
|
||||
#include "millis.h"
|
||||
#include "timepulse.h"
|
||||
|
||||
// This define copied from .../src/hal/stm_lib/src/stm32f10x_rcc.c
|
||||
#define CR_HSITRIM_Mask ((uint32_t)0xFFFFFF07)
|
||||
|
||||
// Define below pulls the current trim register value out of the Calibration Register (CR)
|
||||
// Resulting value will be between 0-31.
|
||||
|
||||
#define CURRENT_TRIM ((RCC->CR & ~CR_HSITRIM_Mask) >>3)
|
||||
|
||||
/*
|
||||
On the DFM-17, PB8 is wired to the GPS Timepulse. We take advantage of this to do a
|
||||
processor speed calibration. HSITRIM[4:0] allows for 32 values to adjust the HSI clock
|
||||
speed. The center (16) value is "neutral". Each trim value above or below 16 adjusts
|
||||
the clock by approximately 40kHZ (0.5% of the 8MHZ clock speed) (per AN2868). 0.5% is about
|
||||
5ms per second, so if we detect that we're off by more than 5 millisconds between timepulses, we
|
||||
will suggest a recalibration. The "calib_suggestion" variable is a static that will be maintained
|
||||
by the timepulse IRQ and can be used at any time it's convenient to adjust the clock speed.
|
||||
*/
|
||||
|
||||
int calib_suggestion = 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;
|
||||
|
||||
|
||||
void timepulse_init(void)
|
||||
{
|
||||
// Initialize pin PB8 as floating input
|
||||
GPIO_InitTypeDef gpio_init;
|
||||
gpio_init.GPIO_Pin = GPIO_Pin_8;
|
||||
gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
||||
gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
|
||||
GPIO_Init(GPIOB, &gpio_init);
|
||||
|
||||
// PB8 is connected to interrupt line 8, set trigger on the configured edge and enable the interrupt
|
||||
EXTI_InitTypeDef exti_init;
|
||||
exti_init.EXTI_Line = EXTI_Line8;
|
||||
exti_init.EXTI_Mode = EXTI_Mode_Interrupt;
|
||||
exti_init.EXTI_Trigger = EXTI_Trigger_Rising;
|
||||
exti_init.EXTI_LineCmd = ENABLE;
|
||||
EXTI_Init(&exti_init);
|
||||
|
||||
// Attach interrupt line to port B
|
||||
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource8);
|
||||
|
||||
// PB8 is connected to EXTI_Line8, which has EXTI9_5_IRQn vector. Use priority 0 for now.
|
||||
NVIC_InitTypeDef NVIC_InitStruct;
|
||||
NVIC_InitStruct.NVIC_IRQChannel = EXTI9_5_IRQn;
|
||||
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
|
||||
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
|
||||
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&NVIC_InitStruct);
|
||||
|
||||
// Pull the current calibration to start
|
||||
calib_suggestion = CURRENT_TRIM;
|
||||
}
|
||||
|
||||
// This handler is (at present) only being used for the Timepulse interrupt, so we shouldn't need
|
||||
// to do additional testing for the cause of the interrupt.
|
||||
|
||||
void EXTI9_5_IRQHandler(void)
|
||||
{
|
||||
uint32_t m = millis();
|
||||
int delta;
|
||||
|
||||
EXTI_ClearITPendingBit(EXTI_Line8);
|
||||
timepulsed++;
|
||||
if (old_millis == 0) {
|
||||
old_millis = m; // First timepulse. Just store millis.
|
||||
} else {
|
||||
d_millis = m - old_millis;
|
||||
delta = (int) (m - old_millis - 1000) / 5;
|
||||
if (((delta + calib_suggestion) >= 0) &&
|
||||
((delta + calib_suggestion <= 31)) ) {
|
||||
// If the delta makes sense, apply to the suggestion. Otherwise, skip.
|
||||
calib_suggestion += delta;
|
||||
}
|
||||
old_millis = m;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#ifndef __TIMEPULSE_H
|
||||
#define __TIMEPULSE_H
|
||||
|
||||
extern void timepulse_init(void);
|
||||
extern int calib_suggestion;
|
||||
extern volatile int timepulsed;
|
||||
extern volatile uint32_t d_millis;
|
||||
|
||||
#endif // __TIMEPULSE_H
|
|
@ -49,7 +49,7 @@ void usart_gps_init(uint32_t baud_rate, bool enable_irq)
|
|||
|
||||
NVIC_InitTypeDef nvic_init;
|
||||
nvic_init.NVIC_IRQChannel = USART_IRQ;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 15;
|
||||
nvic_init.NVIC_IRQChannelPreemptionPriority = 3;
|
||||
nvic_init.NVIC_IRQChannelSubPriority = 2;
|
||||
nvic_init.NVIC_IRQChannelCmd = ENABLE;
|
||||
NVIC_Init(&nvic_init);
|
||||
|
|
12
src/main.c
12
src/main.c
|
@ -12,6 +12,7 @@
|
|||
#include "radio.h"
|
||||
#include "config.h"
|
||||
#include "log.h"
|
||||
#include "hal/timepulse.h"
|
||||
|
||||
#ifdef RS41
|
||||
#include "hal/i2c.h"
|
||||
|
@ -115,6 +116,17 @@ int main(void)
|
|||
goto gps_init;
|
||||
}
|
||||
|
||||
#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, d_millis);
|
||||
timepulsed = 0;
|
||||
}
|
||||
}
|
||||
#endif //DFM17
|
||||
|
||||
#if defined(RS41)
|
||||
log_info("Si4032 init\n");
|
||||
si4032_init();
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
|
||||
// TODO: Add support for multiple APRS baud rates
|
||||
// This delay is for DFM-17 radiosondes
|
||||
#define symbol_delay_bell_202_1200bps_us 820
|
||||
#define symbol_delay_bell_202_1200bps_us 821
|
||||
|
||||
static volatile bool radio_si4063_state_change = false;
|
||||
static volatile uint32_t radio_si4063_freq = 0;
|
||||
|
|
Ładowanie…
Reference in New Issue