diff modulator.c @ 27:e1d8fe3e418a

Run PIOs at 1x with delays and sync. Can now use a single trigger to set both DAC & ctrl. DAC [still] jitters against the ctrl though..
author Daniel O'Connor <darius@dons.net.au>
date Wed, 26 Feb 2025 11:03:59 +1030
parents 336f06fa6e47
children 600a394629e6
line wrap: on
line diff
--- a/modulator.c	Tue Feb 25 17:03:51 2025 +1030
+++ b/modulator.c	Wed Feb 26 11:03:59 2025 +1030
@@ -362,9 +362,9 @@
     }
     dac_sm = pio_claim_unused_sm(pulse_pio, true);
     // Data is GPIO7 to GPIO14, clock is GPIO15
-    // Clock divisor of 2 so it runs at 60MHz and
-    // generates a 30MHz clock
-    dac_program_init(pulse_pio, dac_sm, dac_pio_sm_offset, DACOUT_GPIO, 2);
+    // Clock divisor of 1 but the PIO has delays so it runs at 60MHz
+    // and generates a 30MHz clock
+    dac_program_init(pulse_pio, dac_sm, dac_pio_sm_offset, DACOUT_GPIO, 1);
 
     // Configure a channel to write 32 bits at a time to PIO0
     // SM0's TX FIFO, paced by the data request signal from that peripheral.
@@ -398,7 +398,7 @@
       __breakpoint();
     }
     ctrl_sm = pio_claim_unused_sm(pulse_pio, true);
-    ctrl_program_init(pulse_pio, ctrl_sm, ctrl_pio_sm_offset, CTRLOUT_GPIO, 2);
+    ctrl_program_init(pulse_pio, ctrl_sm, ctrl_pio_sm_offset, CTRLOUT_GPIO, 1);
 
     // Configure a channel to write 32 bits at a time to PIO0
     // SM0's TX FIFO, paced by the data request signal from that peripheral.
@@ -427,9 +427,19 @@
       __breakpoint();
     }
     uint trigger_sm = pio_claim_unused_sm(pulse_pio, true);
-    trigger_program_init(pulse_pio, trigger_sm, trigger_pio_sm_offset, TRIGIN_GPIO, 2);
+    trigger_program_init(pulse_pio, trigger_sm, trigger_pio_sm_offset, TRIGIN_GPIO, 1);
 #endif
 
+    // Start & sync all state machines
+    // This is necessary to avoid any jitter and to make the
+    // trigger sync work correctly
+    pio_enable_sm_mask_in_sync(pulse_pio,
+			       1u << dac_sm |
+			       1u << ctrl_sm |
+#ifdef WITH_TRIGGER
+			       1u << trigger_sm
+#endif
+			       );
     //
     // Setup PWM
     // Used here to output a trigger which gets fed back into the trigger SM
@@ -439,8 +449,8 @@
     // could further subdivide in the IRQ handler
     pwm_config c = pwm_get_default_config();
     pwm_config_set_clkdiv_int(&c, 250);
-    // 8Hz
-    pwm_config_set_wrap(&c, 60000 - 1);
+    // 80Hz
+    pwm_config_set_wrap(&c, 6000 - 1);
 
     gpio_set_function(TRIGOUT_GPIO, GPIO_FUNC_PWM);