uint64_t time_last_local; // timestamp at last time sync
uint64_t time_last_world; // offset at last time sync
-/**
- * Generate time stamp for an async event:
- * local: drift compensated wall-clock time
- * world: nanoseconds in world time world
- * valid: local timestamp at valid valid
- */
-//uint64_t time_to_local(uint64_t world, uint64_t valid)
-//{
-// uint64_t now =
-// local = + (stamp);
-//}
-
/**
* Generate time stamp for an async event:
* time: drift compensated wall-clock time
return time_last_world + elapsed;
}
-/**
- * Compensate the Real-Time-Clock oscillator for
- * temperature and drift errors. Called at 1Hz and
- * synchronous to the RTC 1Hz output.
- */
-void time_rtc_comp(void)
-{
- // todo
-}
-
/**
* Synchronize the timer internal state with updates
* from an external time sync message.
emit_pcr = (uint32_t*)(PORTA_BASE + pin);
// Enable clocks
- SIM->SCGC6 |= SIM_SCGC6_TPM0_MASK;
-
+ SIM->SCGC6 |= SIM_SCGC6_TPM1_MASK;
SIM->SOPT2 |= SIM_SOPT2_TPMSRC(1);
- SIM->SOPT4 |= SIM_SOPT4_TPM1CLKSEL_MASK;
// Set pin mode
emit_pcr[0] = PORT_PCR_ISF_MASK
| mode;
// Setup Timer/PWM Module
- TPM0->SC = TPM_SC_TOF_MASK
- | TPM_SC_PS(1); // 24 MHz clock ?
- TPM0->CNT = TPM_CNT_COUNT(0);
- TPM0->MOD = TPM_MOD_MOD(0xFFFF);
+ TPM1->SC = TPM_SC_TOF_MASK;
+ TPM1->CNT = TPM_CNT_COUNT(0);
+ TPM1->MOD = TPM_MOD_MOD(0xFFFF);
- TPM0->CONTROLS[0].CnSC = TPM_CnSC_CHF_MASK // clear flag
- | TPM_CnSC_MSB_MASK // pulse output on match
- | TPM_CnSC_MSA_MASK // ..
- | TPM_CnSC_ELSA_MASK; // pulse high
+ TPM1->CONTROLS[0].CnSC = TPM_CnSC_CHF_MASK // clear flag
+ | TPM_CnSC_MSB_MASK // set output highon match,
+ | TPM_CnSC_ELSB_MASK // cleared on overflow
+ | TPM_CnSC_ELSA_MASK; // ..
- TPM0->CONTROLS[0].CnV = 0xFFFF; // time delay
-
- TPM0->STATUS = TPM_STATUS_CH0F_MASK
+ TPM1->STATUS = TPM_STATUS_CH0F_MASK
| TPM_STATUS_TOF_MASK;
- TPM0->CONF = TPM_CONF_CSOO_MASK;
+ TPM1->CONF = TPM_CONF_CSOO_MASK;
}
void emit_enable(uint64_t start, uint64_t period)
{
- const int slack_tick = 0xC000; // tune based on emit_worst
+ const int slack_tick = 0x8000; // tune based on emit_worst
emit_start = start;
emit_period = period;
time_printf("emit scheduled", emit_due);
}
+#define CLOCKS(x) ((uint16_t)((x) * 24 / 1000))
+
void emit_schedule(uint64_t when)
{
- uint64_t now = time_to_world(tdma_time());
- uint16_t delay = (uint16_t)(when-now);
-
- // Clear pending flags
- //emit_pcr[0] |= PORT_PCR_ISF_MASK
+ uint64_t now = time_to_world(tdma_time());
+ uint64_t start = when - now;
+ uint64_t stop = start + 100000;
// Disable timer
- TPM0->SC = TPM_SC_TOF_MASK;
+ TPM1->SC = TPM_SC_TOF_MASK;
// Set transmit time
- TPM0->CNT = TPM_CNT_COUNT(0);
- TPM0->CONTROLS[0].CnV = delay;
+ TPM1->CONTROLS[0].CnV = CLOCKS(start);
+ TPM1->MOD = TPM_MOD_MOD(CLOCKS(stop));
// Start the timer
- TPM0->SC = TPM_SC_TOF_MASK
+ TPM1->SC = TPM_SC_TOF_MASK
+ | TPM_SC_PS(1)
| TPM_SC_CMOD(1);
// Debug
- sirq_printf("emitting event\r\n");
+ //sirq_printf("emitting event\r\n");
}
void emit_transmit(uint64_t local, uint64_t world)
header_t head;
event_msg_t body;
- uint64_t world = time_to_world(local);
+ uint64_t world = time_to_world(local);
ntime_t time = {};
time.seconds = (uint32_t)(world / NSEC_PER_SEC);
int main(int argc, char **argv)
{
tdma_init();
- emit_init(4, PTC1, PullDown);
+ emit_init(3, PTE20, PullDown);
+
+ //pin = 1;
// Open serial ports
sirq_dbg = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc