view 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
line wrap: on
line source

/******************************************************************
*******************************************************************
**
** This is proprietary unpublished source code, property
** of Genesis Software.  Use or disclosure without prior
** agreement is expressly prohibited.
**
** Copyright (c) 2021 Genesis Software, all rights reserved.
**
*******************************************************************
 ******************************************************************/

/*
** MODULATOR.C
**
** Create modulation shape
**
*/

#include <stdio.h>
#include <string.h>
#include "bitstring.h"

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include "hardware/dma.h"
#include "hardware/interp.h"
#include "hardware/irq.h"
#include "hardware/pll.h"
#include "hardware/pio.h"
#include "hardware/pwm.h"
#include "hardware/structs/pll.h"
#include "hardware/structs/clocks.h"
#pragma GCC diagnostic pop

#include "dac.pio.h"
#include "trigger.pio.h"

// https://github.com/howerj/q
// Modified to be Q20.12 rather than Q16.16
#include "q/q.h"

#include "shaped-trap.h"

// Pulse control bits
#define SENSE		0x01
#define GATE		0x02
#define PHINV		0x04
#define PACTIVE		0x08

// DMA channel to feed DAC PIO
static int dma_chan;
// Pulse shape data
uint8_t pulse_data[65536];
// Pulse control data
uint8_t pulse_ctrl[65536];
// PWM slice for PRF timer
unsigned slice_num = 0;
// DAC PIO
PIO pulse_pio = pio0;
// DAC SM
uint pulse_sm;
// Instruction offset for DAC PIO program
uint pulse_pio_sm_offset;
/*
 * Use a DMA channel to feed PIO0 SM0 with pulse data.
 * Each DMA transfer is a single pulse.
 *
 * The PIO state machine waits to be triggered before starting
 * so we can use another state machine to look for the trigger edge.
 *
 * When the DMA is done the IRQ handler will configure it for the next
 * pulse (or not if it should stop). ie reset the PIO state machine
 * back to waiting for an edge and re-arm the DMA.
 */
void
dma_handler(void) {
    // Clear the interrupt request.
    dma_hw->ints0 = 1u << dma_chan;
}


void
pwm_wrap(void) {
  pwm_clear_irq(slice_num);
#if 0
  static unsigned state = 0;

  gpio_put(PICO_DEFAULT_LED_PIN, state);
  state = !state;
#endif

  // Reset DAQ PIO SM so it is waiting for a trigger
  pio_sm_exec(pulse_pio, pulse_sm, pio_encode_jmp(pulse_pio_sm_offset));

  // Setup next pulse DMA address
  dma_channel_set_read_addr(dma_chan, pulse_data, true);

  // Manually trigger DAQ SM (cleared by SM)
  pio0->irq_force = 1 << 0;

  gpio_put(2, 1);
  gpio_put(2, 0);
}

