]> Pileus Git - ~andy/csm213a-hw/commitdiff
Debug PWM output
authorAndy Spencer <andy753421@gmail.com>
Tue, 11 Mar 2014 04:00:55 +0000 (04:00 +0000)
committerAndy Spencer <andy753421@gmail.com>
Tue, 11 Mar 2014 04:07:04 +0000 (04:07 +0000)
hw2/control.c
hw2/main.cpp
hw2/makefile

index ad97c75b887cdc47c265abede86b0b0024197793..743c0a32ce78f54bbc2b7246127c1cd22ccdb08c 100644 (file)
@@ -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+");
index f7b76715e07e170726d7106c2c59eb44f90bf122..5065c3d939acb8e3d3ae6373c69ca744595cdf10 100644 (file)
 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
index 07982822dc6d5f9c0c50a8310a621a3e61e6e3f5..83ff18724715542f5b945145213edf4ebdffc5a9 100644 (file)
@@ -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: