changeset 89:ff7775cc34c4 kittz

temp! full cyclic SPI
author Dmitry Romanov <kitt@bk.ru>
date Fri, 23 Nov 2018 16:52:21 +0300
parents 3db7389d49cc
children 83857eb3b12b
files Discovery/Src/base.c Discovery/Src/data_exchange_main.c Discovery/Src/ostc.c Discovery/Src/settings.c Small_CPU/Src/baseCPU2.c Small_CPU/Src/scheduler.c Small_CPU/Src/spi.c ostc4pack/OSTC4_Firmware.bin ostc4pack/OSTC4_RTE.bin ostc4pack/OSTC4_RTE_upload.bin ostc4pack/OSTC4_firmware_upload.bin
diffstat 11 files changed, 794 insertions(+), 826 deletions(-) [+]
line wrap: on
line diff
--- a/Discovery/Src/base.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Discovery/Src/base.c	Fri Nov 23 16:52:21 2018 +0300
@@ -520,6 +520,7 @@
 //  ===============================================================================
 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
 {
+	DataEX_call();
 #ifdef DEMOMODE
     if(htim->Instance==TIM7)
     {
@@ -556,7 +557,7 @@
     case BaseInfo:
         updateSetpointStateUsed();
         DateEx_copy_to_dataOut();
-        DataEX_call();
+//        DataEX_call();
         DataEX_copy_to_LifeData(&modeChange);
 //foto session :-)  stateRealGetPointerWrite()->lifeData.battery_charge = 99;
 //foto session :-)  stateSimGetPointerWrite()->lifeData.battery_charge = 99;
@@ -576,7 +577,7 @@
         break;
     case BaseStop:
         DateEx_copy_to_dataOut();
-        DataEX_call();
+//        DataEX_call();
         DataEX_control_connection_while_asking_for_sleep();
         break;
     default:
@@ -584,7 +585,7 @@
         if(get_globalState() == StUART_RTECONNECT)
         {
             DateEx_copy_to_dataOut();
-            DataEX_call();
+//            DataEX_call();
             DataEX_copy_to_LifeData(0);
         }
         break;
--- a/Discovery/Src/data_exchange_main.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Discovery/Src/data_exchange_main.c	Fri Nov 23 16:52:21 2018 +0300
@@ -306,7 +306,7 @@
 	uint8_t SPI_DMA_answer = 0;
 	
 	HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_SET);
-	delayMicros(10); //~exchange time(+20% reserve)
+	delayMicros(30); //~exchange time(+20% reserve)
 	HAL_GPIO_WritePin(SMALLCPU_CSB_GPIO_PORT,SMALLCPU_CSB_PIN,GPIO_PIN_RESET);
 	/* one cycle with NotChipSelect true to clear slave spi buffer */
 
@@ -326,7 +326,7 @@
 
 //HAL_GPIO_WritePin(OSCILLOSCOPE2_GPIO_PORT,OSCILLOSCOPE2_PIN,GPIO_PIN_RESET); /* only for testing with Oscilloscope */
 
-	SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE+1);
+	SPI_DMA_answer = HAL_SPI_TransmitReceive_DMA(&cpu2DmaSpi, (uint8_t *)&dataOut, (uint8_t *)&dataIn, EXCHANGE_BUFFERSIZE);
 //	HAL_Delay(3);
 	if(SPI_DMA_answer != HAL_OK)
     DataEX_Error_Handler(SPI_DMA_answer);
--- a/Discovery/Src/ostc.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Discovery/Src/ostc.c	Fri Nov 23 16:52:21 2018 +0300
@@ -81,7 +81,7 @@
     cpu2DmaSpi.Init.CLKPolarity         = SPI_POLARITY_LOW;
     cpu2DmaSpi.Init.CLKPhase            = SPI_PHASE_1EDGE;
     cpu2DmaSpi.Init.NSS                 = SPI_NSS_SOFT;//SPI_NSS_HARD_OUTPUT;//SPI_NSS_SOFT;
-    cpu2DmaSpi.Init.BaudRatePrescaler   = SPI_BAUDRATEPRESCALER_64;
+    cpu2DmaSpi.Init.BaudRatePrescaler   = SPI_BAUDRATEPRESCALER_128;
     cpu2DmaSpi.Init.FirstBit            = SPI_FIRSTBIT_MSB;
     cpu2DmaSpi.Init.TIMode              = SPI_TIMODE_DISABLED;
     cpu2DmaSpi.Init.CRCCalculation 		= SPI_CRCCALCULATION_DISABLED;
--- a/Discovery/Src/settings.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Discovery/Src/settings.c	Fri Nov 23 16:52:21 2018 +0300
@@ -55,8 +55,8 @@
 {
     .versionFirst   = 1,
     .versionSecond 	= 4,
-    .versionThird   = 4,
-    .versionBeta    = 0,
+    .versionThird   = 5,
+    .versionBeta    = 1,
 
     /* 4 bytes with trailing 0 */
     .signature = "mh",
--- a/Small_CPU/Src/baseCPU2.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Small_CPU/Src/baseCPU2.c	Fri Nov 23 16:52:21 2018 +0300
@@ -153,51 +153,38 @@
 #include "stm32f4xx_hal.h"
 #include <stdio.h>
 
-uint8_t hasExternalClock(void)
-{
-    if( (TM_OTP_Read( 0, 0 ) > 0) && (TM_OTP_Read( 0, 0 ) < 0xFF) )
-        return 1;
-    else
-        return 0;
+uint8_t hasExternalClock(void) {
+	if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF))
+		return 1;
+	else
+		return 0;
 }
 
 // SHALL LOAD AT 0x08000000 + 0x00005000 = 0x08005000.
 // See CPU2-RTE.ld
 const SFirmwareData cpu2_FirmwareData __attribute__(( section(".firmware_data") ))
-    =
-    {
-      .versionFirst = 1,
-      .versionSecond = 5,
-      .versionThird = 2,
-      .versionBeta = 0,
+= { .versionFirst = 1, .versionSecond = 5, .versionThird = 2, .versionBeta = 0,
 
-      /* 4 bytes with trailing 0 */
-      .signature = "mh",
+/* 4 bytes with trailing 0 */
+.signature = "mh",
 
-      .release_year = 18,
-      .release_month = 7,
-      .release_day = 29,
-      .release_sub = 0,
+.release_year = 18, .release_month = 7, .release_day = 29, .release_sub = 0,
 
-      /* max 48 with trailing 0 */
-      //release_info ="12345678901234567890123456789012345678901"
-      .release_info = "compass stuff",
+		/* max 48 with trailing 0 */
+		//release_info ="12345678901234567890123456789012345678901"
+		.release_info = "compass stuff",
 
-      /* for safety reasons and coming functions */
-      .magic[0] = FIRMWARE_MAGIC_FIRST,
-      .magic[1] = FIRMWARE_MAGIC_SECOND,
-      .magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */
-      .magic[3] = FIRMWARE_MAGIC_END
-    };
+		/* for safety reasons and coming functions */
+		.magic[0] = FIRMWARE_MAGIC_FIRST, .magic[1] = FIRMWARE_MAGIC_SECOND,
+		.magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */
+		.magic[3] = FIRMWARE_MAGIC_END };
 
-uint8_t firmwareVersionHigh(void)
-{
-    return cpu2_FirmwareData.versionFirst;
+uint8_t firmwareVersionHigh(void) {
+	return cpu2_FirmwareData.versionFirst;
 }
 
-uint8_t firmwareVersionLow(void)
-{
-    return cpu2_FirmwareData.versionSecond;
+uint8_t firmwareVersionLow(void) {
+	return cpu2_FirmwareData.versionSecond;
 }
 
 /** @addtogroup OSTC4
@@ -273,10 +260,9 @@
 
 #define REGULAR_RUN
 
-int __io_putchar(int ch)
-{
-    ITM_SendChar( ch );
-    return ch;
+int __io_putchar(int ch) {
+	ITM_SendChar(ch);
+	return ch;
 }
 
 /* Private functions ---------------------------------------------------------*/
@@ -287,269 +273,280 @@
  * @retval None
  */
 
-int main(void)
-{
-    HAL_Init();
-    SystemClock_Config();
+int main(void) {
+	HAL_Init();
+	SystemClock_Config();
 
-    HAL_SYSTICK_Config( HAL_RCC_GetHCLKFreq() / 1000 );
-    HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK );
-    HAL_NVIC_SetPriority( SysTick_IRQn, 0, 0 );
+	HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq() / 1000);
+	HAL_SYSTICK_CLKSourceConfig( SYSTICK_CLKSOURCE_HCLK);
+	HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
 
-    MX_RTC_init();
-    GPIO_LED_Init();
-    GPIO_new_DEBUG_Init(); // added 170322 hw
-    initGlobals();
+	MX_RTC_init();
+	GPIO_LED_Init();
+	GPIO_new_DEBUG_Init(); // added 170322 hw
+	initGlobals();
 
-    printf("CPU2-RTE running...\n");
+	printf("CPU2-RTE running...\n");
 
-    MX_I2C1_Init();
-    if( global.I2C_SystemStatus != HAL_OK )
-    {
-        if( MX_I2C1_TestAndClear() == GPIO_PIN_RESET )
-        {
-            MX_I2C1_TestAndClear(); // do it a second time
-        }
-        MX_I2C1_Init();
-    }
+	MX_I2C1_Init();
+	if (global.I2C_SystemStatus != HAL_OK) {
+		if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) {
+			MX_I2C1_TestAndClear(); // do it a second time
+		}
+		MX_I2C1_Init();
+	}
 
-    //dangerous:	TM_OTP_Write(0,0, 0x01);
+	//dangerous:	TM_OTP_Write(0,0, 0x01);
 #ifdef REGULAR_RUN
-    global.sensorError[SENSOR_PRESSURE_ID] = init_pressure();
-    global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID];
-    if( global.I2C_SystemStatus != HAL_OK )
-    {
-        if( MX_I2C1_TestAndClear() == GPIO_PIN_RESET )
-        {
-            MX_I2C1_TestAndClear(); // do it a second time
-        }
-        MX_I2C1_Init();
-        global.sensorError[SENSOR_PRESSURE_ID] = init_pressure();
-        global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID];
-    }
+	global.sensorError[SENSOR_PRESSURE_ID] = init_pressure();
+	global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID];
+	if (global.I2C_SystemStatus != HAL_OK) {
+		if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) {
+			MX_I2C1_TestAndClear(); // do it a second time
+		}
+		MX_I2C1_Init();
+		global.sensorError[SENSOR_PRESSURE_ID] = init_pressure();
+		global.I2C_SystemStatus = global.sensorError[SENSOR_PRESSURE_ID];
+	}
 
