aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/FPGA/FPGA.c
blob: 62d283f0baec3f7d0168fb0093fbaf3dc6249927 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
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!
// source: StubValveRequest.proto

#ifndef PROTOBUF_StubValveRequest_2eproto__INCLUDED
#define PROTOBUF_StubValveRequest_2eproto__INCLUDED

#include <string>

#include <google/protobuf/stubs/common.h>

#if GOOGLE_PROTOBUF_VERSION < 3004000
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please update
#error your headers.
#endif
#if 3004000 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers.  Please
#error regenerate this file with a newer version of protoc.
#endif

#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_table_driven.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/metadata.h>
#include <google/protobuf/message.h>
#include <google/protobuf/repeated_field.h>  // IWYU pragma: export
#include <google/protobuf/extension_set.h>  // IWYU pragma: export
#include <google/protobuf/unknown_field_set.h>
// @@protoc_insertion_point(includes)
namespace Tango {
namespace PMR {
namespace Stubs {
class StubValveRequest;
class StubValveRequestDefaultTypeInternal;
extern StubValveRequestDefaultTypeInternal _StubValveRequest_default_instance_;
}  // namespace Stubs
}  // namespace PMR
}  
#include "include.h"
#include <stdbool.h>
#include <stdlib.h>
#include <stdint.h>

#include "driverlib/epi.h"
#include "inc/hw_memmap.h"
#include <driverlib/gpio.h>
#include "drivers/FPGA/FPGA_GPIO/FPGA_GPIO.h"

#include "FPGA_Rename.h"
#include "FPGA_COMM.h"
#include "FPGA.h"

#include "driverlib/sysctl.h" //for SysCtlDelay
#include <driverlib/sysctl.h>
#include <DataDef.h>


unsigned short GPO_01_Reg;

extern F3_GPO_01_REG F3_GPO_01_Reg;

int FPGA_Test()
{


    F3_GPO_01_Reg.bits.F3_LUBRICANT_VALVE = 1;
    F3_GPO_01_Reg.bits.F3_GPO_LED3 = 1;
    F3_GPO_01_Reg.bits.F3_GPO_LED2 = 0;
    F3_GPO_01_Reg.bits.F3_GPO_LED1 = 1;
    F3_GPO_01_Reg.bits.F3_GPO_EXTWINDER_SSR11_CTRL = 1;
    F3_GPO_01_Reg.bits.F3_GPO_BUZZER = 0;
    F3_GPO_01_Reg.bits.F3_SPARE2_ROTENC_CLK = 1;
    F3_GPO_01_Reg.bits.F3_SPARE1_ROTENC_CLK = 1;
    F3_GPO_01_Reg.bits.RESERVE = 0xF5;

    GPO_01_Reg = F3_GPO_01_Reg.ushort;

    return 0;
}

int FPGA_Test_ReadBack(unsigned char FPGA_NUM, unsigned short Value, unsigned short *ReadBackValue)// = 0x1234)
{

    //TODO to update the deley
    unsigned short readValue = 0;
     if(FPGA_NUM == 1)
     {
         F1_Test = Value;
         SysCtlDelay(1000);
         readValue = F1_Test;
         *ReadBackValue = readValue;
         if(Value == (uint16_t) ~((unsigned int) readValue))
             return PASSED;
         if((Value == 0xFFFF) && (readValue == 0))
             return PASSED;
     }

     if(FPGA_NUM == 2)
     {
         F2_Test = Value;
         SysCtlDelay(100);
         readValue = F2_Test;
         *ReadBackValue = readValue;
         if(Value == (uint16_t) ~((unsigned int) readValue))
             return PASSED;
         if((Value == 0xFFFF) && (readValue == 0))
             return PASSED;
     }

     if(FPGA_NUM == 3)
     {
         F3_Test = Value;
         SysCtlDelay(100);
         readValue = F3_Test;
         *ReadBackValue = readValue;
         if(Value == (uint16_t) ~((unsigned int) readValue))
             return PASSED;
         if((Value == 0xFFFF) && (readValue == 0))
             return PASSED;
     }

    return FAILED;
}