// Calculate pulse shape data
// TODO: predistortion, proper sense, gate, phase, active, T/R switch
// Could encode them as bit stream like data but more compact would be
// (say) a list of counts to toggle pins at
// Need to add pre/postgate/sense/phase counters
unsigned
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) {
  uint32_t shapesamples, nsamples, idx, bit1startup, bit1stopup;
  q_t dcscale, stepsize;
  char tmps[20];
  interp_config cfg;

  if (ncode == 1) {
    // Number of samples for half of the pulse
    // Do division first so we don't overflow Q16.16
    shapesamples = qtoi(qmul(qdiv(qint(plen), qint(100)), qint(shapelen / 2)));
    // Number of samples for everything
    // XXX: Need the +1 otherwise slew2 is truncated
    nsamples = shapesamples * 2 + slew1 + slew2 + 1;
  } else {
    shapesamples = plen / 2;
    nsamples = shapesamples * 2 * ncode + codegap * (ncode - 1) + slew1 + slew2 + 1;
  }

  // Number of steps per samples in the pulse shape
  stepsize = qdiv(qint(shapelen), qint(shapesamples));
  qsprint(stepsize, tmps, sizeof(tmps));
  printf("shapelen = %d shapesamples = %lu nsamples = %lu stepsize = %s\n", shapelen, shapesamples, nsamples, tmps);

  // Check the requested pulse will not overflow given data
  if (nsamples > datalen) {
    printf("Pulse too long (%ld > %u)\n", nsamples, datalen);
    return 0;
  }
  // Check it is not too short
  if (shapesamples < 2) {
    printf("Pulse too short (%lu < %d)\n", shapesamples, 2);
    return 0;
  }
  // Or too long (will overflow for loop variable)
  if (qtoi(shapesamples) > 65535) {
    printf("Shape too long (%u > %d)\n", qtoi(shapesamples), 65535);
    return 0;
  }

  // Setup interp 0 lane 0 to generate index into shape table
  // Mask start is 0 because we use 8 bit samples
  cfg = interp_default_config();
  interp_config_set_shift(&cfg, QBITS);
  interp_config_set_mask(&cfg, 0, 32 - QBITS);
  interp_config_set_blend(&cfg, true);
  interp_set_config(interp0, 0, &cfg);

  // Setup interp 0 lane 1 to LERP each sample pair
  cfg = interp_default_config();
  interp_config_set_shift(&cfg, QBITS - 8);
  interp_config_set_signed(&cfg, false);
  interp_config_set_cross_input(&cfg, true); // unsigned blending
  interp_set_config(interp0, 1, &cfg);

  // Setup interp 1 lane 0 to clamp 0-255
  cfg = interp_default_config();
  interp_config_set_clamp(&cfg, true);
  interp_config_set_shift(&cfg, 0);
  interp_config_set_mask(&cfg, 0, 8);
  interp_config_set_signed(&cfg, false);
  interp_set_config(interp1, 0, &cfg);
  interp1->base[0] = 0;
  interp1->base[1] = 255;

  interp0->accum[0] = 0; // Initial offset into shape table
  interp0->base[2] = (uintptr_t)shape; // Start of shape table

  dcscale = qdiv(qsub(qint(256), qint(dcofs)), qint(255));
  qsprint(dcscale, tmps, sizeof(tmps));
  printf("dcscale = %s\n", tmps);

  memset(pulse_data, 0, sizeof(pulse_data));
  memset(pulse_ctrl, 0, sizeof(pulse_ctrl));
  idx = 0;

  // Up slew
  for (uint16_t i = 0; i < slew1; i++) {
    data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(i)), qint(slew1)));
    ctrl[idx] |= PACTIVE;
  }
  for (uint16_t c = 0; c < ncode; c++) {
    if (c == 0)
      bit1startup = idx;

    uint ctrltmp = PACTIVE;
    if (code[c] == '0')
      ctrltmp |= PHINV;

    // Pulse up
    if (c == 0) {
      interp0->accum[0] = 0; // Initial offset into shape table
      interp0->base[2] = (uintptr_t)shape; // Start of shape table
    }
    for (uint16_t i = 0; i < shapesamples; i++) {
      if (c == 0) {
	// Get sample pair
	uint8_t *sample_pair = (uint8_t *) interp0->peek[2];
	// Ask lane 1 for a LERP, using the lane 0 accumulator
	interp0->base[0] = sample_pair[0];
	interp0->base[1] = sample_pair[1];
	uint8_t peek = interp0->peek[1];
	// Apply DC offset scaling & clamp
	interp1->accum[0] = dcofs + qtoi(qmul(qint(peek), dcscale));
	data[idx++] = interp1->peek[0];
	// Update interpolator for next point
	interp0->add_raw[0] = stepsize;
      } else
	// Already done it before, just copy the previous instance
	data[idx++] = data[bit1startup + i];
      ctrl[idx] = ctrltmp;
    }
    if (c == 0)
      bit1stopup = idx - 1;
    // Pulse down
    // Since the pulse is symmetrical just copy the up slope in reverse
    // XXX: if we had asymmetrical predistortion this wouldn't be true
    for (uint16_t i = 0; i < shapesamples; i++) {
      data[idx++] = data[bit1stopup - i];
      // Could replace this with a separate loop to poke it into place
      // Similarly for TR switch when implemented
      if (i == 0 && c == 0)
	ctrl[idx] = ctrltmp | SENSE;
      else
	ctrl[idx] = ctrltmp;
    }

    // Code gap
    if (c < ncode - 1)
      for (uint16_t i = 0; i < codegap; i++) {
	data[idx++] = dcofs;
	ctrl[idx] = ctrltmp;
      }
  }

  // Down slew
  for (uint16_t i = 0; i < slew2 + 1; i++) {
    data[idx++] = qtoi(qdiv(qmul(qint(dcofs), qint(slew2 - i)), qint(slew2)));
    ctrl[idx] |= PACTIVE;
  }
  return idx - 1;
}

