aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/Heater/TemperatureSensor.c
blob: 0b5d8ebc2846b8a7020a09cca730c69c870977e7 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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 */
/* Generated by the protocol buffer compiler.  DO NOT EDIT! */
/* Generated from: MessageContainer.proto */

/* Do not generate deprecated warnings for self */
#ifndef PROTOBUF_C__NO_DEPRECATED
#define PROTOBUF_C__NO_DEPRECATED
#endif

#include "MessageContainer.pb-c.h"
void   message_container__init
                     (MessageContainer         *message)
{
  static const MessageContainer init_value = MESSAGE_CONTAINER__INIT;
  *message = init_value;
}
size_t message_container__get_packed_size
                     (const MessageContainer *message)
{
  assert(message->base.descriptor == &message_container__descriptor);
  return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
}
size_t message_container__pack
                     (const MessageContainer *message,
                      uint8_t       *out)
{
  assert(message->base.descriptor == &message_container__descriptor);
  return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
}
size_t message_container__pack_to_buffer
                     (const MessageContainer *message,
                      ProtobufCBuffer *buffer)
{
  assert(message->base.descriptor == &message_container__descriptor);
  return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
}
MessageContainer *
       message_container__unpack
                     (ProtobufCAllocator  *allocator,
                      size_t               len,
                      const uint8_t       *data)
{
  return (MessageContainer *)
     protobuf_c_message_unpack (&message_container__descriptor,
                                allocator, len, data);
}
void   message_container__free_unpacked
                     (MessageContainer *message,
                      ProtobufCAllocator *allocator)
{
  if(!message)
    return;
  assert(message->base.descriptor == &message_container__descriptor);
  protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
}
static const ProtobufCFieldDescriptor message_container__field_descriptors[7] =
{
  {
    "Type",
    1,
    PROTOBUF_C_LABEL_OPTIONAL,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 */
/*
 * 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/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>
#include "drivers/Heater/TemperatureSensor.h"
#include "Drivers/I2C_Communication/Head_Card/PT100/Head_PT100_ADC.h"

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

SENSOR_CONFIG_STAGES_ENUM SensConfigStages[MAX_MAIN_CARD_TEMP_SENS_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_MAIN_CARD_TEMP_SENS_ID;i++) // for now 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_MAIN_CARD_TEMP_SENS_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_MAIN_CARD_TEMP_SENS_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;
    if(Head_Type == HEAD_TYPE_SYLKO)
    {
        len = usnprintf(str, 160, "Prepare H1: ,%d, H2: ,%d, H3: ,%d, H4: ,%d, H5: ,%d, H6: ,%d, H7: ,%d, H8: ,%d, H9: ,%d, H10: ,%d, H11: ,%d, H12: ,%d,  M: ,%d, D: ,%d,",
                        TempSensorResponse[HEAD_PT100_ZONE_1_0X80_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_ZONE_2_0X80_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_ZONE_3_0X82_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_ZONE_4_0X82_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_ZONE_5_0X84_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_ZONE_6_0X84_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_ZONE_7_0X86_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_ZONE_8_0X86_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_ZONE_9_0X88_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_ZONE_10_0X88_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_ZONE_11_0X8A_0].Temperature_C_mult_by_100/100,TempSensorResponse[HEAD_PT100_ZONE_12_0X8A_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_MIXER_0X8E_0].Temperature_C_mult_by_100/100,  TempSensorResponse[TEMP_SENSE_ANALOG_DRYER_TEMP1].Temperature_C_mult_by_100/100);

    }
    else if(Head_Type == HEAD_TYPE_SYLKO_WITHOUT_CARD)
    {
        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[HEAD6_PT100].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[MIXER_PT100].Temperature_C_mult_by_100/100);
        //Prepare H1: 78 H2: 111 H3: 137 H4: 135 H5: 144 H6: 75 D1: 120 D2: 167 D3: 158 M: 129
    }
    if(Head_Type == HEAD_TYPE_STAPLE_SPUN)
    {
        len = usnprintf(str, 160, "Prepare H1: ,%d, H2: ,%d, H3: ,%d, AL1:  AR1: M: ,%d, D: ,%d,",
                        TempSensorResponse[HEAD_PT100_ZONE_1_0X80_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_ZONE_2_0X80_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_ZONE_3_0X82_0].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_AIR_HEATER_1_0X8C_0].Temperature_C_mult_by_100/100, TempSensorResponse[HEAD_PT100_AIR_HEATER_2_0X8C_1].Temperature_C_mult_by_100/100,
                        TempSensorResponse[HEAD_PT100_MIXER_0X8E_0].Temperature_C_mult_by_100/100,  TempSensorResponse[TEMP_SENSE_ANALOG_DRYER_TEMP1].Temperature_C_mult_by_100/100);
    }
    else
    {
        len = usnprintf(str, 150, "Unknown head type");
    }

    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_MAIN_CARD_TEMP_SENS_ID][MaxAvrSamples] = {0};

    static uint8_t SampleIndex[MAX_MAIN_CARD_TEMP_SENS_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_MAIN_CARD_TEMP_SENS_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_MAIN_CARD_TEMP_SENS_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_MAIN_CARD_TEMP_SENS_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(SensorId < MAX_MAIN_CARD_TEMP_SENS_ID)
  {
        if (Data & 0x800000)
            Data |= 0xff000000;

        Resistance_mult_by_100 = Calc_Resistance(SensorId, Data); // 10875 ohm for 22.46 deg
  }
  else if(SensorId < MAX_HEAD_CARD_TEMP_SENS_ID)
  {
      Resistance_mult_by_100 = HeadADCPT100_Calc_Resistance(SensorId, Data);
  }


    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_MAIN_CARD_TEMP_SENS_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_MAIN_CARD_TEMP_SENS_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;
}


uint32_t Test_ADS1220_Internal_Temperature_Sensor(TEMPERATURE_SENSOR_ID_ENUM SensorId)
{
    uint32_t Data = 0;

    SysCtlDelay(5000);
    TempSensConfig[SensorId].Reg1.bits.TS             = 0x01;//Bit 1     Enables the Internal temperature sensor
    FPGA_SensorConfig_callback(SensorId, SENSOR_CONFIG_REG1) ;
    SysCtlDelay(5000);
/*    FPGA_SensorConfig_callback(SensorId, SENSOR_CONFIG_REG2) ;
    SysCtlDelay(5000);
    FPGA_SensorConfig_callback(SensorId, SENSOR_CONFIG_REG3) ;
    SysCtlDelay(5000);*/

    TemperatureSensorSync(SensorId);
    SysCtlDelay(3000);
    TemperatureSendSensorDummyClk(SensorId);
    SysCtlDelay(4000);

    SPIGetFPGAResponse(SensorId, &Data);

    Data = Data & 0xFFFFFF;//24bit

    Data = Data >> 10;//need the 14 left bits  // According to ADS1220 data sheet page 31 8.3.13

    Data*=0.03125;// According to ADS1220 data sheet page 31 8.3.13

//    SysCtlDelay(5000);
//    TempSensConfig[SensorId].Reg1.bits.TS             = 0x00;//Bit 1     Disables the Internal temperature sensor
//    FPGA_SensorConfig_callback(SensorId, SENSOR_CONFIG_REG1) ;
//    SysCtlDelay(5000);


    return Data;//Temperature_C

}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------