-    global.dataSendToMaster.sensorErrors =
-        global.sensorError[SENSOR_PRESSURE_ID];
-    init_surface_ring();
-    init_battery_gas_gauge();
-    HAL_Delay( 10 );
-    battery_gas_gauge_get_data();
+	global.dataSendToMaster.sensorErrors =
+			global.sensorError[SENSOR_PRESSURE_ID];
+	init_surface_ring();
+	init_battery_gas_gauge();
+	HAL_Delay(10);
+	battery_gas_gauge_get_data();
 //	battery_gas_gauge_set(0);
 
-    global.lifeData.battery_voltage = get_voltage();
-    global.lifeData.battery_charge = get_charge();
-    copyBatteryData();
+	global.lifeData.battery_voltage = get_voltage();
+	global.lifeData.battery_charge = get_charge();
+	copyBatteryData();
 
-    MX_SPI3_Init();
-    if( !scheduleSetButtonResponsiveness() )
-    {
-        HAL_Delay( 1 );
-        scheduleSetButtonResponsiveness(); // init
-        HAL_Delay( 1 );
-        if( !scheduleSetButtonResponsiveness() ) // send again, if problem it's not my problem here.
-        {
-            HAL_Delay( 1 );
-            scheduleSetButtonResponsiveness(); // init
-            HAL_Delay( 1 );
-        }
-    }
+	MX_SPI3_Init();
+	if (!scheduleSetButtonResponsiveness()) {
+		HAL_Delay(1);
+		scheduleSetButtonResponsiveness(); // init
+		HAL_Delay(1);
+		if (!scheduleSetButtonResponsiveness()) // send again, if problem it's not my problem here.
+		{
+			HAL_Delay(1);
+			scheduleSetButtonResponsiveness(); // init
+			HAL_Delay(1);
+		}
+	}
 
-    ADCx_Init();
-    GPIO_Power_MainCPU_Init();
-    global.mode = MODE_POWERUP;
+	ADCx_Init();
+	GPIO_Power_MainCPU_Init();
+	global.mode = MODE_POWERUP;
 #else
-    init_pressure();
-    init_surface_ring();
+	init_pressure();
+	init_surface_ring();
 
-    ADCx_Init();
-    GPIO_Power_MainCPU_Init();
-    global.mode = MODE_TEST;
+	ADCx_Init();
+	GPIO_Power_MainCPU_Init();
+	global.mode = MODE_TEST;
 #endif
-    while( 1 )
-    {
-        printf("Global mode = %d\n", global.mode);
+	while (1) {
+		printf("Global mode = %d\n", global.mode);
 
-        switch( global.mode )
-        {
-        case MODE_POWERUP:
-        case MODE_BOOT:
-            //				ReInit_battery_charger_status_pins();
-            compass_init( 0, 7 );
-            accelerator_init();
-            wireless_init();
-            if( global.mode == MODE_BOOT )
-            {
-                GPIO_Power_MainCPU_OFF();
-                HAL_Delay( 100 ); // for GPIO_Power_MainCPU_ON();
-                GPIO_Power_MainCPU_ON();
-            }
-            SPI_synchronize_with_Master();
-            MX_DMA_Init();
-            MX_SPI1_Init();
-            MX_EXTI_wireless_Init();
-            SPI_Start_single_TxRx_with_Master();
-            EXTI_Test_Button_Init();
+		switch (global.mode) {
+		case MODE_POWERUP:
+		case MODE_BOOT:
+			//				ReInit_battery_charger_status_pins();
+			compass_init(0, 7);
+			accelerator_init();
+			wireless_init();
+			if (global.mode == MODE_BOOT) {
+				GPIO_Power_MainCPU_OFF();
+				HAL_Delay(100); // for GPIO_Power_MainCPU_ON();
+				GPIO_Power_MainCPU_ON();
+			}
+			SPI_synchronize_with_Master();
+			MX_DMA_Init();
+			MX_SPI1_Init();
+			MX_EXTI_wireless_Init();
+			SPI_Start_single_TxRx_with_Master();
+			EXTI_Test_Button_Init();
+
+			/*
+			 uint8_t dataWireless[64];
+			 while(1)
+			 {
+			 wireless_evaluate_and_debug(dataWireless,64);
+			 }
+			 */
+			global.mode = MODE_SURFACE;
+			break;
 
-            /*
-             uint8_t dataWireless[64];
-             while(1)
-             {
-             wireless_evaluate_and_debug(dataWireless,64);
-             }
-             */
-            global.mode = MODE_SURFACE;
-            break;
+		case MODE_CALIB:
+//            scheduleCompassCalibrationMode();
+			break;
+
+		case MODE_SURFACE:
+
 
-        case MODE_CALIB:
-            scheduleCompassCalibrationMode();
-            break;
-
-        case MODE_SURFACE:
-            scheduleSurfaceMode();
-            break;
-
-        case MODE_TEST:
-            GPIO_Power_MainCPU_ON();
-            SPI_synchronize_with_Master();
-            MX_DMA_Init();
-            MX_SPI1_Init();
-//				MX_EXTI_wireless_Init();
-            SPI_Start_single_TxRx_with_Master();
-//				EXTI_Test_Button_Init();
+//			uint32_t tickstart = 0;
+//			uint32_t ticksdiff = 0;
+//			uint32_t lasttick = 0;
+//			tickstart = HAL_GetTick();
+//			uint8_t counterPressure100msec = 0;
+//			uint8_t counterCompass100msec = 0;
+//			uint8_t counterAmbientLight100msec = 0;
+//			uint16_t counterWireless1msec = 0;
+//			uint16_t counter1ms=0;
+//			while (global.mode == MODE_SURFACE) {
+//
+//				lasttick = HAL_GetTick();
+//				ticksdiff = time_elapsed_ms(tickstart, lasttick);
+//
+//				if (ticksdiff >= counter1ms) {
+//
+//					global.lifeData.ascent_rate_meter_per_min = 0;
+//					counter1ms++;
+//					if(counter1ms>1000){
+//						counter1ms=0;
+//						global.check_sync_not_running++;
+//
+//					}
+//
+//				}
+//
+//			}
 
-            scheduleTestMode();
-            break;
-
-        case MODE_DIVE:
-            backup.no_fly_time_minutes = global.no_fly_time_minutes;
-            backup.seconds_since_last_dive = global.seconds_since_last_dive;
-
-            vpm_init( &global.vpm, global.conservatism, global.repetitive_dive,
-                      global.seconds_since_last_dive );
-            global.no_fly_time_minutes = 0;
-            global.lifeData.dive_time_seconds = 0;
-            global.lifeData.dive_time_seconds_without_surface_time = 0;
-            scheduleDiveMode();
-            // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1;
+//        	if(global.check_sync_not_running>10){
+//        		SPI_Start_single_TxRx_with_Master();
+//        	}
+//        	schedule_check_resync();
+//        	pressure_update();
+//        	compass_read();
+//        	acceleration_read();
+//        	compass_calc();
+			scheduleSurfaceMode();
+			break;
 
-            if( global.lifeData.dive_time_seconds > 60 )
-            {
-                //No Fly time 60% of desaturationtime after dive
-                global.no_fly_time_minutes = decom_calc_desaturation_time(
-                    global.lifeData.tissue_nitrogen_bar,
-                    global.lifeData.tissue_helium_bar,
-                    global.lifeData.pressure_surface_bar ) * 60 / 100;
-                if( global.no_fly_time_minutes < (24 * 60) )
-                    global.no_fly_time_minutes = 24 * 60;
-            }
-            else
-            {
-                global.no_fly_time_minutes = backup.no_fly_time_minutes;
-                global.seconds_since_last_dive = backup.seconds_since_last_dive;
-            }
-
-            global.lifeData.dive_time_seconds = 0;
-            global.lifeData.dive_time_seconds_without_surface_time = 0;
-            global.lifeData.counterSecondsShallowDepth = 0;
-
-            backup.no_fly_time_minutes = 0;
-            backup.seconds_since_last_dive = 0;
-            break;
+		case MODE_TEST:
+			break;
 
-        case MODE_SHUTDOWN:
-            HAL_Delay( 200 );
-            global.mode = MODE_SLEEP;
-            MX_SPI3_Init();
-            break;
-
-        case MODE_SLEEP:
-            /*
-             sleep_prepare();
-             scheduleSleepMode_test();
-             */
-            /*
-             GPIO_Power_MainCPU_OFF();
-             EXTI_Test_Button_DeInit();
-             EXTI_Wakeup_Button_Init();
-             NOT_USED_AT_THE_MOMENT_scheduleSleepMode();
-             */
-            EXTI_Test_Button_DeInit();
-            MX_EXTI_wireless_DeInit();
-            if( hasExternalClock() )
-                SystemClock_Config_HSI();
-            sleep_prepare();
+		case MODE_DIVE:
+//            backup.no_fly_time_minutes = global.no_fly_time_minutes;
+//            backup.seconds_since_last_dive = global.seconds_since_last_dive;
+//
+//            vpm_init( &global.vpm, global.conservatism, global.repetitive_dive,
+//                      global.seconds_since_last_dive );
+//            global.no_fly_time_minutes = 0;
+//            global.lifeData.dive_time_seconds = 0;
+//            global.lifeData.dive_time_seconds_without_surface_time = 0;
+//            scheduleDiveMode();
+//            // done now in scheduler prior to change mode: global.seconds_since_last_dive = 1;
+//
+//            if( global.lifeData.dive_time_seconds > 60 )
+//            {
+//                //No Fly time 60% of desaturationtime after dive
+//                global.no_fly_time_minutes = decom_calc_desaturation_time(
+//                    global.lifeData.tissue_nitrogen_bar,
+//                    global.lifeData.tissue_helium_bar,
+//                    global.lifeData.pressure_surface_bar ) * 60 / 100;
+//                if( global.no_fly_time_minutes < (24 * 60) )
+//                    global.no_fly_time_minutes = 24 * 60;
+//            }
+//            else
+//            {
+//                global.no_fly_time_minutes = backup.no_fly_time_minutes;
+//                global.seconds_since_last_dive = backup.seconds_since_last_dive;
+//            }
+//
+//            global.lifeData.dive_time_seconds = 0;
+//            global.lifeData.dive_time_seconds_without_surface_time = 0;
+//            global.lifeData.counterSecondsShallowDepth = 0;
+//
+//            backup.no_fly_time_minutes = 0;
+//            backup.seconds_since_last_dive = 0;
+			break;
 
-            GPIO_LED_Init();
+		case MODE_SHUTDOWN:
+			HAL_Delay(200);
+			global.mode = MODE_SLEEP;
+			MX_SPI3_Init();
+			break;
+
+		case MODE_SLEEP:
+			/*
+			 sleep_prepare();
+			 scheduleSleepMode_test();
+			 */
+			/*
+			 GPIO_Power_MainCPU_OFF();
+			 EXTI_Test_Button_DeInit();
+			 EXTI_Wakeup_Button_Init();
+			 NOT_USED_AT_THE_MOMENT_scheduleSleepMode();
+			 */
+			EXTI_Test_Button_DeInit();
+			MX_EXTI_wireless_DeInit();
+			if (hasExternalClock())
+				SystemClock_Config_HSI();
+			sleep_prepare();
 
-            scheduleSleepMode();
-            if( hasExternalClock() )
-                SystemClock_Config_HSE();
-            GPIO_LED_Init();
-            EXTI_Wakeup_Button_DeInit();
-            ADCx_Init();
-            GPIO_Power_MainCPU_Init();
-            GPIO_Power_MainCPU_ON();
-            compass_init( 0, 7 );
-            accelerator_init();
-            wireless_init();
-            SPI_synchronize_with_Master();
-            MX_DMA_Init();
-            MX_SPI1_Init();
-            MX_EXTI_wireless_Init();
-            SPI_Start_single_TxRx_with_Master();
+			GPIO_LED_Init();
 
-            // EXTILine0_Button_DeInit(); not now, later after testing
-            break;
-        }
-    }
+			scheduleSleepMode();
+			if (hasExternalClock())
+				SystemClock_Config_HSE();
+			GPIO_LED_Init();
+			EXTI_Wakeup_Button_DeInit();
+			ADCx_Init();
+			GPIO_Power_MainCPU_Init();
+			GPIO_Power_MainCPU_ON();
+			compass_init(0, 7);
+			accelerator_init();
+			wireless_init();
+			SPI_synchronize_with_Master();
+			MX_DMA_Init();
+			MX_SPI1_Init();
+			MX_EXTI_wireless_Init();
+			SPI_Start_single_TxRx_with_Master();
+
+			// EXTILine0_Button_DeInit(); not now, later after testing
+			break;
+		}
+	}
 }
 
 /** @brief Button feedback - EXTI line detection callbacks
  * @param GPIO_Pin: Specifies the pins connected EXTI line
  * @retval None
  */
