Added lf timer clock and start sequencer.c to replace cron

main-solar-only
Richard Meadows 2015-11-20 18:15:06 +00:00
rodzic 939d30ebfe
commit 49a3439e03
7 zmienionych plików z 262 dodań i 50 usunięć

Wyświetl plik

@ -256,6 +256,8 @@
#define TC2_INT_PRIO 3 /* XOSC Measure Timer */
#define TC4_INT_PRIO 3 /* LF timer */
#define ADC_INT_PRIO 3 /* ADC */
#endif /* HW_CONFIG_H */

Wyświetl plik

@ -0,0 +1,31 @@
/*
* Data collection and transmission sequence
* Copyright (C) 2015 Richard Meadows <richardeoin>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SEQUENCER_H
#define SEQUENCER_H
#include "samd20.h"
void run_sequencer(uint32_t n);
#endif /* SEQUENCER_H */

Wyświetl plik

@ -41,14 +41,17 @@ void hf_clock_enable(void);
void hf_clock_disable(void);
/** GCLK0 */
void glck0_to_hf_clock(void);
void gclk0_to_hf_clock(void);
void gclk0_to_lf_clock(void);
/** GCLK1 */
void gclk1_enable(void);
void gclk1_disable(void);
void gclk1_init(void);
/** Measurement */
void measure_xosc(enum xosc_measurement_t measurement_t, measurement_result_t callback);
/** LF Timer */
void lf_tick_start(void);
void lf_tick_stop(void);
#endif /* XOSC_H */

Wyświetl plik

