potcv

This Object is only for Euxoloti 1.0 (first beta versions). It reads each Pot and CV In. SPI Config is included.
Author: Paul
License: BSD
Github: euxo/1 0/potcv.axo

Inlets

None

Outlets

frac32.bipolar cvin2

frac32.bipolar cvin3

frac32.bipolar cvin4

frac32.bipolar cvin5

frac32.bipolar cvin6

frac32.bipolar cvin7

frac32.bipolar cvin8

frac32.positive pot1

frac32.positive pot2

frac32.positive pot3

frac32.positive pot4

frac32.positive pot5

frac32.positive pot6

frac32.positive pot7

frac32.positive pot8

frac32.positive cvin1

Declaration
int32_t pot1, pot2, pot3, pot4, pot5, pot6, pot7, pot8;
int32_t cvin1, cvin2, cvin3, cvin4, cvin5, cvin6, cvin7, cvin8;
// int32_t Go1,Go2,Go3,Go4,Go5,Go6,Go7,Go8;

uint8_t *txbuf;
uint8_t *rxbuf;
int32_t *potBuf;
int32_t *potBufOld;
int32_t *cvBuf;
int32_t *cvBufOld;

msg_t ThreadX2() {
  static uint8_t _txbuf[7] __attribute__((section(".sram2")));
  static uint8_t _rxbuf[6] __attribute__((section(".sram2")));
  static int32_t _potBuf[8] __attribute__((section(".sram2")));
  static int32_t _potBufOld[8] __attribute__((section(".sram2")));
  static int32_t _cvBuf[8] __attribute__((section(".sram2")));
  static int32_t _cvBufOld[8] __attribute__((section(".sram2")));

  txbuf = _txbuf;
  rxbuf = _rxbuf;
  potBuf = _potBuf;
  potBufOld = _potBufOld;
  cvBuf = _cvBuf;
  cvBufOld = _cvBufOld;

  palWritePad(GPIOA, 2, 1);
  palWritePad(GPIOA, 3, 1);

  while (!chThdShouldTerminate()) {
    /*
       // gat8
          // gat8 message is attached at the end of each SPI transaction.
          // check each input state
          if (Go1 == 0) {Go1 = 0x01;} else {Go1 = 0x00;}
          if (Go2 == 0) {Go2 = 0x02;} else {Go2 = 0x00;}
          if (Go3 == 0) {Go3 = 0x04;} else {Go3 = 0x00;}
          if (Go4 == 0) {Go4 = 0x08;} else {Go4 = 0x00;}
          if (Go5 == 0) {Go5 = 0x10;} else {Go5 = 0x00;}
          if (Go6 == 0) {Go6 = 0x20;} else {Go6 = 0x00;}
          if (Go7 == 0) {Go7 = 0x40;} else {Go7 = 0x00;}
          if (Go8 == 0) {Go8 = 0x80;} else {Go8 = 0x00;}

          // join each bit state to one byte
           txbuf[5]=(Go1|Go2|Go3|Go4|Go5|Go6|Go7|Go8);
  */

    // POT SECTION
    txbuf[2] = 0b00000000;
    txbuf[3] = 0b00000000;
    txbuf[4] = 0b00000000;
    txbuf[5] = 0b00000000;

    for (int pin = 0; pin < 8; pin++) {
      // the following code counts through the bit code to read each single ADC
      // channel. example ro read ADC channel 1 txbuf[0] = 0b01100000; txbuf[1]
      // = 0b00000000; txbuf[2| = 0b00000000;

      txbuf[0] = pin < 4 ? 0b01100000 : 0b11100000;

      if (pin % 4 == 0) { // pin == 0 || pin == 4
        txbuf[1] = 0b00000000;
      } else if (pin % 4 == 1) { // pin == 1 || pin == 5
        txbuf[1] = 0b00000010;
      } else if (pin % 4 == 2) { // pin == 2 || pin == 6
        txbuf[1] = 0b00000001;
      } else {
        txbuf[1] = 0b00000011;
      }

      palWritePad(GPIOA, 3, 0);             // enable pot read
      spiSelect(&SPID1);                    // START SPI
      spiExchange(&SPID1, 6, txbuf, rxbuf); // write/read at the same time
      spiUnselect(&SPID1);                  // SPI STOP
      palWritePad(GPIOA, 3, 1);             // disable pot read

      potBuf[pin] = (rxbuf[4] << 16 | rxbuf[3] << 8 | rxbuf[2]) << 8;

      pot1 = potBuf[1];
      pot2 = potBuf[0];
      pot3 = potBuf[2];
      pot4 = potBuf[3];
      pot5 = potBuf[5];
      pot6 = potBuf[7];
      pot7 = potBuf[6];
      pot8 = potBuf[4];

      chThdSleepMilliseconds(2);
    }

    // cv section
    txbuf[2] = 0b00000000;
    txbuf[3] = 0b00000000;
    txbuf[4] = 0b00000000;
    txbuf[5] = 0b00000000;

    for (int pin = 0; pin < 8; pin++) {
      txbuf[0] = pin < 4 ? 0b01100000 : 0b11100000;

      if (pin % 4 == 0) { // pin == 0 || pin == 4
        txbuf[1] = 0b00000000;
      } else if (pin % 4 == 1) { // pin == 1 || pin == 5
        txbuf[1] = 0b00000010;
      } else if (pin % 4 == 2) { // pin == 2 || pin == 6
        txbuf[1] = 0b00000001;
      } else {
        txbuf[1] = 0b00000011;
      }

      palWritePad(GPIOA, 2, 0);             // enable cv read
      spiSelect(&SPID1);                    // START SPI
      spiExchange(&SPID1, 6, txbuf, rxbuf); // write/read at the same time
      spiUnselect(&SPID1);                  // SPI STOP
      palWritePad(GPIOA, 2, 1);             // disable cv read

      cvBuf[pin] = ((rxbuf[4] << 16 | rxbuf[3] << 8 | rxbuf[2]) << 8);
      // if (pin < 7){ cvBuf[pin] = (cvBuf[pin]-(1<<26))<<1;}
      cvin1 = 0x07FFFFFF - cvBuf[7];
      cvin2 = ((0x07FFFFFF - cvBuf[3]) - (1 << 26)) << 1;
      cvin3 = ((0x07FFFFFF - cvBuf[2]) - (1 << 26)) << 1;
      cvin4 = ((0x07FFFFFF - cvBuf[6]) - (1 << 26)) << 1;
      cvin5 = ((0x07FFFFFF - cvBuf[1]) - (1 << 26)) << 1;
      cvin6 = ((0x07FFFFFF - cvBuf[4]) - (1 << 26)) << 1;
      cvin7 = ((0x07FFFFFF - cvBuf[0]) - (1 << 26)) << 1;
      cvin8 = ((0x07FFFFFF - cvBuf[5]) - (1 << 26)) << 1;

      chThdSleepMilliseconds(2);
    }

    chThdSleepMilliseconds(1);
  }
  chThdExit((msg_t)0);
}
static msg_t ThreadX(void *arg) { ((attr_parent *)arg)->ThreadX2(); }
WORKING_AREA(waThreadX, 1024);
Thread *Thd;
Init
cvin1 = 0;
cvin2 = 0;
cvin3 = 0;
cvin4 = 0;
cvin5 = 0;
cvin6 = 0;
cvin7 = 0;
cvin8 = 0;
pot1 = 0;
pot2 = 0;
pot3 = 0;
pot4 = 0;
pot5 = 0;
pot6 = 0;
pot7 = 0;
pot8 = 0;
// Go1=0; Go2=0; Go3=0; Go4=0; Go5=0; Go6=0; Go7=0; Go8=0;