int  FPGA_ReadVersion(unsigned char FPGA_NUM, unsigned char *Version, unsigned char *Year, unsigned char *Month, unsigned char *Day)
{
    VER1 Ver1;
    VER2 Ver2;
#ifndef EVALUATION_BOARD

    switch(FPGA_NUM)
    {
        case 1:
                Ver1.ushort = F1_Ver1_D;
                Ver2.ushort = F1_Ver2_D;
            break;
        case 2:
                Ver1.ushort = F2_Ver1_D;
                Ver2.ushort = F2_Ver2_D;
            break;
        case 3:
                Ver1.ushort = F3_Ver1_D;
                Ver2.ushort = F3_Ver2_D;
            break;
        default:
            break;
    }

 /*   if( (Ver1.bytes.Month > 12) || (Ver1.bytes.Day > 31) || (Ver2.bytes.Year < 17) )
    {
        return FAILED;
    }
*/

    *Month = Ver1.bytes.Month;
    *Day = Ver1.bytes.Day;
    *Year = Ver2.bytes.Year;  // to check how many digits is needed
    *Version = Ver2.bytes.Ver_num;
#endif
    return PASSED;
}

void FPGA_Init()
{

#ifndef EVALUATION_BOARD
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_6); // start FPGA1 (clear HW RESET)
    ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_6, 0);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_6); // start FPGA2 (clear HW RESET)
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_6, 0);
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTH_BASE, GPIO_PIN_4); // start FPGA3 (clear HW RESET)
    ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_4, 0);
    delayms(1);
    ROM_GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_6, GPIO_PIN_6);
    ROM_GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_6, GPIO_PIN_6);
    ROM_GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_4, GPIO_PIN_4);

    // Enable EPI
    SysCtlPeripheralEnable(SYSCTL_PERIPH_EPI0);
    while (!(SysCtlPeripheralReady(SYSCTL_PERIPH_EPI0)));

    //PreScale + PWM
    //60MHz / PreScaler / (PWM High + PWM Low)
    //for SPI Motor driver the maximum is 5MHz the default is the FPGA is 4 (60/2/(2+3)) = 4

    //EPI Host-Bus 8 Configuration 3 (EPIHB8CFG3)
    //    Value Description
    //    0x0 Active WRn is 2 EPI clocks
    //    0x1 Active WRn is 4 EPI clocks <--
    //    0x2 Active WRn is 6 EPI clocks
    //    0x3 Active WRn is 8 EPI clocks

//    EPIModeSet(EPI0_BASE, EPI_MODE_HB16);
//    EPIConfigHB16Set(EPI0_BASE, EPI_HB16_MODE_ADMUX | EPI_HB16_WRWAIT_1 | EPI_HB16_RDWAIT_1 | EPI_HB16_ALE_LOW | EPI_HB16_WORD_ACCESS, 0);
//    EPIConfigHB16Set(EPI0_BASE, EPI_HB16_WRWAIT_1 | EPI_HB16_RDWAIT_1, 0);

    EPIModeSet(EPI0_BASE, EPI_MODE_GENERAL);
    //uint32_t temp = *(uint32_t)(EPI0_BASE+0x10);

//    volatile uint32_t *temp;
//    temp = (volatile uint32_t *) (EPI0_BASE+0x10);

    // Set EPI Mode
    EPIConfigGPModeSet(EPI0_BASE, EPI_GPMODE_CLKPIN |EPI_GPMODE_FRAME50 | EPI_GPMODE_ASIZE_12 | EPI_GPMODE_DSIZE_16, 4, 0);
    //Set EPI CLK
    EPIDividerSet(EPI0_BASE, 10);//60MHz

    EPIAddressMapSet(EPI0_BASE, EPI_ADDR_RAM_BASE_6  | EPI_ADDR_RAM_SIZE_64KB);

//    *temp |= 0x50;


    //FPGA_WRITE();
    //1. Disable all FPGA's WD:
    Control_WD(DISABLE,0);
#endif

}


