123

Dependencies:   mbed

Fork of LG by igor Apu

Revision:
198:78dd6d14d108
Parent:
197:7a05523bf588
--- a/DeviceISACS.c	Wed Oct 19 10:55:05 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,309 +0,0 @@
-#include "Device.h"
-
-extern Device device;
-
-void InitISACSOutputDefaultSettings(void){
-  device.isacs.regulator.settings.start.enabled = 1;
-  device.isacs.regulator.settings.regular.enabled = 1;
-  device.isacs.regulator.settings.reset.enabled = 1;
-  
-  device.isacs.regulator.settings.transfer.points = 16;
-  device.isacs.regulator.settings.transfer.error[0] = -0x20000; //-2.0V
-  device.isacs.regulator.settings.transfer.correction[0] = 0x10000; //1.0
-  device.isacs.regulator.settings.transfer.error[1] = -0x1bbbb; //-1.7333333333333334V
-  device.isacs.regulator.settings.transfer.correction[1] = 0xb473; //0.7048888888888889
-  device.isacs.regulator.settings.transfer.error[2] = -0x17777; //-1.4666666666666668V
-  device.isacs.regulator.settings.transfer.correction[2] = 0x7aa7; //0.47911111111111115
-  device.isacs.regulator.settings.transfer.error[3] = -0x13333; //-1.2V
-  device.isacs.regulator.settings.transfer.correction[3] = 0x4fdf; //0.31199999999999994
-  device.isacs.regulator.settings.transfer.error[4] = -0xeeee; //-0.9333333333333333V
-  device.isacs.regulator.settings.transfer.correction[4] = 0x3161; //0.1928888888888889
-  device.isacs.regulator.settings.transfer.error[5] = -0xaaaa; //-0.6666666666666667V
-  device.isacs.regulator.settings.transfer.correction[5] = 0x1c71; //0.11111111111111113
-  device.isacs.regulator.settings.transfer.error[6] = -0x6666; //-0.3999999999999999V
-  device.isacs.regulator.settings.transfer.correction[6] = 0xe56; //0.05599999999999999
-  device.isacs.regulator.settings.transfer.error[7] = -0x2222; //-0.1333333333333333V
-  device.isacs.regulator.settings.transfer.correction[7] = 0x452; //0.016888888888888884
-  device.isacs.regulator.settings.transfer.error[8] = 0x2222; //0.1333333333333333V
-  device.isacs.regulator.settings.transfer.correction[8] = -0x452; //-0.016888888888888884
-  device.isacs.regulator.settings.transfer.error[9] = 0x6666; //0.3999999999999999V
-  device.isacs.regulator.settings.transfer.correction[9] = -0xe56; //-0.05599999999999999
-  device.isacs.regulator.settings.transfer.error[10] = 0xaaaa; //0.6666666666666665V
-  device.isacs.regulator.settings.transfer.correction[10] = -0x1c71; //-0.11111111111111108
-  device.isacs.regulator.settings.transfer.error[11] = 0xeeee; //0.9333333333333331V
-  device.isacs.regulator.settings.transfer.correction[11] = -0x3161; //-0.1928888888888888
-  device.isacs.regulator.settings.transfer.error[12] = 0x13333; //1.2000000000000002V
-  device.isacs.regulator.settings.transfer.correction[12] = -0x4fdf; //-0.31200000000000006
-  device.isacs.regulator.settings.transfer.error[13] = 0x17777; //1.4666666666666668V
-  device.isacs.regulator.settings.transfer.correction[13] = -0x7aa7; //-0.47911111111111115
-  device.isacs.regulator.settings.transfer.error[14] = 0x1bbbb; //1.7333333333333334V
-  device.isacs.regulator.settings.transfer.correction[14] = -0xb473; //-0.7048888888888889
-  device.isacs.regulator.settings.transfer.error[15] = 0x20000; //2.0V
-  device.isacs.regulator.settings.transfer.correction[15] = -0x10000; //-1.0
-  
-  device.isacs.output.settings.transfer.points = 2;
-  //Control point 1.5 volt - low output limit
-  device.isacs.output.settings.transfer.voltage[0] = 0x00017fff;
-  device.isacs.output.settings.transfer.code[0] = 800;
-  //Control point 12.5 volt - high output limit
-  device.isacs.output.settings.transfer.voltage[1] = 0x000c7fff;
-  device.isacs.output.settings.transfer.code[1] = 2048;
-  device.isacs.output.settings.start.voltage = 0x000c7fff; //+12.5V start
-  device.isacs.output.settings.reset.voltage = 0x00077fff; //+7.5V if regulator disabled at reset time
-  device.isacs.output.state.min = 0x00077fff; //+7.5V
-  device.isacs.output.state.max = 0x000c7fff; //+12.5V
-}
-
-void InitISACSOutputState(void){
-}
-
-void DeviceStartISACSOutput(void){
-}
-
-void InitISACSRegulatorDefaultSettings(void){
-  //Startup settings
-  device.isacs.regulator.settings.start.enabled = 1;
-  device.isacs.regulator.settings.start.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format
-  device.isacs.regulator.settings.start.scale = 1; //Scale factor
-  //Regular operation settings
-  device.isacs.regulator.settings.regular.enabled = 1;
-  device.isacs.regulator.settings.regular.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format
-  device.isacs.regulator.settings.regular.scale = 1; //Scale factor
-  //Reset operation settings
-  device.isacs.regulator.settings.reset.enabled = 1;
-  device.isacs.regulator.settings.reset.reference = 0x00020000; //Reference voltage 2V in 16.16 signed fixed point format
-  device.isacs.regulator.settings.reset.scale = 1; //Scale factor
-}
-
-void InitISACSRegulatorState(void){
-  device.isacs.regulator.state.enabled = device.isacs.regulator.settings.start.enabled;
-  device.isacs.regulator.state.reference = device.isacs.regulator.settings.start.reference;
-  device.isacs.regulator.state.scale = device.isacs.regulator.settings.start.scale;
-  device.isacs.regulator.state.error = 0;
-  device.isacs.regulator.state.correction = 0;
-}
-
-void DeviceStartISACSRegulator(void){
-}
-
-void InitISACSDefaultSettings(void){
-  InitISACSPotentiometersDefaultSettings();
-  InitISACSRegulatorDefaultSettings();
-  InitISACSOutputDefaultSettings();
-}
-
-void InitISACSState(void){
-  InitISACSPotentiometersState();
-  InitISACSRegulatorState();
-  InitISACSOutputState();
-}
-
-void DeviceStartISACS(void){
-  DeviceStartISACSPotentiometers();
-  DeviceStartISACSRegulator();
-  DeviceStartISACSOutput();
-}
-
-/*
-//Count leading zeroes
-static uint8_t clz(uint32_t x) {
-  uint8_t result = 0;
-  if (x == 0) return 32;
-  while (!(x & 0xF0000000)) { result += 4; x <<= 4; }
-  while (!(x & 0x80000000)) { result += 1; x <<= 1; }
-  return result;
-}
-//Count trailing zeroes
-static uint8_t ctz(uint32_t x) {
-  uint8_t result = 0;
-  if (x == 0) return 32;
-  while (!(x & 0xF0000000)) { result += 4; x <<= 4; }
-  while (!(x & 0x80000000)) { result += 1; x <<= 1; }
-  return result;
-}
-*/
-
-int32_t isacsInterpolate(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 isacsInput(uint32_t raw) {
-  int32_t r1, r2;
-  for (uint8_t i = 1; i < device.isacs.input.settings.transfer.points; i++) {
-    r1 = device.isacs.input.settings.transfer.raw[i - 1];
-    if (raw < r1) {
-      return device.isacs.input.settings.transfer.V[i - 1];
-    }
-
-    r2 = device.isacs.input.settings.transfer.raw[i];
-    if (raw < r2) {
-      int32_t v1 = device.isacs.input.settings.transfer.V[i - 1];
-      int32_t v2 = device.isacs.input.settings.transfer.V[i];
-      return isacsInterpolate(raw, r1, r2, v1, v2);
-    }
-  }
-  return device.isacs.input.settings.transfer.V[device.isacs.input.settings.transfer.points - 1];  
-}
-
-int32_t isacsCorrection(int32_t error) {
-  int32_t e1, e2;
-  for (uint8_t i = 1; i < device.isacs.regulator.settings.transfer.points; i++) {
-    e1 = device.isacs.regulator.settings.transfer.error[i - 1];
-    if (error < e1) {
-      return device.isacs.regulator.settings.transfer.correction[i - 1];
-    }
-
-    e2 = device.isacs.regulator.settings.transfer.error[i];
-    if (error < e2) {
-      int32_t c1 = device.isacs.regulator.settings.transfer.correction[i - 1];
-      int32_t c2 = device.isacs.regulator.settings.transfer.correction[i];
-      return isacsInterpolate(error, e1, e2, c1, c2);
-    }
-  }
-  return device.isacs.regulator.settings.transfer.correction[device.isacs.regulator.settings.transfer.points - 1];
-}
-
-uint32_t isacsOutput(int32_t voltage) {
-  int32_t v1, v2;
-  for (uint8_t i = 1; i < device.isacs.output.settings.transfer.points; i++) {
-    v1 = device.isacs.output.settings.transfer.voltage[i - 1];
-    if (voltage < v1) {
-      return device.isacs.output.settings.transfer.code[i - 1];
-    }
-
-    v2 = device.isacs.output.settings.transfer.voltage[i];
-    if (voltage < v2) {
-      int32_t c1 = device.isacs.output.settings.transfer.code[i - 1];
-      int32_t c2 = device.isacs.output.settings.transfer.code[i];
-      return isacsInterpolate(voltage, v1, v2, c1, c2);
-    }
-  }
-  return device.isacs.output.settings.transfer.code[device.isacs.output.settings.transfer.points - 1];
-}
-
-void isacsProcess(void) {
-  device.isacs.input.state.sum += device.controller.SSP.in[4];
-  if (device.measurement.counter == 31) {
-    device.isacs.input.state.raw = device.isacs.input.state.sum >> 5;
-    device.isacs.input.state.sum = 0;
-    device.isacs.input.state.V = isacsInput(device.isacs.input.state.raw);
-    
-    if (device.isacs.regulator.state.enabled) {
-      device.isacs.regulator.state.error = device.isacs.regulator.state.reference - device.isacs.input.state.V;
-      device.isacs.regulator.state.correction = (device.isacs.regulator.state.scale * isacsCorrection(device.isacs.regulator.state.error)) >> 16;
-      device.isacs.output.state.voltage += device.isacs.regulator.state.correction;
-      device.controller.SSP.out[0] = isacsOutput(device.isacs.output.state.voltage);
-    }
-  }
-}
-
-int16_t var_HF_reg(void) {
-  //Value:           HFO voltage - V - -13.2V...10.7V
-  //Internal format: device.isacs.output.state.voltage - device.isacs.output.state.min...device.isacs.output.state.max [V] in 16.16 format
-  //Output format:   -8192 * (V + 1.3) / 3 - dimensionless units in 16.0 format
-  
-  //Voltage:         V + 1.3 = (device.isacs.output.state.voltage + 0x00014ccd) / 65536
-  //Output:          -8192 * (V + 1.3) / 3 = -(device.isacs.output.state.voltage + 0x00014ccd) / 24
-  
-  int32_t V = -(device.isacs.output.state.voltage + 0x00014ccd) / 24;
-  if (V > 32767) V = 32767;
-  if (V < -32768) V = -32768;
-  
-  return (int16_t)V;
-}
-
-int16_t var_HF_dif(void) {
-  //Value:           HFO regulator input signal error - [V]
-  //Internal format: device.isacs.regulator.state.error - [V] in 16.16 format
-  //Output format:   (V / 2.0) * 15000 - dimensionless units in 16.0 format
-  
-  //Voltage:         V = device.isacs.regulator.state.error / 65536
-  //Output:          (V / 2.0) * 15000 = device.isacs.regulator.state.error * 1875 / 16384
-  
-  int32_t V = device.isacs.regulator.state.error * 1875 / 16384;
-  if (V > 32767) V = 32767;
-  if (V < 0) V = 0;
-  
-  return (int16_t)V;
-}
-
-uint16_t param_HF_ref(void) {
-  //Value:           HFO regulator input signal reference voltage - V - 2V
-  //Internal format: device.isacs.regulator.state.reference - [V] in 16.16 format
-  //Output format:   (V / 2.0) * 15000 - dimensionless units in 16.0 format
-  
-  //Voltage:         V = device.isacs.regulator.state.reference / 65536
-  //Output:          V = device.isacs.regulator.state.reference * 1875 / 16384
-  
-  //TODO: state/settings
-  
-  int32_t V = device.isacs.regulator.state.reference * 1875 / 16384;
-  if (V > 32767) V = 32767;
-  if (V < 0) V = 0;
-  
-  return (uint16_t)V;
-}
-
-//HF_scl s16 HFO gain factor
-uint16_t param_HF_scl(void) {
-  //Value:           ISACS output regulator scale - S - -13.2V...10.7V
-  //Internal format: device.isacs.regulator.state.scale - dimensionless units in 16.16 format
-  //Output format:   S / 65536 - dimensionless units in 16.0 format
-  
-  int32_t scl = device.isacs.regulator.state.scale / 65536;
-  if (scl > 32767) scl = 32767;
-  if (scl < 0) scl = 0;
-  
-  return (uint16_t)scl;
-}
-
-uint16_t param_HF_min(void) {
-  //Value:           HFO maximum voltage - V - -13.2V...10.7V
-  //Internal format: device.isacs.output.state.max - [V] in 16.16 format
-  //Output format:   -8192 * (V + 1.3) / 3 - dimensionless units in 16.0 format
-  
-  //Voltage:         V + 1.3 = (device.isacs.output.state.max + 0x00014ccd) / 65536
-  //Output:          -8192 * (V + 1.3) / 3 = -(device.isacs.output.state.max + 0x00014ccd) / 24
-  
-  //TODO: state/settings
-  
-  int32_t V = -(device.isacs.output.state.max + 0x00014ccd) / 24;
-  if (V > 32767) V = 32767;
-  if (V < -32768) V = -32768;
-  
-  return (uint16_t)V;
-}
-
-uint16_t param_HF_max(void) {
-  //Value:           HFO minimum voltage - V - -13.2V...10.7V
-  //Internal format: device.isacs.output.state.min - [V] in 16.16 format
-  //Output format:   -8192 * (V + 1.3) / 3 - dimensionless units in 16.0 format
-  
-  //Voltage:         V + 1.3 = (device.isacs.output.state.min + 0x00014ccd) / 65536
-  //Output:          -8192 * (V + 1.3) / 3 = -(device.isacs.output.state.min + 0x00014ccd) / 24
-  
-  //TODO: state/settings
-  
-  int32_t V = -(device.isacs.output.state.min + 0x00014ccd) / 24;
-  if (V > 32767) V = 32767;
-  if (V < -32768) V = -32768;
-  
-  return (uint16_t)V;
-}
\ No newline at end of file