]> Pileus Git - ~andy/csm213a-hw/blobdiff - hw2/main.cpp
Cleanup comments and move test.cpp
[~andy/csm213a-hw] / hw2 / main.cpp
index d4c0b046737222fb00127e02c68619fc32c76359..2c0f2746e716f3064431fc1ee08901322fb5167e 100644 (file)
  *   Time synchronization is performed in both directions.
  */
 
-/****************
- * Testing vars *
- ****************/
-
-uint32_t test_xmt_enab  = 0;
-uint64_t test_xmt_time0 = 0;
-uint64_t test_xmt_time1 = 0;
-
-uint32_t test_rcv_enab  = 0;
-uint64_t test_rcv_time  = 0;
-
 /*******************
  * Timer functions *
  *******************/
@@ -40,18 +29,6 @@ uint64_t test_rcv_time  = 0;
 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
@@ -63,14 +40,10 @@ 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)
+uint64_t time_to_local(uint64_t world)
 {
-       // todo
+       uint64_t elapsed = world - time_last_world;
+       return time_last_local + elapsed;
 }
 
 /**
@@ -127,16 +100,6 @@ void time_ext_sync(uint64_t local, uint64_t world)
                        (uint32_t)(error % (int64_t)NSEC_PER_SEC));
 #endif
 //#endif
-
-       // .000000284
-       // .000000253
-       // .000000264
-       // .000000451
-       // .000000284
-       // .000000267
-       // .000000223
-       // .000000326
-
 }
 
 void time_printf(const char *label, uint64_t local)
@@ -154,13 +117,44 @@ void time_printf(const char *label, uint64_t local)
  * Signal generation *
  *********************/
 
+// for 50 Mhz clock  50/1000 = 1/20  (PLL/2)
+
+// for 48 Mhz clock  48/1000 = 6/125 (FLL)
+// for 24 Mhz clock, 24/1000 = 3/125
+// for 12 Mhz clock, 12/1000 = 3/250
+// for  6 Mhz clock,  6/1000 = 3/500
+// for  3 Mhz clock,  3/1000 = 3/1000
+
+#define EMIT_PS 1
+
+//#if EMIT_PS == 0
+//#define EMIT_CLOCKS(nsec) ((uint16_t)((nsec)   / 20))
+//#define EMIT_NSEC(clocks) ((uint16_t)((clocks) * 20))
+
+#if EMIT_PS == 0
+#define EMIT_CLOCKS(nsec) ((uint32_t)((nsec)   * 6 / 125))
+#define EMIT_NSEC(clocks) ((uint32_t)((clocks) * 125 / 6))
+#elif EMIT_PS == 1
+#define EMIT_CLOCKS(nsec) ((uint32_t)((nsec)   * 3 / 125))
+#define EMIT_NSEC(clocks) ((uint32_t)((clocks) * 125 / 3))
+#elif EMIT_PS == 2
+#define EMIT_CLOCKS(nsec) ((uint32_t)((nsec)   * 3 / 250))
+#define EMIT_NSEC(clocks) ((uint32_t)((clocks) * 250 / 3))
+#elif EMIT_PS == 3
+#define EMIT_CLOCKS(nsec) ((uint32_t)((nsec)   * 3 / 500))
+#define EMIT_NSEC(clocks) ((uint32_t)((clocks) * 500 / 3))
+#elif EMIT_PS == 4
+#define EMIT_CLOCKS(nsec) ((uint32_t)((nsec)   * 3 / 1000))
+#define EMIT_NSEC(clocks) ((uint32_t)((clocks) * 1000 / 3))
+#endif
+
 static uint32_t *emit_pcr    = 0; // transmit pin name
 
-static uint64_t  emit_start  = 0; // transmit start time
+static uint64_t  emit_start  = 0; // transmit start time (world time)
 static uint64_t  emit_period = 0; // transmit period
-static uint64_t  emit_due    = 0; // next transmit time
+static uint64_t  emit_due    = 0; // next transmit (world time)
 