-void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
-{
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
 
-    if( GPIO_Pin == WIRELSS_RISING_GPIO_PIN )
-    {
-        wireless_trigger_RisingEdgeSilence();
-    }
+	if (GPIO_Pin == WIRELSS_RISING_GPIO_PIN) {
+		wireless_trigger_RisingEdgeSilence();
+	}
 
-    else
+	else
 
-    if( GPIO_Pin == WIRELSS_FALLING_GPIO_PIN )
-    {
-        wireless_trigger_FallingEdgeSignalHigh();
-    }
+	if (GPIO_Pin == WIRELSS_FALLING_GPIO_PIN) {
+		wireless_trigger_FallingEdgeSignalHigh();
+	}
 
-    else
+	else
 
-    if( GPIO_Pin == BUTTON_OSTC_GPIO_PIN )
-    {
-        if( global.mode == MODE_SLEEP )
-        {
-            global.mode = MODE_BOOT;
-        }
-    }
+	if (GPIO_Pin == BUTTON_OSTC_GPIO_PIN) {
+		if (global.mode == MODE_SLEEP) {
+			global.mode = MODE_BOOT;
+		}
+	}
 
-    else
+	else
 
-    if( GPIO_Pin == BUTTON_TEST_GPIO_PIN )
-    {
-        if( !global.demo_mode && (global.mode == MODE_SURFACE) )
-        {
-            global.demo_mode = 1;
-            global.mode = MODE_DIVE;
-        }
-        else if( global.demo_mode && (global.mode == MODE_DIVE)
-            && (global.lifeData.dive_time_seconds > 10) )
-        {
-            global.demo_mode = 0;
-            global.dataSendToMaster.mode = MODE_ENDDIVE;
-            global.deviceDataSendToMaster.mode = MODE_ENDDIVE;
-        }
-    }
+	if (GPIO_Pin == BUTTON_TEST_GPIO_PIN) {
+		if (!global.demo_mode && (global.mode == MODE_SURFACE)) {
+			global.demo_mode = 1;
+			global.mode = MODE_DIVE;
+		} else if (global.demo_mode && (global.mode == MODE_DIVE)
+				&& (global.lifeData.dive_time_seconds > 10)) {
+			global.demo_mode = 0;
+			global.dataSendToMaster.mode = MODE_ENDDIVE;
+			global.deviceDataSendToMaster.mode = MODE_ENDDIVE;
+		}
+	}
 }
 
 /**
@@ -573,47 +570,44 @@
  * @retval None
  */
 