@ -69,6 +69,13 @@ void powermananger_init(void)
*/
void init(enum init_type init_t)
{
/**
* OSC8M should be considered unstable due to the temperature range. Therefore
* we need to switch to a stable low frequency clock right away.
* --------------------------------------------------------------------------
*/
gclk0_to_lf_clock();
/**
* Reset to get the system in a safe state
* --------------------------------------------------------------------------
@ -82,14 +89,15 @@ void init(enum init_type init_t)
* ---------------------------------------------------------------------------
*/
/* Switch to high frequency clock */
hf_clock_init();
hf_clock_enable();
gclk0_to_hf_clock();
gclk1_init();
/* Clock up to 14MHz with 0 wait states */
system_flash_set_waitstates(SYSTEM_WAIT_STATE_1_8V_14MHZ);
/* Up the clock rate to 4MHz */
system_clock_source_osc8m_set_config(SYSTEM_OSC8M_DIV_2, /* Prescaler */
false, /* Run in Standby */
false); /* Run on Demand */
/* Restart the GCLK Module */
system_gclk_init();
system_events_init();

Wyświetl plik

@ -40,9 +40,11 @@
#include "data.h"
#include "backlog.h"
#include "pips.h"
#include "xosc.h"
#include "sequencer.h"
#define CALLSIGN "UBSEDS11"
#define APRS_COMMENT "CONTESTIA/434.6"
#define CALLSIGN "UBSEDSX"
#define APRS_COMMENT ""
/**
* Formats a UKHAS telemetry string for the given datapoint
@ -179,16 +181,12 @@ void aprs_telemetry(struct tracker_datapoint* dp) {
aprs_set_datapoint(dp);
/* Set comment */
if ((dp->time.minute % 4) == 0) {
aprs_set_comment(APRS_COMMENT);
} else {
backlog_dp_ptr = get_backlog();
backlog_dp_ptr = get_backlog();
if (backlog_dp_ptr != NULL) { /* Backlog comment if we can */
aprs_set_backlog_comment(backlog_dp_ptr);
} else {
aprs_set_comment(APRS_COMMENT);
}
if (backlog_dp_ptr != NULL) { /* Backlog comment if we can */
aprs_set_backlog_comment(backlog_dp_ptr);
} else {
aprs_set_comment(APRS_COMMENT);
}
/* Set frequency */
@ -213,29 +211,30 @@ void aprs_telemetry(struct tracker_datapoint* dp) {
*/
void pips_telemetry(void)
{
/* Pips */
telemetry_start(TELEMETRY_PIPS, 0xFFFF);
/* Pips - 10 seconds */
telemetry_start(TELEMETRY_PIPS, 10);
while (telemetry_active()) {
idle(IDLE_TELEMETRY_ACTIVE);
}
}
volatile uint8_t tick_flag = 0;
volatile uint8_t run_flag = 1; /* run immediately after init */
/**
* Called at 1Hz by the GPS
* Called on each tick of the low frequency clock
*/
void gps_tick(uint32_t sequence)
void lf_tick(uint32_t tick)
{
/* Sequence not used */
(void)sequence;
/* When we're due to run again */
if (tick >= 20) {
/* Stop */
lf_tick_stop();
/* Update internal time representation */
cron_tick();
/* Raise the tick flag */
tick_flag = 1;
/* Raise the run flag */
run_flag = 1;
}
}
/**
@ -244,6 +243,8 @@ void gps_tick(uint32_t sequence)
*/
int main(void)
{
uint32_t n = 1;
/* Init */
init(INIT_NORMAL);
@ -253,10 +254,29 @@ int main(void)
/* Turn off LED to show we've initialised correctly */
led_off();
/* Clocks off */
gclk0_to_lf_clock();
hf_clock_disable();
while (1) {
/* Run cron job */
if (tick_flag) {
tick_flag = 0; do_cron();
/* Run sequence */
if (run_flag) {
run_flag = 0;
/* Clocks on */
hf_clock_enable();
gclk0_to_hf_clock();
/* Run */
//run_sequencer(n++);
for (int i = 0; i < 100*1000; i++);
/* Clocks off */
gclk0_to_lf_clock();
hf_clock_disable();
/* LF timing */
lf_tick_start();
}
/* Idle */

Wyświetl plik

@ -0,0 +1,103 @@
/*
* Data collection and transmission sequence
* Copyright (C) 2015 Richard Meadows <richardeoin>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <string.h>
#include "samd20.h"
#include "cron.h"
#include "gps.h"
#include "data.h"
#include "hw_config.h"
#include "watchdog.h"
#include "backlog.h"
#include "location.h"
void rtty_telemetry(struct tracker_datapoint* dp);
void contestia_telemetry(struct tracker_datapoint* dp);
void aprs_telemetry(struct tracker_datapoint* dp);
void pips_telemetry(void);
/**
* Run telemetry sequence
*/
void telemetry_sequence(struct tracker_datapoint* dp, uint32_t n)
{
/* Always update geofence */
telemetry_location_update(dp->longitude, dp->latitude);
#ifdef TELEMETRY_USE_GEOFENCE
if (telemetry_location_tx_allow()) {
#endif
/* Contestia */
/* if (t->second == TELEM_TOM) { */
/* contestia_telemetry(dp); */
/* /\* Pip *\/ */
/* } else if ((t->second % 1) == 0) { */
/* pips_telemetry(); */
/* } */
#ifdef TELEMETRY_USE_GEOFENCE
}
#endif
/* APRS */
#ifdef APRS_ENABLE
#endif
}
/**
* Run sequence n
*/
void run_sequencer(uint32_t n)
{
struct tracker_datapoint* dp;
/* Trigger GPS */
/* Async data */
collect_data_async();
/* Wait for GPS */
/* Data */
dp = collect_data();
/* Telemetry */
telemetry_sequence(dp, n);
/* Backlog */
if ((n % 60) == 10) { /* Every hour, start ten minutes */
record_backlog(dp);
}
}

Wyświetl plik

@ -112,7 +112,7 @@ void hf_clock_disable(void)
/* Disable TCXO to save power */
#ifdef SI4xxx_TCXO_REG_EN_PIN
port_pin_set_output_level(SI4xxx_TCXO_REG_EN_PIN, 1);
port_pin_set_output_level(SI4xxx_TCXO_REG_EN_PIN, 0);
#endif
#else
@ -133,10 +133,10 @@ void hf_clock_disable(void)
/**
* Switches GLCK0 to the HF clock
*/
void glck0_to_hf_clock(void)
void gclk0_to_hf_clock(void)
{
/* Configure GCLK0 to XOSC / OSC8M */
system_gclk_gen_set_config(GCLK_GENERATOR_1,
system_gclk_gen_set_config(GCLK_GENERATOR_0,
#if USE_XOSC
GCLK_SOURCE_XOSC, /* Source */
#else
@ -148,7 +148,7 @@ void glck0_to_hf_clock(void)
#else
OSC8M_GCLK_DIVIDE, /* Division Factor */
#endif
false, /* Run in standby */
true, /* Run in standby */
false); /* Output Pin Enable */
}
/**
@ -165,7 +165,7 @@ void gclk0_to_lf_clock(void)
#endif
false, /* High When Disabled */
1, /* Division Factor */
false, /* Run in standby */
true, /* Run in standby */
false); /* Output Pin Enable */
}
@ -176,9 +176,9 @@ void gclk0_to_lf_clock(void)
*/
/**
* Enables GCLK1. The appropriate source should have been disabled already
* Inits GCLK1. The appropriate source should have been enabled already
*/
void gclk1_enable(void)
void gclk1_init(void)
{
/* Configure GCLK1 */
system_gclk_gen_set_config(GCLK_GENERATOR_1,
@ -200,15 +200,6 @@ void gclk1_enable(void)
system_gclk_gen_enable(GCLK_GENERATOR_1);
}
/**
* Disable GCLK1
*/
void gclk1_disable(void)
{
system_gclk_gen_disable(GCLK_GENERATOR_1);
}
/**
* =============================================================================
* Measurement =======================================================
@ -425,3 +416,57 @@ void TC2_Handler(void) {
}
}
}
/**
* =============================================================================
* LF Tick =======================================================
* =============================================================================
*/
void lf_tick(uint32_t tick);
uint32_t lf_tick_count;
void lf_tick_start(void) {
/* Timer 4 runs on GCLK0 */
bool t4_capture_channel_enables[] = {false, false};
uint32_t t4_compare_channel_values[] = {64, 0x0000};
/* Divide by 64*256 = 16384 */
tc_init(TC4,
GCLK_GENERATOR_0,
TC_COUNTER_SIZE_16BIT,
TC_CLOCK_PRESCALER_DIV256,
TC_WAVE_GENERATION_MATCH_FREQ,
TC_RELOAD_ACTION_GCLK,
TC_COUNT_DIRECTION_UP,
TC_WAVEFORM_INVERT_OUTPUT_NONE,
false, /* Oneshot */
true, /* Run in standby */
0x0000, /* Initial value */
0x0000, /* Top value */
t4_capture_channel_enables, /* Capture Channel Enables */
t4_compare_channel_values); /* Compare Channels Values */
/* Enable Interrupt */
TC4->COUNT16.INTENSET.reg = TC_INTENSET_MC0;
irq_register_handler(TC4_IRQn, TC4_INT_PRIO); /* Low Priority */
tc_enable(TC4);
tc_start_counter(TC4);
lf_tick_count = 1;
}
void lf_tick_stop(void) {
tc_stop_counter(TC4);
tc_disable(TC4);
}
void TC4_Handler(void)
{
if (tc_get_status(TC4) & TC_STATUS_CHANNEL_0_MATCH) {
tc_clear_status(TC4, TC_STATUS_CHANNEL_0_MATCH);
lf_tick(lf_tick_count++);
}
}