-static uint32_t  emit_slack  = 0; // how far ahead we need to schedule
+static uint32_t  emit_slack  = 0; // how far ahead we need to schedule, in us
 static uint32_t  emit_worst  = 0; // worst-case latency in task table
 
 void emit_init(int alt, PinName pin, PinMode mode)
@@ -169,10 +163,22 @@ 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;
+
+       // Reset PLL Source
+       //SIM->SOPT2 &= ~SIM_SOPT2_PLLFLLSEL_MASK;
+
+       // Debug print on SOPT2
+       // -- mbed may set PLLFLL when configuring UART0
+       // SOPT2: u0src=1 tpmsrc=1 USBSRC PLL/2 clkos=0 rtcos
+       sirq_printf("SOPT2: u0src=%d tpmsrc=%d %s %s clkos=%d %s\r\n",
+               (SIM->SOPT2 & SIM_SOPT2_UART0SRC_MASK)     >> SIM_SOPT2_UART0SRC_SHIFT,
+               (SIM->SOPT2 & SIM_SOPT2_TPMSRC_MASK)       >> SIM_SOPT2_TPMSRC_SHIFT,
+               (SIM->SOPT2 & SIM_SOPT2_UART0SRC_MASK)     ? "USBSRC" : "usbsrc",
+               (SIM->SOPT2 & SIM_SOPT2_PLLFLLSEL_MASK)    ? "PLL/2"  : "FLL",
+               (SIM->SOPT2 & SIM_SOPT2_CLKOUTSEL_MASK)    >> SIM_SOPT2_CLKOUTSEL_SHIFT,
+               (SIM->SOPT2 & SIM_SOPT2_RTCCLKOUTSEL_MASK) ? "RTCOS"  : "rtcos");
 
        // Set pin mode
        emit_pcr[0]            = PORT_PCR_ISF_MASK
@@ -180,69 +186,104 @@ 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);
-
-       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->SC               = TPM_SC_TOF_MASK;
+       TPM1->CNT              = TPM_CNT_COUNT(0);
+       TPM1->MOD              = TPM_MOD_MOD(0xFFFF);
 
-       TPM0->CONTROLS[0].CnV  = 0xFFFF;              // time delay
+       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->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_clocks = 0x4000; // tune based on emit_worst
+
        emit_start  = start;
        emit_period = period;
        emit_due    = start + period;
 
-       emit_slack  = 10000; // tune based on emit_worst
+       emit_slack  = EMIT_NSEC(slack_clocks);
 
-       time_printf("scheuled emit - ", emit_due);
+       time_printf("emit scheduled", emit_due);
 }
 
 void emit_schedule(uint64_t when)
 {
-       uint64_t now   = tdma_time();
-       uint16_t delay = (uint16_t)(when-now);
-
-       // Clear pending flags
-       //emit_pcr[0]           |= PORT_PCR_ISF_MASK
+       uint64_t local  = time_to_local(when) * 3 / 125;
+       uint32_t width  = EMIT_CLOCKS(10000);
 
        // Disable timer
-       TPM0->SC               = TPM_SC_TOF_MASK;
+       TPM1->SC               = TPM_SC_TOF_MASK;
+
+       __disable_irq();
+
+       uint64_t now    = ((uint64_t)~PIT->LTMR64H << 32)
+                       | ((uint64_t)~PIT->LTMR64L);
+       uint32_t delta  = local - now;
+       uint32_t start  = delta >> (EMIT_PS-1); // convert to clocks
+       uint32_t stop   = start + width;        // end time
 
        // Set transmit time
-       TPM0->CNT              = TPM_CNT_COUNT(0);
-       TPM0->CONTROLS[0].CnV  = delay;
+       TPM1->CONTROLS[0].CnV  = start;
+       TPM1->MOD              = TPM_MOD_MOD(stop);
 
        // Start the timer
-       TPM0->SC               = TPM_SC_TOF_MASK
+       TPM1->SC               = TPM_SC_TOF_MASK
+                              | TPM_SC_PS(EMIT_PS)
                               | TPM_SC_CMOD(1);
+
+       __enable_irq();
+
+       // Test
+       //int64_t  cnv = TPM1->CONTROLS[0].CnV;
+       //int64_t  mod = TPM1->MOD;
+       //int64_t  due = local - tdma_time();
+       //sirq_printf("%6d -- cnv=%04x mod=%04x due=%04x start=%04x\r\n",
+       //              (int)(cnv - EMIT_CLOCKS(due)),
+       //              (int)cnv, (int)mod,
+       //              (int)EMIT_CLOCKS(due), EMIT_CLOCKS(start));
+
+       // Clock testing
+       //uint32_t test_tpm0 =  TPM1->CNT;
+       //uint32_t test_pit0 = ~PIT->CHANNEL[0].CVAL;
+       //for (int i = 0; i < 100; i++)
+       //      asm("nop");
+       //uint32_t test_tpm1 =  TPM1->CNT;
+       //uint32_t test_pit1 = ~PIT->CHANNEL[0].CVAL;
+
+       //uint32_t test_tpm  = test_tpm1 - test_tpm0;
+       //uint32_t test_pit  = test_pit1 - test_pit0;
+       //sirq_printf("pit/tpm: %d - tpm=%08x/%08x=%d pit=%08x/%08x=%d\r\n",
+       //              test_tpm - test_pit,
+       //              test_tpm0, test_tpm1, test_tpm,
+       //              test_pit0, test_pit1, test_pit);
+
+       // Debug output
+       //time_printf("emitting event", when);
 }
 