-void SystemClock_Config(void)
-{
-    if( hasExternalClock() )
-        SystemClock_Config_HSE();
-    else
-        SystemClock_Config_HSI();
+void SystemClock_Config(void) {
+	if (hasExternalClock())
+		SystemClock_Config_HSE();
+	else
+		SystemClock_Config_HSI();
 }
 
-void SYSCLKConfig_STOP(void)
-{
-    SYSCLKConfig_STOP_HSI();
+void SYSCLKConfig_STOP(void) {
+	SYSCLKConfig_STOP_HSI();
 }
 
-void SystemClock_Config_HSE(void)
-{
-    RCC_OscInitTypeDef RCC_OscInitStruct;
-    RCC_ClkInitTypeDef RCC_ClkInitStruct;
+void SystemClock_Config_HSE(void) {
+	RCC_OscInitTypeDef RCC_OscInitStruct;
+	RCC_ClkInitTypeDef RCC_ClkInitStruct;
 //  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
 
-    __PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE();
+	__PWR_CLK_ENABLE(); // is identical to __HAL_RCC_PWR_CLK_ENABLE();
 
-    __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE2 );
+	__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
 
-    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE;
-    RCC_OscInitStruct.HSEState = RCC_HSE_ON;
-    //RCC_OscInitStruct.LSEState = RCC_LSE_ON;
-    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
-    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
-    RCC_OscInitStruct.PLL.PLLM = 8;
-    RCC_OscInitStruct.PLL.PLLN = 320;
-    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
-    RCC_OscInitStruct.PLL.PLLQ = 4;
-    HAL_RCC_OscConfig( &RCC_OscInitStruct );
+	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; //|RCC_OSCILLATORTYPE_LSE;
+	RCC_OscInitStruct.HSEState = RCC_HSE_ON;
+	//RCC_OscInitStruct.LSEState = RCC_LSE_ON;
+	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
+	RCC_OscInitStruct.PLL.PLLM = 8;
+	RCC_OscInitStruct.PLL.PLLN = 320;
+	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
+	RCC_OscInitStruct.PLL.PLLQ = 4;
+	HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
-    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
-        | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
-    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
-    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
-    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
-    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-    HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_2 );
+	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
+			| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
+	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
+	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
+	HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
 
 //  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
 //  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
@@ -623,44 +617,43 @@
 
 //  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
 
-    /* SysTick_IRQn interrupt configuration */
+	/* SysTick_IRQn interrupt configuration */
 //  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
 }
 
-void SystemClock_Config_HSI(void)
-{
-    RCC_ClkInitTypeDef RCC_ClkInitStruct;
-    RCC_OscInitTypeDef RCC_OscInitStruct;
+void SystemClock_Config_HSI(void) {
+	RCC_ClkInitTypeDef RCC_ClkInitStruct;
+	RCC_OscInitTypeDef RCC_OscInitStruct;
 
-    /* Enable Power Control clock */
-    __HAL_RCC_PWR_CLK_ENABLE();
+	/* Enable Power Control clock */
+	__HAL_RCC_PWR_CLK_ENABLE();
 
-    /* The voltage scaling allows optimizing the power consumption when the device is
-     clocked below the maximum system frequency, to update the voltage scaling value 
-     regarding system frequency refer to product datasheet.  */
-    __HAL_PWR_VOLTAGESCALING_CONFIG( PWR_REGULATOR_VOLTAGE_SCALE2 );
+	/* The voltage scaling allows optimizing the power consumption when the device is
+	 clocked below the maximum system frequency, to update the voltage scaling value
+	 regarding system frequency refer to product datasheet.  */
+	__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
 
-    /* Enable HSI Oscillator and activate PLL with HSI as source */
-    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
-    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
-    RCC_OscInitStruct.HSICalibrationValue = 0x10;
-    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
-    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
-    RCC_OscInitStruct.PLL.PLLM = 16;
-    RCC_OscInitStruct.PLL.PLLN = 320;
-    RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
-    RCC_OscInitStruct.PLL.PLLQ = 4;
-    HAL_RCC_OscConfig( &RCC_OscInitStruct );
+	/* Enable HSI Oscillator and activate PLL with HSI as source */
+	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+	RCC_OscInitStruct.HSIState = RCC_HSI_ON;
+	RCC_OscInitStruct.HSICalibrationValue = 0x10;
+	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
+	RCC_OscInitStruct.PLL.PLLM = 16;
+	RCC_OscInitStruct.PLL.PLLN = 320;
+	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
+	RCC_OscInitStruct.PLL.PLLQ = 4;
+	HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
-    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
-     clocks dividers */
-    RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
-        | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
-    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
-    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
-    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
-    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-    HAL_RCC_ClockConfig( &RCC_ClkInitStruct, FLASH_LATENCY_2 );
+	/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
+	 clocks dividers */
+	RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK
+			| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
+	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
+	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
+	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
+	HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);
 }
 /*	
  RCC_OscInitTypeDef RCC_OscInitStruct;
@@ -707,55 +700,53 @@
  * @param  None
  * @retval None
  */
-void SYSCLKConfig_STOP_HSE(void)
-{
-    RCC_ClkInitTypeDef RCC_ClkInitStruct;
-    RCC_OscInitTypeDef RCC_OscInitStruct;
-    uint32_t pFLatency = 0;
+void SYSCLKConfig_STOP_HSE(void) {
+	RCC_ClkInitTypeDef RCC_ClkInitStruct;
+	RCC_OscInitTypeDef RCC_OscInitStruct;
+	uint32_t pFLatency = 0;
 
-    /* Get the Oscillators configuration according to the internal RCC registers */
-    HAL_RCC_GetOscConfig( &RCC_OscInitStruct );
+	/* Get the Oscillators configuration according to the internal RCC registers */
+	HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
 
-    /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
-    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
-    RCC_OscInitStruct.HSIState = RCC_HSE_ON;
-    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
-    HAL_RCC_OscConfig( &RCC_OscInitStruct );
+	/* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
+	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
+	RCC_OscInitStruct.HSIState = RCC_HSE_ON;
+	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+	HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
-    /* Get the Clocks configuration according to the internal RCC registers */
-    HAL_RCC_GetClockConfig( &RCC_ClkInitStruct, &pFLatency );
+	/* Get the Clocks configuration according to the internal RCC registers */
+	HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
 
-    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
-     clocks dividers */
-    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
-    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
-    HAL_RCC_ClockConfig( &RCC_ClkInitStruct, pFLatency );
+	/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
+	 clocks dividers */
+	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
+	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+	HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency);
 }
 
-void SYSCLKConfig_STOP_HSI(void)
-{
-    RCC_ClkInitTypeDef RCC_ClkInitStruct;
-    RCC_OscInitTypeDef RCC_OscInitStruct;
-    uint32_t pFLatency = 0;
+void SYSCLKConfig_STOP_HSI(void) {
+	RCC_ClkInitTypeDef RCC_ClkInitStruct;
+	RCC_OscInitTypeDef RCC_OscInitStruct;
+	uint32_t pFLatency = 0;
 
-    /* Get the Oscillators configuration according to the internal RCC registers */
-    HAL_RCC_GetOscConfig( &RCC_OscInitStruct );
+	/* Get the Oscillators configuration according to the internal RCC registers */
+	HAL_RCC_GetOscConfig(&RCC_OscInitStruct);
 
-    /* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
-    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
-    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
-    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
-    RCC_OscInitStruct.HSICalibrationValue = 0x10;
-    HAL_RCC_OscConfig( &RCC_OscInitStruct );
+	/* After wake-up from STOP reconfigure the system clock: Enable HSI and PLL */
+	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
+	RCC_OscInitStruct.HSIState = RCC_HSI_ON;
+	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+	RCC_OscInitStruct.HSICalibrationValue = 0x10;
+	HAL_RCC_OscConfig(&RCC_OscInitStruct);
 
-    /* Get the Clocks configuration according to the internal RCC registers */
-    HAL_RCC_GetClockConfig( &RCC_ClkInitStruct, &pFLatency );
+	/* Get the Clocks configuration according to the internal RCC registers */
+	HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &pFLatency);
 
-    /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
-     clocks dividers */
-    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
-    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
-    HAL_RCC_ClockConfig( &RCC_ClkInitStruct, pFLatency );
+	/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
+	 clocks dividers */
+	RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
+	RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+	HAL_RCC_ClockConfig(&RCC_ClkInitStruct, pFLatency);
 }
 
 /**
@@ -763,9 +754,8 @@
  * @param None
  * @retval None
  */
-void HAL_SYSTICK_Callback(void)
-{
-    HAL_IncTick();
+void HAL_SYSTICK_Callback(void) {
+	HAL_IncTick();
 }
 
 /**
@@ -796,66 +786,59 @@
  }
  */
 
-static void GPIO_LED_Init(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void GPIO_LED_Init(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    __GPIOC_CLK_ENABLE();
-    GPIO_InitStructure.Pin = GPIO_PIN_3;
-    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
-    GPIO_InitStructure.Pull = GPIO_PULLUP;
-    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
-    HAL_GPIO_Init( GPIOC, &GPIO_InitStructure );
+	__GPIOC_CLK_ENABLE();
+	GPIO_InitStructure.Pin = GPIO_PIN_3;
+	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+	GPIO_InitStructure.Pull = GPIO_PULLUP;
+	GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
+	HAL_GPIO_Init( GPIOC, &GPIO_InitStructure);
 }
 
-void GPIO_new_DEBUG_Init(void)
-{
+void GPIO_new_DEBUG_Init(void) {
 #ifdef DEBUG_PIN_ACTIVE
-    GPIO_InitTypeDef GPIO_InitStructure;
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    __GPIOC_CLK_ENABLE();
-    GPIO_InitStructure.Pin = GPIO_PIN_3;
-    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
-    GPIO_InitStructure.Pull = GPIO_PULLUP;
-    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
-    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
+	__GPIOC_CLK_ENABLE();
+	GPIO_InitStructure.Pin = GPIO_PIN_3;
+	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+	GPIO_InitStructure.Pull = GPIO_PULLUP;
+	GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
+	HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
 #endif
 }
 
-void GPIO_new_DEBUG_LOW(void)
-{
+void GPIO_new_DEBUG_LOW(void) {
 #ifdef DEBUG_PIN_ACTIVE
-    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET);
+	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_RESET);
 #endif
 }
 
-void GPIO_new_DEBUG_HIGH(void)
-{
+void GPIO_new_DEBUG_HIGH(void) {
 #ifdef DEBUG_PIN_ACTIVE
-    HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET);
+	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_3,GPIO_PIN_SET);
 #endif
 }
 
-static void GPIO_Power_MainCPU_Init(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
-    __GPIOC_CLK_ENABLE();
-    GPIO_InitStructure.Pin = GPIO_PIN_0;
-    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
-    GPIO_InitStructure.Pull = GPIO_PULLUP;
-    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
-    HAL_GPIO_Init( GPIOC, &GPIO_InitStructure );
-    HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET );
+static void GPIO_Power_MainCPU_Init(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
+	__GPIOC_CLK_ENABLE();
+	GPIO_InitStructure.Pin = GPIO_PIN_0;
+	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+	GPIO_InitStructure.Pull = GPIO_PULLUP;
+	GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+	HAL_GPIO_Init( GPIOC, &GPIO_InitStructure);
+	HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
 }
 
-static void GPIO_Power_MainCPU_ON(void)
-{
-    HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET );
+static void GPIO_Power_MainCPU_ON(void) {
+	HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
 }
 
-static void GPIO_Power_MainCPU_OFF(void)
-{
-    HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_SET );
+static void GPIO_Power_MainCPU_OFF(void) {
+	HAL_GPIO_WritePin( GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
 }
 
 /**
@@ -864,111 +847,105 @@
  * @retval None
  */
 
-static void EXTI_Wakeup_Button_Init(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void EXTI_Wakeup_Button_Init(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    __HAL_RCC_GPIOA_CLK_ENABLE();
-    BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE();
-    GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN;
-    GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
-    HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure );
+	__HAL_RCC_GPIOA_CLK_ENABLE();
+	BUTTON_OSTC_HAL_RCC_GPIO_CLK_ENABLE();
+	GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN;
+	GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
+	HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure);
 
-    HAL_NVIC_SetPriority( BUTTON_OSTC_IRQn, 0x0F, 0 );
-    HAL_NVIC_EnableIRQ( BUTTON_OSTC_IRQn );
+	HAL_NVIC_SetPriority( BUTTON_OSTC_IRQn, 0x0F, 0);
+	HAL_NVIC_EnableIRQ( BUTTON_OSTC_IRQn);
 }
 
-static void EXTI_Wakeup_Button_DeInit(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void EXTI_Wakeup_Button_DeInit(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
-    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
+	GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
+	GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
 
-    GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN;
-    HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure );
-    HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn );
+	GPIO_InitStructure.Pin = BUTTON_OSTC_GPIO_PIN;
+	HAL_GPIO_Init( BUTTON_OSTC_GPIO_PORT, &GPIO_InitStructure);
+	HAL_NVIC_DisableIRQ( BUTTON_OSTC_IRQn);
 }
 
-static void EXTI_Test_Button_Init(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void EXTI_Test_Button_Init(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    BUTTON_TEST_GPIO_CLK_ENABLE();
-    GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN;
-    GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
-    GPIO_InitStructure.Pull = GPIO_PULLUP;
-    HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure );
-    HAL_NVIC_SetPriority( BUTTON_TEST_IRQn, 0x0F, 0 );
-    HAL_NVIC_EnableIRQ( BUTTON_TEST_IRQn );
+	BUTTON_TEST_GPIO_CLK_ENABLE();
+	GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN;
+	GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
+	GPIO_InitStructure.Pull = GPIO_PULLUP;
+	HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure);
+	HAL_NVIC_SetPriority( BUTTON_TEST_IRQn, 0x0F, 0);
+	HAL_NVIC_EnableIRQ( BUTTON_TEST_IRQn);
 }
 
-static void EXTI_Test_Button_DeInit(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void EXTI_Test_Button_DeInit(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
-    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
+	GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
+	GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
 
-    GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN;
-    HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure );
-    HAL_NVIC_DisableIRQ( BUTTON_TEST_IRQn );
+	GPIO_InitStructure.Pin = BUTTON_TEST_GPIO_PIN;
+	HAL_GPIO_Init( BUTTON_TEST_GPIO_PORT, &GPIO_InitStructure);
+	HAL_NVIC_DisableIRQ( BUTTON_TEST_IRQn);
 }
 
-static void MX_EXTI_wireless_Init(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void MX_EXTI_wireless_Init(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    WIRELSS_POWER_HAL_RCC_GPIO_CLK_ENABLE();
-    GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
-    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
-    HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure );
-    HAL_GPIO_WritePin( WIRELSS_POWER_GPIO_PORT, WIRELSS_POWER_GPIO_PIN,
-                       GPIO_PIN_SET );
+	WIRELSS_POWER_HAL_RCC_GPIO_CLK_ENABLE();
+	GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
+	GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
+	HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure);
+	HAL_GPIO_WritePin( WIRELSS_POWER_GPIO_PORT, WIRELSS_POWER_GPIO_PIN,
+			GPIO_PIN_SET);
 
-    WIRELSS_RISING_HAL_RCC_GPIO_CLK_ENABLE();
-    GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN;
-    GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
-    HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure );
+	WIRELSS_RISING_HAL_RCC_GPIO_CLK_ENABLE();
+	GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN;
+	GPIO_InitStructure.Mode = GPIO_MODE_IT_RISING;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
+	HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure);
 
-    HAL_NVIC_SetPriority( WIRELSS_RISING_IRQn, 0x02, 0 );
-    HAL_NVIC_EnableIRQ( WIRELSS_RISING_IRQn );
+	HAL_NVIC_SetPriority( WIRELSS_RISING_IRQn, 0x02, 0);
+	HAL_NVIC_EnableIRQ( WIRELSS_RISING_IRQn);
 
-    WIRELSS_FALLING_HAL_RCC_GPIO_CLK_ENABLE();
-    GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN;
-    GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
-    HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure );
+	WIRELSS_FALLING_HAL_RCC_GPIO_CLK_ENABLE();
+	GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN;
+	GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
+	HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure);
 
-    HAL_NVIC_SetPriority( WIRELSS_FALLING_IRQn, 0x02, 0 );
-    HAL_NVIC_EnableIRQ( WIRELSS_FALLING_IRQn );
+	HAL_NVIC_SetPriority( WIRELSS_FALLING_IRQn, 0x02, 0);
+	HAL_NVIC_EnableIRQ( WIRELSS_FALLING_IRQn);
 
 }
 
-static void MX_EXTI_wireless_DeInit(void)
-{
-    GPIO_InitTypeDef GPIO_InitStructure;
+static void MX_EXTI_wireless_DeInit(void) {
+	GPIO_InitTypeDef GPIO_InitStructure;
 
-    GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
-    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
-    GPIO_InitStructure.Pull = GPIO_NOPULL;
+	GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
+	GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+	GPIO_InitStructure.Pull = GPIO_NOPULL;
 
-    GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN;
-    HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure );
+	GPIO_InitStructure.Pin = WIRELSS_RISING_GPIO_PIN;
+	HAL_GPIO_Init( WIRELSS_RISING_GPIO_PORT, &GPIO_InitStructure);
 
-    GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN;
-    HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure );
+	GPIO_InitStructure.Pin = WIRELSS_FALLING_GPIO_PIN;
+	HAL_GPIO_Init( WIRELSS_FALLING_GPIO_PORT, &GPIO_InitStructure);
 
-    GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
-    HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure );
+	GPIO_InitStructure.Pin = WIRELSS_POWER_GPIO_PIN;
+	HAL_GPIO_Init( WIRELSS_POWER_GPIO_PORT, &GPIO_InitStructure);
 
-    HAL_NVIC_DisableIRQ( WIRELSS_RISING_IRQn );
-    HAL_NVIC_DisableIRQ( WIRELSS_FALLING_IRQn );
+	HAL_NVIC_DisableIRQ( WIRELSS_RISING_IRQn);
+	HAL_NVIC_DisableIRQ( WIRELSS_FALLING_IRQn);
 }
 
 /* NUCLEO C 13
@@ -995,77 +972,75 @@
  }
  */
 
-void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle)
-{
+void HAL_I2C_MasterTxCpltCallback(I2C_HandleTypeDef *I2cHandle) {
 
 }
 
-void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle)
-{
+void HAL_I2C_MasterRxCpltCallback(I2C_HandleTypeDef *I2cHandle) {
 
 }
 
-void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle)
-{
+void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *I2cHandle) {
 
 }
 
-void sleep_prepare(void)
-{
-    EXTI_Wakeup_Button_Init();
-    /*
-     GPIO_InitStruct.Pull = GPIO_PULLUP;
-     GPIO_InitStruct.Pin =  GPIO_PIN_0;
-     HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-     */
-    compass_sleep();
-    HAL_Delay( 100 );
-    accelerator_sleep();
-    HAL_Delay( 100 );
+void sleep_prepare(void) {
+	EXTI_Wakeup_Button_Init();
+	/*
+	 GPIO_InitStruct.Pull = GPIO_PULLUP;
+	 GPIO_InitStruct.Pin =  GPIO_PIN_0;
+	 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+	 */
+	compass_sleep();
+	HAL_Delay(100);
+	accelerator_sleep();
+	HAL_Delay(100);
 
-    I2C_DeInit();
-    MX_SPI_DeInit();
-    MX_SPI3_DeInit();
-    ADCx_DeInit();
+	I2C_DeInit();
+	MX_SPI_DeInit();
+	MX_SPI3_DeInit();
+	ADCx_DeInit();
 
-    GPIO_InitTypeDef GPIO_InitStruct;
+	GPIO_InitTypeDef GPIO_InitStruct;
 
-    __HAL_RCC_GPIOA_CLK_ENABLE();
-    __HAL_RCC_GPIOB_CLK_ENABLE();
-    __HAL_RCC_GPIOC_CLK_ENABLE();
-    __HAL_RCC_GPIOH_CLK_ENABLE();
+	__HAL_RCC_GPIOA_CLK_ENABLE();
+	__HAL_RCC_GPIOB_CLK_ENABLE();
+	__HAL_RCC_GPIOC_CLK_ENABLE();
+	__HAL_RCC_GPIOH_CLK_ENABLE();
 
-    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
-    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
-    GPIO_InitStruct.Pull = GPIO_NOPULL;
-    GPIO_InitStruct.Pin = GPIO_PIN_All;
-    HAL_GPIO_Init( GPIOH, &GPIO_InitStruct );
+	GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
+	GPIO_InitStruct.Pull = GPIO_NOPULL;
+	GPIO_InitStruct.Pin = GPIO_PIN_All;
+	HAL_GPIO_Init( GPIOH, &GPIO_InitStruct);
 #ifdef DEBUGMODE
-    GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */
+	GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_3 | GPIO_PIN_8 | GPIO_PIN_9); /* debug */
 #endif
-    HAL_GPIO_Init( GPIOB, &GPIO_InitStruct );
+	HAL_GPIO_Init( GPIOB, &GPIO_InitStruct);
 
-    GPIO_InitStruct.Pin = GPIO_PIN_All
-        ^ ( GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_14 | GPIO_PIN_15); /* power off & charger in & charge out & OSC32*/
-    HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );
+	GPIO_InitStruct.Pin =
+			GPIO_PIN_All
+					^ ( GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_14
+							| GPIO_PIN_15); /* power off & charger in & charge out & OSC32*/
+	HAL_GPIO_Init( GPIOC, &GPIO_InitStruct);
 
-    GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0);
+	GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0);
 #ifdef DEBUGMODE
-    GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */
+	GPIO_InitStruct.Pin = GPIO_PIN_All ^ ( GPIO_PIN_0 | GPIO_PIN_13 | GPIO_PIN_14); /* wake up button & debug */
 #endif
-    HAL_GPIO_Init( GPIOA, &GPIO_InitStruct );
+	HAL_GPIO_Init( GPIOA, &GPIO_InitStruct);
 
-    GPIO_InitStruct.Pin = GPIO_PIN_All;
-    HAL_GPIO_Init( GPIOH, &GPIO_InitStruct );
+	GPIO_InitStruct.Pin = GPIO_PIN_All;
+	HAL_GPIO_Init( GPIOH, &GPIO_InitStruct);
 
-    GPIO_Power_MainCPU_OFF();
+	GPIO_Power_MainCPU_OFF();
 
 #ifndef DEBUGMODE
-    __HAL_RCC_GPIOB_CLK_DISABLE();
+	__HAL_RCC_GPIOB_CLK_DISABLE();
 #endif
-    __HAL_RCC_GPIOH_CLK_DISABLE();
+	__HAL_RCC_GPIOH_CLK_DISABLE();
 
-    HAL_Delay( 1000 );
+	HAL_Delay(1000);
 }
 
 /*
@@ -1123,13 +1098,13 @@
  */
 void assert_failed(uint8_t* file, uint32_t line)
 {
-    /* User can add his own implementation to report the file name and line number,
-     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
+	/* User can add his own implementation to report the file name and line number,
+	 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 
-    /* Infinite loop */
-    while (1)
-    {
-    }
+	/* Infinite loop */
+	while (1)
+	{
+	}
 }
 #endif
 
--- a/Small_CPU/Src/scheduler.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Small_CPU/Src/scheduler.c	Fri Nov 23 16:52:21 2018 +0300
@@ -181,6 +181,7 @@
 
 void scheduleSpecial_Evaluate_DataSendToSlave(void)
 {
+	schedule_check_resync();
 	global.dataSendToSlavePending = 0;
 	if(!global.dataSendToSlaveIsValid) return;
 	
@@ -269,6 +270,38 @@
 	deviceDataFlashValid = 0;
 	memcpy(&DeviceDataFlash, &global.dataSendToSlave.data.DeviceData, sizeof(SDevice));
 	deviceDataFlashValid = 1;
+
+
+	//TODO: (kittz) split by current mode.
+// new hw 170523
+//	if (global.I2C_SystemStatus != HAL_OK) {
+//		MX_I2C1_TestAndClear();
+//		MX_I2C1_Init();
+//		if (!is_init_pressure_done()) {
+//			init_pressure();
+//		}
+//	}
+
+
+//	pressure_update();
+
+//	compass_read();
+//	acceleration_read();
+//	compass_calc();
+
+
+//	copyPressureData();
+//	battery_gas_gauge_get_data();
+//	if(global.mode==MODE_CALIB)copyCompassData();
+//	copyCnsAndOtuData();
+//	copyTimeData();
+//	copyBatteryData();
+//	copyDeviceData();
+//	copyVpmCrushingData();
+//	deviceDataFlashValid = 1;
+//	scheduleUpdateDeviceData();
+
+
 }
 
 
@@ -406,15 +439,15 @@
 void schedule_check_resync(void)
 {
 	//TODO: (kittz) test for stability
-	if((global.check_sync_not_running >= 10))
+	if((global.check_sync_not_running >= 2))
 	{
-		global.dataSendToSlaveIsNotValidCount = 0;
-		global.check_sync_not_running = 0;
-		global.sync_error_count++;
-		MX_SPI_DeInit();
-		HAL_Delay(30); /* could be closer to length of data transmission 23.Feb.2015 hw */
-		MX_DMA_Init();
-		MX_SPI1_Init();
+//		global.dataSendToSlaveIsNotValidCount = 0;
+//		global.check_sync_not_running = 0;
+//		global.sync_error_count++;
+//		MX_SPI_DeInit();
+//		HAL_Delay(30); /* could be closer to length of data transmission 23.Feb.2015 hw */
+//		MX_DMA_Init();
+//		MX_SPI1_Init();
 		SPI_Start_single_TxRx_with_Master();
 	}
 }
@@ -743,7 +776,7 @@
 	while(global.mode == MODE_SURFACE)
 	{
 	    printf("surface...\n");
-
+//	    SPI_Start_single_TxRx_with_Master();
 		schedule_check_resync();
 		lasttick = HAL_GetTick();
 		ticksdiff = time_elapsed_ms(tickstart,lasttick);
@@ -773,7 +806,7 @@
 				global.lifeData.ascent_rate_meter_per_min = 0;
 				copyPressureData();
 				counterPressure100msec++;
-				
+
 				if(scheduleCheck_pressure_reached_dive_mode_level())
 					global.mode = MODE_DIVE;
 		}
@@ -796,7 +829,7 @@
 			copyAmbientLightData();
 			counterAmbientLight100msec++;
 		}
-		//Evaluate tissues, toxic data, etc. once a second 
+		//Evaluate tissues, toxic data, etc. once a second
 		if(ticksdiff >= 1000)
 		{
 			if(clearDecoNow)
@@ -811,11 +844,11 @@
 				vpm_init(&global.vpm, global.conservatism, global.repetitive_dive, global.seconds_since_last_dive);
 				clearDecoNow = 0;
 			}
-	
+
 			//Set back tick counter
 			tickstart = HAL_GetTick();
-			
-			
+
+
 			if(global.seconds_since_last_dive)
 			{
 				schedule_update_timer_helper(-1);
@@ -823,7 +856,7 @@
 //				if(global.seconds_since_last_dive > 777900) // a bit more than nine days [seconds]
 //					global.seconds_since_last_dive = 0;
 			}
-			
+
 			if(global.accidentRemainingSeconds)
 			{
 				global.accidentRemainingSeconds--;
@@ -831,7 +864,7 @@
 					global.accidentFlag = 0;
 			}
 			global.dataSendToMaster.accidentFlags = global.accidentFlag;
-			
+
 			update_surface_pressure(1);
 			scheduleUpdateLifeData(0);
 			decom_oxygen_calculate_otu_degrade(&global.lifeData.otu, global.seconds_since_last_dive);
@@ -839,7 +872,7 @@
 			global.lifeData.desaturation_time_minutes = decom_calc_desaturation_time(global.lifeData.tissue_nitrogen_bar,global.lifeData.tissue_helium_bar,global.lifeData.pressure_surface_bar);
 			battery_charger_get_status_and_contral_battery_gas_gauge(0);
 			battery_gas_gauge_get_data();
-			
+
 			copyCnsAndOtuData();
 			copyTimeData();
 			copyBatteryData();
@@ -855,7 +888,7 @@
 					init_pressure();
 				}
 			}
-			
+
 			counterCompass100msec = 0;
 			counterPressure100msec = 0;
 			counterAmbientLight100msec = 0;
--- a/Small_CPU/Src/spi.c	Thu Nov 22 12:02:55 2018 +0300
+++ b/Small_CPU/Src/spi.c	Fri Nov 23 16:52:21 2018 +0300
@@ -1,23 +1,23 @@
 /**
-  ******************************************************************************
-  * @file    spi.c 
-  * @author  heinrichs weikamp gmbh
-  * @version V0.0.1
-  * @date    16-Sept-2014
-  * @brief   Source code for spi control
-  *           
-  @verbatim                 
-  ============================================================================== 
-                        ##### How to use #####
-  ============================================================================== 
-  @endverbatim
-  ******************************************************************************
-  * @attention
-  *
-  * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
-  *
-  ******************************************************************************
-  */ 
+ ******************************************************************************
+ * @file    spi.c
+ * @author  heinrichs weikamp gmbh
+ * @version V0.0.1
+ * @date    16-Sept-2014
+ * @brief   Source code for spi control
+ *
+ @verbatim
+ ==============================================================================
+ ##### How to use #####
+ ==============================================================================
+ @endverbatim
+ ******************************************************************************
+ * @attention
+ *
+ * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
+ *
+ ******************************************************************************
+ */
 
 /* Includes ------------------------------------------------------------------*/
 #include "spi.h"
@@ -29,9 +29,8 @@
 extern void GPIO_new_DEBUG_LOW(void);
 extern void GPIO_new_DEBUG_HIGH(void);
 
-
-uint8_t	data_error = 0;
-uint32_t	data_error_time = 0;
+uint8_t data_error = 0;
+uint32_t data_error_time = 0;
 
 static void SPI_Error_Handler(void);
 
@@ -46,193 +45,171 @@
 DMA_HandleTypeDef hdma_rx;
 
 // SPI3 init function
