#include "init_l.h"

uint8_t mm = 0;
uint8_t tt = 50;

static void	RCC_Config_l(void);
static void MX_GPIO_Init_l(void);
static void TIM_Config_l(void);
		
static void RTC_Init(void);
static void ADC_Config(void);
static void NVIC_Config(void);
static void ADC_Config(void);

void I2C_Config(void);
void SPI_Init(void);
void IWDG_Init(void);

uint8_t er = 0;

void Init_Config(void) {
	
	RCC_Config_l();
	MX_GPIO_Init_l();
	TIM_Config_l();	
	TM_ILI9341_Init();	
	I2C_Config();
  ADC_Config();
	RC5_Receiver_Init();	
	NVIC_Config();
	RTC_Init();	
	HAL_Delay(10);
	
}


static void RCC_Config_l(void) {
	
	__HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
	__HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
	
	__HAL_RCC_TIM1_CLK_ENABLE();
	__HAL_RCC_TIM2_CLK_ENABLE();
	__HAL_RCC_TIM3_CLK_ENABLE();
	__HAL_RCC_TIM4_CLK_ENABLE();
	__HAL_RCC_TIM6_CLK_ENABLE();
	__HAL_RCC_TIM7_CLK_ENABLE();
	__HAL_RCC_TIM8_CLK_ENABLE();
	__HAL_RCC_TIM9_CLK_ENABLE();
	__HAL_RCC_TIM11_CLK_ENABLE();
	__HAL_RCC_TIM12_CLK_ENABLE();
	
	__HAL_RCC_I2C1_CLK_ENABLE();
  __HAL_RCC_ETH_CLK_ENABLE();
	__HAL_RCC_ADC1_CLK_ENABLE();
	__HAL_RCC_ADC2_CLK_ENABLE();
	__HAL_RCC_DMA2_CLK_ENABLE();
	__HAL_RCC_SPI3_CLK_ENABLE();
	
	__HAL_RCC_RTC_ENABLE();
	
}

static void MX_GPIO_Init_l(void)
{
  GPIO_InitTypeDef GPIO_InitStruct;
	
	/* IR ************************************************/
	GPIO_InitStruct.Pin = IR;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(IR_PORT, &GPIO_InitStruct);

	/* i2c ************************************************/
	GPIO_InitStruct.Pin = I2C_D1 | I2C_D2;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
  HAL_GPIO_Init(I2C_PORT, &GPIO_InitStruct);
	
	//--------------------------------------------------------------		 
	/* spi ************************************************/
	GPIO_InitStruct.Pin = SPI_SCK_PIN | SPI_MOSI_PIN | SPI_MISO_PIN;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
	HAL_GPIO_Init(SPI_GPIO_PORT, &GPIO_InitStruct);
	/* WRX ************************************************/
   GPIO_InitStruct.Pin     = ILI9341_WRX_PIN;
   GPIO_InitStruct.Mode    = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull    = GPIO_NOPULL;
   GPIO_InitStruct.Speed   = GPIO_SPEED_HIGH;
   HAL_GPIO_Init(ILI9341_WRX_PORT, &GPIO_InitStruct); 
	 /* RST ************************************************/
   GPIO_InitStruct.Pin     = ILI9341_RST_PIN;
   GPIO_InitStruct.Mode    = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull    = GPIO_NOPULL;
   GPIO_InitStruct.Speed   = GPIO_SPEED_HIGH;
   HAL_GPIO_Init(ILI9341_RST_PORT, &GPIO_InitStruct);
	 HAL_GPIO_WritePin(ILI9341_RST_PORT, ILI9341_RST_PIN, GPIO_PIN_SET);
	 /* CS ************************************************/
   GPIO_InitStruct.Pin     = ILI9341_CS_PIN;
   GPIO_InitStruct.Mode    = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull    = GPIO_NOPULL;
   GPIO_InitStruct.Speed   = GPIO_SPEED_HIGH;
   HAL_GPIO_Init(ILI9341_CS_PORT, &GPIO_InitStruct);
	 
	 HAL_GPIO_WritePin(ILI9341_CS_PORT, ILI9341_CS_PIN, GPIO_PIN_RESET);
	 
	 //TFT
	   /*Configure GPIO pins : PB3 PB4 PB5 */
//  GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
//  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
//  GPIO_InitStruct.Pull = GPIO_NOPULL;
//  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
//  GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
//  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//	
//	 HAL_GPIO_WritePin(GPIOD, GPIO_PIN_6, GPIO_PIN_SET);
	////
	
		/* TFT_IRQ DETECT ************************************************/
	GPIO_InitStruct.Pin = TFT_IRQ;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(TFT_IRQ_PORT, &GPIO_InitStruct);
	

		/* ILI LED************************************************/
	GPIO_InitStruct.Pin = LED_ILI; 
	GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF3_TIM11;
	HAL_GPIO_Init(LED_ILI_PORT, &GPIO_InitStruct);

//	GPIO_InitStruct.Pin = LED_ILI;
//  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//  GPIO_InitStruct.Pull = GPIO_PULLDOWN;
//  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
//  HAL_GPIO_Init(LED_ILI_PORT, &GPIO_InitStruct);
//	HAL_GPIO_WritePin(LED_ILI_PORT, LED_ILI, GPIO_PIN_RESET);
//	HAL_GPIO_WritePin(LED_ILI_PORT, LED_ILI, GPIO_PIN_SET);
//	HAL_GPIO_WritePin(LED_ILI_PORT, LED_ILI, GPIO_PIN_RESET);

	//--------------------------------------------------------------
	
	/* ZERO DETECT ************************************************/
	GPIO_InitStruct.Pin = ZERO;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ZERO_PORT, &GPIO_InitStruct);
	
	/* BEPPER ************************************************/	
	GPIO_InitStruct.Pin = BEPPER;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
	HAL_GPIO_Init(BEPPER_PORT, &GPIO_InitStruct);
	
