//*****************************************************************************
//
//  Program pro otestovani funkcnosti prevodnikove karty  / Sensor WIM
//
//*****************************************************************************

//Hlavickove soubory-----------
#include <common.h>
#include <string.h>

#include "main.h"
#include <SPIdriver.h>
#include <LEDdriver.h>
#include <ADCdriver.h>
#include <TEMPdriver.h>
#include <CANdriver.h>
#include <EEPROMdriver.h>
#include <UARTCmds.h>
#include <UARTdriver.h>
#include <I2Cdriver.h>
#include <ExtPort.h>

//#include <ExtPORTdriver.cpp>

// The error routine that is called if the driver library encounters an error---
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

// Globalni promenne a objekty
struct MainStruct SensorWIM;
volatile s_system_flags sys;
volatile s_request_flags request;

/*****************************************************************************/
//Hlavni funkce mikrokontroleru------------------------------------------------
int main(void){
  
 //Promenne****************
  unsigned char test;
  unsigned short usTest;
  
 //Program*****************
    Initialization();                                                           //Inicializace vsech pouzivanych modulu a pinu mikrokontroleru
    
    SensorWIM.DACValue=0x03c0;   //0x03ca                                        //Naastaveni hodnoty odesilane na D/A prevodnik / velikost proudu LD
    LEDSetBias(SensorWIM.DACValue);                                              //Sepnuti prahoveho proudu diodou

  //testovani knihoven------------------------------------------  
      SensorWIM.Temperature=25.0;
    //zapis hodnoty do EEPROM a overeni
      EEPROMWrite(0x32, (unsigned char *)&SensorWIM.Temperature, 4);    //zapsani 4B float cisla do EEPROM
      SensorWIM.Temperature=0.0;
      EEPROMRead(0x32, (unsigned char *)&SensorWIM.Temperature, 4);     //cteni 4B float cisla z EEPROM
     
      test=0x47;
      EEPROMByteWrite(0x28,&test);                  //zapis bytu do EEPROM
      test=0x00;
      EEPROMByteRead(0x28,&test);                   //Cteni bytu z EEPROM
      
      WriteDigOut(0x01);                            //zapis na digitalni vystupy
      
      test=0;
      test=ReadDigIn();                             //cteni z digitalni vstupu
      
      SetLEDState(0xfd,0xff);                       //stav na LED / zapnuto=0, vypnuto=1
      
      usTest=0;
      usTest=ReadAddr();                            //cteni adres nastavenych na DIP a propojkach
  //konec testovani knihoven-----------------------------------
    
      
    //nekonecna smycka
    while(1)  
    {
      for (unsigned char i=0;i<NO_ADC;i++)
      {
        SensorWIM.ADCValue[i] = ADCGetValue(i);                                 //Provedeni jednoho mereni (vygenerovani pulzu, zmereni, vycteni a vraceni hodnoty z daneho A/D)
        if((SensorWIM.ADCValue[i]==0)||(SensorWIM.ADCValue[i]==65535))          //vyctena hodnota neni nulova /vzdy sum/ a ani nesmi byt plny rozsah
          SensorWIM.CardState|=(0x10<<i);                                       //pokud nastane limitace, nastavim priznak
      }
      SensorWIM.Temperature=TEMPGetValue();                                     //precteni teploty ze snimace DS7505 
          
      //UARTSendData((char const*)&value,2);                                    //Odeslani zmerene hodnoty na USB / pres UART
      
      if (sys.all_flags) ProcessSystFlags();                                    // Process wake-up events

      if (request.all_flags) ProcessRequests();                                 // Process actions requested by logic modules
      
      
      //CANSendMessage((char const *)&SensorWIM.ADCValue[0]);                     //Odeslani dat na CAN sbernici                                               
    }           
}
/*****************************************************************************/

//ClockInit / Inicializace hodinoveho kmitoctu proc.---------------------------
void ClockInit(void)
{

    SysCtlClockSet(SYSCTL_SYSDIV_2 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);       //Systemove hodiny deleny 2; pouzito PLL, nastaven zdroj hodin externi krystal 16.00 MHz
    
    unsigned long clock = SysCtlClockGet();                                     //Zjisteni hodnoty hod.kmit.proc.
    if(clock != 50000000) sys.flag.clock_error = true;                          // Overeni spravneho nastaveni hodinoveho kmitoctu procesoru (50 MHz]   
    else SysTickPeriodSet(clock / 12);                                          // pokud 50 MHz, nastaveni casovace (12 preruseni za vterinu - 83ms) pro zpozdeni
}


//Initialize-------------------------------------------------------------------
void Initialization(void)
{ 
    //Inicializace hodin.kmit.proc.
    ClockInit();                                                                //Vychozi nastaveni hodinoveho kmitoctu / XTAL 16.00 MHz
    
    //Inicializace komunikacnich modulu
    I2C_Init();                                                                 //Vychozi nastaveni IIC sbernice
    SPI_Init();                                                                 //Vychozi nastaveni pro SPI sbernici
    UARTInit();                                                                 //Inicializace UART modulu / 115 200 Baud, 8dat.bitu, zadna parita, jeden stop bit  
    //CANInitialize();                                                          //Pocatecni nastaveni CAN modulu a odeslani zpravy
    
    // Inicializace periferii
    LEDInit();                                                                  //Inicializace pinu pro rizeni LD, vypnut proud LD / vychozi nastaveni
    TEMPInit();                                                                 //Inicializace snimace teploty
    ADCInit();                                                                  //Inicializace pinu pro rizeni A/D prevodu
}

//ProcessRequests / Process requested actions outside ISR context---------------
void ProcessRequests(void)
{
    if (request.flag.uart_message_received) UARTProcessCmd();                   //zpracovani prichozi komunikace UART
    request.all_flags = 0;                                                      // Reset priznaku preruseni
}

//ProcessSystFlags / Process internal wakeup events-----------------------------
void ProcessSystFlags(void)
{
    if (sys.flag.clock_error)
    {
    }
    if (sys.flag.uart_message_unknown)
    {
    }
    request.all_flags = 0;                                                      // Reset request flag
}
