|
11 | 11 | this->gain_b = gain; |
12 | 12 | this->gain_c = gain; |
13 | 13 | this->max_adc_value = max_adc_value; |
14 | | - |
15 | 14 | }; |
16 | 15 |
|
17 | 16 |
|
|
24 | 23 | // TODO check that driver is linked |
25 | 24 |
|
26 | 25 | // Done init PIO |
27 | | - // TODO init ADC via SPI |
28 | | - // TODO init DMA to transfer ADC data to memory buffer |
| 26 | + // TODO init ADC via SPI (Only for ADC with MOSI input) |
| 27 | + // Done init DMA to transfer ADC data to memory buffer |
29 | 28 | // TODO init timer to trigger PIO conversions at required frequency (check driver settings) |
30 | 29 | // TDB: do we need config input to know which timer slice and channel to use? or can we pick automatically? |
31 | 30 | // TODO start everything up |
32 | 31 |
|
33 | | - |
34 | 32 | float sck_hz = 20e6; |
35 | 33 | PIO pio = pio0; |
36 | | - int sm = pio_claim_unused_sm(pio0, false); |
37 | | - if (sm < 0) { pio = pio1; sm = pio_claim_unused_sm(pio1, true); } |
| 34 | + int sm = pio_claim_unused_sm(pio0, true); |
| 35 | + //if (sm < 0) { pio = pio1; sm = pio_claim_unused_sm(pio1, true); } //For now, let say we have to use PIO0, this is simpler for quick DMA setup |
38 | 36 |
|
39 | 37 | // --- patch program instructions with chosen trigger pin --- |
40 | 38 | size_t prog_len = bu79100g_parallel3_program.length; |
|
65 | 63 | pio_sm_set_consecutive_pindirs(pio, sm, this->pinCSB, 1, true); // CS out |
66 | 64 | pio_sm_set_consecutive_pindirs(pio, sm, this->pinD0, 3, false); // D0..D2 in |
67 | 65 |
|
68 | | - // Shift config: right, autopush every 24 bits (two pushes per conversion) |
69 | | - sm_config_set_in_shift(&c, true, true, 24); |
| 66 | + // Shift config: right, autopush every 32 bits (two pushes per conversion) |
| 67 | + sm_config_set_in_shift(&c, true, true, 32); |
70 | 68 |
|
71 | 69 | // SCK ≈ clk_sys / (2 * clkdiv) because each SCK period = 2 instructions |
72 | 70 | float div = (float)clock_get_hz(clk_sys) / (2.0f * sck_hz); |
73 | 71 | sm_config_set_clkdiv(&c, div); |
74 | 72 |
|
75 | | - // Init & start the SM |
| 73 | + // Init the SM |
76 | 74 | pio_sm_init(pio, sm, off, &c); |
| 75 | + |
| 76 | + //DMA Setup |
| 77 | + //dma_a is set to read from PIO RX FIFO and write to 'buff' buffer memory |
| 78 | + dma_a = dma_claim_unused_channel(true); |
| 79 | + dma_b = dma_claim_unused_channel(true); |
| 80 | + // ---------------------- DMA A: PIO RX FIFO -> buff[] with WRITE ring ---------------------- |
| 81 | + dma_channel_config ca = dma_channel_get_default_config(dma_a); |
| 82 | + channel_config_set_read_increment(&ca, false); |
| 83 | + channel_config_set_write_increment(&ca, true); |
| 84 | + channel_config_set_transfer_data_size(&ca, DMA_SIZE_32); |
| 85 | + channel_config_set_dreq(&ca, DREQ_PIO0_RX0 + sm); |
| 86 | + channel_config_set_chain_to(&ca, dma_b); // A -> B when count hits 0 |
| 87 | + |
| 88 | + // Enable WRITE-side ring over the whole buffer span (power-of-two bytes) |
| 89 | + // size_bits = log2(RING_BYTES) |
| 90 | + channel_config_set_ring(&ca, /*write=*/true, /*size_bits=*/__builtin_ctz(RING_BYTES)); |
| 91 | + |
| 92 | + dma_channel_set_config(dma_a, &ca, false); |
| 93 | + dma_channel_set_read_addr(dma_a, &pio->rxf[sm], false); |
| 94 | + dma_channel_set_write_addr(dma_a, (void*)buff, false); |
| 95 | + dma_channel_set_trans_count(dma_a, RING_WORDS, false); |
| 96 | + |
| 97 | + // ---------------------- DMA B: rearm A with ONE 32-bit write ---------------------- |
| 98 | + dma_channel_config cb = dma_channel_get_default_config(dma_b); |
| 99 | + channel_config_set_read_increment(&cb, false); |
| 100 | + channel_config_set_write_increment(&cb, false); |
| 101 | + channel_config_set_transfer_data_size(&cb, DMA_SIZE_32); |
| 102 | + |
| 103 | + // Write 1 word to A.AL1_TRANSFER_COUNT_TRIG (this sets count and re-starts A) |
| 104 | + dma_channel_configure( |
| 105 | + dma_b, &cb, |
| 106 | + (void*)&dma_hw->ch[dma_a].al1_transfer_count_trig, |
| 107 | + (const void*)&reload_count, |
| 108 | + 1, false |
| 109 | + ); |
| 110 | + |
| 111 | + // Go! |
| 112 | + dma_channel_start(dma_a); |
77 | 113 | pio_sm_set_enabled(pio, sm, true); |
78 | 114 |
|
| 115 | + |
79 | 116 | return 0; |
80 | 117 | }; |
81 | 118 |
|
|
0 commit comments