//	
/* FLOOD ************************************************/
	GPIO_InitStruct.Pin = FLOOD;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(FLOOD_PORT, &GPIO_InitStruct);
	
/* TRIAC ************************************************/
	GPIO_InitStruct.Pin = TRIAC;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(TRIAC_PORT, &GPIO_InitStruct);

/* SPIRT ************************************************/
	GPIO_InitStruct.Pin = PWM_SPIRT;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
	HAL_GPIO_Init(PWM_SPIRT_PORT, &GPIO_InitStruct);

/* DIMROT ************************************************/
	GPIO_InitStruct.Pin = PWM_DIMROT;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
	HAL_GPIO_Init(PWM_DIMROT_PORT, &GPIO_InitStruct);
	
/* MOTOR ************************************************/
	
	GPIO_InitStruct.Pin = PWM_MOTOR;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_PULLUP;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF3_TIM8;
	HAL_GPIO_Init(PWM_MOTOR_PORT, &GPIO_InitStruct);

/* ADC ************************************************/
	GPIO_InitStruct.Pin = ADC_TRIAC;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADC_TRIAC_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = ADC5;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADC5_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = ADC_MPX;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADC_MPX_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = ADC_SPIRT;
  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(ADC_SPIRT_PORT, &GPIO_InitStruct);
	
	/* COOLER ************************************************/
	GPIO_InitStruct.Pin = COOLER;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(COOLER_PORT, &GPIO_InitStruct);
	HAL_GPIO_WritePin(COOLER_PORT, COOLER, GPIO_PIN_SET);
	
/* ETHERNET************************************************/
  GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_7;
  //GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL ; 
  GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5;
  GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
	GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
	GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13;
  GPIO_InitStruct.Alternate = GPIO_AF11_ETH;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); 

  GPIO_InitStruct.Pin = PHY_RST_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;	
  HAL_GPIO_Init(DEV_RST_PORT, &GPIO_InitStruct);
	
	HAL_GPIO_WritePin(DEV_RST_PORT, PHY_RST_PIN, GPIO_PIN_RESET);
	HAL_Delay(2);
	HAL_GPIO_WritePin(DEV_RST_PORT, PHY_RST_PIN, GPIO_PIN_SET);
	HAL_Delay(2);
	
}

