From: Andy Spencer Date: Tue, 11 Mar 2014 04:00:55 +0000 (+0000) Subject: Debug PWM output X-Git-Url: http://pileus.org/git/?p=~andy%2Fcsm213a-hw;a=commitdiff_plain;h=44f6e18cbb1dbb1821a61dc18a0f80bf28e9a6e3 Debug PWM output --- diff --git a/hw2/control.c b/hw2/control.c index ad97c75..743c0a3 100644 --- a/hw2/control.c +++ b/hw2/control.c @@ -58,9 +58,10 @@ int main(int argc, char **argv) body.world.seconds = ts.tv_sec; body.world.nanosec = ts.tv_nsec; body.start.seconds = ts.tv_sec; - body.start.nanosec = ts.tv_nsec; - body.period.seconds = 1; - body.period.nanosec = 0; + body.start.nanosec = 0; //ts.tv_nsec; + body.period.seconds = 0; + body.period.nanosec = 100000000; + // [ ][ ] // Transmit message FILE *fd = fopen(opt_tty, "a+"); diff --git a/hw2/main.cpp b/hw2/main.cpp index f7b7671..5065c3d 100644 --- a/hw2/main.cpp +++ b/hw2/main.cpp @@ -29,18 +29,6 @@ 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 @@ -52,16 +40,6 @@ uint64_t time_to_world(uint64_t local) 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. @@ -148,10 +126,8 @@ void emit_init(int alt, PinName pin, PinMode mode) 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 @@ -159,27 +135,24 @@ void emit_init(int alt, PinName pin, PinMode mode) | 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; @@ -190,27 +163,28 @@ void emit_enable(uint64_t start, uint64_t 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) @@ -341,7 +315,7 @@ void serial_send_event(uint16_t event, uint64_t local) 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); @@ -646,7 +620,9 @@ void background(void) 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 diff --git a/hw2/makefile b/hw2/makefile index 0798282..83ff187 100644 --- a/hw2/makefile +++ b/hw2/makefile @@ -14,6 +14,7 @@ control: control.c messages.h run: all control install.sh @./install.sh $(PROG).bin @./control $(UART0) 0 + @sleep 0.0001 @./control $(UART1) 1 sync terms: