aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c
blob: c724e39d437e18cadbaeb7c978d4941ff51c2dfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #003388 } /* Name.Builtin */
.highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */
.highlight .no { color: #003366; font-weight: bold } /* Name.Constant */
.highlight .nd { color: #555555 } /* Name.Decorator */
.highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */
.highlight .nl { color: #336699; font-style: italic } /* Name.Label */
.highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */
.highlight .py { color: #336699; font-weight: bold } /* Name.Property */
.highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #336699 } /* Name.Variable */
.highlight .ow { color: #008800 } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */
.highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */
.highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */
.highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */
.highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */
.highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */
.highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */
.highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */
.highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */
.highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */
.highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */
.highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */
.highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */
.highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */
.highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */
.highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */
.highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */
.highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */
.highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
<UserControl x:Class="Tango.PPC.Power.Views.MainView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:vm="clr-namespace:Tango.PPC.Power.ViewModels"
             xmlns:controls="clr-namespace:Tango.SharedUI.Controls;assembly=Tango.SharedUI"
             xmlns:global="clr-namespace:Tango.PPC.Power"
             xmlns:touch="clr-namespace:Tango.Touch.Controls;assembly=Tango.Touch"
             xmlns:local="clr-namespace:Tango.PPC.Power.Views"
             mc:Ignorable="d" 
             d:DesignHeight="1280" d:DesignWidth="800" d:DataContext="{d:DesignInstance Type=vm:MainViewVM, IsDesignTimeCreatable=False}" DataContext="{x:Static global:ViewModelLocator.MainViewVM}">

    <Grid Background="{StaticResource TangoMidBackgroundBrush}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="1*"/>
        </Grid.RowDefinitions>

        <Border Padding="20" Background="{StaticResource TangoPrimaryBackgroundBrush}" BorderThickness="0 0 0 1" BorderBrush="{StaticResource TangoDividerBrush}">
            <Border.Effect>
                <DropShadowEffect Color="Silver" ShadowDepth="0" BlurRadius="20" Opacity="1" />
            </Border.Effect>
            <TextBlock VerticalAlignment="Center" FontSize="{StaticResource TangoHeaderFontSize}" FontWeight="SemiBold">Power</TextBlock>
        </Border>

        <UniformGrid Grid.Row="1" Rows="2" Margin="20 50 20 20">

            <StackPanel Margin="-90 0 0 0" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Image Source="../Images/tablet.png" Width="256" RenderOptions.BitmapScalingMode="Fant" VerticalAlignment="Top" HorizontalAlignment="Left" />

                <Grid VerticalAlignment="Top" Margin="40 0 0 0">
                    <StackPanel>
                        <controls:TableGrid RowHeight="40" MakeFirstColumnVerticalAlignmentBottom="False">
                            <TextBlock FontWeight="SemiBold">CPU:</TextBlock>
                            <TextBlock>38%</TextBlock>
                            <TextBlock FontWeight="SemiBold">RAM:</TextBlock>
                            <TextBlock>50%</TextBlock>
                            <TextBlock FontWeight="SemiBold">UWF:</TextBlock>
                            <TextBlock>Active</TextBlock>
                            <TextBlock FontWeight="SemiBold">Free Space:</TextBlock>
                            <touch:TouchProgressBar Margin="10 5 0 0" Height="10" VerticalAlignment="Top" Maximum="100" Value="60" Foreground="{StaticResource TangoSuccessBrush}" />
                        </controls:TableGrid>

                        <touch:TouchButton Height="60" Margin="0 0 0 0" CornerRadius="30" Width="250" Command="{Binding RestartCommand}">RESTART</touch:TouchButton>
                    </StackPanel>
                </Grid>
            </StackPanel>

            <Grid>
                <Rectangle VerticalAlignment="Top" HorizontalAlignment="Stretch" Margin="100 0" StrokeThickness="1" Stroke="{StaticResource TangoDividerBrush}"></Rectangle>
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
                    <Image Source="../Images/machine.png" Width="256" RenderOptions.BitmapScalingMode="Fant" VerticalAlignment="Top" HorizontalAlignment="Left" />

                    <Grid Margin="0 0 0 0" VerticalAlignment="Top">
                        <StackPanel Margin="20 0 0 0">
                            <controls:TableGrid RowHeight="40" MakeFirstColumnVerticalAlignmentBottom="False" HorizontalAlignment="Left">
                                <TextBlock FontWeight="SemiBold">Firmware Version:</TextBlock>
                                <TextBlock Text="{Binding MachineProvider.MachineOperator.DeviceInformation.Version,TargetNullValue='N/A',FallbackValue='N/A'}"></TextBlock>
                                <TextBlock FontWeight="SemiBold">Status:</TextBlock>
                                <TextBlock Text="{Binding MachineProvider.MachineOperator.Status,Converter={StaticResource EnumToDescriptionConverter}}"></TextBlock>
                            </controls:TableGrid>

                            <UniformGrid Columns="3">
                                <touch:TouchButton Style="{StaticResource TangoRoundTouchButton}" Background="{StaticResource TangoErrorBrush}" Width="90" Height="90">TURN OFF</touch:TouchButton>
                                <touch:TouchButton Margin="10 0 0 0" Style="{StaticResource TangoRoundTouchButton}" Background="{StaticResource TangoWarningBrush}" Width="90" Height="90">STAND BY</touch:TouchButton>
                                <touch:TouchButton Margin="10 0 0 0" Style="{StaticResource TangoRoundTouchButton}" Background="{StaticResource TangoSuccessBrush}" Width="90" Height="90">GET READY</touch:TouchButton>
                            </UniformGrid>
                        </StackPanel>
                    </Grid>
                </StackPanel>
            </Grid>
        </UniformGrid>
    </Grid>
</UserControl>
href='#n451'>451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
/*
 * TemperatureSensor.c
 *
 *  Created on: 15 ���� 2018
 *      Author: shlomo
 */

#include "include.h"
#include "heater.h"
#include "TemperatureSensor.h"
#include "PMR/Hardware/HardwarePidControlType.pb-c.h"
#include "Drivers/Heater/ADS1220.h"
#include "Drivers/Heater/TemperatureSensor.h"
#include "drivers/FPGA/FPGA_SPI_Comm.h"
#include "drivers/FPGA/FPGA_Comm.h"
#include "modules/control/millisecTask.h"
#include "Modules/AlarmHandling/AlarmHandling.h"
#include "PT100RTD.h"
#include <DataDef.h>

uint32_t TemperatureSensorReadFromFPGA_Res(TEMPERATURE_SENSOR_ID_ENUM SensorId);
//uint32_t TemperatureSendSensorDummyClk(TEMP_SENSE_ANALOG_DYEINGH_TEMP1);



SENSOR_CONFIG_STAGES_ENUM SensConfigStages[MAX_TEMPERATURE_SENSOR_ID] = {SENSOR_CONFIG_REG0}; // TODO remove buffer if it is the same for all the sensors

uint8_t FPGA_SensorInitConfigReg()
{
     #define Rref 1650  //ohm
     //#define Rref 825 //ohm

    uint8_t i;

    for(i=0;i<MAX_TEMPERATURE_SENSOR_ID;i++) // for noe we are using the same configuration to all of them
    {
        // - - - - - - - - - - Reg0 - - - - - - - - - -
        TempSensConfig[i].Reg0.bits.PGA_BYPASS     = 0x00 ;//Bit 0   PGA enabled (default)

        if(Rref==1650)
            TempSensConfig[i].Reg0.bits.GAIN       = 0x04;//Bits 1-3  Gain = 16
            //TempSensConfig[i].Reg0.bits.GAIN       = 0x03;//Bits 1-3  Gain = 8 michael
        else if(Rref == 825)
            TempSensConfig[i].Reg0.bits.GAIN       = 0x03;//Bits 1-3  Gain = 8

       //TempSensConfig[i].Reg0.bits.MUX            = 0x06;//Bits 4-7  AINP = AIN1, AINN = AIN0
        TempSensConfig[i].Reg0.bits.MUX            = 0x00;//Bits 4-7  AINP = AIN0, AINN = AIN1 (default) ANATOLY

        // - - - - - - - - - - Reg1 - - - - - - - - - -
        TempSensConfig[i].Reg1.bits.BCS            = 0x00;//Bit 0     Current sources off (default)
        TempSensConfig[i].Reg1.bits.TS             = 0x00;//Bit 1     Disables temperature sensor (default)
        TempSensConfig[i].Reg1.bits.CM             = 0x01;//Bit 2     Continuous conversion mode
        TempSensConfig[i].Reg1.bits.MODE           = 0x00;//Bits 3-4  Normal mode (256-kHz modulator clock, default)
        //TempSensConfig[i].Reg1.bits.DR             = 0x06;//Bits 5-7  Data rate 1000 SPS (Sample every 1 mSec)
        TempSensConfig[i].Reg1.bits.DR             = 0x00;//Bits 5-7  Data rate 20 SPS (Sample every 50 mSec)

        // - - - - - - - - - - Reg2 - - - - - - - - - -
        if(Rref==1650)
            //TempSensConfig[i].Reg2.bits.IDAC       = 0x03;//Bits 0-2  IDAC current setting 100 uA SERGEY
            TempSensConfig[i].Reg2.bits.IDAC       = 0x05;//Bits 0-2  IDAC current setting 500 uA MICHAEL
        else if(Rref == 825)
            TempSensConfig[i].Reg2.bits.IDAC       = 0x06;//Bits 0-2  IDAC current setting 1000 uA

        TempSensConfig[i].Reg2.bits.PSW            = 0x00;//Bit  3    Switch is always open (default)
        TempSensConfig[i].Reg2.bits.HZ50_60        = 0x01;//Bits 4-5  No 50-Hz or 60-Hz rejection (default)
        TempSensConfig[i].Reg2.bits.VREF           = 0x01;//Bits 6-7  Internal 2.048-V reference selected (default)

        // - - - - - - - - - - Reg3 - - - - - - - - - -
        TempSensConfig[i].Reg3.bits.Always_write_0 = 0x00;//Bit  0;
        TempSensConfig[i].Reg3.bits.DRDYM          = 0x00;//Bit  1    Only the dedicated DRDY pin is used to indicate when data are ready (default)
        TempSensConfig[i].Reg3.bits.I2MUX          = 0x04;//Bits 2-4  IDAC2 connected to AIN3/REFN1
        TempSensConfig[i].Reg3.bits.I1MUX          = 0x03;//Bits 5-7  IDAC1 connected to AIN2

    }

    return OK;
}

uint32_t TemperatureSensorsReset(void)
{
    #ifndef EVALUATION_BOARD
    uint8_t i;

        for(i=0;i<MAX_TEMPERATURE_SENSOR_ID; i++)
        {
            //Reset the device
            SPISendFPGARequest((TEMPERATURE_SENSOR_ID_ENUM)(i),ADS1220_CMD_RESET, 1);
            //Wait at least (50 us + 32 � t(CLK)) after the RESET command is sent before sending any other command.

        }
    #endif
   SysCtlDelay(100); // Must deley after hard reset
   //FPGA_SensorInitConfigReg();

    return OK;
}

uint32_t FPGA_SensorConfig_callback(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_t Stage)
{
    #ifndef EVALUATION_BOARD

    uint32_t temp = 0;
    if (SensorId > MAX_TEMPERATURE_SENSOR_ID)
        return ERROR;

    #define nn 0x00 // Send nn+1 - see datasheet page 36

    switch(Stage)
    {
        case SENSOR_CONFIG_REG0:
                //SensConfigStages[SensorId]++;
                temp = ADS1220_CMD_WREG | (ADS1220_0_REGISTER<<2) | nn;
                temp = temp << 8;
                temp |= TempSensConfig[SensorId].Reg0.Byte;
                temp = temp << 16;

                //MillisecWriteToTempSensor(SensorId, temp, 4, NULL);
            break;

         case SENSOR_CONFIG_REG1:
                 //SensConfigStages[SensorId]++;
                 temp = ADS1220_CMD_WREG | (ADS1220_1_REGISTER<<2) | nn;
                 temp = temp << 8;
                 temp |= TempSensConfig[SensorId].Reg1.Byte;
                 temp = temp << 16;

                 //MillisecWriteToTempSensor(SensorId, temp, 4, NULL);
            break;

         case SENSOR_CONFIG_REG2:
                 //SensConfigStages[SensorId]++;
                 temp = ADS1220_CMD_WREG | (ADS1220_2_REGISTER<<2) | nn;
                 temp = temp << 8;
                 temp |= TempSensConfig[SensorId].Reg2.Byte;
                 temp = temp << 16;

                 //MillisecWriteToTempSensor(SensorId, temp, 4, NULL);
            break;

         case SENSOR_CONFIG_REG3:
                // SensConfigStages[SensorId]++;
                 temp = ADS1220_CMD_WREG | (ADS1220_3_REGISTER<<2) | nn;
                 temp = temp << 8;
                 temp |= TempSensConfig[SensorId].Reg3.Byte;
                 temp = temp << 16;

                 //MillisecWriteToTempSensor(SensorId, temp, 4, NULL);
            break;
/*
         case SENSOR_CONFIG_RESTART_CONVERSIONS:
                 SensConfigStages[SensorId]++;
                 //Start or restart conversions
                 //The START/SYNC command is used to start a single conversion, or (when sent during an
                 //ongoing conversion) to reset the digital filter, and then restarts a single new conversion
                 MillisecWriteToTempSensor(SensorId, ADS1220_CMD_SYNC, 1, NULL);

               temp = ADS1220_CMD_SYNC;//ADS1220_CMD_RDATA; // TODO remove the func TemperatureSensorSync and move the sync to this case
               temp = temp << 24;

            break;
         case SENSOR_CONFIG_END:
             break;*/
         default:
             return ERROR;
    }

    SPISendFPGARequest(SensorId,temp, 2);

    #endif

    return OK;
}


int TemperatureSensorRead(TEMPERATURE_SENSOR_ID_ENUM SensorId)
{
    return TempSensorResponse[SensorId].Temperature_C_mult_by_100;
}

int TemperatureListString(char* str)
{
    int len;
    len = usnprintf(str, 150, "Prepare H1: %d H2: %d H3: %d H4: %d H5: %d H6: %d D1: %d D2: %d D3: %d M: %d",TempSensorResponse[TEMP_SENSE_ANALOG_DYEINGH_TEMP1].Temperature_C_mult_by_100/100,
                    TempSensorResponse[TEMP_SENSE_ANALOG_DYEINGH_TEMP2].Temperature_C_mult_by_100/100,TempSensorResponse[TEMP_SENSE_ANALOG_DYEINGH_TEMP3].Temperature_C_mult_by_100/100,
                    TempSensorResponse[TEMP_SENSE_ANALOG_DYEINGH_TEMP4].Temperature_C_mult_by_100/100,TempSensorResponse[TEMP_SENSE_ANALOG_DYEINGH_TEMP5].Temperature_C_mult_by_100/100,
                    TempSensorResponse[TEMP_SENSE_ANALOG_MIXCHIP_TEMP].Temperature_C_mult_by_100/100,TempSensorResponse[TEMP_SENSE_ANALOG_DRYER_TEMP1].Temperature_C_mult_by_100/100,
                    TempSensorResponse[TEMP_SENSE_ANALOG_DRYER_TEMP2].Temperature_C_mult_by_100/100,TempSensorResponse[TEMP_SENSE_ANALOG_DRYER_TEMP3].Temperature_C_mult_by_100/100
                    ,TempSensorResponse[TEMP_SENSE_AN_ENCLOSURETEMP3].Temperature_C_mult_by_100/100);
    return len;
}



uint32_t Filter_Temparature_Measurement(TEMPERATURE_SENSOR_ID_ENUM SensorId, int32_t Current_Measurement_C, uint32_t NoOfAvrSamples)
{
    #define MaxAvrSamples 10 //MAX of average samples

    uint32_t Filtered_Measurement;

    static int32_t Sample_buf[MAX_TEMPERATURE_SENSOR_ID][MaxAvrSamples] = {0};

    static uint8_t SampleIndex[MAX_TEMPERATURE_SENSOR_ID] = {0};

    uint8_t i;
    uint32_t Status = OK;

    if (NoOfAvrSamples > MaxAvrSamples)
        NoOfAvrSamples = MaxAvrSamples;
    #ifdef TEMPERATURE_MEASUREMENT_HiGH_LIMIT
        if (Current_Measurement_C > MAX_TEMPERATURE_MEASUREMENT)
        {
            Current_Measurement_C  = MAX_TEMPERATURE_MEASUREMENT;
            Status =  ERROR;
        }
    #endif

    #ifdef TEMPERATURE_MEASUREMENT_LOW_LIMIT
        if (Current_Measurement_C < MIN_TEMPERATURE_MEASUREMENT)
        {

            Current_Measurement_C  = MAX_TEMPERATURE_MEASUREMENT;
            Status =  ERROR;
        }
    #endif

    Sample_buf[SensorId][SampleIndex[SensorId]] = Current_Measurement_C;

    SampleIndex[SensorId] = SampleIndex[SensorId]++;

    if(SampleIndex[SensorId] > (NoOfAvrSamples - 1))
        SampleIndex[SensorId] = 0;

    int32_t TempSum = 0;

    for(i=0; i<NoOfAvrSamples; i++)
    {
        TempSum += Sample_buf[SensorId][i];
    }

    Filtered_Measurement = TempSum / NoOfAvrSamples;

    TempSensorResponse[SensorId].Temperature_C_mult_by_100 = Filtered_Measurement;

    return Status;
}


uint32_t Filter_Error_Temparature_Measurements(TEMPERATURE_SENSOR_ID_ENUM SensorId, int32_t Current_Measurement_C)
{
    static uint8_t counter[MAX_TEMPERATURE_SENSOR_ID];

    uint32_t Status = OK;

    if (Current_Measurement_C >= ERROR_TEMPERATURE_MEASUREMENT)
    {
        if(counter[SensorId]==MAX_ERR_SAMPLES)
        {
            Status =  ERROR;
        }
        else
        {
            counter[SensorId]+=1;
            Status =  WARNING;
        }
    }
    else
    {
        counter[SensorId] = 0;
    }

    return Status;
}

uint32_t TemperatureSensorSync(TEMPERATURE_SENSOR_ID_ENUM SensorId)
{
    uint32_t Status = OK;

    // send the command byte
    //SPISendFPGARequest( SensorId,ADS1220_CMD_RDATA, 4);

    //MillisecReadFromTempSensor(SensorId, ADS1220_CMD_RDATA, 4, TemperatureSensorReadFromFPGA_Res);

    ///////////

    uint32_t temp = 0;

    if(SensorId > MAX_TEMPERATURE_SENSOR_ID)
        Status = ERROR;

    temp = ADS1220_CMD_SYNC;//ADS1220_CMD_RDATA;
    temp = temp << 24;
#ifndef EVALUATION_BOARD

    SPISendFPGARequest(SensorId,temp, 2);
    //////////
    //MillisecReadFromTempSensor(SensorId, ADS1220_CMD_RDATA, 4, TemperatureSensorReadFromFPGA_Res);
#endif

    return Status;
}

uint32_t TemperatureSendSensorDummyClk(TEMPERATURE_SENSOR_ID_ENUM SensorId)
{
    uint32_t Status = OK;

    uint32_t temp = 0;

    if(SensorId > MAX_TEMPERATURE_SENSOR_ID)
        Status = ERROR;

    temp = 0x10FFFFFF ;
#ifndef EVALUATION_BOARD

    SPISendFPGARequest(SensorId,temp, 4);
#endif
    return Status;
}





uint32_t Calc_Resistance(TEMPERATURE_SENSOR_ID_ENUM SensorId, unsigned int Current_Measurement)
{

    uint32_t Resistance_100;

    unsigned int Factor_2Rref_E_H;
    unsigned int Factor_2Rref_E_L;

    //Resistance = Measurement / (2^23) *2 * Rref / Gain

    Factor_2Rref_E_H = 200 * Rref; // 200 to calculate Resistance*100

    Factor_2Rref_E_L = 8388608 * (2<<(TempSensConfig[SensorId].Reg0.bits.GAIN -1));//2^23*Gain

    Resistance_100 = (float)Current_Measurement  / (float)Factor_2Rref_E_L * (float)Factor_2Rref_E_H;

    return Resistance_100;
}

//int32_t RTD (uint32_t Resistance_mult_by_100) //Resistance * 100
//// find the temperature using  Lookup table
//{
//    int16_t i = 0;
//    int32_t Temperature_c;
//
//    const int PT100_TABLE_MAXIDX = ((sizeof(Pt100_table) / sizeof(Pt100_table[0])) - 1) ;
//
//    while( (Resistance_mult_by_100 < Pt100_table[i]) && (i < PT100_TABLE_MAXIDX) )
//    {
//        i++;
//    }
//
//    Temperature_c = i-200;
//
//    return Temperature_c;
//}
//uint32_t timeconsumption[200];
//uint32_t tcounter=0;
int32_t RTD(uint32_t Resistance_mult_by_100) //Resistance * 100
// find the temperature using Lookup table
{
    int16_t i = 0,j;
    int32_t Temperature_C_mult_by_100;
    int32_t temp;

    const int PT100_TABLE_MAXIDX = ((sizeof(Pt100_table) / sizeof(Pt100_table[0])) - 1); // 0..PT100_TABLE_MAXIDX
//    timeconsumption[tcounter] = HibernateRTCSSGet();
    for (j = 0;j < 15;j++)
    {
        if (Resistance_mult_by_100 >= Pt100_table[Pt100_short_table[j]])
        {
            i =Pt100_short_table[j];
        }
        else
            break;
    }
    while ((Resistance_mult_by_100 > Pt100_table[i]) && (i < PT100_TABLE_MAXIDX ))
    {
        if (Resistance_mult_by_100 < Pt100_table[i + 1])
        {
            temp = (i * 100) + ((Resistance_mult_by_100 - Pt100_table[i]) * 100 / (Pt100_table[i + 1] - Pt100_table[i]));
        }
        i++;
    }

    if (Resistance_mult_by_100 >= Pt100_table[PT100_TABLE_MAXIDX])
        return PT100_TABLE_MAXIDX - 200;
    else if (Resistance_mult_by_100 <= Pt100_table[0])
        return - 200;
    else if (Resistance_mult_by_100 == Pt100_table[i])
    {
        temp = (i * 100); // for Temperature * 100
    }


    Temperature_C_mult_by_100 = temp - 20000; // The lookup table start from 200 deg (20000 for Temperature * 100)
//    timeconsumption[tcounter] = HibernateRTCSSGet()-timeconsumption[tcounter];
//    if (++tcounter>99) tcounter=0;

    return Temperature_C_mult_by_100;
}

uint32_t CalculateTemperatures(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint32_t Data)
{
    int32_t Current_Measurement_C = 0;

    uint32_t Resistance_mult_by_100;
    if (Data & 0x800000)
        Data |= 0xff000000;

    Resistance_mult_by_100 = Calc_Resistance(SensorId, Data); // 10875 ohm for 22.46 deg

    Current_Measurement_C = RTD (Resistance_mult_by_100);

    #ifdef WithTempSensorFiltrer
        uint32_t NoOfAvrSamples = 5;//TODO - how many Samples
        Status = Filter_Temparature_Measurement(SensorId, Current_Measurement_C, NoOfAvrSamples);
    #else
        TempSensorResponse[SensorId].Temperature_C_mult_by_100 = Current_Measurement_C;
    #endif
return Current_Measurement_C;
}
uint32_t TemperatureSensorReadFromFPGA_Res(TEMPERATURE_SENSOR_ID_ENUM SensorId)
{
    uint32_t Status = OK;

    uint32_t Data = 0;

    //uint32_t Data;
    //TemperatureSendSensorDummyClk(SensorId);//TODO check if it is ok to add it here without deley !!!!!!!
#ifndef EVALUATION_BOARD
    SPIGetFPGAResponse(SensorId, &Data);
#endif
//    INT2CHAR Int2Char; // TODO to open and test to limit to 24 bit (It should be read 3 low bytes only)
//    Int2Char.uint = Data;
//    Int2Char.uchar.UCHAR_3 = 0;
//    Data = Int2Char.uint;

    // sign extend data //TODO ?
    //AlarmHandling_CalculateTemperatures(SensorId,Data);
    CalculateTemperatures(SensorId,Data);
    return Status;
}


//-------------------------------------------------------------------------------------------------

uint32_t Read_Reg[MAX_TEMPERATURE_SENSOR_ID][4];


uint32_t TemperatureSensorReadRegFromFPGA_Res(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint8_t Reg)
{
    SHORT2CHAR Short2Char;

    uint32_t Status = OK;

    uint32_t Data;
#ifndef EVALUATION_BOARD
    SPIGetFPGAResponse(SensorId, &Data);
#endif
    Short2Char.uint = Data;
    Read_Reg[SensorId][Reg] = Short2Char.uchar.LSB;

    return Status;
}

uint32_t TemperatureSensorReadRegFromFPGA(TEMPERATURE_SENSOR_ID_ENUM SensorId, uint8_t Reg)
{
    uint32_t Status = OK;

    uint32_t temp = 0;

    if(Reg > ADS1220_3_REGISTER)
        Status = ERROR;

    temp = ADS1220_CMD_RREG | ((Reg<<2) );
    temp = temp << 24;
#ifndef EVALUATION_BOARD

    SPISendFPGARequest(SensorId,temp, 2);
    //MillisecReadFromTempSensor(SensorId, temp, 4, TemperatureSensorReadFromFPGA_Res);

    SysCtlDelay(3000);
#endif
    TemperatureSensorReadRegFromFPGA_Res( SensorId,  Reg) ;


    return Status;
}

void FPGA_SensorInitConfig()
{
    int Sensor=0,Register = 0;

    FPGA_SensorInitConfigReg(); // Initial the configuration registers

    for(Register=0;Register<=SENSOR_CONFIG_REG3;Register++)
    {
        for(Sensor=0;Sensor<MAX_TEMPERATURE_SENSOR_ID;Sensor++)
        {
            FPGA_SensorConfig_callback( (TEMPERATURE_SENSOR_ID_ENUM)(Sensor), Register);
        }
        SysCtlDelay(1000);
    }
}

//#define TEMP_SENSE_ANALOG TEMP_SENSE_ANALOG_DYEINGH_TEMP5

void Debug_Start_PT00(TEMPERATURE_SENSOR_ID_ENUM TEMP_SENSE_ANALOG)
{

    TemperatureSensorSync(TEMP_SENSE_ANALOG);
    SysCtlDelay(500);//5000 A

    //uint32_t Data = 0;

    while(1)
    {
        TemperatureSendSensorDummyClk(TEMP_SENSE_ANALOG_DYEINGH_TEMP1);
        SysCtlDelay(5000);
        TemperatureSensorReadFromFPGA_Res(TEMP_SENSE_ANALOG_DYEINGH_TEMP1);
        SysCtlDelay(100000);
    }

    //TemperatureSensorRead(TEMP_SENSE_ANALOG_DYEINGH_TEMP1);

    //The temparature will be at:
    //TempSensorResponse[TEMP_SENSE_ANALOG_DYEINGH_TEMP1].Temperature_C_mult_by_100

    //return;
}

//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------