static void ADC_Config(void){
	static ADC_ChannelConfTypeDef ADC_sConfig;
	static DMA_HandleTypeDef  hdma_adc;	
	
	ADC_InjectionConfTypeDef sConfigInjected;
	ADC_ChannelConfTypeDef sConfig_ADC5;
	
	ADC_InjectionConfTypeDef sConfigInjected_MPX;
	ADC_ChannelConfTypeDef sConfig_ADC_MPX;

/* ADC1************************************************/
	AdcHandle.Instance          					= ADC1;
  AdcHandle.Init.ClockPrescaler 				= ADC_CLOCKPRESCALER_PCLK_DIV2;
  AdcHandle.Init.Resolution 						= ADC_RESOLUTION12b;
  AdcHandle.Init.ScanConvMode 					= DISABLE;
  AdcHandle.Init.ContinuousConvMode 		= DISABLE;
  AdcHandle.Init.DiscontinuousConvMode 	= DISABLE;
  AdcHandle.Init.NbrOfDiscConversion 		= 0;
  AdcHandle.Init.ExternalTrigConvEdge 	= ADC_EXTERNALTRIGCONVEDGE_RISINGFALLING;
  AdcHandle.Init.ExternalTrigConv 			= ADC_EXTERNALTRIGCONV_T2_TRGO;
	
  AdcHandle.Init.DataAlign 							= ADC_DATAALIGN_RIGHT;
  AdcHandle.Init.NbrOfConversion 				= 1;
  AdcHandle.Init.DMAContinuousRequests 	= ENABLE;
  AdcHandle.Init.EOCSelection 					= EOC_SEQ_CONV;
      
	hdma_adc.Instance 								= DMA2_Stream0;
  hdma_adc.Init.Channel  						= DMA_CHANNEL_0;
  hdma_adc.Init.Direction 					= DMA_PERIPH_TO_MEMORY;
  hdma_adc.Init.PeriphInc 					= DMA_PINC_DISABLE;
  hdma_adc.Init.MemInc 							= DMA_MINC_ENABLE;
  hdma_adc.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  hdma_adc.Init.MemDataAlignment 		= DMA_MDATAALIGN_HALFWORD;
  hdma_adc.Init.Mode 								= DMA_CIRCULAR;
  hdma_adc.Init.Priority 						= DMA_PRIORITY_HIGH;
  hdma_adc.Init.FIFOMode 						= DMA_FIFOMODE_DISABLE;         
  hdma_adc.Init.FIFOThreshold 			= DMA_FIFO_THRESHOLD_HALFFULL;
  hdma_adc.Init.MemBurst 						= DMA_MBURST_SINGLE;
  hdma_adc.Init.PeriphBurst 				= DMA_PBURST_SINGLE; 
  HAL_DMA_Init(&hdma_adc);

  __HAL_LINKDMA(&AdcHandle, DMA_Handle, hdma_adc);

  HAL_ADC_Init(&AdcHandle);
  ADC_sConfig.Channel = ADC_CHANNEL_TRIAC;
  ADC_sConfig.Rank = 1;
  ADC_sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
  ADC_sConfig.Offset = 0;
  HAL_ADC_ConfigChannel(&AdcHandle, &ADC_sConfig);
  //HAL_ADC_Start_DMA(&AdcHandle, (uint32_t*)&ADCxConvertedValue, 1024);	


/* ADC2************************************************/
  AdcHandle_ADC.Instance = ADC2;
  
  AdcHandle_ADC.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
  AdcHandle_ADC.Init.Resolution = ADC_RESOLUTION12b;
  AdcHandle_ADC.Init.ScanConvMode = ENABLE;
  AdcHandle_ADC.Init.ContinuousConvMode = DISABLE;
  AdcHandle_ADC.Init.DiscontinuousConvMode = DISABLE;
  AdcHandle_ADC.Init.NbrOfDiscConversion = 0;
  AdcHandle_ADC.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
	AdcHandle_ADC.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  AdcHandle_ADC.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  AdcHandle_ADC.Init.NbrOfConversion = 2;
  AdcHandle_ADC.Init.DMAContinuousRequests = DISABLE;
  AdcHandle_ADC.Init.EOCSelection = EOC_SINGLE_CONV;
	
  HAL_ADC_Init(&AdcHandle_ADC);
	
  sConfig_ADC5.Rank = 1;
  sConfig_ADC5.SamplingTime = ADC_SAMPLETIME_480CYCLES;
	sConfig_ADC5.Offset = 0;
	sConfig_ADC5.Channel = ADC_CHANNEL5;
  HAL_ADC_ConfigChannel(&AdcHandle_ADC, &sConfig_ADC5);
	
	sConfig_ADC5.Channel = ADC_CHANNEL_MPX;
	HAL_ADC_ConfigChannel(&AdcHandle_ADC, &sConfig_ADC5);
	
	sConfig_ADC5.Channel = ADC_CHANNEL_SPIRT;
	HAL_ADC_ConfigChannel(&AdcHandle_ADC, &sConfig_ADC5);
	
  sConfigInjected.InjectedNbrOfConversion = 3;
  sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_480CYCLES;
	sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGCONV_T1_CC1;
  sConfigInjected.AutoInjectedConv = ENABLE;
  sConfigInjected.InjectedDiscontinuousConvMode = DISABLE;
  sConfigInjected.InjectedOffset = 0;
	sConfigInjected.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONVEDGE_NONE;
	
	sConfigInjected.InjectedChannel = ADC_CHANNEL5;
	sConfigInjected.InjectedRank = 1;
	HAL_ADCEx_InjectedConfigChannel(&AdcHandle_ADC, &sConfigInjected);
	
	sConfigInjected.InjectedChannel = ADC_CHANNEL_MPX;
  sConfigInjected.InjectedRank = 2;
  HAL_ADCEx_InjectedConfigChannel(&AdcHandle_ADC, &sConfigInjected);
	
	sConfigInjected.InjectedChannel = ADC_CHANNEL_SPIRT;
  sConfigInjected.InjectedRank = 3;
  HAL_ADCEx_InjectedConfigChannel(&AdcHandle_ADC, &sConfigInjected);
	
	HAL_ADCEx_InjectedStart(&AdcHandle_ADC);

}

