aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Drivers/I2C_Communication/DAC/Blower.c
blob: 307d815ee99d7de84034b58be065789ae265b6b5 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*
 * Blower.c
 *
 *  Created on: Aug 1, 2018
 *      Author: avi
 *
 *      AD5691R
 *      support standard (100 kHz) and fast (400 kHz) data transfer modes
 *      I2C 12 bits DAC chip U207
 */
#include <stdint.h>
#include <stdbool.h>
#include "include.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/tm4c1294ncpdt.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom.h"
#include "driverlib/rom_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/i2c.h"
#include "drivers/I2C_Communication/I2C.h"
#include "Drivers/I2C_Communication/I2C_Task.h"
#include "Blower.h"
#include "modules/control/control.h"
#include "modules/general/process.h"
#include "modules/diagnostics/diagnostics.h"
#include "drivers/FPGA/FPGA.h"

DAC_Union DAC;

uint32_t Write_Control_Register()
{
    uint32_t status = OK;

    DAC.Bytes.Command   = AD5691R_CMD_WRITE_CONTROL_REG;
    DAC.Bytes.Data_High = AD5691R_Control_Normal_Mode | AD5691R_Control_Reference_Enabled | AD5691R_Control_Gain_0V_VREF;
    DAC.Bytes.Data_Low  = AD5691R_DONT_CARE_DATA_BYTE;

    status = I2C_Write(2, I2C_DAC_ADDRESS, DAC.Write_DAC_I2C_Buf, 3);

    return status;
}

uint32_t Write_DAC_and_Input_Register(uint32_t Bits)
{
    uint32_t status = OK;

    DAC.Bytes.Command   = AD5691R_CMD_WRITE_INPUT_N_UPDATE_REG;

    DAC.Bytes.Data_High = Bits;

    DAC.Bytes.Data_Low  = AD5691R_DONT_CARE_DATA_BYTE;

    status = I2C_Write(DAC_I2C_BASE, I2C_DAC_ADDRESS, DAC.Write_DAC_I2C_Buf, 3);

    return status;
}

uint32_t Write_DAC_and_Input_Register_temp(uint32_t Bits)//to test enc
{
    uint32_t status = OK;

    DAC.Bytes.Command   = AD5691R_CMD_WRITE_INPUT_N_UPDATE_REG;


    DAC.Bytes.Data_High = (Bits >> 4);

    DAC.Bytes.Data_Low  = ((Bits & 0xf)<<4);

    status = I2C_Write(DAC_I2C_BASE, I2C_DAC_ADDRESS, DAC.Write_DAC_I2C_Buf, 3);

    return status;
}

uint32_t Write_Input_Register()
{
    uint32_t status = OK;

    DAC.Bytes.Command   = AD5691R_CMD_WRITE_INPUT_REG;
    DAC.Bytes.Data_High = 0xFF;
    DAC.Bytes.Data_Low  = AD5691R_DONT_CARE_DATA_BYTE;

    status = I2C_Write(DAC_I2C_BASE, I2C_DAC_ADDRESS, DAC.Write_DAC_I2C_Buf, 3);

    return status;
}

uint32_t Update_DAC_register()
{
    uint32_t status = OK;

    DAC.Bytes.Command   = AD5691R_CMD_UPDATE_DAC_REG;
    DAC.Bytes.Data_High = AD5691R_DONT_CARE_DATA_BYTE;
    DAC.Bytes.Data_Low  = AD5691R_DONT_CARE_DATA_BYTE;

    status = I2C_Write(DAC_I2C_BASE, I2C_DAC_ADDRESS, DAC.Write_DAC_I2C_Buf, 3);

    return status;
}

uint32_t Write_NOP()
{
    uint32_t status = OK;

    DAC.Bytes.Command   = AD5691R_CMD_NOOP;
    DAC.Bytes.Data_High = AD5691R_DONT_CARE_DATA_BYTE;
    DAC.Bytes.Data_Low  = AD5691R_DONT_CARE_DATA_BYTE;

    status = I2C_Write(DAC_I2C_BASE, I2C_DAC_ADDRESS, DAC.Write_DAC_I2C_Buf, 3);

    return status;
}


uint8_t DAC_mV2Bits(VoutmV) // 0-8V
{
    //The function acording to the Orcad shceme : VOUT=VIN+(VIN-0.2)*(9.76/4.12) ( VIN 0-2.5V -> VOUT 0-8V)
    //=> Vin = (Vout + 0.4737)/3.3689
    //Vin is the VAmpin (VDACout)
    uint8_t Bits;
    uint32_t temp;

    if(VoutmV > 8000)
        VoutmV = 8000;// 8V is the output for 0xFF (2.5V after Amp. we get 8V)

    temp = VoutmV + 473;// Vamp - Vampin

    temp =   temp * 0xFF;//Vampin(=VDACout) -> bits)
    temp/=3368;// Vamp - Vampin
    temp/=2.5;//Vampin(=VDACout) -> bits)

    Bits = temp & 0xFF;//8bit

    return Bits;
}

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

bool blowerStatus = false;
uint32_t voltage = 0;
uint32_t getBlowerState(void)
{
    if (blowerStatus == false)
        return 0;
    else
        return voltage;
}
uint32_t Turn_the_Blower_On()
{
    uint32_t status = OK;


    blowerStatus = true;
    voltage = Default_Voltage;
    if (WHS_Type == WHS_TYPE_UNKNOWN)
    {
#ifndef EVALUATION_BOARD
        status = Write_Control_Register();
        status |= Write_DAC_and_Input_Register(DAC_mV2Bits(Default_Voltage));
#endif
    }
    else //new WHS
    {
        Trigger_SetWHSBlowerVoltage(Default_Voltage);
    }
    DiaglosticChangeBlowerData();
    return status;
}