int
main(void) {
    absolute_time_t then, now;

    // Set sysclk to 120MHz
    set_sys_clock_khz(120000, true);

    stdio_init_all();
    printf("\n\n\nIniting\n");

    // Needed otherwise timer related functions hang under debugging
    // https://github.com/raspberrypi/pico-sdk/issues/1152#issuecomment-1418248639
    timer_hw->dbgpause = 0;

    gpio_init(PICO_DEFAULT_LED_PIN);
    gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT);
    gpio_init(2);
    gpio_set_dir(2, GPIO_OUT);
#if 0
    for (unsigned i = 7; i < 7 + 9; i++) {
      printf("GPIO %d\n", i);
      gpio_init(i);
      gpio_set_dir(i, GPIO_OUT);
      printf("on\n");
      gpio_put(i, 1);
      __breakpoint();
      printf("off\n");
      gpio_put(i, 0);
      __breakpoint();
    }
#endif

    uint32_t idx;
    uint16_t plen;
    char *code;
    if (1) {
      plen = 8000;
      code = "1110010";
    } else {
      plen = 53000;
      code = "1";
    }

    uint8_t codegap = 4;
    uint8_t slew1 = 10;
    uint8_t slew2 = 10;
    uint8_t dcofs = 110;
    then = get_absolute_time();
    if ((idx = compute_pulse(pulse_data, pulse_ctrl, sizeof(pulse_data),
			     plen, code, strlen(code),
			     shaped_trap, sizeof(shaped_trap),
			     codegap, slew1, slew2, dcofs)) == 0) {
      printf("Failed to compute pulse\n");
      while (1)
	;
    }
    now = get_absolute_time();
    unsigned long long diff = absolute_time_diff_us(then, now);
    printf("Pulse computation took %lld usec and created %lu samples - %.1f nsec/sample\n",
	   diff, idx, (float)diff * 1000.0 / idx);
    //__breakpoint();

    // Load the clocked_input program, and configure a free state machine
    // to run the program.
    pulse_pio_sm_offset = pio_add_program(pulse_pio, &dac_program);
    uint pulse_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, pulse_sm, pulse_pio_sm_offset, 7, 2);

    // 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.
    dma_chan = dma_claim_unused_channel(true);
    dma_channel_config dmac = dma_channel_get_default_config(dma_chan);
    channel_config_set_transfer_data_size(&dmac, DMA_SIZE_32);
    channel_config_set_read_increment(&dmac, true);
    channel_config_set_dreq(&dmac, DREQ_PIO0_TX0);

    dma_channel_configure(
        dma_chan,
        &dmac,
        &pio0_hw->txf[0], // Write address (only need to set this once)
        NULL,             // Don't provide a read address yet
        (idx + 1) >> 2,	  // Transfer count (round up to 4 bytes)
        false             // Don't start yet
    );

    // Tell the DMA to raise IRQ line 0 when the channel finishes a block
    dma_channel_set_irq0_enabled(dma_chan, true);

    // Configure the processor to run dma_handler() when DMA IRQ 0 is asserted
    irq_set_exclusive_handler(DMA_IRQ_0, dma_handler);
    irq_set_enabled(DMA_IRQ_0, true);

    // 120MHz / 250 = 480kHz base
    // Maximum divisor is only 256 which limits the low end,
    // 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);
    pwm_init(slice_num, &c, true);
    pwm_clear_irq(slice_num);
    pwm_set_irq_enabled(slice_num, true);
    irq_set_exclusive_handler(PWM_IRQ_WRAP, pwm_wrap);
    irq_set_enabled(PWM_IRQ_WRAP, true);
    pwm_init(slice_num, &c, true);

    // Everything else from this point is interrupt-driven. The processor has
    // time to sit and think about its early retirement -- maybe open a bakery?
    while (true) {
      dma_channel_wait_for_finish_blocking(dma_chan);
	gpio_put(PICO_DEFAULT_LED_PIN, 1);
	sleep_ms(100);
	gpio_put(PICO_DEFAULT_LED_PIN, 0);
	sleep_ms(100);
    }

    __breakpoint();
}