static void NVIC_Config(void)
{
	
/* LINK ************************************************/
	HAL_NVIC_SetPriority(EXTI9_5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI9_5_IRQn);
	
/* RC5 ************************************************/
	HAL_NVIC_SetPriority(RC5_TIM_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(RC5_TIM_IRQn);	
  HAL_NVIC_SetPriority(RC5_EXTI_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(RC5_EXTI_IRQn);
	
/* TIMER ************************************************/
  HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
	
///* TRIAC ************************************************/
  HAL_NVIC_SetPriority(TIM8_BRK_TIM12_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM8_BRK_TIM12_IRQn);
		
/* ZERO ************************************************/
	HAL_NVIC_SetPriority(EXTI3_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(EXTI3_IRQn);
	
	/* ZERO ************************************************/
	HAL_NVIC_SetPriority(TIM1_BRK_TIM9_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM1_BRK_TIM9_IRQn);
	
	 /* DMA interrupt init */
  //HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 0);
  //HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);

/* SysTick ************************************************/
  HAL_NVIC_SetPriority(SysTick_IRQn, 0x0, 0x0);
	
}


void I2C_Config(void) {
	
	
	if(HAL_I2C_GetState(&hi2c1) == HAL_I2C_STATE_RESET)
  {
		
		hi2c1.Instance              = I2C1;
    hi2c1.Init.ClockSpeed       = 400000;
    hi2c1.Init.DutyCycle        = I2C_DUTYCYCLE_2;
    hi2c1.Init.OwnAddress1      = 0;
    hi2c1.Init.AddressingMode   = I2C_ADDRESSINGMODE_7BIT;
    hi2c1.Init.DualAddressMode  = I2C_DUALADDRESS_DISABLED;
    hi2c1.Init.OwnAddress2      = 0;
    hi2c1.Init.GeneralCallMode  = I2C_GENERALCALL_DISABLED;
    hi2c1.Init.NoStretchMode    = I2C_NOSTRETCH_DISABLED;  
		
		
		if(HAL_I2C_Init(&hi2c1) == HAL_OK)
		{
//			  if(EEPROM_IO_IsDeviceReady(EEPROM_I2C_ADDRESS, 3000) != HAL_OK)
//				{
//					uint8_t t = 22;
//				}
//				else{

					//	BSP_EEPROM_WriteBuffer(&tt, 0, sizeof(uint8_t));	
					//	BSP_EEPROM_ReadBuffer(&mm ,0, sizeof(uint8_t));	
				//}
				
				
			
		 OW_Search(&hi2c1);
	   ConvertTemperature_ds18b20(&hi2c1);
		}
	}
//	if(HAL_I2C_GetState(&hi2c1) == HAL_I2C_STATE_RESET)
//  {
//		hi2c1.Instance = I2C1;
//		hi2c1.Init.ClockSpeed = 400000;
//		hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
//		hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED;
//		hi2c1.Init.OwnAddress1 = 0;
//		hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
//		if(HAL_I2C_Init(&hi2c1) != HAL_OK)		
//		{}
//		
//		OW_Search(&hi2c1);
//		
//		ConvertTemperature_ds18b20(&hi2c1);
//	
//	}
}


void TIM_Config_l(void) {
  TIM_MasterConfigTypeDef sMasterConfig;

/* TIM1 Dimrot************************************************/
	TimHandlePWM_Dimrot.Instance = TIM1; 
  TimHandlePWM_Dimrot.Init.Prescaler = 42000-1;
  TimHandlePWM_Dimrot.Init.Period = 8000-1;//2 ρεκ
	//TimHandlePWM.Init.Period = 14400-1;//3,6 ρεκ
  TimHandlePWM_Dimrot.Init.ClockDivision = 0;
  TimHandlePWM_Dimrot.Init.CounterMode = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&TimHandlePWM_Dimrot);

  sConfig_Dimrot.OCMode 			= TIM_OCMODE_PWM1;
  sConfig_Dimrot.OCPolarity 	= TIM_OCPOLARITY_HIGH;
  sConfig_Dimrot.OCFastMode 	= TIM_OCFAST_DISABLE;
  sConfig_Dimrot.Pulse 				= 8000;  
  HAL_TIM_PWM_ConfigChannel(&TimHandlePWM_Dimrot, &sConfig_Dimrot, TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&TimHandlePWM_Dimrot, TIM_CHANNEL_1);
	
/* TIM3 Spirt************************************************/
	TimHandlePWM_Spirt.Instance = TIM3; 
  TimHandlePWM_Spirt.Init.Prescaler = 37800-1;
  TimHandlePWM_Spirt.Init.Period = 8000-1;//2 ρεκ
  TimHandlePWM_Spirt.Init.ClockDivision = 0;
  TimHandlePWM_Spirt.Init.CounterMode = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&TimHandlePWM_Spirt);

  sConfig_Spirt.OCMode 			= TIM_OCMODE_PWM1;
  sConfig_Spirt.OCPolarity 	= TIM_OCPOLARITY_HIGH;
  sConfig_Spirt.OCFastMode 	= TIM_OCFAST_DISABLE;
  sConfig_Spirt.Pulse 			= 0;  
  HAL_TIM_PWM_ConfigChannel(&TimHandlePWM_Spirt, &sConfig_Spirt, TIM_CHANNEL_4);
	HAL_TIM_PWM_Start(&TimHandlePWM_Spirt, TIM_CHANNEL_4);
	
	/* TIM8 Spirt************************************************/
	//Prescaler = 200 Period = 300 Freq = 2.8 Khz
	//Prescaler = 100 Period = 300 Freq = 5.6 Khz
	//Prescaler = 50 Period = 300 Freq = 11.2 Khz
	//Prescaler = 20 Period = 300 Freq = 27.9.6 Khz
	
	
	//Prescaler = 60 Period = 200 Freq  = 14 Khz
	//Prescaler = 40 Period = 200 Freq  = 21 Khz
	//Prescaler = 30 Period = 200 Freq  = 28 Khz
	//Prescaler = 25 Period = 200 Freq  = 33.5 Khz
	//Prescaler = 20 Period = 200 Freq  = 42 Khz
	//Prescaler = 16 Period = 200 Freq  = 52 Khz
	//Prescaler = 14 Period = 200 Freq  = 60 Khz
	//Prescaler = 11 Period = 200 Freq  = 70 Khz
	//Prescaler = 11 Period = 200 Freq  = 76 Khz
	//Prescaler = 10 Period = 200 Freq  = 83 Khz
	//Prescaler = 9 Period = 200 Freq  = 93 Khz
	//Prescaler = 8 Period = 200 Freq  = 105 Khz
	//Prescaler = 7 Period = 200 Freq  = 120 Khz
	//Prescaler = 6 Period = 200 Freq  = 140 Khz
	
	//Prescaler = 15 Period = 100 Freq  = 112 Khz
	//Prescaler = 20 Period = 100 Freq  = 83.7 Khz
	//Prescaler = 50 Period = 100 Freq  = 33.6 Khz
	//Prescaler = 84 Period = 100 Freq  = 20.0 Khz
	//Prescaler = 100 Period = 100 Freq = 16.8 Khz
	//Prescaler = 200 Period = 100 Freq = 8.4 Khz
	//Prescaler = 400 Period = 100 Freq = 4.2 Khz
	//Prescaler = 700 Period = 100 Freq = 2.4 Khz
	//Prescaler = 1200 Period = 100 Freq = 1.3 Khz
	//Prescaler = 1500 Period = 100 Freq = 1.1 Khz
	//Prescaler = 1600 Period = 100 Freq = 1.05 Khz
	//Prescaler = 1700 Period = 100 Freq = 988 hz
	
	TimHandlePWM_Motor.Instance = TIM8; 
  //TimHandlePWM_Motor.Init.Prescaler = 84-1;
	//TimHandlePWM_Motor.Init.Prescaler = 25-1; //33.5 Khz
	//TimHandlePWM_Motor.Init.Prescaler = 50-1; //16.8 Khz   water
	//TimHandlePWM_Motor.Init.Prescaler = 100-1; //8.39 Khz
	//TimHandlePWM_Motor.Init.Prescaler = 200-1; //4.20 Khz
	//TimHandlePWM_Motor.Init.Prescaler = 500-1; //1.68 Khz
	//TimHandlePWM_Motor.Init.Prescaler = 1000-1; //840 hz
	//TimHandlePWM_Motor.Init.Prescaler = 2500-1; //336 hz
	//TimHandlePWM_Motor.Init.Prescaler = 6000-1; //  hz
  TimHandlePWM_Motor.Init.Prescaler = 9000-1; // 93 hz // osmo
  TimHandlePWM_Motor.Init.Period = MAX_MOTOR-1;//2 ρεκ
  TimHandlePWM_Motor.Init.ClockDivision = 0;
  TimHandlePWM_Motor.Init.CounterMode = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&TimHandlePWM_Motor);

  sConfig_Motor.OCMode 			= TIM_OCMODE_PWM1;
  sConfig_Motor.OCPolarity 	= TIM_OCPOLARITY_HIGH;
  sConfig_Motor.OCFastMode 	= TIM_OCFAST_DISABLE;
  sConfig_Motor.Pulse 			= 0;  
  HAL_TIM_PWM_ConfigChannel(&TimHandlePWM_Motor, &sConfig_Motor, TIM_CHANNEL_2);
	HAL_TIM_PWM_Start(&TimHandlePWM_Motor, TIM_CHANNEL_2);

	
	/* TIM4 BUZZER************************************************/
	//BEPPER
	TimHandlePWM_Buzzer.Instance = TIM4; 
	TimHandlePWM_Buzzer.Init.Prescaler = 21000-1;
	//TimHandlePWM_Buzzer.Init.Period = 5000-1;//2 ρεκ
	TimHandlePWM_Buzzer.Init.Period = 14400-1;//3,6 ρεκ
	TimHandlePWM_Buzzer.Init.ClockDivision = 0;
	TimHandlePWM_Buzzer.Init.CounterMode = TIM_CLOCKDIVISION_DIV1;
	HAL_TIM_PWM_Init(&TimHandlePWM_Buzzer);

	sConfig_Buzzer.OCMode 			= TIM_OCMODE_PWM1;
	sConfig_Buzzer.OCPolarity 	= TIM_OCPOLARITY_HIGH;
	sConfig_Buzzer.OCFastMode 	= TIM_OCFAST_DISABLE;
	//sConfig_Buzzer.Pulse 			  = 1000;  
	sConfig_Buzzer.Pulse 			  = 0;  
	HAL_TIM_PWM_ConfigChannel(&TimHandlePWM_Buzzer, &sConfig_Buzzer, TIM_CHANNEL_4);
	HAL_TIM_PWM_Start(&TimHandlePWM_Buzzer, TIM_CHANNEL_4);
	
	
/* TIM2 ************************************************/
	TimHandleADC.Instance = TIM2;
	TimHandleADC.Init.Prescaler = 1-1;//9500   1000-412, 800-512, 500-816, 
  TimHandleADC.Init.Period = 1;
  TimHandleADC.Init.ClockDivision = 0;
  TimHandleADC.Init.CounterMode = TIM_COUNTERMODE_UP;
  HAL_TIM_Base_Init(&TimHandleADC);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
	HAL_TIMEx_MasterConfigSynchronization(&TimHandleADC, &sMasterConfig);
	
	HAL_TIM_Base_Start_IT(&TimHandleADC) ;
	
/* TIM6 ************************************************/
	TimHandle6.Instance = TIM6;
	TimHandle6.Init.Prescaler = 42000-1;
  TimHandle6.Init.Period = 500;
  TimHandle6.Init.ClockDivision = 0;
  TimHandle6.Init.CounterMode = TIM_COUNTERMODE_UP;
  HAL_TIM_Base_Init(&TimHandle6);	
	HAL_TIM_Base_Start_IT(&TimHandle6) ;	
	
/* TIM12 ************************************************/	
	TimHandleTRIAC.Instance = TIM12;
	TimHandleTRIAC.Init.Prescaler = 365-1;//400
  TimHandleTRIAC.Init.Period 		= 1;
	TimHandleTRIAC.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	TimHandleTRIAC.Init.CounterMode = TIM_COUNTERMODE_UP;
	
	HAL_TIM_Base_Init(&TimHandleTRIAC);	
	//HAL_TIM_Base_Start_IT(&TimHandleTRIAC) ;

/* TIM9 ************************************************/
	TimHandle50ms.Instance = TIM9;
	TimHandle50ms.Init.Prescaler = 42000-1; //2000 Hz
  TimHandle50ms.Init.Period = 10;//100
  TimHandle50ms.Init.ClockDivision = 0;
  TimHandle50ms.Init.CounterMode = TIM_COUNTERMODE_UP;
  HAL_TIM_Base_Init(&TimHandle50ms);	
	HAL_TIM_Base_Start_IT(&TimHandle50ms) ;	
	
	/* TIM11 LED ILI************************************************/
	TimHandlePWM_LED_ILI.Instance = TIM11; 
	//TimHandlePWM_LED_ILI.Init.Prescaler = 15000-1; //112 //Hz
	TimHandlePWM_LED_ILI.Init.Prescaler = 1670-1; //1 KHz
	//TimHandlePWM_LED_ILI.Init.Prescaler = 2000-1; //560 Hz
	
  TimHandlePWM_LED_ILI.Init.Period = 100-1;
  TimHandlePWM_LED_ILI.Init.ClockDivision = 0;
  TimHandlePWM_LED_ILI.Init.CounterMode = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_PWM_Init(&TimHandlePWM_LED_ILI);

  sConfig__LED_ILI.OCMode 			= TIM_OCMODE_PWM1;
  sConfig__LED_ILI.OCPolarity 	= TIM_OCPOLARITY_HIGH;
  sConfig__LED_ILI.OCFastMode 	= TIM_OCFAST_DISABLE;
  sConfig__LED_ILI.Pulse 			  = 0;  
  HAL_TIM_PWM_ConfigChannel(&TimHandlePWM_LED_ILI, &sConfig__LED_ILI, TIM_CHANNEL_1);
	HAL_TIM_PWM_Start(&TimHandlePWM_LED_ILI, TIM_CHANNEL_1);

	__HAL_TIM_SetCompare(&TimHandlePWM_LED_ILI,TIM_CHANNEL_1,70);
}