//---------------------------------- Limit_Switches ------------------------------------------------

//Limit_Switch1_REG   Limit_Switch1;
//Limit_Switch2_REG   Limit_Switch2;
//Limit_Switch3_REG   Limit_Switch3;
//
//void FPGA_Read_limit_Switches()
//{
//    Limit_Switch1.ushort = F1_GPI_LS1_D;
//    Limit_Switch2.ushort = F1_GPI_LS2_D;
//    Limit_Switch3.ushort = F1_GPI_LS3_D;
//}

uint32_t Calculate_Tacho_Fan_Speed(uint32_t OSC_IN, uint8_t PPR, uint16_t Prescaler, uint16_t Tacho_reg) //Oscilator IN , Pulse/Round
{
    uint32_t   temp, Fan_Speed_RPM;

    temp = 60 / PPR;//60 Sec
    temp = temp   * OSC_IN;
    temp = temp / Tacho_reg;
    Fan_Speed_RPM = (temp /  Prescaler);
    return Fan_Speed_RPM;

}

uint32_t Fans_Speed_RPM[MAX_FANS];


uint8_t Read_Fans_Tacho()
{
    FANS_STATUS Fans_Status;

#ifndef EVALUATION_BOARD

    // The big Fan in the drawer
    Fans_Speed_RPM[DRAWER_B]  = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg0);
    Fans_Speed_RPM[DRAWER_S0] = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg1);
    Fans_Speed_RPM[DRAWER_S1] = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg2);
    Fans_Speed_RPM[DRAWER_S2] = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg3);
    Fans_Speed_RPM[DRAWER_S3] = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg4);
    Fans_Speed_RPM[SYSTEM_0]  = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg5);
    Fans_Speed_RPM[SYSTEM_1]  = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg6);
    Fans_Speed_RPM[SYSTEM_2]  = Calculate_Tacho_Fan_Speed(FPGA_Freq, 0X02, F1_Prescaler1_reg5, F1_Tacho_reg7);

    if( Fans_Speed_RPM[DRAWER_B] < 1000 )  // need to work around 3000 RPM
        Fans_Status.bit.DRAWER_BIG = ERROR;// not working / Low Speed
    else
        Fans_Status.bit.DRAWER_BIG = OK;//working (Speed ~0x400)

    // The 4 small Fans in the drawer
    // F1_gpi_FANS
    // �0� Fan not working
    // �1� Fan working
    if (F1_gpi_FANS == 0x0F )
    {
        Fans_Status.bit.DRAWER_SMALL0 = OK;//working
        Fans_Status.bit.DRAWER_SMALL1 = OK;//working
        Fans_Status.bit.DRAWER_SMALL2 = OK;//working
        Fans_Status.bit.DRAWER_SMALL3 = OK;//working
    }
    else
    {
        bool F1_FAN1_TACH =  F1_gpi_FANS & 0x01;
        bool F1_FAN2_TACH = (F1_gpi_FANS & 0x02)>>0x01;
        bool F1_FAN3_TACH = (F1_gpi_FANS & 0x04)>>0x02;
        bool F1_FAN4_TACH = (F1_gpi_FANS & 0x08)>>0x03;

        if(( Fans_Speed_RPM[DRAWER_S0] < 3000 ) && ( F1_FAN1_TACH == 0)) // need to work around  5000 RPM
        {
            Fans_Status.bit.DRAWER_SMALL0 = ERROR;/// not working / Low Speed
        }
        else
        {
            Fans_Status.bit.DRAWER_SMALL0 = OK;//working (Speed ~0x400)
        }

        if(( Fans_Speed_RPM[DRAWER_S1] < 3000 ) && ( F1_FAN2_TACH == 0)) // Small Fan in the drawer // need to work around 5000 RPM
        {
            Fans_Status.bit.DRAWER_SMALL1 = ERROR;// not working / Low Speed
        }
        else
        {
            Fans_Status.bit.DRAWER_SMALL1 = OK;//working (Speed ~0x400)
        }
        if(( Fans_Speed_RPM[DRAWER_S2] < 3000 ) && ( F1_FAN3_TACH == 0)) // Small Fan in the drawer // need to work around 5000 RPM
        {
            Fans_Status.bit.DRAWER_SMALL2 = ERROR;// not working / Low Speed
        }
        else
        {
            Fans_Status.bit.DRAWER_SMALL2 = OK;//working (Speed ~0x400)
        }
        if(( Fans_Speed_RPM[DRAWER_S3] < 3000 ) && ( F1_FAN4_TACH == 0)) // Small Fan in the drawer // need to work around 3050 RPM
        {
            Fans_Status.bit.DRAWER_SMALL3 = ERROR;// not working / Low Speed
        }
        else
        {
            Fans_Status.bit.DRAWER_SMALL3 = OK;//working (Speed ~0x400) < 1000 )  // need to work around 5000 RPM
        }

    }

    if( Fans_Speed_RPM[SYSTEM_0] < 1000 )  // need to work around  3050 RPM
    {
        Fans_Status.bit.SYSTEM_FAN0 = ERROR;// not working / Low Speed
    }
    else
    {
        Fans_Status.bit.SYSTEM_FAN0 = OK;//working (Speed ~0x400)
    }

    if( Fans_Speed_RPM[SYSTEM_1] < 1000 )  // need to work around 3050 RPM
    {
        Fans_Status.bit.SYSTEM_FAN1 = ERROR;// not working / Low Speed
    }
    else
    {
        Fans_Status.bit.SYSTEM_FAN1 = OK;//working (Speed ~0x400)
    }

    if( Fans_Speed_RPM[SYSTEM_2] < 1000 )  // need to work around 3050 RPM
    {
        Fans_Status.bit.SYSTEM_FAN2 = ERROR;// not working / Low Speed
    }
    else
    {
        Fans_Status.bit.SYSTEM_FAN2 = OK;//working (Speed ~0x400)
    }