-void emit_transmit(void)
+void emit_transmit(uint64_t local, uint64_t world)
 {
-       static uint64_t prev;
-
-       // Get a fresh timestamp
-       uint64_t world = tdma_time();
+       static uint64_t prev = 0;
 
        // Record how how much time we have to reschedule
-       if (prev && world - prev > emit_worst)
-               emit_worst = prev;
+       if (prev && (local-prev) > emit_worst)
+               emit_worst = (local-prev);
+       prev = local;
 
        // Schedule task if needed
-       if (world+emit_slack > emit_due)
+       if (emit_due && emit_period &&
+           world+emit_slack > emit_due) {
                emit_schedule(emit_due);
+               emit_due += emit_period;
+       }
 }
 
 /************************
@@ -257,18 +298,12 @@ typedef struct {
 
 static uint32_t serial_device_id   = 0;
 
-const  uint64_t serial_sync_delay  = NSEC_PER_SEC / 100; // 1hz
+const  uint64_t serial_sync_delay  = NSEC_PER_SEC / 100;
 static uint64_t serial_sync_due    = 0;
 
 static tdma_t  *serial_tdma_rcv    = NULL;
 static tdma_t  *serial_tdma_xmt    = NULL;
 
-static uint64_t serial_prev_local  = 0;
-static uint64_t serial_prev_seq    = 0;
-
-static uint64_t serial_xmt_local   = 0;
-static uint64_t serial_xmt_seq     = 0;
-
 /**
  * Convert world to local time
  */
@@ -286,6 +321,20 @@ ntime_t serial_write_time(uint64_t time)
        return buf;
 }
 
+int serial_time_stamp(tdma_t *port, uint64_t *local, uint64_t *world,
+               const char *msg)
+{
+       int valid = tdma_stamp(port, local);
+       *world = time_to_world(*local);
+
+       if (!valid)
+               sirq_printf("%s -- missing\r\n", msg);
+       //else
+       //      time_printf(msg, current);
+
+       return valid;
+}
+
 /**
  * Output initialization message init message
  */
@@ -301,53 +350,41 @@ void serial_send_sync(sirq_t *port, uint64_t now)
        if (serial_sync_due == 0 || now < serial_sync_due)
                return; // not ready
 
-       //sirq_printf("sending sync\r\n");
-
-       // Calculate world time
-       uint64_t local = 0;
-       uint64_t world = time_to_world(serial_xmt_local);
-
        // Message data
        header_t   head;
        sync_msg_t body;
 
-       // Transmit sync message
+       // Write header
        head.header = MSG_HEADER;
        head.msgid  = MSG_ID_SYNC;
        head.length = sizeof(body);
        head.cksum  = 0; // todo
 
-       body.seq          = serial_xmt_seq;
-       body.time.seconds = world / NSEC_PER_SEC;
-       body.time.nanosec = world % NSEC_PER_SEC;
-
-       tdma_stop(serial_tdma_rcv);
-
-       test_xmt_enab  = 1;
-       test_xmt_time0 = 0;
-       test_xmt_time1 = 0;
-
+       tdma_stop(serial_tdma_rcv, 0);
        tdma_start(serial_tdma_xmt);
+
        sirq_write(port, &head, sizeof(head));
-       sirq_write(port, &body, sizeof(body));
-       tdma_stop(serial_tdma_xmt);
-
-       // save transmit time
-       //local = test_xmt_time1;
-       int valid = tdma_stamp(serial_tdma_xmt, &local);
-       if (!valid) {
-               sirq_printf("sync transmit time -- missed\r\n");
-       } else {
-               //time_printf("sync transmit time ", local);
-               //time_printf("sync transmit test0", test_xmt_time0);
-               //time_printf("sync transmit test1", test_xmt_time1);
-       }
 
+       tdma_stop(serial_tdma_xmt, 100);
        tdma_start(serial_tdma_rcv);
 
-       serial_xmt_seq  += 1;
+       // Save transmit time
+       uint64_t local = 0, world = 0;
+       serial_time_stamp(serial_tdma_xmt, &local, &world,
+                       "sync time transmit");
+
+       // Debug output
+       //sirq_printf("sync time transmit\r\n");
+       //time_printf("  local", local);
+       //time_printf("  world", world);
+
+       // Write body with updated time and send
+       body.time = serial_write_time(world);
+
+       sirq_write(port, &body, sizeof(body));
+
+       // Queue next transmit time
        serial_sync_due  = 0;
-       serial_xmt_local = local;
 }
 
 /**
@@ -355,44 +392,33 @@ void serial_send_sync(sirq_t *port, uint64_t now)
  *   event: id of the received event
  *   time:  compensated timestamp of the event
  */
-void serial_send_event(uint16_t event, uint64_t local)
+void serial_send_event(sirq_t *port, uint16_t event, uint64_t local)
 {
-//     uint64_t world = time_to_world(local);
-
-       // Message data
-#if 0
-       header_t    head;
-       event_msg_t body;
+       //time_printf("event received", local);
 
-       ntime_t time = {};
-       time.seconds = (uint32_t)(world / NSEC_PER_SEC);
-       time.nanosec = (uint32_t)(world % NSEC_PER_SEC);
+       // Convert timestamp
+       uint64_t world = time_to_world(local);
+       ntime_t  ltime = serial_write_time(local);
+       ntime_t  wtime = serial_write_time(world);
 
-       sirq_printf("event received - %08x:%08x - %u.%09u\r\n",
-               (uint32_t)(local >> 32), (uint32_t)local,
-               time.seconds, time.nanosec);
+       // Message data
+       header_t    head = {};
+       event_msg_t body = {};
 
        // Transmit sync message
        head.header = MSG_HEADER;
-       head.msgid  = MSG_ID_SYNC;
+       head.msgid  = MSG_ID_EVENT;
        head.length = sizeof(body);
        head.cksum  = 0; // todo
 
-       body.seq          = serial_xmt_seq;
-       body.time.seconds = world / NSEC_PER_SEC;
-       body.time.nanosec = world % NSEC_PER_SEC;
-
-       tdma_stop(serial_tdma_rcv);
+       body.device = serial_device_id;
+       body.event  = event;
+       body.world  = wtime;
+       body.local  = ltime;
 
-       test_xmt_enab  = 1;
-       test_xmt_time0 = 0;
-       test_xmt_time1 = 0;
-
-       tdma_start(serial_tdma_xmt);
+       // Transmit message to BBB
        sirq_write(port, &head, sizeof(head));
        sirq_write(port, &body, sizeof(body));
-       tdma_stop(serial_tdma_xmt);
-#endif
 }
 
 /**
@@ -400,11 +426,22 @@ void serial_send_event(uint16_t event, uint64_t local)
  */
 void serial_handle_init(init_msg_t *msg)
 {
+       sirq_printf("initialize: %s %s %s %s %s\r\n",
+               msg->valid & MSG_VALID_DEVICE ? "DEV"    : "dev",
+               msg->valid & MSG_VALID_START  ? "START"  : "start",
+               msg->valid & MSG_VALID_PERIOD ? "PERIOD" : "period",
+               msg->valid & MSG_VALID_WORLD  ? "WORLD"  : "world",
+               msg->valid & MSG_VALID_SYNC   ? "SYNC"   : "sync");
+       sirq_printf("  dev    -- %d\r\n", msg->device);
+       time_printf("  start ", serial_read_time(msg->start));
+       time_printf("  period", serial_read_time(msg->period));
+       time_printf("  world ", serial_read_time(msg->world));
+
        if (msg->valid & MSG_VALID_DEVICE)
                serial_device_id = msg->device;
 
-       if ((msg->valid & MSG_VALID_START) ||
-           (msg->valid & MSG_VALID_PERIOD)) {
+       if (msg->valid & MSG_VALID_START ||
+           msg->valid & MSG_VALID_PERIOD) {
                uint64_t start  = serial_read_time(msg->start);
                uint64_t period = serial_read_time(msg->period);
                emit_enable(start, period);
@@ -415,6 +452,9 @@ void serial_handle_init(init_msg_t *msg)
                uint64_t local = tdma_time();
                time_ext_init(local, world);
        }
+
+       if (msg->valid & MSG_VALID_SYNC)
+               serial_sync_due = tdma_time() + serial_sync_delay;
 }
 
 /**
@@ -422,31 +462,26 @@ void serial_handle_init(init_msg_t *msg)
  */
 void serial_handle_sync(sync_msg_t *msg)
 {
-       // Read receive timestamp for next time sync message
-       uint64_t current = 0;
-       int valid = tdma_stamp(serial_tdma_rcv, &current);
-       if (!valid)
-               sirq_printf("sync receive time  -- missing\r\n");
-       //else
-       //      time_printf("sync receive time ", current);
-       tdma_stop(serial_tdma_rcv);
+       // Read receive timestamp
+       uint64_t local = 0, world = 0;
+       serial_time_stamp(serial_tdma_rcv, &local, &world,
+                       "sync time receive ");
+       tdma_stop(serial_tdma_rcv, 0);
 
-       // Lookup times
-       uint64_t world = ((uint64_t)msg->time.seconds) * NSEC_PER_SEC
-                      + ((uint64_t)msg->time.nanosec);
+       // Lookup reference time from message
+       uint64_t reference = serial_read_time(msg->time);
 
-       // Valid times timestamp
-       if (serial_prev_seq == (msg->seq-1)) {
-               uint64_t local = serial_prev_local;
-               time_ext_sync(local, world);
-       }
+       // Debug output
+       //sirq_printf("sync time receive\r\n");
+       //time_printf("  local", local);
+       //time_printf("  world", world);
+       //time_printf("  ref  ", reference);
+
+       // Synchronize the clocks
+       time_ext_sync(local, reference);
 
        // Queue transmit to other board
        serial_sync_due   = tdma_time() + serial_sync_delay;
-
-       // Update states
-       serial_prev_local = current;
-       serial_prev_seq   = msg->seq;
 }
 
 /**
@@ -582,8 +617,8 @@ void task_events(uint64_t local, uint64_t world)
 #endif
 
        if (tdma_stamp(tdma_evt, &event))
-               serial_send_event(0, event);
-       tdma_stop(tdma_evt);
+               serial_send_event(sirq_bbb, 0, event);
+       tdma_stop(tdma_evt, 0);
        tdma_start(tdma_evt);
 }
 
@@ -602,7 +637,7 @@ void task_leds(uint64_t local, uint64_t world)
 
 void task_emit(uint64_t local, uint64_t world)
 {
-       emit_transmit();
+       emit_transmit(local, world);
 }
 
 void task_debug(uint64_t local, uint64_t world)
@@ -612,6 +647,8 @@ void task_debug(uint64_t local, uint64_t world)
 
        //sirq_debug(sirq_mbed);
 
+       //serial_send_event(sirq_bbb, 1, local);
+
 #ifdef VERBOSE
        sirq_printf("background - %6u.%02u -> %u.%02u\r\n",
                        (uint32_t)(local / NSEC_PER_SEC),
@@ -636,7 +673,7 @@ static struct {
        uint64_t due;
 } tasks[] = {
        { task_serial, 0          }, // always
-       { task_events, 1000000000 }, // always -- testing
+       { task_events, 0          }, // always -- testing
        { task_sync,   0          }, // always
        { task_emit,   0          }, // always
        { task_leds,   100000000  }, // 10hz
@@ -662,13 +699,15 @@ int main(int argc, char **argv)
 {
        tdma_init();
 
+       //pin = 1;
+
        // Open serial ports
        sirq_dbg   = sirq_open(SIRQ_UART0, USBTX, USBRX, 115200); // to pc
        sirq_bbb   = sirq_open(SIRQ_UART1, PTE0,  PTE1,  115200); // to bbb
        sirq_mbed  = sirq_open(SIRQ_UART2, PTD3,  PTD2,  115200); // to mbed
 
        // Setup timers
-       tdma_evt   = tdma_open(TDMA_CHAN0, 3, PTC9,  PullUp); // async event
+       tdma_evt   = tdma_open(TDMA_CHAN0, 3, PTC9,  PullDown); // async event
 
        // mbed time sync
        tdma_rcv   = tdma_open(TDMA_CHAN2, 3, PTD2,  PullUp);   // time sync rcv
@@ -687,39 +726,10 @@ int main(int argc, char **argv)
        serial_tdma_rcv = tdma_rcv;
        serial_tdma_xmt = tdma_xmt;
 
-       // Test clocks
-       //MCG->C1    = 0x05; // was 0x1A
-       //MCG->C2    = 0x2C; // was 0x24
-       //MCG->C3    = 0x91; // was 0x91
-       //MCG->C4    = 0x10; // was 0x10
-       //MCG->C5    = 0x01; // was 0x01
-       //MCG->C6    = 0x40; // was 0x40
-       //MCG->S     = 0x6E; // was 0x6E
-       //MCG->SC    = 0x02; // was 0x02
-       //MCG->ATCVH = 0x00; // was 0x00
-       //MCG->ATCVL = 0x00; // was 0x00
-       //MCG->C7    = 0x00; // was 0x00
-       //MCG->C8    = 0x80; // was 0x80
-       //MCG->C9    = 0x00; // was 0x00
-       //MCG->C10   = 0x00; // was 0x00
-
-       //sirq_printf("MGC - C1    %02hx\r\n", MCG->C1);     // 1A
-       //sirq_printf("MGC - C2    %02hx\r\n", MCG->C2);     // 24
-       //sirq_printf("MGC - C3    %02hx\r\n", MCG->C3);     // 91
-       //sirq_printf("MGC - C4    %02hx\r\n", MCG->C4);     // 10
-       //sirq_printf("MGC - C5    %02hx\r\n", MCG->C5);     // 01
-       //sirq_printf("MGC - C6    %02hx\r\n", MCG->C6);     // 40
-       //sirq_printf("MGC - S     %02hx\r\n", MCG->S);      // 6E
-       //sirq_printf("MGC - SC    %02hx\r\n", MCG->SC);     // 02
-       //sirq_printf("MGC - ATCVH %02hx\r\n", MCG->ATCVH);  // 00
-       //sirq_printf("MGC - ATCVL %02hx\r\n", MCG->ATCVL);  // 00
-       //sirq_printf("MGC - C7    %02hx\r\n", MCG->C7);     // 00
-       //sirq_printf("MGC - C8    %02hx\r\n", MCG->C8);     // 80
-       //sirq_printf("MGC - C9    %02hx\r\n", MCG->C9);     // 00
-       //sirq_printf("MGC - C10   %02hx\r\n", MCG->C10);    // 00
+       // Setup event generation
+       emit_init(3, PTE20, PullDown);
 
        // Run background loop
-       printf("hello");
        while (true)
                background();