-void MX_SPI3_Init(void)
-{
-  hspi3.Instance 								= SPI3;
-  hspi3.Init.Mode 							= SPI_MODE_MASTER;
-  hspi3.Init.Direction 					= SPI_DIRECTION_2LINES;
-  hspi3.Init.DataSize 					= SPI_DATASIZE_8BIT;
-  hspi3.Init.CLKPolarity 				= SPI_POLARITY_HIGH;
-  hspi3.Init.CLKPhase 					= SPI_PHASE_1EDGE;
-  hspi3.Init.NSS 								= SPI_NSS_SOFT;
-  hspi3.Init.BaudRatePrescaler	= SPI_BAUDRATEPRESCALER_256;
-  hspi3.Init.FirstBit 					= SPI_FIRSTBIT_MSB;
-  hspi3.Init.TIMode 						= SPI_TIMODE_DISABLED;
-  hspi3.Init.CRCCalculation 		= SPI_CRCCALCULATION_DISABLED;
-  hspi3.Init.CRCPolynomial			= 7;
-  HAL_SPI_Init(&hspi3);
+void MX_SPI3_Init(void) {
+	hspi3.Instance = SPI3;
+	hspi3.Init.Mode = SPI_MODE_MASTER;
+	hspi3.Init.Direction = SPI_DIRECTION_2LINES;
+	hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
+	hspi3.Init.CLKPolarity = SPI_POLARITY_HIGH;
+	hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
+	hspi3.Init.NSS = SPI_NSS_SOFT;
+	hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
+	hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
+	hspi3.Init.TIMode = SPI_TIMODE_DISABLED;
+	hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
+	hspi3.Init.CRCPolynomial = 7;
+	HAL_SPI_Init(&hspi3);
 }
 
-void MX_SPI3_DeInit(void)
-{
-  HAL_SPI_DeInit(&hspi3);
+void MX_SPI3_DeInit(void) {
+	HAL_SPI_DeInit(&hspi3);
 }
 
-uint8_t SPI3_ButtonAdjust(uint8_t *arrayInput, uint8_t *arrayOutput)
-{
+uint8_t SPI3_ButtonAdjust(uint8_t *arrayInput, uint8_t *arrayOutput) {
 	HAL_StatusTypeDef status;
 	uint8_t answer[10];
 	uint8_t rework[10];
 
 	rework[0] = 0xFF;
-	for(int i = 0; i < 3; i++)
-	{
+	for (int i = 0; i < 3; i++) {
 		// limiter
-		if(arrayInput[i] == 0xFF)
+		if (arrayInput[i] == 0xFF)
 			arrayInput[i] = 0xFE;
-		if(arrayInput[i] >= 15)
-		{
+		if (arrayInput[i] >= 15) {
 			// copy - ausl�se-schwelle
-			rework[i+1] = arrayInput[i];
+			rework[i + 1] = arrayInput[i];
 			// wieder-scharf-schalte-schwelle
-			rework[i+3+1] = arrayInput[i] - 10;
-		}
-		else
-		if(arrayInput[i] >= 10)
-		{
+			rework[i + 3 + 1] = arrayInput[i] - 10;
+		} else if (arrayInput[i] >= 10) {
 			// copy - ausl�se-schwelle
-			rework[i+1] = arrayInput[i];
+			rework[i + 1] = arrayInput[i];
 			// wieder-scharf-schalte-schwelle
-			rework[i+3+1] = arrayInput[i] - 5;
-		}
-		else
-		{
+			rework[i + 3 + 1] = arrayInput[i] - 5;
+		} else {
 			// copy - ausl�se-schwelle
-			rework[i+1] = 7;
+			rework[i + 1] = 7;
 			// wieder-scharf-schalte-schwelle
-			rework[i+3+1] = 6;
+			rework[i + 3 + 1] = 6;
 		}
 	}
 
 	status = HAL_OK; /* = 0 */
-	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_SET);
-	for(int i=0;i<7;i++)
-	{
+	HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
+	for (int i = 0; i < 7; i++) {
+		HAL_Delay(10);
+		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_RESET);
 		HAL_Delay(10);
-		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_RESET);
+		status += HAL_SPI_TransmitReceive(&hspi3, &rework[i], &answer[i], 1,
+				20);
 		HAL_Delay(10);
-		status += HAL_SPI_TransmitReceive(&hspi3, &rework[i], &answer[i], 1,20);
-		HAL_Delay(10);
-		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_SET);
+		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
 	}
-	
-	if(status == HAL_OK)
-	{
-		for(int i = 0; i < 3; i++)
-		{
-			arrayOutput[i] = answer[i+2]; // first not, return of 0xFF not
-		}	
+
+	if (status == HAL_OK) {
+		for (int i = 0; i < 3; i++) {
+			arrayOutput[i] = answer[i + 2]; // first not, return of 0xFF not
+		}
 		return 1;
-	}
-	else
-		
+	} else
+
 		return 0;
 }
 
-
 // SPI5 init function
-void MX_SPI1_Init(void)
-{
-  hspi1.Instance 								= SPI1;
-  hspi1.Init.Mode 							= SPI_MODE_SLAVE;
-  hspi1.Init.Direction 					= SPI_DIRECTION_2LINES;
-  hspi1.Init.DataSize 					= SPI_DATASIZE_8BIT;
-  hspi1.Init.CLKPolarity 				= SPI_POLARITY_LOW;
-  hspi1.Init.CLKPhase 					= SPI_PHASE_1EDGE;
-  hspi1.Init.NSS 								= SPI_NSS_HARD_INPUT;//SPI_NSS_SOFT;
-  hspi1.Init.BaudRatePrescaler	= SPI_BAUDRATEPRESCALER_128;
-  hspi1.Init.FirstBit 					= SPI_FIRSTBIT_MSB;
-  hspi1.Init.TIMode 						= SPI_TIMODE_DISABLED;
-  hspi1.Init.CRCCalculation 		= SPI_CRCCALCULATION_DISABLED;//_DISABLED; _ENABLED;
-  hspi1.Init.CRCPolynomial			= 7;
-  HAL_SPI_Init(&hspi1);
+void MX_SPI1_Init(void) {
+	hspi1.Instance = SPI1;
+	hspi1.Init.Mode = SPI_MODE_SLAVE;
+	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
+	hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
+	hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
+	hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
+	hspi1.Init.NSS = SPI_NSS_HARD_INPUT; //SPI_NSS_SOFT;
+	hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_128;
+	hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
+	hspi1.Init.TIMode = SPI_TIMODE_DISABLED;
+	hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; //_DISABLED; _ENABLED;
+	hspi1.Init.CRCPolynomial = 7;
+	HAL_SPI_Init(&hspi1);
 }
 
-void MX_SPI_DeInit(void)
-{
-  HAL_SPI_DeInit(&hspi1);
+void MX_SPI_DeInit(void) {
+	HAL_SPI_DeInit(&hspi1);
 }
 
+void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi) {
 
-void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
-{
-
-  GPIO_InitTypeDef GPIO_InitStruct;
+	GPIO_InitTypeDef GPIO_InitStruct;
 
-  if(hspi->Instance==SPI1)
-  {
-    // Peripheral clock enable
-    __SPI1_CLK_ENABLE();
-    __GPIOA_CLK_ENABLE();
+	if (hspi->Instance == SPI1) {
+		// Peripheral clock enable
+		__SPI1_CLK_ENABLE();
+		__GPIOA_CLK_ENABLE();
 		//SPI1 GPIO Configuration  
 		//PA4   ------> SPI1_CS 
 		//PA5   ------> SPI1_SCK
 		//PA6   ------> SPI1_MISO 
 		//PA7   ------> SPI1_MOSI 
-		
-    GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
+
+		GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
 //    GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
-    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
-    GPIO_InitStruct.Pull = GPIO_PULLUP;
-    GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
-    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
-    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+		GPIO_InitStruct.Pull = GPIO_PULLUP;
+		GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
+		GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
+		HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
 		//##-3- Configure the DMA streams ##########################################
 		// Configure the DMA handler for Transmission process 
-		hdma_tx.Instance                 = DMA2_Stream3;
-		hdma_tx.Init.Channel             = DMA_CHANNEL_3;
-		hdma_tx.Init.Direction           = DMA_MEMORY_TO_PERIPH;
-		hdma_tx.Init.PeriphInc           = DMA_PINC_DISABLE;
-		hdma_tx.Init.MemInc              = DMA_MINC_ENABLE;
+		hdma_tx.Instance = DMA2_Stream3;
+		hdma_tx.Init.Channel = DMA_CHANNEL_3;
+		hdma_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
+		hdma_tx.Init.PeriphInc = DMA_PINC_DISABLE;
+		hdma_tx.Init.MemInc = DMA_MINC_ENABLE;
 		hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
-		hdma_tx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
-		hdma_tx.Init.Mode                = DMA_NORMAL;
-		hdma_tx.Init.Priority            = DMA_PRIORITY_LOW;
-		hdma_tx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;         
-		hdma_tx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
-		hdma_tx.Init.MemBurst            = DMA_MBURST_INC4;
-		hdma_tx.Init.PeriphBurst         = DMA_PBURST_INC4;
-		
-		HAL_DMA_Init(&hdma_tx);   
-		
+		hdma_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+		hdma_tx.Init.Mode = DMA_NORMAL;
+		hdma_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
+		hdma_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+		hdma_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
+		hdma_tx.Init.MemBurst = DMA_MBURST_INC4;
+		hdma_tx.Init.PeriphBurst = DMA_PBURST_INC4;
+
+		HAL_DMA_Init(&hdma_tx);
+
 		// Associate the initialized DMA handle to the the SPI handle
 		__HAL_LINKDMA(hspi, hdmatx, hdma_tx);
-			
+
 		// Configure the DMA handler for Transmission process
-		hdma_rx.Instance                 = DMA2_Stream0;
-		hdma_rx.Init.Channel             = DMA_CHANNEL_3;
-		hdma_rx.Init.Direction           = DMA_PERIPH_TO_MEMORY;
-		hdma_rx.Init.PeriphInc           = DMA_PINC_DISABLE;
-		hdma_rx.Init.MemInc              = DMA_MINC_ENABLE;
+		hdma_rx.Instance = DMA2_Stream0;
+		hdma_rx.Init.Channel = DMA_CHANNEL_3;
+		hdma_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
+		hdma_rx.Init.PeriphInc = DMA_PINC_DISABLE;
+		hdma_rx.Init.MemInc = DMA_MINC_ENABLE;
 		hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
-		hdma_rx.Init.MemDataAlignment    = DMA_MDATAALIGN_BYTE;
-		hdma_rx.Init.Mode                = DMA_NORMAL;
-		hdma_rx.Init.Priority            = DMA_PRIORITY_HIGH;
-		hdma_rx.Init.FIFOMode            = DMA_FIFOMODE_DISABLE;         
-		hdma_rx.Init.FIFOThreshold       = DMA_FIFO_THRESHOLD_FULL;
-		hdma_rx.Init.MemBurst            = DMA_MBURST_INC4;
-		hdma_rx.Init.PeriphBurst         = DMA_PBURST_INC4; 
+		hdma_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
+		hdma_rx.Init.Mode = DMA_NORMAL;
+		hdma_rx.Init.Priority = DMA_PRIORITY_HIGH;
+		hdma_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
+		hdma_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
+		hdma_rx.Init.MemBurst = DMA_MBURST_INC4;
+		hdma_rx.Init.PeriphBurst = DMA_PBURST_INC4;
 
 		HAL_DMA_Init(&hdma_rx);
-    
-  // Associate the initialized DMA handle to the the SPI handle
-  __HAL_LINKDMA(hspi, hdmarx, hdma_rx);
-    
-  //##-4- Configure the NVIC for DMA #########################################
-  //NVIC configuration for DMA transfer complete interrupt (SPI3_RX)
-  HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 1, 0);   
-  HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
+
+		// Associate the initialized DMA handle to the the SPI handle
+		__HAL_LINKDMA(hspi, hdmarx, hdma_rx);
 
-	// NVIC configuration for DMA transfer complete interrupt (SPI1_TX)
-  HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 1, 1);
-  HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
-	}
-	else
-  if(hspi->Instance==SPI3)
-  {
-    __GPIOC_CLK_ENABLE();
-    __SPI3_CLK_ENABLE();
+		//##-4- Configure the NVIC for DMA #########################################
+		//NVIC configuration for DMA transfer complete interrupt (SPI3_RX)
+		HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 1, 0);
+		HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
+
+		// NVIC configuration for DMA transfer complete interrupt (SPI1_TX)
+		HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 1, 1);
+		HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
+	} else if (hspi->Instance == SPI3) {
+		__GPIOC_CLK_ENABLE();
+		__SPI3_CLK_ENABLE();
 
 		//SPI1 GPIO Configuration  
 		//PC10   ------> SPI3_SCK
@@ -240,27 +217,26 @@
 		//PC12   ------> SPI3_MOSI 
 		//PA15   ------> SPI3_NSS (official)
 		//PC9    ------> SPI3_NSS (hw)
-		
-    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
-    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
-    GPIO_InitStruct.Pull = GPIO_PULLUP;
-    GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
-    GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
-    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
+
+		GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
+		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+		GPIO_InitStruct.Pull = GPIO_PULLUP;
+		GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
+		GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
+		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
 		GPIO_InitStruct.Pin = GPIO_PIN_9;
 		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 		GPIO_InitStruct.Pull = GPIO_PULLUP;
 		GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
-		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); 
+		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
-		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9,GPIO_PIN_SET);
+		HAL_GPIO_WritePin(GPIOC, GPIO_PIN_9, GPIO_PIN_SET);
 	}
 }
 