palSetPadMode(GPIOA, 4, PAL_MODE_OUTPUT_PUSHPULL); // NSS
palSetPadMode(GPIOA, 5, PAL_MODE_OUTPUT_PUSHPULL); // SCK
palSetPadMode(GPIOA, 7, PAL_MODE_OUTPUT_PUSHPULL); // MOSI
// palSetPadMode(GPIOA, 4, PAL_MODE_ALTERNATE(5));// NSS
palSetPadMode(GPIOA, 5, PAL_MODE_ALTERNATE(5)); // SCK
palSetPadMode(GPIOA, 6, PAL_MODE_ALTERNATE(5)); // MISO
palSetPadMode(GPIOA, 7, PAL_MODE_ALTERNATE(5)); // MOSI

palSetPadMode(GPIOA, 3, PAL_MODE_OUTPUT_PUSHPULL);
palSetPadMode(GPIOA, 2, PAL_MODE_OUTPUT_PUSHPULL);

static const SPIConfig spicfg = {NULL, GPIOA, 4,
                                 0 | (4 << 3) | SPI_CR1_LSBFIRST};
spiStart(&SPID1, &spicfg);

Thd = chThdCreateStatic(waThreadX, sizeof(waThreadX), NORMALPRIO, ThreadX,
                        (void *)this);
Control Rate
outlet_pot1 = this->pot1;
outlet_pot2 = this->pot2;
outlet_pot3 = this->pot3;
outlet_pot4 = this->pot4;
outlet_pot5 = this->pot5;
outlet_pot6 = this->pot6;
outlet_pot7 = this->pot7;
outlet_pot8 = this->pot8;

outlet_cvin1 = this->cvin1;
outlet_cvin2 = this->cvin2;
outlet_cvin3 = this->cvin3;
outlet_cvin4 = this->cvin4;
outlet_cvin5 = this->cvin5;
outlet_cvin6 = this->cvin6;
outlet_cvin7 = this->cvin7;
outlet_cvin8 = this->cvin8;
Dispose
chThdTerminate(Thd);
chThdWait(Thd);

spiStop(&SPID1);
palSetPadMode(GPIOA, 4, PAL_MODE_INPUT_ANALOG);
palSetPadMode(GPIOA, 5, PAL_MODE_INPUT_ANALOG);
palSetPadMode(GPIOA, 6, PAL_MODE_INPUT_ANALOG);
palSetPadMode(GPIOA, 7, PAL_MODE_INPUT_ANALOG);

Privacy

© 2024 Zrna Research