123
Fork of LG by
DeviceSequencer.c@197:7a05523bf588, 2016-10-19 (annotated)
- Committer:
- Kovalev_D
- Date:
- Wed Oct 19 10:55:05 2016 +0000
- Revision:
- 197:7a05523bf588
- Parent:
- 173:7f938afb0447
modul
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Diletant | 173:7f938afb0447 | 1 | #include "Device.h" |
Diletant | 173:7f938afb0447 | 2 | extern Device device; |
Diletant | 173:7f938afb0447 | 3 | |
Diletant | 173:7f938afb0447 | 4 | void InitSequencerDefaultSettings(void){ |
Diletant | 173:7f938afb0447 | 5 | device.sequencer.sampler.settings.enabled = 1; |
Diletant | 173:7f938afb0447 | 6 | device.sequencer.sampler.settings.amplitude = 0x1e0000; //30V |
Diletant | 173:7f938afb0447 | 7 | device.sequencer.sampler.settings.position[0] = 0; //starting output sample position |
Diletant | 173:7f938afb0447 | 8 | device.sequencer.sampler.settings.position[1] = 16; //starting reference (delayed) sample position |
Diletant | 173:7f938afb0447 | 9 | device.sequencer.sampler.settings.sequence[0] = 0; |
Diletant | 173:7f938afb0447 | 10 | device.sequencer.sampler.settings.sequence[1] = 6424; |
Diletant | 173:7f938afb0447 | 11 | device.sequencer.sampler.settings.sequence[2] = 12785; |
Diletant | 173:7f938afb0447 | 12 | device.sequencer.sampler.settings.sequence[3] = 19024; |
Diletant | 173:7f938afb0447 | 13 | device.sequencer.sampler.settings.sequence[4] = 25080; |
Diletant | 173:7f938afb0447 | 14 | device.sequencer.sampler.settings.sequence[5] = 30893; |
Diletant | 173:7f938afb0447 | 15 | device.sequencer.sampler.settings.sequence[6] = 36410; |
Diletant | 173:7f938afb0447 | 16 | device.sequencer.sampler.settings.sequence[7] = 41576; |
Diletant | 173:7f938afb0447 | 17 | device.sequencer.sampler.settings.sequence[8] = 46341; |
Diletant | 173:7f938afb0447 | 18 | device.sequencer.sampler.settings.sequence[9] = 50660; |
Diletant | 173:7f938afb0447 | 19 | device.sequencer.sampler.settings.sequence[10] = 54491; |
Diletant | 173:7f938afb0447 | 20 | device.sequencer.sampler.settings.sequence[11] = 57798; |
Diletant | 173:7f938afb0447 | 21 | device.sequencer.sampler.settings.sequence[12] = 60547; |
Diletant | 173:7f938afb0447 | 22 | device.sequencer.sampler.settings.sequence[13] = 62714; |
Diletant | 173:7f938afb0447 | 23 | device.sequencer.sampler.settings.sequence[14] = 64277; |
Diletant | 173:7f938afb0447 | 24 | device.sequencer.sampler.settings.sequence[15] = 65220; |
Diletant | 173:7f938afb0447 | 25 | device.sequencer.sampler.settings.sequence[16] = 65536; |
Diletant | 173:7f938afb0447 | 26 | device.sequencer.sampler.settings.sequence[17] = 65220; |
Diletant | 173:7f938afb0447 | 27 | device.sequencer.sampler.settings.sequence[18] = 64277; |
Diletant | 173:7f938afb0447 | 28 | device.sequencer.sampler.settings.sequence[19] = 62714; |
Diletant | 173:7f938afb0447 | 29 | device.sequencer.sampler.settings.sequence[20] = 60547; |
Diletant | 173:7f938afb0447 | 30 | device.sequencer.sampler.settings.sequence[21] = 57798; |
Diletant | 173:7f938afb0447 | 31 | device.sequencer.sampler.settings.sequence[22] = 54491; |
Diletant | 173:7f938afb0447 | 32 | device.sequencer.sampler.settings.sequence[23] = 50660; |
Diletant | 173:7f938afb0447 | 33 | device.sequencer.sampler.settings.sequence[24] = 46341; |
Diletant | 173:7f938afb0447 | 34 | device.sequencer.sampler.settings.sequence[25] = 41576; |
Diletant | 173:7f938afb0447 | 35 | device.sequencer.sampler.settings.sequence[26] = 36410; |
Diletant | 173:7f938afb0447 | 36 | device.sequencer.sampler.settings.sequence[27] = 30893; |
Diletant | 173:7f938afb0447 | 37 | device.sequencer.sampler.settings.sequence[28] = 25080; |
Diletant | 173:7f938afb0447 | 38 | device.sequencer.sampler.settings.sequence[29] = 19024; |
Diletant | 173:7f938afb0447 | 39 | device.sequencer.sampler.settings.sequence[30] = 12785; |
Diletant | 173:7f938afb0447 | 40 | device.sequencer.sampler.settings.sequence[31] = 6424; |
Diletant | 173:7f938afb0447 | 41 | device.sequencer.sampler.settings.sequence[32] = 0; |
Diletant | 173:7f938afb0447 | 42 | device.sequencer.sampler.settings.sequence[33] = -6424; |
Diletant | 173:7f938afb0447 | 43 | device.sequencer.sampler.settings.sequence[34] = -12785; |
Diletant | 173:7f938afb0447 | 44 | device.sequencer.sampler.settings.sequence[35] = -19024; |
Diletant | 173:7f938afb0447 | 45 | device.sequencer.sampler.settings.sequence[36] = -25080; |
Diletant | 173:7f938afb0447 | 46 | device.sequencer.sampler.settings.sequence[37] = -30893; |
Diletant | 173:7f938afb0447 | 47 | device.sequencer.sampler.settings.sequence[38] = -36410; |
Diletant | 173:7f938afb0447 | 48 | device.sequencer.sampler.settings.sequence[39] = -41576; |
Diletant | 173:7f938afb0447 | 49 | device.sequencer.sampler.settings.sequence[40] = -46341; |
Diletant | 173:7f938afb0447 | 50 | device.sequencer.sampler.settings.sequence[41] = -50660; |
Diletant | 173:7f938afb0447 | 51 | device.sequencer.sampler.settings.sequence[42] = -54491; |
Diletant | 173:7f938afb0447 | 52 | device.sequencer.sampler.settings.sequence[43] = -57798; |
Diletant | 173:7f938afb0447 | 53 | device.sequencer.sampler.settings.sequence[44] = -60547; |
Diletant | 173:7f938afb0447 | 54 | device.sequencer.sampler.settings.sequence[45] = -62714; |
Diletant | 173:7f938afb0447 | 55 | device.sequencer.sampler.settings.sequence[46] = -64277; |
Diletant | 173:7f938afb0447 | 56 | device.sequencer.sampler.settings.sequence[47] = -65220; |
Diletant | 173:7f938afb0447 | 57 | device.sequencer.sampler.settings.sequence[48] = -65536; |
Diletant | 173:7f938afb0447 | 58 | device.sequencer.sampler.settings.sequence[49] = -65220; |
Diletant | 173:7f938afb0447 | 59 | device.sequencer.sampler.settings.sequence[50] = -64277; |
Diletant | 173:7f938afb0447 | 60 | device.sequencer.sampler.settings.sequence[51] = -62714; |
Diletant | 173:7f938afb0447 | 61 | device.sequencer.sampler.settings.sequence[52] = -60547; |
Diletant | 173:7f938afb0447 | 62 | device.sequencer.sampler.settings.sequence[53] = -57798; |
Diletant | 173:7f938afb0447 | 63 | device.sequencer.sampler.settings.sequence[54] = -54491; |
Diletant | 173:7f938afb0447 | 64 | device.sequencer.sampler.settings.sequence[55] = -50660; |
Diletant | 173:7f938afb0447 | 65 | device.sequencer.sampler.settings.sequence[56] = -46341; |
Diletant | 173:7f938afb0447 | 66 | device.sequencer.sampler.settings.sequence[57] = -41576; |
Diletant | 173:7f938afb0447 | 67 | device.sequencer.sampler.settings.sequence[58] = -36410; |
Diletant | 173:7f938afb0447 | 68 | device.sequencer.sampler.settings.sequence[59] = -30893; |
Diletant | 173:7f938afb0447 | 69 | device.sequencer.sampler.settings.sequence[60] = -25080; |
Diletant | 173:7f938afb0447 | 70 | device.sequencer.sampler.settings.sequence[61] = -19024; |
Diletant | 173:7f938afb0447 | 71 | device.sequencer.sampler.settings.sequence[62] = -12785; |
Diletant | 173:7f938afb0447 | 72 | device.sequencer.sampler.settings.sequence[63] = -6424; |
Diletant | 173:7f938afb0447 | 73 | |
Diletant | 173:7f938afb0447 | 74 | device.sequencer.output.analog.settings.enabled = 1; |
Diletant | 173:7f938afb0447 | 75 | device.sequencer.output.logic.settings.enabled = 0; |
Diletant | 173:7f938afb0447 | 76 | device.sequencer.output.analog.settings.transfer.points = 16; |
Diletant | 173:7f938afb0447 | 77 | device.sequencer.output.analog.settings.transfer.code[0] = 0x0; |
Diletant | 173:7f938afb0447 | 78 | device.sequencer.output.analog.settings.transfer.voltage[0] = -0x1e0000; //-30.0V |
Diletant | 173:7f938afb0447 | 79 | device.sequencer.output.analog.settings.transfer.code[1] = 0x1100; |
Diletant | 173:7f938afb0447 | 80 | device.sequencer.output.analog.settings.transfer.voltage[1] = -0x1a0000; //-26.0V |
Diletant | 173:7f938afb0447 | 81 | device.sequencer.output.analog.settings.transfer.code[2] = 0x2200; |
Diletant | 173:7f938afb0447 | 82 | device.sequencer.output.analog.settings.transfer.voltage[2] = -0x160000; //-22.0V |
Diletant | 173:7f938afb0447 | 83 | device.sequencer.output.analog.settings.transfer.code[3] = 0x3300; |
Diletant | 173:7f938afb0447 | 84 | device.sequencer.output.analog.settings.transfer.voltage[3] = -0x120000; //-18.0V |
Diletant | 173:7f938afb0447 | 85 | device.sequencer.output.analog.settings.transfer.code[4] = 0x4440; |
Diletant | 173:7f938afb0447 | 86 | device.sequencer.output.analog.settings.transfer.voltage[4] = -0xe0000; //-14.0V |
Diletant | 173:7f938afb0447 | 87 | device.sequencer.output.analog.settings.transfer.code[5] = 0x5540; |
Diletant | 173:7f938afb0447 | 88 | device.sequencer.output.analog.settings.transfer.voltage[5] = -0xa0000; //-10.0V |
Diletant | 173:7f938afb0447 | 89 | device.sequencer.output.analog.settings.transfer.code[6] = 0x6640; |
Diletant | 173:7f938afb0447 | 90 | device.sequencer.output.analog.settings.transfer.voltage[6] = -0x60000; //-6.0V |
Diletant | 173:7f938afb0447 | 91 | device.sequencer.output.analog.settings.transfer.code[7] = 0x7740; |
Diletant | 173:7f938afb0447 | 92 | device.sequencer.output.analog.settings.transfer.voltage[7] = -0x20000; //-2.0V |
Diletant | 173:7f938afb0447 | 93 | device.sequencer.output.analog.settings.transfer.code[8] = 0x8880; |
Diletant | 173:7f938afb0447 | 94 | device.sequencer.output.analog.settings.transfer.voltage[8] = 0x20000; //2.0V |
Diletant | 173:7f938afb0447 | 95 | device.sequencer.output.analog.settings.transfer.code[9] = 0x9980; |
Diletant | 173:7f938afb0447 | 96 | device.sequencer.output.analog.settings.transfer.voltage[9] = 0x60000; //6.0V |
Diletant | 173:7f938afb0447 | 97 | device.sequencer.output.analog.settings.transfer.code[10] = 0xaa80; |
Diletant | 173:7f938afb0447 | 98 | device.sequencer.output.analog.settings.transfer.voltage[10] = 0xa0000; //10.0V |
Diletant | 173:7f938afb0447 | 99 | device.sequencer.output.analog.settings.transfer.code[11] = 0xbb80; |
Diletant | 173:7f938afb0447 | 100 | device.sequencer.output.analog.settings.transfer.voltage[11] = 0xe0000; //14.0V |
Diletant | 173:7f938afb0447 | 101 | device.sequencer.output.analog.settings.transfer.code[12] = 0xccc0; |
Diletant | 173:7f938afb0447 | 102 | device.sequencer.output.analog.settings.transfer.voltage[12] = 0x120000; //18.0V |
Diletant | 173:7f938afb0447 | 103 | device.sequencer.output.analog.settings.transfer.code[13] = 0xddc0; |
Diletant | 173:7f938afb0447 | 104 | device.sequencer.output.analog.settings.transfer.voltage[13] = 0x160000; //22.0V |
Diletant | 173:7f938afb0447 | 105 | device.sequencer.output.analog.settings.transfer.code[14] = 0xeec0; |
Diletant | 173:7f938afb0447 | 106 | device.sequencer.output.analog.settings.transfer.voltage[14] = 0x1a0000; //26.0V |
Diletant | 173:7f938afb0447 | 107 | device.sequencer.output.analog.settings.transfer.code[15] = 0xffc0; |
Diletant | 173:7f938afb0447 | 108 | device.sequencer.output.analog.settings.transfer.voltage[15] = 0x1e0000; //30.0V |
Diletant | 173:7f938afb0447 | 109 | } |
Diletant | 173:7f938afb0447 | 110 | |
Diletant | 173:7f938afb0447 | 111 | void InitSequencerState(void){ |
Diletant | 173:7f938afb0447 | 112 | device.sequencer.sampler.state.enabled = device.sequencer.sampler.settings.enabled; |
Diletant | 173:7f938afb0447 | 113 | device.sequencer.sampler.state.amplitude = device.sequencer.sampler.settings.amplitude; |
Diletant | 173:7f938afb0447 | 114 | device.sequencer.sampler.state.position[0] = device.sequencer.sampler.settings.position[0]; //starting output sample position |
Diletant | 173:7f938afb0447 | 115 | device.sequencer.sampler.state.position[1] = device.sequencer.sampler.settings.position[1]; //starting reference (delayed) sample position |
Diletant | 173:7f938afb0447 | 116 | device.sequencer.sampler.state.sample[0] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[0]]; |
Diletant | 173:7f938afb0447 | 117 | device.sequencer.sampler.state.sample[1] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[1]]; |
Diletant | 173:7f938afb0447 | 118 | device.sequencer.sampler.state.voltage = device.sequencer.sampler.state.amplitude * device.sequencer.sampler.state.sample[0]; |
Diletant | 173:7f938afb0447 | 119 | |
Diletant | 173:7f938afb0447 | 120 | device.sequencer.output.analog.state.enabled = device.sequencer.output.analog.settings.enabled; |
Diletant | 173:7f938afb0447 | 121 | device.sequencer.output.logic.state.enabled = device.sequencer.output.logic.settings.enabled; |
Diletant | 173:7f938afb0447 | 122 | } |
Diletant | 173:7f938afb0447 | 123 | |
Diletant | 173:7f938afb0447 | 124 | void DeviceStartSequencer(void){ |
Diletant | 173:7f938afb0447 | 125 | if (device.sequencer.output.logic.state.enabled) { |
Diletant | 173:7f938afb0447 | 126 | //Setup P0.26: GPIO output |
Diletant | 173:7f938afb0447 | 127 | LPC_PINCON->PINSEL1 &= ~(3<<20); //P0.26 is GPIO pin (write 00b in bits 21:20 of PINSEL1) |
Diletant | 173:7f938afb0447 | 128 | LPC_PINCON->PINMODE1 |= (3<<20); //P0.26 pull-down resistor on (write 11b in bits 21:20 of PINMODE0) |
Diletant | 173:7f938afb0447 | 129 | LPC_GPIO0->FIODIR |= (1<<26); //P0.26 is output (write 1b in bit 4 of FIODIR) |
Diletant | 173:7f938afb0447 | 130 | LPC_GPIO0->FIOSET |= (1<<26); //off |
Diletant | 173:7f938afb0447 | 131 | } else if (device.sequencer.output.analog.state.enabled) { |
Diletant | 173:7f938afb0447 | 132 | LPC_SC->PCLKSEL0 &= ~(0x3<<22); //00b CLK/4; 01b CLK; 10b CLK/2; 11b CLK/8 |
Diletant | 173:7f938afb0447 | 133 | //Pin 0.26 function select AOUT (10b) |
Diletant | 173:7f938afb0447 | 134 | LPC_PINCON->PINSEL1 &= ~(3<<20); |
Diletant | 173:7f938afb0447 | 135 | LPC_PINCON->PINSEL1 |= 0x2 << 20; //P0.26 is AOUT pin (write 10b in bits 21:20 of PINSEL1) |
Diletant | 173:7f938afb0447 | 136 | } |
Diletant | 173:7f938afb0447 | 137 | } |
Diletant | 173:7f938afb0447 | 138 | |
Diletant | 173:7f938afb0447 | 139 | int32_t sequencerInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) { |
Diletant | 173:7f938afb0447 | 140 | int32_t ma, mb; |
Diletant | 173:7f938afb0447 | 141 | while (1) { |
Diletant | 173:7f938afb0447 | 142 | if (a1 == a) return b1; |
Diletant | 173:7f938afb0447 | 143 | if (a2 == a) return b2; |
Diletant | 173:7f938afb0447 | 144 | |
Diletant | 173:7f938afb0447 | 145 | ma = (a1 + a2) >> 1; |
Diletant | 173:7f938afb0447 | 146 | mb = (b1 + b2) >> 1; |
Diletant | 173:7f938afb0447 | 147 | |
Diletant | 173:7f938afb0447 | 148 | if (a < ma) { |
Diletant | 173:7f938afb0447 | 149 | if (a2 == ma) return mb; |
Diletant | 173:7f938afb0447 | 150 | if (b1 == mb) return mb; |
Diletant | 173:7f938afb0447 | 151 | a2 = ma; b2 = mb; |
Diletant | 173:7f938afb0447 | 152 | } else if (a > ma) { |
Diletant | 173:7f938afb0447 | 153 | if (a1 == ma) return mb; |
Diletant | 173:7f938afb0447 | 154 | if (b2 == mb) return mb; |
Diletant | 173:7f938afb0447 | 155 | a1 = ma; b1 = mb; |
Diletant | 173:7f938afb0447 | 156 | } else return mb; |
Diletant | 173:7f938afb0447 | 157 | } |
Diletant | 173:7f938afb0447 | 158 | } |
Diletant | 173:7f938afb0447 | 159 | |
Diletant | 173:7f938afb0447 | 160 | int32_t sequencerOutput(uint32_t voltage) { |
Diletant | 173:7f938afb0447 | 161 | int32_t v1, v2; |
Diletant | 173:7f938afb0447 | 162 | for (uint8_t i = 1; i < device.sequencer.output.analog.settings.transfer.points; i++) { |
Diletant | 173:7f938afb0447 | 163 | v1 = device.sequencer.output.analog.settings.transfer.voltage[i - 1]; |
Diletant | 173:7f938afb0447 | 164 | if (voltage < v1) { |
Diletant | 173:7f938afb0447 | 165 | return device.sequencer.output.analog.settings.transfer.code[i - 1]; |
Diletant | 173:7f938afb0447 | 166 | } |
Diletant | 173:7f938afb0447 | 167 | |
Diletant | 173:7f938afb0447 | 168 | v2 = device.sequencer.output.analog.settings.transfer.voltage[i]; |
Diletant | 173:7f938afb0447 | 169 | if (voltage < v2) { |
Diletant | 173:7f938afb0447 | 170 | int32_t c1 = device.sequencer.output.analog.settings.transfer.code[i - 1]; |
Diletant | 173:7f938afb0447 | 171 | int32_t c2 = device.sequencer.output.analog.settings.transfer.code[i]; |
Diletant | 173:7f938afb0447 | 172 | return sequencerInterpolate(voltage, v1, v2, c1, c2); |
Diletant | 173:7f938afb0447 | 173 | } |
Diletant | 173:7f938afb0447 | 174 | } |
Diletant | 173:7f938afb0447 | 175 | return device.sequencer.output.analog.settings.transfer.code[device.sequencer.output.analog.settings.transfer.points - 1]; |
Diletant | 173:7f938afb0447 | 176 | } |
Diletant | 173:7f938afb0447 | 177 | |
Diletant | 173:7f938afb0447 | 178 | void sequencerProcess(void){ |
Diletant | 173:7f938afb0447 | 179 | //Sample |
Diletant | 173:7f938afb0447 | 180 | device.sequencer.sampler.state.sample[0] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[0]]; |
Diletant | 173:7f938afb0447 | 181 | device.sequencer.sampler.state.sample[1] = device.sequencer.sampler.settings.sequence[device.sequencer.sampler.state.position[1]]; |
Diletant | 173:7f938afb0447 | 182 | device.sequencer.sampler.state.voltage = device.sequencer.sampler.state.amplitude * device.sequencer.sampler.state.sample[0]; |
Diletant | 173:7f938afb0447 | 183 | //Set output |
Diletant | 173:7f938afb0447 | 184 | if (device.sequencer.output.logic.state.enabled) { |
Diletant | 173:7f938afb0447 | 185 | if (device.sequencer.sampler.state.sample[0] > 0) { |
Diletant | 173:7f938afb0447 | 186 | LPC_GPIO0->FIOCLR |= (1<<26); //1 |
Diletant | 173:7f938afb0447 | 187 | } else { |
Diletant | 173:7f938afb0447 | 188 | LPC_GPIO0->FIOSET |= (1<<26); //0 |
Diletant | 173:7f938afb0447 | 189 | } |
Diletant | 173:7f938afb0447 | 190 | } else if (device.sequencer.output.analog.state.enabled) { |
Diletant | 173:7f938afb0447 | 191 | device.sequencer.output.analog.state.voltage = device.sequencer.sampler.state.voltage; |
Diletant | 173:7f938afb0447 | 192 | //Warning! bit 16 - 0/1: Update rate 1MHz/400kHz; bits 15...6 - code; bits 5...0 - reserved, write 000000 |
Kovalev_D | 197:7a05523bf588 | 193 | LPC_DAC->DACR = sequencerOutput(device.sequencer.output.analog.state.voltage); |
Kovalev_D | 197:7a05523bf588 | 194 | //LPC_DAC->CR = sequencerOutput(device.sequencer.output.analog.state.voltage); |
Diletant | 173:7f938afb0447 | 195 | } |
Diletant | 173:7f938afb0447 | 196 | //Process sequencer state |
Diletant | 173:7f938afb0447 | 197 | device.sequencer.sampler.state.position[0]++; |
Diletant | 173:7f938afb0447 | 198 | if (device.sequencer.sampler.state.position[0] == 64) device.sequencer.sampler.state.position[0] = 0; |
Diletant | 173:7f938afb0447 | 199 | device.sequencer.sampler.state.position[1]++; |
Diletant | 173:7f938afb0447 | 200 | if (device.sequencer.sampler.state.position[1] == 64) device.sequencer.sampler.state.position[1] = 0; |
Diletant | 173:7f938afb0447 | 201 | } |