void RTC_Init()
{
	RTC_TimeTypeDef  stimestructure;
  __HAL_RCC_RTC_CONFIG(RCC_RTCCLKSOURCE_LSE);
	
	RtcHandle.Instance = RTC;
  RtcHandle.Init.HourFormat 		= RTC_HOURFORMAT_24;
  RtcHandle.Init.AsynchPrediv 	= 0x7F;
  RtcHandle.Init.SynchPrediv 		= 0x00FF;
  RtcHandle.Init.OutPut 				= RTC_OUTPUT_DISABLE;
  RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  RtcHandle.Init.OutPutType 		= RTC_OUTPUT_TYPE_OPENDRAIN;
  
   if(HAL_RTC_Init(&RtcHandle) != HAL_OK)
  { 
		 er = 1;
	}
//  stimestructure.Hours = 0x01;
//  stimestructure.Minutes = 0x01;
//  stimestructure.Seconds = 0x00;
//  stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
//  stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
//  stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
//  
//  if(HAL_RTC_SetTime(&RtcHandle,&stimestructure,FORMAT_BCD) != HAL_OK)
//  {
//		er = 2;
//	} 
	
}

void IWDG_Init()
{	
	
	#if !DEBUG_PROC
		Hiwdg.Instance = IWDG;
		Hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
		Hiwdg.Init.Reload = 500;//100
		HAL_IWDG_Init(&Hiwdg);	
		
		HAL_IWDG_Start(&Hiwdg);	
	#endif /* DEBUG_PROC */
}


