/* ###################################################################
**     Filename    : main.c
**     Project     : KL25
**     Processor   : MKL25Z128VLK4
**     Version     : Driver 01.01
**     Compiler    : GNU C Compiler
**     Date/Time   : 2017-01-17, 17:40, # CodeGen: 0
**     Abstract    :
**         Main module.
**         This module contains user's application code.
**     Settings    :
**     Contents    :
**         No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.01
** @brief
**         Main module.
**         This module contains user's application code.
*/         
/*!
**  @addtogroup main_module main module documentation
**  @{
*/         
/* MODULE main */
/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "clockMan1.h"
#include "pin_init.h"
#include "osa1.h"
#include "tpmTmr1.h"
#include "gpio1.h"
#include "i2cCom1.h"
#include "uartCom1.h"
#include "DbgCs1.h"
#include "pitTimer1.h"
#include "tpmTmr2.h"
#include "adConv1.h"
#if CPU_INIT_CONFIG
  #include "Init_Config.h"
#endif
/* User includes (#include below this line is not maintained by Processor Expert) */

#include "TLC3335.h"
#include "DS3231_RealTime.h"
#include "uart2usb.h"
#include "SaveData.h"
#include "MCP9801_Temp.h"

#define IRQ_SIZE_BUFFER	10

volatile uint32_t irqCount = 0, irqOldCount = 0;
volatile bool lptmrFlag = false, disMeasFlag = false;



LTC3335_Config drvConfig;
DS3231_realTime	realTimeConfig;
MCP9801_temperature tempConfig;
bool RloadEnable = false;

PE_ISR(PORTD_IRQHandler)
{
	PORT_HAL_ClearPortIntFlag(PORTD);
	irqCount++;
}

PE_ISR(PORTA_IRQHandler)
{
	PORT_HAL_ClearPortIntFlag(PORTA);
	disMeasFlag = true;
}


/*lint -save  -e970 Disable MISRA rule (6.3) checking. */
int main(void)
/*lint -restore Enable MISRA rule (6.3) checking. */
{
  save_data_t saveData =
  {
		  .timePeriod = 0,
		  .saveDataFlag = false,
  };

  /*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
  /*** End of Processor Expert internal initialization.                    ***/

  //TPM_DRV_PwmStop(TPM0_IDX, &tpmTmr1_ChnConfig1, 1);
  TPM_DRV_PwmStop(TPM0_IDX, &tpmTmr1_ChnConfig0, 0);

  LTC3335_DefaultConfig(&drvConfig);
  DS3231_GetDefaultConfig(&realTimeConfig);
  MCP9801_GetDefaultConfig(&tempConfig);

  PIT_DRV_SetTimerPeriodByUs(pitTimer1_IDX, pitTimer1_CHANNEL, 1000000);

#ifdef TIME_CALIBRATION
  realTimeConfig.date = 11;
  realTimeConfig.year = 17;
  realTimeConfig.month = 4;
  realTimeConfig.hours = 19;
  realTimeConfig.minutes = 32;
  realTimeConfig.seconds = 0;
  if(DS3231_WriteAll(&realTimeConfig))
  {
	  PRINTF("err");
  }
#endif


  LTC3335_Init(&drvConfig);
  LTC3335_SetCounterTest(&drvConfig);
  MCP9801_SetResolution(&tempConfig, MCP9801_10bit);

  GPIO_DRV_SetPinOutput(LEDRGB_GREEN);
  GPIO_DRV_ClearPinOutput(MEASURE_LED);

  while(1)
  {
	  if(lptmrFlag)
	  {
		  lptmrFlag = false;
		  SaveData(&saveData);
	  }
	  if(disMeasFlag)
	  {
		  GPIO_DRV_SetPinOutput(LEDRGB_GREEN);
		  GPIO_DRV_ClearPinOutput(MEASURE_LED);
		  PIT_DRV_StopTimer(pitTimer1_IDX, pitTimer1_CHANNEL);
		  PIT_DRV_SetTimerPeriodByUs(pitTimer1_IDX, pitTimer1_CHANNEL, 1000000);
		  LTC3335_Enable(&drvConfig, false);
		  UART_StopPWM(&tpmTmr1_ChnConfig0);
		  GPIO_DRV_ClearPinOutput(RLOAD_PIN);
		  saveData.saveDataFlag = false;
		  disMeasFlag = false;
	  }
	  if(ADC16_DRV_GetChnFlag(adConv1_IDX, 0, kAdcChnConvCompleteFlag))
	  {
		  LTC3335_GetInVoltage(&drvConfig);
	  }

	  UART_Routine(&drvConfig, &realTimeConfig, &saveData);
  }

  /*** Don't write any code pass this line, or it will be deleted during code generation. ***/
  /*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
  #ifdef PEX_RTOS_START
    PEX_RTOS_START();                  /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
  #endif
  /*** End of RTOS startup code.  ***/
  /*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
  for(;;){}
  /*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
**     This file was created by Processor Expert 10.5 [05.21]
**     for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