#endif

    return Fans_Status.Uchar;
}

//------------------------- WHS ----------------------
//uint32_t WHS_Read_Blower_Tach()
//{
//    //TODO: check if we need to change the Prescaler
//    //F2_Prescaler1_reg10  - prescaled clocks for counter of signal Blower Tacho. 8 bits
//
//
//    /*
//    RPM=60* (Sys_clk/PreScalar)/ F2_Tacho_reg0
//
//    Where :
//
//    Sys_clk                                =25*10^6 (25Mhz)
//    PresScalar(default)           =250
//
//    Mati
//     */
//    uint32_t RPM;
//    uint32_t Temp = 6000000; // 60* (Sys_clk/PreScalar)
//    RPM = Temp / GPI_BLOWER_TACH;
//
//    return RPM;
//}

//------------------------- Dryer Blower ----------------------
/*
uint32_t Dryer_Read_Blower_Tach()
{
    uint32_t RPM;
    //TBD
    RPM =  WHS_Read_Blower_Tach(); // Temporary using WHS Tacho
    return RPM;
}
*/
uint32_t Drayer_Fan_Speed_RPM = 0;

uint32_t Get_Dryer_Fan_Tacho()
{
    return Drayer_Fan_Speed_RPM;
}
uint32_t Read_Dryer_Fan_Tacho()
{
#ifndef EVALUATION_BOARD
    Drayer_Fan_Speed_RPM = Calculate_Tacho_Fan_Speed(FPGA_Freq, 12, F1_Prescaler1_reg5, F1_Tacho_reg8);
    return Drayer_Fan_Speed_RPM;
#else
    return 100;
#endif
}

void Control_Dryer_Fan_PWM(uint8_t PWM_Command_Precent)// 0 - 100%
{
    // change to cycle to 100 in order to work with %, with constant FREQ

    uint8_t Freq = 0xFF;//divider Clock = 25M/divider

    if(PWM_Command_Precent > 100)
        PWM_Command_Precent = 100;
#ifndef EVALUATION_BOARD
    GPO_BLOWER_PWM_FREQ = Freq;
    GPO_BLOWER_PWM_LOW  = PWM_Command_Precent + 1;
    GPO_BLOWER_PWM_HIGH = 101 - PWM_Command_Precent;
#endif
    // low + high = 0xFF in order use the same freq (and change the freq only by Add 0x112).
    // there is option to change only the high (low + freq constasnt) this will chnga the freq
}

