diff --git a/src/main.c b/src/main.c --- a/src/main.c +++ b/src/main.c @@ -12,27 +12,45 @@ #include "gpio.h" #include "gps.h" - -void sysclk_init(void); - #define WSPR_DEFAULT_FREQ 10140100UL #define WSPR_TONE_SPACING 146 // ~1.46 Hz #define WSPR_CTC 10672 // CTC value for WSPR +// Private functions +void sysclk_init(void); +void enter_sleep(void); +void enter_deepsleep(void); + +// Test stuff char call[7] = "KD8TDF"; char loc[5] = "EN72"; uint8_t dbm = 10; uint8_t tx_buffer[255]; - +// Frequencies and channel info uint32_t freq = WSPR_DEFAULT_FREQ; uint8_t symbol_count = WSPR_SYMBOL_COUNT; uint16_t ctc = WSPR_CTC; uint16_t tone_spacing = WSPR_TONE_SPACING; volatile uint8_t proceed = 0; +// Bring up TCXO and oscillator IC void encode_wspr(void) { + HAL_GPIO_WritePin(OSC_NOTEN, 0); + HAL_GPIO_WritePin(TCXO_EN, 1); + HAL_Delay(100); + + // Bring up the chip + si5351_init(i2c_get(), SI5351_CRYSTAL_LOAD_8PF, 0); + si5351_set_correction(0); + //si5351_set_pll(SI5351_PLL_FIXED, SI5351_PLLA); + //si5351_set_ms_source(SI5351_CLK0, SI5351_PLLA); + si5351_set_freq(WSPR_DEFAULT_FREQ * 100, 0, SI5351_CLK0); + si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_8MA); // Set for max power if desired (8ma max) + si5351_output_enable(SI5351_CLK0, 1); + //si5351_pll_reset(SI5351_PLLA); + // Encode message to transmit wspr_encode(call, loc, dbm, tx_buffer); @@ -53,8 +71,12 @@ void encode_wspr(void) // Disable transmitter si5351_output_enable(SI5351_CLK0, 0); + + HAL_GPIO_WritePin(OSC_NOTEN, 1); + HAL_GPIO_WritePin(TCXO_EN, 0); } + TIM_HandleTypeDef htim1; int main(void) @@ -67,8 +89,9 @@ int main(void) adc_init(); i2c_init(); - HAL_GPIO_WritePin(OSC_NOTEN, 0); - HAL_GPIO_WritePin(TCXO_EN, 1); + // Disable ICs + HAL_GPIO_WritePin(OSC_NOTEN, 1); + HAL_GPIO_WritePin(TCXO_EN, 0); // Start timer for WSPR @@ -91,19 +114,21 @@ int main(void) jtencode_init(); //gps_init(); - si5351_init(i2c_get(), SI5351_CRYSTAL_LOAD_8PF, 0); - si5351_set_correction(0); - //si5351_set_pll(SI5351_PLL_FIXED, SI5351_PLLA); - //si5351_set_ms_source(SI5351_CLK0, SI5351_PLLA); - si5351_set_freq(WSPR_DEFAULT_FREQ * 100, 0, SI5351_CLK0); - si5351_drive_strength(SI5351_CLK0, SI5351_DRIVE_2MA); // Set for max power if desired (8ma max) - si5351_output_enable(SI5351_CLK0, 1); - //si5351_pll_reset(SI5351_PLLA); uint32_t led_timer = HAL_GetTick(); uint32_t last_gps = HAL_GetTick(); uint32_t last_wspr = 0xfffff; // start immediately. + HAL_GPIO_TogglePin(LED_BLUE); + HAL_Delay(100); + HAL_GPIO_TogglePin(LED_BLUE); + HAL_Delay(100); + HAL_GPIO_TogglePin(LED_BLUE); + HAL_Delay(100); + HAL_GPIO_TogglePin(LED_BLUE); + HAL_Delay(100); + + while (1) { if(HAL_GetTick() - last_wspr > 120000) @@ -117,15 +142,37 @@ int main(void) HAL_GPIO_TogglePin(LED_BLUE); led_timer = HAL_GetTick(); } - if(HAL_GetTick() - last_gps > 100) + if(HAL_GetTick() - last_gps > 3000) { // gps_process(); last_gps = HAL_GetTick(); } + + enter_sleep(); } + } +void enter_sleep(void) +{ + //HAL_SuspendTick(); + HAL_TIM_Base_Stop_IT(&htim1); + HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI); + HAL_TIM_Base_Start_IT(&htim1); + //HAL_ResumeTick(); +} + +void enter_deepsleep(void) +{ + // Request to enter STOP mode with regulator in low power mode + HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); + // After wake-up from STOP reconfigure the PLL + sysclk_init(); +} + + + // Initialize system clocks void sysclk_init(void) {