uint32_t Control_Voltage_To_Blower(uint32_t mV)
{
    uint32_t status = OK;
    if (mV >7500)
    {
        Report("------------ Set Blower Voltage: too high!-------", __FILE__,__LINE__, mV, RpMessage, (int)headairflow, 0);
        return ERROR;
    }
    if (WHS_Type == WHS_TYPE_UNKNOWN)
    {
#ifndef EVALUATION_BOARD
        status |= Write_DAC_and_Input_Register(DAC_mV2Bits(mV));
#endif
    }
    else //new WHS
    {
        Trigger_SetWHSBlowerVoltage(mV);
        //Report("------------ Set Blower Voltage:-----------------", __FILE__,__LINE__, mV, RpMessage, (int)headairflow, 0);
    }
    voltage = mV;
    DiaglosticChangeBlowerData();

    return status;
}

uint32_t Turn_the_Blower_Off()
{
    uint32_t status = OK;

    if (WHS_Type == WHS_TYPE_UNKNOWN)
    {
#ifndef EVALUATION_BOARD
        status |= Write_DAC_and_Input_Register(0);
#endif
    }
    else //new WHS
    {
        //Trigger_SetWHSBlowerVoltage(0);
    }
    blowerStatus = false;

    DiaglosticChangeBlowerData();
    return status;
}
/*
uint32_t mInitial_mV, mTarget_mV;
uint32_t mInterval = eOneSecond*20;
uint32_t BlowerControlId = 0xFF;

uint32_t Gradual_Increase_Blower_Callback(uint32_t DispenserId, uint32_t ReadValue)
{
    if ((mTarget_mV-mInitial_mV)<100)
    {
        Control_Voltage_To_Blower(mTarget_mV);
        Report("Finished Increasing blower",__FILE__,__LINE__,(int)mTarget_mV,RpWarning,(int)millisecondCounter,0);

        if (SafeRemoveControlCallback(BlowerControlId, Gradual_Increase_Blower_Callback )==OK)
            BlowerControlId = 0xFF;
        else
            Report("Remove control callback failed",__FILE__,__LINE__,(int)1,RpWarning,(int)BlowerControlId,0);
        return OK;
    }
    mInitial_mV = mInitial_mV+100;
    Control_Voltage_To_Blower(mInitial_mV);
    Report("Increasing blower",__FILE__,__LINE__,(int)mInitial_mV,RpWarning,(int)millisecondCounter,0);
    return OK;
}

uint32_t Gradual_Increase_Blower(uint32_t Initial_mV,uint32_t Target_mV)
{
    mInitial_mV = Initial_mV;
    mTarget_mV = Target_mV;
    if ((mTarget_mV-mInitial_mV)<100)
    {
        Control_Voltage_To_Blower(mTarget_mV);
        Report("Finished Increasing blower",__FILE__,__LINE__,(int)mTarget_mV,RpWarning,(int)millisecondCounter,0);
        return OK;
    }
    mInitial_mV = Initial_mV+100;
    Control_Voltage_To_Blower(mInitial_mV);
    Report("Increasing blower",__FILE__,__LINE__,(int)mInitial_mV,RpWarning,(int)millisecondCounter,0);
    BlowerControlId = AddControlCallback(NULL, Gradual_Increase_Blower_Callback, mInterval,getBlowerState ,0, 0, 0 );
    if (BlowerControlId == 0xFF)
    {
        Report("Add control callback failed",__FILE__,__LINE__,(int)0,RpWarning,(int)BlowerControlId,0);
        return ERROR;
    }
    return OK;
}
uint32_t Cancel_Gradual_Increase_Blower(uint32_t Initial_mV)
{
    Control_Voltage_To_Blower(Initial_mV);
    Report("Cancelled Increasing blower",__FILE__,__LINE__,(int)Initial_mV,RpWarning,(int)millisecondCounter,0);
    if (RemoveControlCallback(BlowerControlId, Gradual_Increase_Blower_Callback )==OK)
    {
        Report("Remove control callback",__FILE__,__LINE__,(int)1,RpWarning,(int)BlowerControlId,0);
        BlowerControlId = 0xFF;
    }
    return OK;
}
*/


//////////////////////////////////////////////////////////////////////////////////////////


extern SCREW_ENC Screw_RotEnc;

extern uint32_t Read_Screw_Encoder();

void Screw_ENC_Velocity_to_DAC()//every 1mSec
{
    static uint32_t Last_position = 0;
    uint32_t Current_Position = 0;
    uint32_t Screw_Velocity = 0;
    uint32_t Value_to_ADC = 0;
    static bool first_time = true;

    int8_t Sign = 1;

    if(first_time)
    {
        Reset_Screw_Encoder();
        first_time = false;

    }
    else
    {
        Read_Screw_Encoder();
        Current_Position = (uint32_t)Screw_RotEnc.Position;

        if(Current_Position > Last_position )
        {
            Screw_Velocity = Current_Position - Last_position;
            Sign = 1;
        }
        else
        {
            Screw_Velocity = Last_position - Current_Position;
            Sign = -1;
        }


        uint32_t temp = (Screw_Velocity << 2 );

        if(temp > 2047)
            temp = 2047;
        /*else
        if(temp < -2047)
            temp = -2047;*/

        Value_to_ADC = 2048 + (Sign * temp);

        //Send to ADC;
        //Write_DAC_and_Input_Register(Value_to_ADC<<4);
        Write_DAC_and_Input_Register_temp(Value_to_ADC);

        Last_position = Current_Position;
    }
}