void Machine_Idle_Breathing_Led() //if (Ten_msTick)
{
    static uint8_t PWM_Command_Precent = 0;// 0 - 100%
    static uint8_t  direction = UP;

    F3_low_var_LED1  = PWM_Command_Precent + 1;
    F3_high_var_LED1 = MAX_PWM_Command + 1 - PWM_Command_Precent;

    if(direction == UP)
    {
        if (PWM_Command_Precent ==  MAX_PWM_Command)
        {
            direction = DOWN; //"0"
        }
        else
        {
            PWM_Command_Precent++;
        }
    }
    else
    if(direction == DOWN)
    {
        if (PWM_Command_Precent ==  0)
        {
            direction = UP;//"1"
        }
        else
        {
            PWM_Command_Precent--;
        }
    }
}

///////////////////////////////////////// Speed_Sensor_TypeII ////////////////////////////////////////////////
void Set_Speed_Sensor_TypeII_Registers(uint32_t Counter, uint32_t Prescaler)
{
    /* Counter  -   Increase the number to higher resolution *
     * Prescaler  - Deccrease the number to higher resolution*/
    F1_Prescaler1_reg6 = Prescaler;
    F1_gpo_cnt_A_reg   = Counter;
}

uint32_t Read_Speed_Sensor_TypeII()//must be delay between Set_Speed_Sensor_TypeII_Registers to Read_Speed_Sensor_TypeII
{

    uint32_t Speed_Hz = 0, temp, temp1;
#ifndef EVALUATION_BOARD

    temp = F1_Tacho_reg9;
    temp1 = FPGA_Freq;
    temp1*=F1_gpo_cnt_A_reg;
    temp1/=temp;
    temp1/=F1_Prescaler1_reg6;
    Speed_Hz = temp1;
#endif

    return Speed_Hz;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////


typedef union
{
    struct
    {
        uint16_t M:10;//0..10
        uint8_t  Spare:5;//11-15
    }Bits;
    uint32_t Word;
}SCREW_ENC_M;

typedef union
{
    struct
    {
         uint16_t L;
         uint16_t M;
    }Word;
    uint32_t Position;
}SCREW_POS;




SCREW_ENC Screw_RotEnc;

uint32_t Read_Screw_Encoder()//the value of Screw_RotEnc.Position is legal only when the status is OK
{
    uint32_t status = OK;

    //uint16_t Save_M = 0;
    //uint16_t Save_L = 0;

    SCREW_ENC_M Screw_Enc_M;
    SCREW_POS Screw_Pos;
/*
    Screw_Enc_M.Word = F1_SCREW_ROTENC_M;;
    Screw_Pos.Word.M = Screw_Enc_M.Bits.M;
    Screw_Pos.Word.L = F1_SCREW_ROTENC_L;
    //read again
    Screw_Enc_M.Word = F1_SCREW_ROTENC_M;
    Save_M = Screw_Enc_M.Bits.M;
    Save_L = F1_SCREW_ROTENC_L;

    if(Save_M != Screw_Pos.Word.M)
    {
        Screw_Pos.Word.M = Save_M;
        Screw_Pos.Word.L = Save_L;
    }

*/
    //First read the LSB (The FPGA Locks the MSB when reading the LSB)
    Screw_Pos.Word.L = F1_SCREW_ROTENC_L;
    Screw_Enc_M.Word = F1_SCREW_ROTENC_M;
    Screw_Pos.Word.M = Screw_Enc_M.Bits.M;

    Screw_RotEnc.Position = Screw_Pos.Position;

    Screw_RotEnc.Index_Counter = F1_SCREW_ROTENC_I;

    return status;
}


void Reset_Screw_Encoder()
{
    F1_SCREW_ROTENC_M = 0;
    F1_SCREW_ROTENC_L = 0;
}