void SPI_Init(void)
{
	
 if(HAL_SPI_GetState(&SpiHandle) == HAL_SPI_STATE_RESET)
  {
    /* SPI configuration -----------------------------------------------------*/
    SpiHandle.Instance = SPI3;
    SpiHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
    SpiHandle.Init.Direction      = SPI_DIRECTION_2LINES;
    SpiHandle.Init.CLKPhase       = SPI_PHASE_1EDGE;
    SpiHandle.Init.CLKPolarity    = SPI_POLARITY_HIGH;
    SpiHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
    SpiHandle.Init.CRCPolynomial  = 7;
    SpiHandle.Init.DataSize       = SPI_DATASIZE_8BIT;
    SpiHandle.Init.FirstBit       = SPI_FIRSTBIT_MSB;
    SpiHandle.Init.NSS            = SPI_NSS_HARD_OUTPUT;
    SpiHandle.Init.TIMode         = SPI_TIMODE_DISABLED;
    SpiHandle.Init.Mode           = SPI_MODE_MASTER;
	
		if(HAL_SPI_Init(&SpiHandle) != HAL_OK){ 
		}
	}	
	
//	  if(HAL_SPI_GetState(&SpiTFTHandle) == HAL_SPI_STATE_RESET)
//  { 
//    /* SPI configuration -----------------------------------------------------*/
//    SpiTFTHandle.Instance = SPI1;
//    SpiTFTHandle.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
//    SpiTFTHandle.Init.Direction      = SPI_DIRECTION_2LINES;
//    SpiTFTHandle.Init.CLKPhase       = SPI_PHASE_1EDGE;
//    SpiTFTHandle.Init.CLKPolarity    = SPI_POLARITY_HIGH;
//    SpiTFTHandle.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
//    SpiTFTHandle.Init.CRCPolynomial  = 7;
//    SpiTFTHandle.Init.DataSize       = SPI_DATASIZE_8BIT;
//    SpiTFTHandle.Init.FirstBit       = SPI_FIRSTBIT_MSB;
//    SpiTFTHandle.Init.NSS            = SPI_NSS_HARD_OUTPUT;
//    SpiTFTHandle.Init.TIMode         = SPI_TIMODE_DISABLED;
//    SpiTFTHandle.Init.Mode           = SPI_MODE_MASTER;
//	
//		if(HAL_SPI_Init(&SpiTFTHandle) != HAL_OK){ 
//		}
//	}		
}




 


