Mercurial > ~darius > hgwebdir.cgi > modulator
annotate modulator.c @ 10:98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 24 Feb 2025 12:12:09 +1030 |
parents | 3acdebd7eec7 |
children | e9d12b36cfcc |
rev | line source |
---|---|
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
1 /****************************************************************** |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
2 ******************************************************************* |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
3 ** |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
4 ** This is proprietary unpublished source code, property |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
5 ** of Genesis Software. Use or disclosure without prior |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
6 ** agreement is expressly prohibited. |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
7 ** |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
8 ** Copyright (c) 2021 Genesis Software, all rights reserved. |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
9 ** |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
10 ******************************************************************* |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
11 ******************************************************************/ |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
12 |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
13 /* |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
14 ** MODULATOR.C |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
15 ** |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
16 ** Create modulation shape |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
17 ** |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
18 */ |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
19 |
5 | 20 #include <stdio.h> |
21 #include <string.h> | |
22 #include "bitstring.h" | |
23 | |
24 #pragma GCC diagnostic push | |
25 #pragma GCC diagnostic ignored "-Wtype-limits" | |
26 #pragma GCC diagnostic ignored "-Wsign-compare" | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 #include "pico/stdlib.h" |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
28 #include "hardware/clocks.h" |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
29 #include "hardware/dma.h" |
5 | 30 #include "hardware/interp.h" |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
31 #include "hardware/irq.h" |
5 | 32 #include "hardware/pll.h" |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
33 #include "hardware/pio.h" |
5 | 34 #include "hardware/pwm.h" |
35 #include "hardware/structs/pll.h" | |
36 #include "hardware/structs/clocks.h" | |
37 #pragma GCC diagnostic pop | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
39 #include "dac.pio.h" |
9 | 40 #include "trigger.pio.h" |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
41 |
5 | 42 // https://github.com/howerj/q |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
43 // Modified to be Q20.12 rather than Q16.16 |
5 | 44 #include "q/q.h" |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
45 |
5 | 46 #include "shaped-trap.h" |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
47 |
9 | 48 // Pulse control bits |
49 #define SENSE 0x01 | |
50 #define GATE 0x02 | |
51 #define PHINV 0x04 | |
52 #define PACTIVE 0x08 | |
53 | |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
54 // DMA channel to feed DAC PIO |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
55 static int dma_chan; |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
56 // Pulse shape data |
5 | 57 uint8_t pulse_data[65536]; |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
58 // Pulse control data |
5 | 59 uint8_t pulse_ctrl[65536]; |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
60 // PWM slice for PRF timer |
9 | 61 unsigned slice_num = 0; |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
62 // DAC PIO |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
63 PIO pulse_pio = pio0; |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
64 // DAC SM |
9 | 65 uint pulse_sm; |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
66 // Instruction offset for DAC PIO program |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
67 uint pulse_pio_sm_offset; |
9 | 68 /* |
69 * Use a DMA channel to feed PIO0 SM0 with pulse data. | |
70 * Each DMA transfer is a single pulse. | |
71 * | |
72 * The PIO state machine waits to be triggered before starting | |
73 * so we can use another state machine to look for the trigger edge. | |
74 * | |
75 * When the DMA is done the IRQ handler will configure it for the next | |
76 * pulse (or not if it should stop). ie reset the PIO state machine | |
77 * back to waiting for an edge and re-arm the DMA. | |
78 */ | |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
79 void |
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
80 dma_handler(void) { |
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
81 // Clear the interrupt request. |
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
82 dma_hw->ints0 = 1u << dma_chan; |
5 | 83 } |
84 | |
85 | |
86 void | |
87 pwm_wrap(void) { | |
88 pwm_clear_irq(slice_num); | |
89 #if 0 | |
90 static unsigned state = 0; | |
91 | |
92 gpio_put(PICO_DEFAULT_LED_PIN, state); | |
93 state = !state; | |
94 #endif | |
95 | |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
96 // Reset DAQ PIO SM so it is waiting for a trigger |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
97 pio_sm_exec(pulse_pio, pulse_sm, pio_encode_jmp(pulse_pio_sm_offset)); |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
98 |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
99 // Setup next pulse DMA address |
5 | 100 dma_channel_set_read_addr(dma_chan, pulse_data, true); |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
101 |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
102 // Manually trigger DAQ SM (cleared by SM) |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
103 pio0->irq_force = 1 << 0; |
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
104 |
9 | 105 gpio_put(2, 1); |
106 gpio_put(2, 0); | |
5 | 107 } |
108 | |
109 // Calculate pulse shape data | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
110 // TODO: predistortion, proper sense, gate, phase, active, T/R switch |
5 | 111 // Could encode them as bit stream like data but more compact would be |
112 // (say) a list of counts to toggle pins at | |
113 // Need to add pre/postgate/sense/phase counters | |
114 unsigned | |
115 compute_pulse(uint8_t *data, uint8_t *ctrl, unsigned datalen, uint16_t plen, char *code, uint8_t ncode, const uint8_t *shape, uint8_t shapelen, uint8_t codegap, uint8_t slew1, uint8_t slew2, uint8_t dcofs) { | |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
116 uint32_t shapesamples, nsamples, idx, bit1startup, bit1stopup; |
5 | 117 q_t dcscale, stepsize; |
118 char tmps[20]; | |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
119 interp_config cfg; |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
120 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
121 if (ncode == 1) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
122 // Number of samples for half of the pulse |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
123 // Do division first so we don't overflow Q16.16 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
124 shapesamples = qtoi(qmul(qdiv(qint(plen), qint(100)), qint(shapelen / 2))); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
125 // Number of samples for everything |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
126 // XXX: Need the +1 otherwise slew2 is truncated |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
127 nsamples = shapesamples * 2 + slew1 + slew2 + 1; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
128 } else { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
129 shapesamples = plen / 2; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
130 nsamples = shapesamples * 2 * ncode + codegap * (ncode - 1) + slew1 + slew2 + 1; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
131 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
132 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
133 // Number of steps per samples in the pulse shape |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
134 stepsize = qdiv(qint(shapelen), qint(shapesamples)); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
135 qsprint(stepsize, tmps, sizeof(tmps)); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
136 printf("shapelen = %d shapesamples = %lu nsamples = %lu stepsize = %s\n", shapelen, shapesamples, nsamples, tmps); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
137 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
138 // Check the requested pulse will not overflow given data |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
139 if (nsamples > datalen) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
140 printf("Pulse too long (%ld > %u)\n", nsamples, datalen); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
141 return 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
142 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
143 // Check it is not too short |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
144 if (shapesamples < 2) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
145 printf("Pulse too short (%lu < %d)\n", shapesamples, 2); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
146 return 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
147 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
148 // Or too long (will overflow for loop variable) |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
149 if (qtoi(shapesamples) > 65535) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
150 printf("Shape too long (%u > %d)\n", qtoi(shapesamples), 65535); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
151 return 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
152 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
153 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
154 // Setup interp 0 lane 0 to generate index into shape table |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
155 // Mask start is 0 because we use 8 bit samples |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
156 cfg = interp_default_config(); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
157 interp_config_set_shift(&cfg, QBITS); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
158 interp_config_set_mask(&cfg, 0, 32 - QBITS); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
159 interp_config_set_blend(&cfg, true); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
160 interp_set_config(interp0, 0, &cfg); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
161 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
162 // Setup interp 0 lane 1 to LERP each sample pair |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
163 cfg = interp_default_config(); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
164 interp_config_set_shift(&cfg, QBITS - 8); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
165 interp_config_set_signed(&cfg, false); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
166 interp_config_set_cross_input(&cfg, true); // unsigned blending |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
167 interp_set_config(interp0, 1, &cfg); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
168 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
169 // Setup interp 1 lane 0 to clamp 0-255 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
170 cfg = interp_default_config(); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
171 interp_config_set_clamp(&cfg, true); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
172 interp_config_set_shift(&cfg, 0); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
173 interp_config_set_mask(&cfg, 0, 8); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
174 interp_config_set_signed(&cfg, false); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
175 interp_set_config(interp1, 0, &cfg); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
176 interp1->base[0] = 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
177 interp1->base[1] = 255; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
178 |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
179 interp0->accum[0] = 0; // Initial offset into shape table |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
180 interp0->base[2] = (uintptr_t)shape; // Start of shape table |
5 | 181 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
182 dcscale = qdiv(qsub(qint(256), qint(dcofs)), qint(255)); |
5 | 183 qsprint(dcscale, tmps, sizeof(tmps)); |
184 printf("dcscale = %s\n", tmps); | |
185 | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
186 memset(pulse_data, 0, sizeof(pulse_data)); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
187 memset(pulse_ctrl, 0, sizeof(pulse_ctrl)); |
5 | 188 idx = 0; |
189 | |
190 // Up slew | |
191 for (uint16_t i = 0; i < slew1; i++) { | |
192 data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(i)), qint(slew1))); | |
193 ctrl[idx] |= PACTIVE; | |
194 } | |
195 for (uint16_t c = 0; c < ncode; c++) { | |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
196 if (c == 0) |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
197 bit1startup = idx; |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
198 |
5 | 199 uint ctrltmp = PACTIVE; |
200 if (code[c] == '0') | |
201 ctrltmp |= PHINV; | |
202 | |
203 // Pulse up | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
204 if (c == 0) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
205 interp0->accum[0] = 0; // Initial offset into shape table |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
206 interp0->base[2] = (uintptr_t)shape; // Start of shape table |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
207 } |
5 | 208 for (uint16_t i = 0; i < shapesamples; i++) { |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
209 if (c == 0) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
210 // Get sample pair |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
211 uint8_t *sample_pair = (uint8_t *) interp0->peek[2]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
212 // Ask lane 1 for a LERP, using the lane 0 accumulator |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
213 interp0->base[0] = sample_pair[0]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
214 interp0->base[1] = sample_pair[1]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
215 uint8_t peek = interp0->peek[1]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
216 // Apply DC offset scaling & clamp |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
217 interp1->accum[0] = dcofs + qtoi(qmul(qint(peek), dcscale)); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
218 data[idx++] = interp1->peek[0]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
219 // Update interpolator for next point |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
220 interp0->add_raw[0] = stepsize; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
221 } else |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
222 // Already done it before, just copy the previous instance |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
223 data[idx++] = data[bit1startup + i]; |
5 | 224 ctrl[idx] = ctrltmp; |
225 } | |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
226 if (c == 0) |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
227 bit1stopup = idx - 1; |
5 | 228 // Pulse down |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
229 // Since the pulse is symmetrical just copy the up slope in reverse |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
230 // XXX: if we had asymmetrical predistortion this wouldn't be true |
5 | 231 for (uint16_t i = 0; i < shapesamples; i++) { |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
232 data[idx++] = data[bit1stopup - i]; |
5 | 233 // Could replace this with a separate loop to poke it into place |
234 // Similarly for TR switch when implemented | |
235 if (i == 0 && c == 0) | |
236 ctrl[idx] = ctrltmp | SENSE; | |
237 else | |
238 ctrl[idx] = ctrltmp; | |
239 } | |
240 | |
241 // Code gap | |
242 if (c < ncode - 1) | |
243 for (uint16_t i = 0; i < codegap; i++) { | |
244 data[idx++] = dcofs; | |
245 ctrl[idx] = ctrltmp; | |
246 } | |
247 } | |
248 | |
249 // Down slew | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
250 for (uint16_t i = 0; i < slew2 + 1; i++) { |
5 | 251 data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(slew2 - i)), qint(slew2))); |
252 ctrl[idx] |= PACTIVE; | |
253 } | |
254 return idx - 1; | |
255 } | |
256 | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
257 int |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
258 main(void) { |
5 | 259 absolute_time_t then, now; |
260 | |
261 // Set sysclk to 120MHz | |
262 set_sys_clock_khz(120000, true); | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
263 |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
264 stdio_init_all(); |
5 | 265 printf("\n\n\nIniting\n"); |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
266 |
5 | 267 // Needed otherwise timer related functions hang under debugging |
268 // https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639 | |
269 timer_hw->dbgpause = 0; | |
270 | |
271 gpio_init(PICO_DEFAULT_LED_PIN); | |
272 gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); | |
9 | 273 gpio_init(2); |
274 gpio_set_dir(2, GPIO_OUT); | |
275 #if 0 | |
276 for (unsigned i = 7; i < 7 + 9; i++) { | |
277 printf("GPIO %d\n", i); | |
278 gpio_init(i); | |
279 gpio_set_dir(i, GPIO_OUT); | |
280 printf("on\n"); | |
281 gpio_put(i, 1); | |
282 __breakpoint(); | |
283 printf("off\n"); | |
284 gpio_put(i, 0); | |
285 __breakpoint(); | |
286 } | |
287 #endif | |
5 | 288 |
289 uint32_t idx; | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
290 uint16_t plen; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
291 char *code; |
9 | 292 if (1) { |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
293 plen = 8000; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
294 code = "1110010"; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
295 } else { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
296 plen = 53000; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
297 code = "1"; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
298 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
299 |
5 | 300 uint8_t codegap = 4; |
301 uint8_t slew1 = 10; | |
302 uint8_t slew2 = 10; | |
303 uint8_t dcofs = 110; | |
304 then = get_absolute_time(); | |
305 if ((idx = compute_pulse(pulse_data, pulse_ctrl, sizeof(pulse_data), | |
306 plen, code, strlen(code), | |
307 shaped_trap, sizeof(shaped_trap), | |
308 codegap, slew1, slew2, dcofs)) == 0) { | |
309 printf("Failed to compute pulse\n"); | |
310 while (1) | |
311 ; | |
312 } | |
313 now = get_absolute_time(); | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
314 unsigned long long diff = absolute_time_diff_us(then, now); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
315 printf("Pulse computation took %lld usec and created %lu samples - %.1f nsec/sample\n", |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
316 diff, idx, (float)diff * 1000.0 / idx); |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
317 //__breakpoint(); |
9 | 318 |
319 // Load the clocked_input program, and configure a free state machine | |
320 // to run the program. | |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
321 pulse_pio_sm_offset = pio_add_program(pulse_pio, &dac_program); |
9 | 322 uint pulse_sm = pio_claim_unused_sm(pulse_pio, true); |
323 // Data is GPIO7 to GPIO14, clock is GPIO15 | |
324 // Clock divisor of 2 so it runs at 60MHz and | |
325 // generates a 30MHz clock | |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
326 dac_program_init(pulse_pio, pulse_sm, pulse_pio_sm_offset, 7, 2); |
9 | 327 |
328 // Configure a channel to write 32 bits at a time to PIO0 | |
329 // SM0's TX FIFO, paced by the data request signal from that peripheral. | |
330 dma_chan = dma_claim_unused_channel(true); | |
331 dma_channel_config dmac = dma_channel_get_default_config(dma_chan); | |
332 channel_config_set_transfer_data_size(&dmac, DMA_SIZE_32); | |
333 channel_config_set_read_increment(&dmac, true); | |
334 channel_config_set_dreq(&dmac, DREQ_PIO0_TX0); | |
335 | |
336 dma_channel_configure( | |
337 dma_chan, | |
338 &dmac, | |
339 &pio0_hw->txf[0], // Write address (only need to set this once) | |
340 NULL, // Don't provide a read address yet | |
341 (idx + 1) >> 2, // Transfer count (round up to 4 bytes) | |
342 false // Don't start yet | |
343 ); | |
344 | |
345 // Tell the DMA to raise IRQ line 0 when the channel finishes a block | |
346 dma_channel_set_irq0_enabled(dma_chan, true); | |
347 | |
348 // Configure the processor to run dma_handler() when DMA IRQ 0 is asserted | |
349 irq_set_exclusive_handler(DMA_IRQ_0, dma_handler); | |
350 irq_set_enabled(DMA_IRQ_0, true); | |
351 | |
5 | 352 // 120MHz / 250 = 480kHz base |
353 // Maximum divisor is only 256 which limits the low end, | |
354 // could further subdivide in the IRQ handler | |
9 | 355 pwm_config c = pwm_get_default_config(); |
5 | 356 pwm_config_set_clkdiv_int(&c, 250); |
357 // 8Hz | |
358 pwm_config_set_wrap(&c, 60000 - 1); | |
359 pwm_init(slice_num, &c, true); | |
360 pwm_clear_irq(slice_num); | |
361 pwm_set_irq_enabled(slice_num, true); | |
362 irq_set_exclusive_handler(PWM_IRQ_WRAP, pwm_wrap); | |
363 irq_set_enabled(PWM_IRQ_WRAP, true); | |
364 pwm_init(slice_num, &c, true); | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
365 |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
366 // Everything else from this point is interrupt-driven. The processor has |
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
367 // time to sit and think about its early retirement -- maybe open a bakery? |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
368 while (true) { |
5 | 369 dma_channel_wait_for_finish_blocking(dma_chan); |
370 gpio_put(PICO_DEFAULT_LED_PIN, 1); | |
371 sleep_ms(100); | |
372 gpio_put(PICO_DEFAULT_LED_PIN, 0); | |
2
0d653f60dec8
Actually get data moving out.
Daniel O'Connor <darius@dons.net.au>
parents:
0
diff
changeset
|
373 sleep_ms(100); |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
374 } |
5 | 375 |
376 __breakpoint(); | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
377 } |