123
Fork of LG by
Diff: DeviceSequencer.c
- Revision:
- 173:7f938afb0447
- Child:
- 197:7a05523bf588
diff -r ef7bf1663645 -r 7f938afb0447 DeviceSequencer.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DeviceSequencer.c Sun Jun 19 03:54:35 2016 +0000 @@ -0,0 +1,201 @@ +#include "Device.h" +extern Device device; + +void InitSequencerDefaultSettings(void){ + device.sequencer.sampler.settings.enabled = 1; + device.sequencer.sampler.settings.amplitude = 0x1e0000; //30V + device.sequencer.sampler.settings.position[0] = 0; //starting output sample position + device.sequencer.sampler.settings.position[1] = 16; //starting reference (delayed) sample position + device.sequencer.sampler.settings.sequence[0] = 0; + device.sequencer.sampler.settings.sequence[1] = 6424; + device.sequencer.sampler.settings.sequence[2] = 12785; + device.sequencer.sampler.settings.sequence[3] = 19024; + device.sequencer.sampler.settings.sequence[4] = 25080; + device.sequencer.sampler.settings.sequence[5] = 30893; + device.sequencer.sampler.settings.sequence[6] = 36410; + device.sequencer.sampler.settings.sequence[7] = 41576; + device.sequencer.sampler.settings.sequence[8] = 46341; + device.sequencer.sampler.settings.sequence[9] = 50660; + device.sequencer.sampler.settings.sequence[10] = 54491; + device.sequencer.sampler.settings.sequence[11] = 57798; + device.sequencer.sampler.settings.sequence[12] = 60547; + device.sequencer.sampler.settings.sequence[13] = 62714; + device.sequencer.sampler.settings.sequence[14] = 64277; + device.sequencer.sampler.settings.sequence[15] = 65220; + device.sequencer.sampler.settings.sequence[16] = 65536; + device.sequencer.sampler.settings.sequence[17] = 65220; + device.sequencer.sampler.settings.sequence[18] = 64277; + device.sequencer.sampler.settings.sequence[19] = 62714; + device.sequencer.sampler.settings.sequence[20] = 60547; + device.sequencer.sampler.settings.sequence[21] = 57798; + device.sequencer.sampler.settings.sequence[22] = 54491; + device.sequencer.sampler.settings.sequence[23] = 50660; + device.sequencer.sampler.settings.sequence[24] = 46341; + device.sequencer.sampler.settings.sequence[25] = 41576; + device.sequencer.sampler.settings.sequence[26] = 36410; + device.sequencer.sampler.settings.sequence[27] = 30893; + device.sequencer.sampler.settings.sequence[28] = 25080; + device.sequencer.sampler.settings.sequence[29] = 19024; + device.sequencer.sampler.settings.sequence[30] = 12785; + device.sequencer.sampler.settings.sequence[31] = 6424; + device.sequencer.sampler.settings.sequence[32] = 0; + device.sequencer.sampler.settings.sequence[33] = -6424; + device.sequencer.sampler.settings.sequence[34] = -12785; + device.sequencer.sampler.settings.sequence[35] = -19024; + device.sequencer.sampler.settings.sequence[36] = -25080; + device.sequencer.sampler.settings.sequence[37] = -30893; + device.sequencer.sampler.settings.sequence[38] = -36410; + device.sequencer.sampler.settings.sequence[39] = -41576; + device.sequencer.sampler.settings.sequence[40] = -46341; + device.sequencer.sampler.settings.sequence[41] = -50660; + device.sequencer.sampler.settings.sequence[42] = -54491; + device.sequencer.sampler.settings.sequence[43] = -57798; + device.sequencer.sampler.settings.sequence[44] = -60547; + device.sequencer.sampler.settings.sequence[45] = -62714; + device.sequencer.sampler.settings.sequence[46] = -64277; + device.sequencer.sampler.settings.sequence[47] = -65220; + device.sequencer.sampler.settings.sequence[48] = -65536; + device.sequencer.sampler.settings.sequence[49] = -65220; + device.sequencer.sampler.settings.sequence[50] = -64277; + device.sequencer.sampler.settings.sequence[51] = -62714; + device.sequencer.sampler.settings.sequence[52] = -60547; + device.sequencer.sampler.settings.sequence[53] = -57798; + device.sequencer.sampler.settings.sequence[54] = -54491; + device.sequencer.sampler.settings.sequence[55] = -50660; + device.sequencer.sampler.settings.sequence[56] = -46341; + device.sequencer.sampler.settings.sequence[57] = -41576; + device.sequencer.sampler.settings.sequence[58] = -36410; + device.sequencer.sampler.settings.sequence[59] = -30893; + device.sequencer.sampler.settings.sequence[60] = -25080; + device.sequencer.sampler.settings.sequence[61] = -19024; + device.sequencer.sampler.settings.sequence[62] = -12785; + device.sequencer.sampler.settings.sequence[63] = -6424; + + device.sequencer.output.analog.settings.enabled = 1; + device.sequencer.output.logic.settings.enabled = 0; + device.sequencer.output.analog.settings.transfer.points = 16; + device.sequencer.output.analog.settings.transfer.code[0] = 0x0; + device.sequencer.output.analog.settings.transfer.voltage[0] = -0x1e0000; //-30.0V + device.sequencer.output.analog.settings.transfer.code[1] = 0x1100; + device.sequencer.output.analog.settings.transfer.voltage[1] = -0x1a0000; //-26.0V + device.sequencer.output.analog.settings.transfer.code[2] = 0x2200; + device.sequencer.output.analog.settings.transfer.voltage[2] = -0x160000; //-22.0V + device.sequencer.output.analog.settings.transfer.code[3] = 0x3300; + device.sequencer.output.analog.settings.transfer.voltage[3] = -0x120000; //-18.0V + device.sequencer.output.analog.settings.transfer.code[4] = 0x4440; + device.sequencer.output.analog.settings.transfer.voltage[4] = -0xe0000; //-14.0V + device.sequencer.output.analog.settings.transfer.code[5] = 0x5540; + device.sequencer.output.analog.settings.transfer.voltage[5] = -0xa0000; //-10.0V + device.sequencer.output.analog.settings.transfer.code[6] = 0x6640; + device.sequencer.output.analog.settings.transfer.voltage[6] = -0x60000; //-6.0V + device.sequencer.output.analog.settings.transfer.code[7] = 0x7740; + device.sequencer.output.analog.settings.transfer.voltage[7] = -0x20000; //-2.0V + device.sequencer.output.analog.settings.transfer.code[8] = 0x8880; + device.sequencer.output.analog.settings.transfer.voltage[8] = 0x20000; //2.0V + device.sequencer.output.analog.settings.transfer.code[9] = 0x9980; + device.sequencer.output.analog.settings.transfer.voltage[9] = 0x60000; //6.0V + device.sequencer.output.analog.settings.transfer.code[10] = 0xaa80; + device.sequencer.output.analog.settings.transfer.voltage[10] = 0xa0000; //10.0V + device.sequencer.output.analog.settings.transfer.code[11] = 0xbb80; + device.sequencer.output.analog.settings.transfer.voltage[11] = 0xe0000; //14.0V + device.sequencer.output.analog.settings.transfer.code[12] = 0xccc0; + device.sequencer.output.analog.settings.transfer.voltage[12] = 0x120000; //18.0V + device.sequencer.output.analog.settings.transfer.code[13] = 0xddc0; + device.sequencer.output.analog.settings.transfer.voltage[13] = 0x160000; //22.0V + device.sequencer.output.analog.settings.transfer.code[14] = 0xeec0; + device.sequencer.output.analog.settings.transfer.voltage[14] = 0x1a0000; //26.0V + device.sequencer.output.analog.settings.transfer.code[15] = 0xffc0; + device.sequencer.output.analog.settings.transfer.voltage[15] = 0x1e0000; //30.0V +} + +void InitSequencerState(void){ + device.sequencer.sampler.state.enabled = device.sequencer.sampler.settings.enabled; + device.sequencer.sampler.state.amplitude = device.sequencer.sampler.settings.amplitude; + device.sequencer.sampler.state.position[0] = device.sequencer.sampler.settings.position[0]; //starting output sample position + device.sequencer.sampler.state.position[1] = device.sequencer.sampler.settings.position[1]; //starting reference (delayed) sample position + device.sequencer.sampler.state.sample[0] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[0]]; + device.sequencer.sampler.state.sample[1] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[1]]; + device.sequencer.sampler.state.voltage = device.sequencer.sampler.state.amplitude * device.sequencer.sampler.state.sample[0]; + + device.sequencer.output.analog.state.enabled = device.sequencer.output.analog.settings.enabled; + device.sequencer.output.logic.state.enabled = device.sequencer.output.logic.settings.enabled; +} + +void DeviceStartSequencer(void){ + if (device.sequencer.output.logic.state.enabled) { + //Setup P0.26: GPIO output + LPC_PINCON->PINSEL1 &= ~(3<<20); //P0.26 is GPIO pin (write 00b in bits 21:20 of PINSEL1) + LPC_PINCON->PINMODE1 |= (3<<20); //P0.26 pull-down resistor on (write 11b in bits 21:20 of PINMODE0) + LPC_GPIO0->FIODIR |= (1<<26); //P0.26 is output (write 1b in bit 4 of FIODIR) + LPC_GPIO0->FIOSET |= (1<<26); //off + } else if (device.sequencer.output.analog.state.enabled) { + LPC_SC->PCLKSEL0 &= ~(0x3<<22); //00b CLK/4; 01b CLK; 10b CLK/2; 11b CLK/8 + //Pin 0.26 function select AOUT (10b) + LPC_PINCON->PINSEL1 &= ~(3<<20); + LPC_PINCON->PINSEL1 |= 0x2 << 20; //P0.26 is AOUT pin (write 10b in bits 21:20 of PINSEL1) + } +} + +int32_t sequencerInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) { + int32_t ma, mb; + while (1) { + if (a1 == a) return b1; + if (a2 == a) return b2; + + ma = (a1 + a2) >> 1; + mb = (b1 + b2) >> 1; + + if (a < ma) { + if (a2 == ma) return mb; + if (b1 == mb) return mb; + a2 = ma; b2 = mb; + } else if (a > ma) { + if (a1 == ma) return mb; + if (b2 == mb) return mb; + a1 = ma; b1 = mb; + } else return mb; + } +} + +int32_t sequencerOutput(uint32_t voltage) { + int32_t v1, v2; + for (uint8_t i = 1; i < device.sequencer.output.analog.settings.transfer.points; i++) { + v1 = device.sequencer.output.analog.settings.transfer.voltage[i - 1]; + if (voltage < v1) { + return device.sequencer.output.analog.settings.transfer.code[i - 1]; + } + + v2 = device.sequencer.output.analog.settings.transfer.voltage[i]; + if (voltage < v2) { + int32_t c1 = device.sequencer.output.analog.settings.transfer.code[i - 1]; + int32_t c2 = device.sequencer.output.analog.settings.transfer.code[i]; + return sequencerInterpolate(voltage, v1, v2, c1, c2); + } + } + return device.sequencer.output.analog.settings.transfer.code[device.sequencer.output.analog.settings.transfer.points - 1]; +} + +void sequencerProcess(void){ + //Sample + device.sequencer.sampler.state.sample[0] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[0]]; + device.sequencer.sampler.state.sample[1] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[1]]; + device.sequencer.sampler.state.voltage = device.sequencer.sampler.state.amplitude * device.sequencer.sampler.state.sample[0]; + //Set output + if (device.sequencer.output.logic.state.enabled) { + if (device.sequencer.sampler.state.sample[0] > 0) { + LPC_GPIO0->FIOCLR |= (1<<26); //1 + } else { + LPC_GPIO0->FIOSET |= (1<<26); //0 + } + } else if (device.sequencer.output.analog.state.enabled) { + device.sequencer.output.analog.state.voltage = device.sequencer.sampler.state.voltage; + //Warning! bit 16 - 0/1: Update rate 1MHz/400kHz; bits 15...6 - code; bits 5...0 - reserved, write 000000 + //LPC_DAC->DACR = sequencerOutput(device.sequencer.output.analog.state.voltage); + LPC_DAC->CR = sequencerOutput(device.sequencer.output.analog.state.voltage); + } + //Process sequencer state + device.sequencer.sampler.state.position[0]++; + if (device.sequencer.sampler.state.position[0] == 64) device.sequencer.sampler.state.position[0] = 0; + device.sequencer.sampler.state.position[1]++; + if (device.sequencer.sampler.state.position[1] == 64) device.sequencer.sampler.state.position[1] = 0; +} \ No newline at end of file