123

Dependencies:   mbed

Fork of LG by igor Apu

Revision:
156:e68ee0bcdcda
Parent:
149:abbf7663d27d
Child:
161:efd949e8d536
--- a/Device.c	Fri May 06 14:08:54 2016 +0000
+++ b/Device.c	Mon May 09 20:03:26 2016 +0000
@@ -10,54 +10,166 @@
   SystemInit1();  // Инициализация контроллера: установка тактовых частот
   SystemCoreClockUpdate1(); // расчет тактовой частоты процессора перед инициализацией UART - 100MHz
   
-  //Init timers
-  Init_TIM1(7812); //Timer 1: CCLK / 7812 = 12800.819Hz; Vibro: Timer1/32 = 400.025Hz;
-  Init_TIM2();     //Timer 2: CCLK / 4 / 250 = 100kHz
-  enable_timer1();
-  enable_timer2();
+  //Load default settings
+  DeviceInitAllWithDefaults();
   
-  //Load default settings
-  InitAllWithDefaults();
+  //Load from flash - override defaults
+  DeviceFlashReadAll();
+
+  //Final initialization
+  DeviceInitAll();
+}
+
+void DeviceStart(void){
+  DeviceEnableMeasurementTimer();
+  DeviceEnableRegularTimer();
+  DevicePhotoDetectorSetPotentiometers();
+  DeviceStartLightUp();
+  DeviceStartDither();
+}
+
+void DeviceInitAllWithDefaults(void){
+  device.settings.address = 0;
+  
+  //Init transmitter variables
+  device.host.transmitter.position = 0;
+  
+  //Init controller
+  InitControllerWithDefaults();
   
-  //Load from flash
-  FlashReadAll();
+  //Init units
+  InitHostProtocolWithDefaults();
+  InitCountersWithDefaults();
+  InitLightUpWithDefaults();
+  InitPhotoDetectorWithDefaults();
+  InitDitherWithDefaults();
+  InitDACWithDefaults();
+  InitPathLengthControlSystemWithDefaults();
+}
 
-  //Init flash memory
-  InitFlash();
-  //Init service port
-  InitServicePort();
-  //Init host commununication port
-  InitHostPort();
+void DeviceInitAll(void){
+  //Init measurement cycle
+  device.measurement.counter = 0;
+  device.measurement.length = 32;
+  //Init regular cycle
+  device.regular.event1Hz = 0;
+  device.regular.event500Hz = 0;
+  device.regular.event1K = 0;
+  device.regular.event10K = 0;
+  device.regular.event100K = 0;
+  device.regular.time100K = 0;
+  device.regular.time10K = 0;
+  device.regular.time1K = 0;
+  device.regular.time500Hz = 0;
+  device.regular.time1Hz = 0;
+  
+  //Init controller
+  InitController();
+  
   //Init host commununication protocol
   InitHostProtocol();
-  InitSSP();
-  
+  //Init counters
+  InitCounters();
   //Init light-up and back light unit
   InitLightUp();
+  //Init photo detector
+  InitPhotoDetector();
   //Init dither
   InitDither();
+  //Init DAC of hfo & plcs
+  InitDAC();
   //Init path length control system
   InitPathLengthControlSystem();
 }
 
-void StartDevice(void){
-  StartLightUp();
-  StartDither();
+void DeviceRegularEvent1Hz(void) {
+    if (device.regular.event1Hz) {
+       device.regular.event1Hz--;
+       device.regular.time1Hz++;
+       
+       DeviceLightUpCycle();
+    }
+}
+
+void DeviceRegularEvent500Hz(void) {
+    if (device.regular.event500Hz) {
+        device.regular.event500Hz--;
+    }
+}
+
+void DeviceRegularEvent1KHz(void) {
+    if (device.regular.event1K) {
+        device.regular.event1K--;
+        device.regular.time1K++;
+        device.regular.time500Hz++;
+        if (device.regular.time1K == 1000) {
+            device.regular.time1K = 0;
+            device.regular.event1Hz++;
+        }
+        if (device.regular.time500Hz == 2) {
+            device.regular.time500Hz = 0;
+            device.regular.event500Hz++;
+       }
+    }
 }
 
-void InitAllWithDefaults(void){
-  device.settings.address = 0;
-  device.settings.uart.startupBaudRate = 38400;
+void DeviceRegularEvent10KHz(void) {
+    if (device.regular.event10K) {
+        device.regular.event10K--;
+        device.regular.time10K++;
+        if (device.regular.time10K == 10){
+            device.regular.time10K = 0;
+            device.regular.event1K++;
+        }
+        
+        if ((device.host.response.type == RESPONSE_DELAYED) || (device.host.response.type == RESPONSE_PERIODIC)){
+            device.host.response.counter += 100;
+            if (device.host.response.counter > device.host.response.trigger) {
+                device.host.response.triggered = 1;
+                device.host.response.counter = 0;
+            }
+        }
+    }
+}
+
+void DeviceRegularEvent100KHz(void) {
+  if (device.regular.event100K) {
+    device.regular.event100K--;
+    
+    device.regular.time100K++;
+    if (device.regular.time100K == 10) {
+      device.regular.time100K = 0;
+      device.regular.event10K++;
+    }
+    
+    DeviceDitherCycle(); //Set/reset vibro 1/2 pins
+  }
+}
+
+void DeviceMeasurementInterruptHandler(void) {
+  //Read QEI
+  DeviceQEIRead();
+  //Count
+  DeviceCount();
   
-  InitFlashWithDefaults();
-  InitServicePortWithDefaults();
-  InitHostPortWithDefaults();
-  InitHostProtocolWithDefaults();
-  InitLightUpWithDefaults();
-  InitSSPWithDefaults();
-  InitDitherWithDefaults();
-  InitPathLengthControlSystemWithDefaults();
+  //Receive ADCs samples using SSP
+  DeviceSSPReceive();
+  //Transmit DACs values using SSP
+  DeviceSSPTransmit(device.measurement.counter & 1);
+  
+  //Update measurement cycle counter
+  device.measurement.counter++;
+  if (device.measurement.counter == device.measurement.length) device.measurement.counter = 0;
+  //Reset dither 10mks resolution counter
+  if (device.measurement.counter == 0) device.dither.state.counter = 0; //First dither half period
+  if (device.measurement.counter == 16) device.dither.state.counter = 0;//Second dither half period
 }
+
+void DeviceRegularInterruptHandler(void) {
+  device.regular.event100K++;
+  device.dither.state.counter++; //Dither 10 mks resolution counter
+}
+
 /*
 int32_t FindByHash(uint32_t hash){
   for (uint32_t i = 0; i < HASH_PARAM_COUNT; i++){