Mercurial > ~darius > hgwebdir.cgi > modulator
annotate modulator.c @ 17:a249e4727b01
Move SM reset & DMA reprogramming to DMA IRQ handler, only trigger in PWM IRQ handler.
Otherwise we would collide with the setup in the main loop.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Tue, 25 Feb 2025 13:40:57 +1030 |
parents | 56a79dce90e9 |
children | f1e44afb41a3 |
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 ** |
16 | 8 ** Copyright (c) 2025 Genesis Software, all rights reserved. |
0
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 | |
23 #pragma GCC diagnostic push | |
24 #pragma GCC diagnostic ignored "-Wtype-limits" | |
25 #pragma GCC diagnostic ignored "-Wsign-compare" | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
26 #include "pico/stdlib.h" |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
27 #include "hardware/clocks.h" |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
28 #include "hardware/dma.h" |
5 | 29 #include "hardware/interp.h" |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
30 #include "hardware/irq.h" |
5 | 31 #include "hardware/pll.h" |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
32 #include "hardware/pio.h" |
5 | 33 #include "hardware/pwm.h" |
34 #include "hardware/structs/pll.h" | |
35 #include "hardware/structs/clocks.h" | |
36 #pragma GCC diagnostic pop | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
37 |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
38 #include "dac.pio.h" |
16 | 39 #include "ctrl.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 |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
48 // Base of DAC pins |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
49 #define DACOUT_GPIO 7 |
16 | 50 // Base of ctrl pins |
51 #define CTRLOUT_GPIO 16 | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
52 // PWM output pin |
16 | 53 #define TRIGOUT_GPIO 23 |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
54 // PIO SM trigger input pin (connected to above for testing) |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
55 #define TRIGIN_GPIO 27 |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
56 |
9 | 57 // Pulse control bits |
16 | 58 #define PACTIVE 0x01 |
59 #define PHINV 0x02 | |
60 #define SENSE1 0x04 | |
61 #define SENSE2 0x08 | |
62 #define GATE 0x10 | |
63 #define TRSW 0x20 | |
64 | |
65 // Pulse shape data | |
66 uint8_t pulse_data[65536] __attribute__((aligned(4))); | |
67 // Pulse control data | |
68 uint8_t pulse_ctrl[65536] __attribute__((aligned(4))); | |
69 // PWM slice for PRF timer | |
70 unsigned slice_num = 0; | |
71 | |
72 // PIO for pulse generation | |
73 PIO pulse_pio = pio0; | |
9 | 74 |
10
98880b18bcc1
Reset DAC PIO and use force trigger to do manual trigger.
Daniel O'Connor <darius@dons.net.au>
parents:
9
diff
changeset
|
75 // DMA channel to feed DAC PIO |
16 | 76 static int dac_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
|
77 // DAC SM |
16 | 78 uint dac_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
|
79 // Instruction offset for DAC PIO program |
16 | 80 uint dac_pio_sm_offset; |
81 | |
82 // DMA channel to feed ctrl PIO | |
83 static int ctrl_dma_chan; | |
84 // Ctrl SM | |
85 uint ctrl_sm; | |
86 // Instruction offset for ctrl PIO program | |
87 uint ctrl_pio_sm_offset; | |
88 | |
9 | 89 /* |
90 * Use a DMA channel to feed PIO0 SM0 with pulse data. | |
91 * Each DMA transfer is a single pulse. | |
92 * | |
93 * The PIO state machine waits to be triggered before starting | |
94 * so we can use another state machine to look for the trigger edge. | |
95 * | |
96 * When the DMA is done the IRQ handler will configure it for the next | |
97 * pulse (or not if it should stop). ie reset the PIO state machine | |
98 * back to waiting for an edge and re-arm the DMA. | |
99 */ | |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
100 void |
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
101 dma_handler(void) { |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
102 // Clear the interrupt request. |
16 | 103 dma_hw->ints0 = 1u << dac_dma_chan; |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
104 |
17
a249e4727b01
Move SM reset & DMA reprogramming to DMA IRQ handler, only trigger in PWM IRQ handler.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
105 printf("DAC: transfers %lu\n", dma_channel_hw_addr(dac_dma_chan)->transfer_count); |
a249e4727b01
Move SM reset & DMA reprogramming to DMA IRQ handler, only trigger in PWM IRQ handler.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
106 printf("DAC: transfers %lu\n", dma_channel_hw_addr(ctrl_dma_chan)->transfer_count); |
a249e4727b01
Move SM reset & DMA reprogramming to DMA IRQ handler, only trigger in PWM IRQ handler.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
107 |
16 | 108 // Reset DAQ & ctrl PIO SMs so they are waiting for a trigger |
109 pio_sm_exec(pulse_pio, dac_sm, pio_encode_jmp(dac_pio_sm_offset)); | |
110 pio_sm_exec(pulse_pio, ctrl_sm, pio_encode_jmp(ctrl_pio_sm_offset)); | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
111 |
16 | 112 // Setup next pulse data & ctrl DMA addresses |
17
a249e4727b01
Move SM reset & DMA reprogramming to DMA IRQ handler, only trigger in PWM IRQ handler.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
113 dma_channel_wait_for_finish_blocking(dac_dma_chan); |
16 | 114 dma_channel_set_read_addr(dac_dma_chan, pulse_data, true); |
17
a249e4727b01
Move SM reset & DMA reprogramming to DMA IRQ handler, only trigger in PWM IRQ handler.
Daniel O'Connor <darius@dons.net.au>
parents:
16
diff
changeset
|
115 dma_channel_wait_for_finish_blocking(ctrl_dma_chan); |
16 | 116 dma_channel_set_read_addr(ctrl_dma_chan, pulse_ctrl, true); |
5 | 117 } |
118 | |
119 | |
120 void | |
121 pwm_wrap(void) { | |
122 pwm_clear_irq(slice_num); | |
16 | 123 |
124 // Manually trigger DAQ SM (cleared by SM) | |
125 pio0->irq_force = 1 << 0; | |
126 | |
127 // 'scope trigger | |
128 gpio_put(2, 1); | |
129 gpio_put(2, 0); | |
5 | 130 } |
131 | |
132 // Calculate pulse shape data | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
133 // TODO: predistortion, proper sense, gate, phase, active, T/R switch |
5 | 134 // Could encode them as bit stream like data but more compact would be |
135 // (say) a list of counts to toggle pins at | |
136 // Need to add pre/postgate/sense/phase counters | |
137 unsigned | |
138 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
|
139 uint32_t shapesamples, nsamples, idx, bit1startup, bit1stopup; |
5 | 140 q_t dcscale, stepsize; |
141 char tmps[20]; | |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
142 interp_config cfg; |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
143 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
144 if (ncode == 1) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
145 // 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
|
146 // 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
|
147 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
|
148 // Number of samples for everything |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
149 // 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
|
150 nsamples = shapesamples * 2 + slew1 + slew2 + 1; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
151 } else { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
152 shapesamples = plen / 2; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
153 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
|
154 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
155 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
156 // 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
|
157 stepsize = qdiv(qint(shapelen), qint(shapesamples)); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
158 qsprint(stepsize, tmps, sizeof(tmps)); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
159 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
|
160 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
161 // 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
|
162 if (nsamples > datalen) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
163 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
|
164 return 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
165 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
166 // Check it is not too short |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
167 if (shapesamples < 2) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
168 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
|
169 return 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
170 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
171 // 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
|
172 if (qtoi(shapesamples) > 65535) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
173 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
|
174 return 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
175 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
176 |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
177 // 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
|
178 // 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
|
179 cfg = interp_default_config(); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
180 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
|
181 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
|
182 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
|
183 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
|
184 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
185 // 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
|
186 cfg = interp_default_config(); |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
187 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
|
188 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
|
189 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
|
190 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
|
191 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
192 // 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
|
193 cfg = interp_default_config(); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
194 interp_config_set_clamp(&cfg, true); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
195 interp_config_set_shift(&cfg, 0); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
196 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
|
197 interp_config_set_signed(&cfg, false); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
198 interp_set_config(interp1, 0, &cfg); |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
199 interp1->base[0] = 0; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
200 interp1->base[1] = 255; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
201 |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
202 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
|
203 interp0->base[2] = (uintptr_t)shape; // Start of shape table |
5 | 204 |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
205 dcscale = qdiv(qsub(qint(256), qint(dcofs)), qint(255)); |
5 | 206 qsprint(dcscale, tmps, sizeof(tmps)); |
207 printf("dcscale = %s\n", tmps); | |
208 | |
16 | 209 memset(pulse_data, 0, datalen); |
210 memset(pulse_ctrl, 0, datalen); | |
5 | 211 idx = 0; |
212 | |
213 // Up slew | |
214 for (uint16_t i = 0; i < slew1; i++) { | |
215 data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(i)), qint(slew1))); | |
216 ctrl[idx] |= PACTIVE; | |
217 } | |
218 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
|
219 if (c == 0) |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
220 bit1startup = idx; |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
221 |
5 | 222 uint ctrltmp = PACTIVE; |
223 if (code[c] == '0') | |
224 ctrltmp |= PHINV; | |
225 | |
226 // Pulse up | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
227 if (c == 0) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
228 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
|
229 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
|
230 } |
5 | 231 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
|
232 if (c == 0) { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
233 // Get sample pair |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
234 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
|
235 // 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
|
236 interp0->base[0] = sample_pair[0]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
237 interp0->base[1] = sample_pair[1]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
238 uint8_t peek = interp0->peek[1]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
239 // Apply DC offset scaling & clamp |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
240 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
|
241 data[idx++] = interp1->peek[0]; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
242 // Update interpolator for next point |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
243 interp0->add_raw[0] = stepsize; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
244 } else |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
245 // 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
|
246 data[idx++] = data[bit1startup + i]; |
5 | 247 ctrl[idx] = ctrltmp; |
248 } | |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
249 if (c == 0) |
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
250 bit1stopup = idx - 1; |
5 | 251 // Pulse down |
7
4ad473648949
Copy pulse up in reverse for pulse down.
Daniel O'Connor <darius@dons.net.au>
parents:
6
diff
changeset
|
252 // 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
|
253 // XXX: if we had asymmetrical predistortion this wouldn't be true |
5 | 254 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
|
255 data[idx++] = data[bit1stopup - i]; |
5 | 256 // Could replace this with a separate loop to poke it into place |
257 // Similarly for TR switch when implemented | |
258 if (i == 0 && c == 0) | |
16 | 259 ctrl[idx] = ctrltmp | SENSE1; |
5 | 260 else |
261 ctrl[idx] = ctrltmp; | |
262 } | |
263 | |
264 // Code gap | |
265 if (c < ncode - 1) | |
266 for (uint16_t i = 0; i < codegap; i++) { | |
267 data[idx++] = dcofs; | |
268 ctrl[idx] = ctrltmp; | |
269 } | |
270 } | |
271 | |
272 // Down slew | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
273 for (uint16_t i = 0; i < slew2 + 1; i++) { |
5 | 274 data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(slew2 - i)), qint(slew2))); |
275 ctrl[idx] |= PACTIVE; | |
276 } | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
277 |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
278 data[idx++] = 0; |
16 | 279 ctrl[idx] = 0; |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
280 |
16 | 281 return idx + 1; |
5 | 282 } |
283 | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
284 int |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
285 main(void) { |
5 | 286 absolute_time_t then, now; |
287 | |
288 // Set sysclk to 120MHz | |
289 set_sys_clock_khz(120000, true); | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
290 |
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
291 stdio_init_all(); |
5 | 292 printf("\n\n\nIniting\n"); |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
293 |
5 | 294 // Needed otherwise timer related functions hang under debugging |
295 // https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639 | |
296 timer_hw->dbgpause = 0; | |
297 | |
298 gpio_init(PICO_DEFAULT_LED_PIN); | |
299 gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); | |
9 | 300 gpio_init(2); |
301 gpio_set_dir(2, GPIO_OUT); | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
302 |
9 | 303 #if 0 |
16 | 304 // GPIO tester to check breadboard wiring |
9 | 305 for (unsigned i = 7; i < 7 + 9; i++) { |
306 printf("GPIO %d\n", i); | |
307 gpio_init(i); | |
308 gpio_set_dir(i, GPIO_OUT); | |
309 printf("on\n"); | |
310 gpio_put(i, 1); | |
311 __breakpoint(); | |
312 printf("off\n"); | |
313 gpio_put(i, 0); | |
314 __breakpoint(); | |
315 } | |
316 #endif | |
5 | 317 |
318 uint32_t idx; | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
319 uint16_t plen; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
320 char *code; |
9 | 321 if (1) { |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
322 plen = 8000; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
323 code = "1110010"; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
324 } else { |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
325 plen = 53000; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
326 code = "1"; |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
327 } |
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
328 |
5 | 329 uint8_t codegap = 4; |
330 uint8_t slew1 = 10; | |
331 uint8_t slew2 = 10; | |
332 uint8_t dcofs = 110; | |
333 then = get_absolute_time(); | |
334 if ((idx = compute_pulse(pulse_data, pulse_ctrl, sizeof(pulse_data), | |
335 plen, code, strlen(code), | |
336 shaped_trap, sizeof(shaped_trap), | |
337 codegap, slew1, slew2, dcofs)) == 0) { | |
338 printf("Failed to compute pulse\n"); | |
339 while (1) | |
340 ; | |
341 } | |
342 now = get_absolute_time(); | |
8
0249d0cecac4
Use interpolator to compute pulse up.
Daniel O'Connor <darius@dons.net.au>
parents:
7
diff
changeset
|
343 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
|
344 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
|
345 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
|
346 //__breakpoint(); |
9 | 347 |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
348 // Load the DAC program, and configure a free state machine |
9 | 349 // to run the program. |
16 | 350 dac_pio_sm_offset = pio_add_program(pulse_pio, &dac_program); |
351 if (dac_pio_sm_offset < 0) { | |
352 printf("Unable to load DAC program\n"); | |
353 __breakpoint(); | |
354 } | |
355 dac_sm = pio_claim_unused_sm(pulse_pio, true); | |
9 | 356 // Data is GPIO7 to GPIO14, clock is GPIO15 |
357 // Clock divisor of 2 so it runs at 60MHz and | |
358 // generates a 30MHz clock | |
16 | 359 dac_program_init(pulse_pio, dac_sm, dac_pio_sm_offset, DACOUT_GPIO, 2); |
9 | 360 |
361 // Configure a channel to write 32 bits at a time to PIO0 | |
362 // SM0's TX FIFO, paced by the data request signal from that peripheral. | |
16 | 363 dac_dma_chan = dma_claim_unused_channel(true); |
364 dma_channel_config dac_dmac = dma_channel_get_default_config(dac_dma_chan); | |
365 channel_config_set_transfer_data_size(&dac_dmac, DMA_SIZE_32); | |
366 channel_config_set_read_increment(&dac_dmac, true); | |
367 channel_config_set_dreq(&dac_dmac, PIO_DREQ_NUM(pulse_pio, dac_sm, true)); | |
9 | 368 |
369 dma_channel_configure( | |
16 | 370 dac_dma_chan, |
371 &dac_dmac, | |
372 &pulse_pio->txf[dac_sm], // Write address | |
373 pulse_data, // Pulse data | |
374 (idx + 3) >> 2, // Transfer count (round up to 4 bytes) | |
375 true // Start, SM will wait for trigger | |
9 | 376 ); |
377 | |
378 // Tell the DMA to raise IRQ line 0 when the channel finishes a block | |
16 | 379 dma_channel_set_irq0_enabled(dac_dma_chan, true); |
9 | 380 |
381 // Configure the processor to run dma_handler() when DMA IRQ 0 is asserted | |
382 irq_set_exclusive_handler(DMA_IRQ_0, dma_handler); | |
383 irq_set_enabled(DMA_IRQ_0, true); | |
384 | |
16 | 385 // Load the ctrl program, and configure a free state machine |
386 // to run the program. | |
387 ctrl_pio_sm_offset = pio_add_program(pulse_pio, &ctrl_program); | |
388 if (ctrl_pio_sm_offset < 0) { | |
389 printf("Unable to load ctrl program\n"); | |
390 __breakpoint(); | |
391 } | |
392 ctrl_sm = pio_claim_unused_sm(pulse_pio, true); | |
393 ctrl_program_init(pulse_pio, ctrl_sm, ctrl_pio_sm_offset, CTRLOUT_GPIO, 2); | |
394 | |
395 // Configure a channel to write 32 bits at a time to PIO0 | |
396 // SM0's TX FIFO, paced by the data request signal from that peripheral. | |
397 ctrl_dma_chan = dma_claim_unused_channel(true); | |
398 dma_channel_config ctrl_dmac = dma_channel_get_default_config(ctrl_dma_chan); | |
399 channel_config_set_transfer_data_size(&ctrl_dmac, DMA_SIZE_32); | |
400 channel_config_set_read_increment(&ctrl_dmac, true); | |
401 channel_config_set_dreq(&ctrl_dmac, PIO_DREQ_NUM(pulse_pio, ctrl_sm, true)); | |
402 | |
403 dma_channel_configure( | |
404 ctrl_dma_chan, | |
405 &ctrl_dmac, | |
406 &pulse_pio->txf[ctrl_sm], // Write address | |
407 pulse_ctrl, // Ctrl data | |
408 (idx + 3) >> 2, // Transfer count (round up to 4 bytes) | |
409 true // Start, SM will wait for trigger | |
410 ); | |
411 // No IRQ, piggyback on the data one | |
412 | |
413 #if 0 | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
414 // Load the trigger program, and configure a free state machine |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
415 // to run the program. |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
416 uint trigger_pio_sm_offset = pio_add_program(pulse_pio, &trigger_program); |
16 | 417 if (trigger_pio_sm_offset < 0) { |
418 printf("Unable to load trigger program\n"); | |
419 __breakpoint(); | |
420 } | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
421 uint trigger_sm = pio_claim_unused_sm(pulse_pio, true); |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
422 trigger_program_init(pulse_pio, trigger_sm, trigger_pio_sm_offset, TRIGIN_GPIO, 2); |
16 | 423 #endif |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
424 // |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
425 // Setup PWM |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
426 // Used here to output a trigger which gets fed back into the trigger SM |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
427 // |
5 | 428 // 120MHz / 250 = 480kHz base |
429 // Maximum divisor is only 256 which limits the low end, | |
430 // could further subdivide in the IRQ handler | |
9 | 431 pwm_config c = pwm_get_default_config(); |
5 | 432 pwm_config_set_clkdiv_int(&c, 250); |
433 // 8Hz | |
434 pwm_config_set_wrap(&c, 60000 - 1); | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
435 |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
436 gpio_set_function(TRIGOUT_GPIO, GPIO_FUNC_PWM); |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
437 |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
438 slice_num = pwm_gpio_to_slice_num(TRIGOUT_GPIO); |
5 | 439 pwm_init(slice_num, &c, true); |
440 pwm_clear_irq(slice_num); | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
441 pwm_set_chan_level(slice_num, PWM_CHAN_A, 1); |
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
442 pwm_set_enabled(slice_num, 1); |
5 | 443 pwm_set_irq_enabled(slice_num, true); |
444 irq_set_exclusive_handler(PWM_IRQ_WRAP, pwm_wrap); | |
445 irq_set_enabled(PWM_IRQ_WRAP, true); | |
11
e9d12b36cfcc
Use PWM output to feed PIO trigger
Daniel O'Connor <darius@dons.net.au>
parents:
10
diff
changeset
|
446 |
3
b10097c3383d
DMA test data repeatedly into PIO FIFO
Daniel O'Connor <darius@dons.net.au>
parents:
2
diff
changeset
|
447 // 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
|
448 // 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
|
449 while (true) { |
5 | 450 gpio_put(PICO_DEFAULT_LED_PIN, 1); |
451 sleep_ms(100); | |
452 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
|
453 sleep_ms(100); |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
454 } |
5 | 455 |
456 __breakpoint(); | |
0
a55e39064a71
First commit of code that compiles.
Daniel O'Connor <darius@dons.net.au>
parents:
diff
changeset
|
457 } |