-void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi){
-  if(hspi->Instance==SPI1)
-  {
+void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi) {
+	if (hspi->Instance == SPI1) {
 		__SPI1_FORCE_RESET();
 		__SPI1_RELEASE_RESET();
 
@@ -268,18 +244,15 @@
 		//PA5   ------> SPI1_SCK
 		//PA6   ------> SPI1_MISO 
 		//PA7   ------> SPI1_MOSI 
-		
-			HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
+
+		HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
 
 		HAL_DMA_DeInit(&hdma_tx);
 		HAL_DMA_DeInit(&hdma_rx);
-		
+
 		HAL_NVIC_DisableIRQ(DMA2_Stream3_IRQn);
 		HAL_NVIC_DisableIRQ(DMA2_Stream0_IRQn);
-	}
-	else
-  if(hspi->Instance==SPI3)
-  {
+	} else if (hspi->Instance == SPI3) {
 		__SPI3_FORCE_RESET();
 		__SPI3_RELEASE_RESET();
 
@@ -289,119 +262,105 @@
 		//PC12   ------> SPI3_MOSI 
 		//PA15   ------> SPI3_NSS (official)
 		//PC9    ------> SPI3_NSS (hw)
-		HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12);
+		HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12);
 	}
 }
 
-void SPI_synchronize_with_Master(void)
-{
-  GPIO_InitTypeDef GPIO_InitStruct;
-
-		__GPIOA_CLK_ENABLE();
-	/**SPI1 GPIO Configuration  
-	PA5   ------> SPI1_SCK
-	*/
-	GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5;
-	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
-	GPIO_InitStruct.Pull = GPIO_PULLUP;
-	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
-	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
-	HAL_Delay(10);
-	while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0);
-	HAL_Delay(10);
-	while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1);
-	HAL_Delay(50);
+void SPI_synchronize_with_Master(void) {
+//	GPIO_InitTypeDef GPIO_InitStruct;
+//
+//	__GPIOA_CLK_ENABLE();
+//	/**SPI1 GPIO Configuration
+//	 PA5   ------> SPI1_SCK
+//	 */
+//	GPIO_InitStruct.Pin = GPIO_PIN_4 | GPIO_PIN_5;
+//	GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
+//	GPIO_InitStruct.Pull = GPIO_PULLUP;
+//	GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
+//	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+//
+//	HAL_Delay(10);
+//	while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4) == 0);
+//	HAL_Delay(10);
+//	while (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_5) == 1);
+//	HAL_Delay(20);
 }
 
-
-void SPI_Start_single_TxRx_with_Master(void)
-{
+void SPI_Start_single_TxRx_with_Master(void) {
 	uint8_t * pOutput;
 
-	if(global.dataSendToSlave.getDeviceDataNow)
-	{
+	if (global.dataSendToSlave.getDeviceDataNow) {
 		global.dataSendToSlave.getDeviceDataNow = 0;
-		pOutput = (uint8_t*)&(global.deviceDataSendToMaster);
-	}
-	else
-	{
-		pOutput = (uint8_t*)&(global.dataSendToMaster);
+		pOutput = (uint8_t*) &(global.deviceDataSendToMaster);
+	} else {
+		pOutput = (uint8_t*) &(global.dataSendToMaster);
 	}
 
-	if(HAL_SPI_TransmitReceive_DMA(&hspi1,pOutput, (uint8_t*)&(global.dataSendToSlave), EXCHANGE_BUFFERSIZE+1) != HAL_OK)
-	{
-		// Transfer error in transmission process 
+	if (HAL_SPI_TransmitReceive_DMA(&hspi1, pOutput,(uint8_t*) &(global.dataSendToSlave), EXCHANGE_BUFFERSIZE)!= HAL_OK) {
+		// Transfer error in transmission process
 		SPI_Error_Handler();
 	}
 }
 
+void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi) {
+	/* restart SPI */
+	if (hspi == &hspi1) {
 
-void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
-{
-	/* restart SPI */
-if(hspi == &hspi1)
-	{
+		global.check_sync_not_running = 0;
+		/* stop data exchange? */
+		if (global.mode == MODE_SHUTDOWN) {
+			global.mode = MODE_SLEEP;
+			global.dataSendToSlavePending = 0;
+			global.dataSendToSlaveIsValid = 1;
+			global.dataSendToSlaveIsNotValidCount = 0;
+			return;
+		}
 
-	global.check_sync_not_running = 0;
-	/* stop data exchange? */
-	if(global.mode == MODE_SHUTDOWN)
-	{
-		global.mode = MODE_SLEEP;
-		global.dataSendToSlavePending = 0;
-		global.dataSendToSlaveIsValid = 1;
-		global.dataSendToSlaveIsNotValidCount = 0;
-		return;
+		/* data consistent? */
+		if (SPI_check_header_and_footer_ok()) {
+//		GPIO_new_DEBUG_HIGH(); //For debug.
+			global.dataSendToSlaveIsValid = 1;
+			global.dataSendToSlaveIsNotValidCount = 0;
+		} else {
+//		GPIO_new_DEBUG_LOW(); //For debug.
+			global.dataSendToSlaveIsValid = 0;
+			global.dataSendToSlaveIsNotValidCount++;
+			global.check_sync_not_running++;
+		}
+		global.dataSendToMaster.power_on_reset = 0;
+		global.deviceDataSendToMaster.power_on_reset = 0;
+
+
+//		if ( !global.dataSendToSlaveStopEval ) {
+//			scheduleSpecial_Evaluate_DataSendToSlave();
+//		}
+		scheduleSpecial_Evaluate_DataSendToSlave();
+		SPI_Start_single_TxRx_with_Master();
 	}
-	
-	/* data consistent? */
-	if(SPI_check_header_and_footer_ok())
-	{
-//		GPIO_new_DEBUG_HIGH(); //For debug.
-		global.dataSendToSlaveIsValid = 1;
-		global.dataSendToSlaveIsNotValidCount = 0;
-	}
-	else
-	{
-//		GPIO_new_DEBUG_LOW(); //For debug.
-		global.dataSendToSlaveIsValid = 0;
-		global.dataSendToSlaveIsNotValidCount++;
-	}
-	global.dataSendToMaster.power_on_reset = 0;
-	global.deviceDataSendToMaster.power_on_reset = 0;
-	if(!global.dataSendToSlaveStopEval)
-		{
-		scheduleSpecial_Evaluate_DataSendToSlave();
-		}
-	SPI_Start_single_TxRx_with_Master();
-  }
 }
 
-
-static uint8_t SPI_check_header_and_footer_ok(void)
-{
-	if(global.dataSendToSlave.header.checkCode[0] != 0xBB)
+static uint8_t SPI_check_header_and_footer_ok(void) {
+	if (global.dataSendToSlave.header.checkCode[0] != 0xBB)
 		return 0;
-	if(global.dataSendToSlave.header.checkCode[1] != 0x01)
+	if (global.dataSendToSlave.header.checkCode[1] != 0x01)
 		return 0;
-	if(global.dataSendToSlave.header.checkCode[2] != 0x01)
+	if (global.dataSendToSlave.header.checkCode[2] != 0x01)
 		return 0;
-	if(global.dataSendToSlave.header.checkCode[3] != 0xBB)
+	if (global.dataSendToSlave.header.checkCode[3] != 0xBB)
 		return 0;
-	if(global.dataSendToSlave.footer.checkCode[0] != 0xF4)
+	if (global.dataSendToSlave.footer.checkCode[0] != 0xF4)
 		return 0;
-	if(global.dataSendToSlave.footer.checkCode[1] != 0xF3)
+	if (global.dataSendToSlave.footer.checkCode[1] != 0xF3)
 		return 0;
-	if(global.dataSendToSlave.footer.checkCode[2] != 0xF2)
+	if (global.dataSendToSlave.footer.checkCode[2] != 0xF2)
 		return 0;
-	if(global.dataSendToSlave.footer.checkCode[3] != 0xF1)
+	if (global.dataSendToSlave.footer.checkCode[3] != 0xF1)
 		return 0;
 
 	return 1;
 }
 
-static void SPI_Error_Handler(void)
-{
+static void SPI_Error_Handler(void) {
 	//The device is locks. Hard to recover.
 //  while(1)
 //  {
@@ -409,11 +368,11 @@
 }
 
 /**
-  * @}
-  */
+ * @}
+ */
 
 /**
-  * @}
-  */
+ * @}
+ */
 
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Binary file ostc4pack/OSTC4_Firmware.bin has changed
Binary file ostc4pack/OSTC4_RTE.bin has changed
Binary file ostc4pack/OSTC4_RTE_upload.bin has changed
Binary file ostc4pack/OSTC4_firmware_upload.bin has changed