123

Dependencies:   mbed

Fork of LG by igor Apu

Committer:
Diletant
Date:
Sun May 22 18:59:20 2016 +0000
Revision:
166:c3c0b8a90d81
Parent:
161:efd949e8d536
Child:
167:bedc0a9d559a
Device & ... update. Not final!!!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Diletant 161:efd949e8d536 1 #include "Device.h"
Diletant 161:efd949e8d536 2
Diletant 161:efd949e8d536 3 extern Device device;
Diletant 161:efd949e8d536 4
Diletant 161:efd949e8d536 5 void InitISACSOutputWithDefaults(void){
Diletant 166:c3c0b8a90d81 6 device.isacs.output.interpolator.settings.points = 2;
Diletant 166:c3c0b8a90d81 7 //Control point 1.5 volt - low output limit
Diletant 166:c3c0b8a90d81 8 device.isacs.output.interpolator.settings.voltage[0] = 0x00017fff;
Diletant 166:c3c0b8a90d81 9 device.isacs.output.interpolator.settings.code[0] = 800;
Diletant 166:c3c0b8a90d81 10 //Control point 12.5 volt - high output limit
Diletant 166:c3c0b8a90d81 11 device.isacs.output.interpolator.settings.voltage[1] = 0x000c7fff;
Diletant 166:c3c0b8a90d81 12 device.isacs.output.interpolator.settings.code[1] = 2048;
Diletant 166:c3c0b8a90d81 13 device.isacs.output.start.settings.voltage = 0x000c7fff; //+12.5V start
Diletant 166:c3c0b8a90d81 14 device.isacs.output.reset.settings.voltage = 0x00077fff; //+7.5V if regulator disabled at reset time
Diletant 161:efd949e8d536 15 }
Diletant 161:efd949e8d536 16
Diletant 161:efd949e8d536 17 void InitISACSOutput(void){
Diletant 166:c3c0b8a90d81 18 //isacsSetOutputVoltage(device.isacs.output.start.settings.voltage);
Diletant 161:efd949e8d536 19 }
Diletant 161:efd949e8d536 20
Diletant 161:efd949e8d536 21 void InitISACSRegulatorWithDefaults(void){
Diletant 161:efd949e8d536 22 //Startup settings
Diletant 166:c3c0b8a90d81 23 device.isacs.regulator.settings.start.enabled = 1;
Diletant 161:efd949e8d536 24 device.isacs.regulator.settings.start.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format
Diletant 161:efd949e8d536 25 device.isacs.regulator.settings.start.scale = 1; //Scale factor
Diletant 161:efd949e8d536 26 //Regular operation settings
Diletant 166:c3c0b8a90d81 27 device.isacs.regulator.settings.regular.enabled = 1;
Diletant 161:efd949e8d536 28 device.isacs.regulator.settings.regular.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format
Diletant 161:efd949e8d536 29 device.isacs.regulator.settings.regular.scale = 1; //Scale factor
Diletant 161:efd949e8d536 30 //Reset operation settings
Diletant 166:c3c0b8a90d81 31 device.isacs.regulator.settings.reset.enabled = 1;
Diletant 161:efd949e8d536 32 device.isacs.regulator.settings.reset.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format
Diletant 161:efd949e8d536 33 device.isacs.regulator.settings.reset.scale = 1; //Scale factor
Diletant 161:efd949e8d536 34 }
Diletant 161:efd949e8d536 35
Diletant 161:efd949e8d536 36 void InitISACSRegulator(void){
Diletant 166:c3c0b8a90d81 37 device.isacs.regulator.state.enabled = device.isacs.regulator.settings.start.enabled;
Diletant 161:efd949e8d536 38 device.isacs.regulator.state.reference = device.isacs.regulator.settings.start.reference;
Diletant 161:efd949e8d536 39 device.isacs.regulator.state.scale = device.isacs.regulator.settings.start.scale;
Diletant 161:efd949e8d536 40 }
Diletant 161:efd949e8d536 41
Diletant 161:efd949e8d536 42 void InitISACSWithDefaults(void){
Diletant 161:efd949e8d536 43 InitISACSOutputWithDefaults();
Diletant 161:efd949e8d536 44 InitISACSRegulatorWithDefaults();
Diletant 161:efd949e8d536 45 InitISACSPotentiometersWithDefaults();
Diletant 161:efd949e8d536 46 }
Diletant 161:efd949e8d536 47
Diletant 161:efd949e8d536 48 void InitISACS(void){
Diletant 161:efd949e8d536 49 InitISACSOutput();
Diletant 161:efd949e8d536 50 InitISACSRegulator();
Diletant 161:efd949e8d536 51 InitISACSPotentiometers();
Diletant 161:efd949e8d536 52 }
Diletant 161:efd949e8d536 53
Diletant 161:efd949e8d536 54 void StartISACS(void){
Diletant 161:efd949e8d536 55 }
Diletant 161:efd949e8d536 56
Diletant 166:c3c0b8a90d81 57 /*
Diletant 166:c3c0b8a90d81 58 //Count leading zeroes
Diletant 166:c3c0b8a90d81 59 static uint8_t clz(uint32_t x) {
Diletant 166:c3c0b8a90d81 60 uint8_t result = 0;
Diletant 166:c3c0b8a90d81 61 if (x == 0) return 32;
Diletant 166:c3c0b8a90d81 62 while (!(x & 0xF0000000)) { result += 4; x <<= 4; }
Diletant 166:c3c0b8a90d81 63 while (!(x & 0x80000000)) { result += 1; x <<= 1; }
Diletant 166:c3c0b8a90d81 64 return result;
Diletant 166:c3c0b8a90d81 65 }
Diletant 166:c3c0b8a90d81 66 //Count trailing zeroes
Diletant 166:c3c0b8a90d81 67 static uint8_t ctz(uint32_t x) {
Diletant 166:c3c0b8a90d81 68 uint8_t result = 0;
Diletant 166:c3c0b8a90d81 69 if (x == 0) return 32;
Diletant 166:c3c0b8a90d81 70 while (!(x & 0xF0000000)) { result += 4; x <<= 4; }
Diletant 166:c3c0b8a90d81 71 while (!(x & 0x80000000)) { result += 1; x <<= 1; }
Diletant 166:c3c0b8a90d81 72 return result;
Diletant 166:c3c0b8a90d81 73 }
Diletant 166:c3c0b8a90d81 74 */
Diletant 166:c3c0b8a90d81 75
Diletant 166:c3c0b8a90d81 76 int32_t isacsInterpolate(int32_t a,int32_t a1,int32_t a2,int32_t b1,int32_t b2) {
Diletant 166:c3c0b8a90d81 77 int32_t ma, mb;
Diletant 166:c3c0b8a90d81 78 while (1) {
Diletant 166:c3c0b8a90d81 79 if (a1 == a) return b1;
Diletant 166:c3c0b8a90d81 80 if (a2 == a) return b2;
Diletant 166:c3c0b8a90d81 81
Diletant 166:c3c0b8a90d81 82 ma = (a1 + a2) >> 1;
Diletant 166:c3c0b8a90d81 83 mb = (b1 + b2) >> 1;
Diletant 166:c3c0b8a90d81 84
Diletant 166:c3c0b8a90d81 85 if (a < ma) {
Diletant 166:c3c0b8a90d81 86 if (a2 == ma) return mb;
Diletant 166:c3c0b8a90d81 87 if (b1 == mb) return mb;
Diletant 166:c3c0b8a90d81 88 a2 = ma; b2 = mb;
Diletant 166:c3c0b8a90d81 89 } else if (a > ma) {
Diletant 166:c3c0b8a90d81 90 if (a1 == ma) return mb;
Diletant 166:c3c0b8a90d81 91 if (b2 == mb) return mb;
Diletant 166:c3c0b8a90d81 92 a1 = ma; b1 = mb;
Diletant 166:c3c0b8a90d81 93 } else return mb;
Diletant 166:c3c0b8a90d81 94 }
Diletant 166:c3c0b8a90d81 95 }
Diletant 166:c3c0b8a90d81 96
Diletant 166:c3c0b8a90d81 97 int32_t isacsInputCodeToVoltage(uint32_t code) {
Diletant 166:c3c0b8a90d81 98 int32_t c1, c2;
Diletant 166:c3c0b8a90d81 99 for (uint8_t i = 1; i < device.isacs.input.interpolator.settings.points; i++) {
Diletant 166:c3c0b8a90d81 100 c1 = device.isacs.input.interpolator.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 101 if (code < c1) {
Diletant 166:c3c0b8a90d81 102 return device.isacs.input.interpolator.settings.voltage[i - 1];
Diletant 166:c3c0b8a90d81 103 }
Diletant 166:c3c0b8a90d81 104
Diletant 166:c3c0b8a90d81 105 c2 = device.isacs.input.interpolator.settings.code[i];
Diletant 166:c3c0b8a90d81 106 if (code < c2) {
Diletant 166:c3c0b8a90d81 107 int32_t v1 = device.isacs.input.interpolator.settings.voltage[i - 1];
Diletant 166:c3c0b8a90d81 108 int32_t v2 = device.isacs.input.interpolator.settings.voltage[i];
Diletant 166:c3c0b8a90d81 109 return isacsInterpolate(code, c1, c2, v1, v2);
Diletant 166:c3c0b8a90d81 110 }
Diletant 166:c3c0b8a90d81 111 }
Diletant 166:c3c0b8a90d81 112 return device.isacs.input.interpolator.settings.voltage[device.isacs.input.interpolator.settings.points - 1];
Diletant 166:c3c0b8a90d81 113 }
Diletant 166:c3c0b8a90d81 114
Diletant 166:c3c0b8a90d81 115 int32_t isacsOutputVoltageToCode(uint32_t voltage) {
Diletant 166:c3c0b8a90d81 116 int32_t v1, v2;
Diletant 166:c3c0b8a90d81 117 for (uint8_t i = 1; i < device.isacs.output.interpolator.settings.points; i++) {
Diletant 166:c3c0b8a90d81 118 v1 = device.isacs.output.interpolator.settings.voltage[i - 1];
Diletant 166:c3c0b8a90d81 119 if (voltage < v1) {
Diletant 166:c3c0b8a90d81 120 return device.isacs.output.interpolator.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 121 }
Diletant 166:c3c0b8a90d81 122
Diletant 166:c3c0b8a90d81 123 v2 = device.isacs.output.interpolator.settings.voltage[i];
Diletant 166:c3c0b8a90d81 124 if (voltage < v2) {
Diletant 166:c3c0b8a90d81 125 int32_t c1 = device.isacs.output.interpolator.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 126 int32_t c2 = device.isacs.output.interpolator.settings.code[i];
Diletant 166:c3c0b8a90d81 127 return isacsInterpolate(voltage, v1, v2, c1, c2);
Diletant 166:c3c0b8a90d81 128 }
Diletant 166:c3c0b8a90d81 129 }
Diletant 166:c3c0b8a90d81 130 return device.isacs.output.interpolator.settings.code[device.isacs.output.interpolator.settings.points - 1];
Diletant 166:c3c0b8a90d81 131 }
Diletant 166:c3c0b8a90d81 132
Diletant 166:c3c0b8a90d81 133 /*
Diletant 166:c3c0b8a90d81 134 void isacsSetOutputVoltage(int32_t voltage) {
Diletant 166:c3c0b8a90d81 135 int32_t v1, v2;
Diletant 166:c3c0b8a90d81 136 for (uint8_t i = 1; i < device.isacs.output.interpolator.settings.points; i++) {
Diletant 166:c3c0b8a90d81 137 v1 = device.isacs.output.interpolator.settings.voltage[i - 1];
Diletant 166:c3c0b8a90d81 138 if (voltage < v1) {
Diletant 166:c3c0b8a90d81 139 uint32_t code = device.isacs.output.interpolator.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 140 device.isacs.output.voltage = v1;
Diletant 166:c3c0b8a90d81 141 device.controller.SSP.out[0] = code;
Diletant 161:efd949e8d536 142 return;
Diletant 161:efd949e8d536 143 }
Diletant 161:efd949e8d536 144
Diletant 166:c3c0b8a90d81 145 v2 = device.isacs.output.interpolator.settings.voltage[i];
Diletant 166:c3c0b8a90d81 146 if (voltage < v2) {
Diletant 166:c3c0b8a90d81 147 //int32_t dv = device.isacs.output.state.voltage - v1;
Diletant 166:c3c0b8a90d81 148 //int32_t dvi = v2 - v1;
Diletant 166:c3c0b8a90d81 149 //int32_t c1 = device.isacs.output.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 150 //int32_t c2 = device.isacs.output.settings.code[i];
Diletant 166:c3c0b8a90d81 151 //int32_t dc = c2 - c1;
Diletant 166:c3c0b8a90d81 152
Diletant 166:c3c0b8a90d81 153 //uint32_t m = 1;
Diletant 166:c3c0b8a90d81 154 //if (device.dac.settings.resolution > 16) {
Diletant 166:c3c0b8a90d81 155 // dv >>= 7; dc >>= 7; dvi >>= 7; m <<= 7;//For 24 bit: (21 - 7) + (24 - 7) - (21 - 7) + 7 = 24
Diletant 166:c3c0b8a90d81 156 //} else if (device.dac.settings.resolution > 12) {
Diletant 166:c3c0b8a90d81 157 // dv >>= 3; dc >>= 3; dvi >>= 3; m <<= 3;//For 16 bit: (21 - 3) + (16 - 3) - (21 - 3) + 3 = 16
Diletant 166:c3c0b8a90d81 158 //} else {
Diletant 166:c3c0b8a90d81 159 // dv >>= 1; dc >>= 1; dvi >>= 1; m <<= 1;//For 12 bit: (21 - 1) + (12 - 1) - (21 - 1) + 1 = 12
Diletant 166:c3c0b8a90d81 160 //}
Diletant 166:c3c0b8a90d81 161 //device.isacs.output.state.code = c1 + dv * dc / dvi * m;
Diletant 166:c3c0b8a90d81 162 //device.isacs.output.state.voltage = voltage;
Diletant 166:c3c0b8a90d81 163
Diletant 166:c3c0b8a90d81 164 int32_t c1 = device.isacs.output.interpolator.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 165 int32_t c2 = device.isacs.output.interpolator.settings.code[i];
Diletant 166:c3c0b8a90d81 166 uint32_t code = isacsInterpolate(voltage, v1, v2, c1, c2);
Diletant 166:c3c0b8a90d81 167 device.isacs.output.voltage = voltage;
Diletant 166:c3c0b8a90d81 168 device.controller.SSP.out[0] = code;
Diletant 161:efd949e8d536 169 return;
Diletant 161:efd949e8d536 170 }
Diletant 161:efd949e8d536 171 }
Diletant 166:c3c0b8a90d81 172 uint32_t code = device.isacs.output.interpolator.settings.code[device.isacs.output.interpolator.settings.points - 1];
Diletant 166:c3c0b8a90d81 173 device.isacs.output.voltage = device.isacs.output.interpolator.settings.voltage[device.isacs.output.interpolator.settings.points - 1];
Diletant 166:c3c0b8a90d81 174 device.controller.SSP.out[0] = code;
Diletant 166:c3c0b8a90d81 175 }
Diletant 166:c3c0b8a90d81 176
Diletant 166:c3c0b8a90d81 177 void isacsSetOutputCode(int32_t code) {
Diletant 166:c3c0b8a90d81 178 int32_t c1, c2;
Diletant 166:c3c0b8a90d81 179 for (uint8_t i = 1; i < device.isacs.output.interpolator.settings.points; i++) {
Diletant 166:c3c0b8a90d81 180 c1 = device.isacs.output.interpolator.settings.code[i - 1];
Diletant 166:c3c0b8a90d81 181 if (code < c1) {
Diletant 166:c3c0b8a90d81 182 device.isacs.output.voltage = device.isacs.output.interpolator.settings.voltage[i - 1];
Diletant 166:c3c0b8a90d81 183 device.controller.SSP.out[0] = c1;
Diletant 166:c3c0b8a90d81 184 return;
Diletant 166:c3c0b8a90d81 185 }
Diletant 166:c3c0b8a90d81 186
Diletant 166:c3c0b8a90d81 187 c2 = device.isacs.output.interpolator.settings.code[i];
Diletant 166:c3c0b8a90d81 188 if (code < c2) {
Diletant 166:c3c0b8a90d81 189 int32_t v1 = device.isacs.output.interpolator.settings.voltage[i - 1];
Diletant 166:c3c0b8a90d81 190 int32_t v2 = device.isacs.output.interpolator.settings.voltage[i];
Diletant 166:c3c0b8a90d81 191 device.isacs.output.voltage = isacsInterpolate(code, c1, c2, v1, v2);
Diletant 166:c3c0b8a90d81 192 device.controller.SSP.out[0] = code;
Diletant 166:c3c0b8a90d81 193 return;
Diletant 166:c3c0b8a90d81 194 }
Diletant 166:c3c0b8a90d81 195 }
Diletant 166:c3c0b8a90d81 196 device.isacs.output.voltage = device.isacs.output.interpolator.settings.voltage[device.isacs.output.interpolator.settings.points - 1];
Diletant 166:c3c0b8a90d81 197 device.controller.SSP.out[0] = device.isacs.output.interpolator.settings.code[device.isacs.output.interpolator.settings.points - 1];
Diletant 166:c3c0b8a90d81 198 }
Diletant 166:c3c0b8a90d81 199 */
Diletant 166:c3c0b8a90d81 200
Diletant 166:c3c0b8a90d81 201 void isacsProcess(void) {
Diletant 166:c3c0b8a90d81 202 device.isacs.input.voltage = isacsInputCodeToVoltage(device.controller.SSP.in[4]);
Diletant 166:c3c0b8a90d81 203 device.isacs.input.sum += device.isacs.input.voltage;
Diletant 166:c3c0b8a90d81 204 if (device.measurement.counter == 31) {
Diletant 166:c3c0b8a90d81 205 device.isacs.input.average = device.isacs.input.sum >> 5;
Diletant 166:c3c0b8a90d81 206 device.isacs.input.sum = 0;
Diletant 166:c3c0b8a90d81 207
Diletant 166:c3c0b8a90d81 208 if (device.isacs.regulator.state.enabled) {
Diletant 166:c3c0b8a90d81 209 device.isacs.regulator.state.error = device.isacs.regulator.state.reference - device.isacs.input.average;
Diletant 166:c3c0b8a90d81 210 device.isacs.output.voltage += device.isacs.regulator.state.error * device.isacs.regulator.state.scale;
Diletant 166:c3c0b8a90d81 211 device.controller.SSP.out[0] = isacsOutputVoltageToCode(device.isacs.output.voltage);
Diletant 166:c3c0b8a90d81 212 }
Diletant 166:c3c0b8a90d81 213 }
Diletant 161:efd949e8d536 214 }