aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Stubs_Handler/Stub_IntADC.c
blob: 737116907888e6cbaa36d7ffc6d905e537a894d6 (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
/*
 * Stub_IntADC.c
 *
 *  Created on: May 23, 2018
 *      Author: avi
 */


#include <Container.h>
#include <DataDef.h>
#include <PMR/Stubs/StubIntADCReadRequest.pb-c.h>
#include <PMR/Stubs/StubIntADCReadResponse.pb-c.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "inc/hw_uart.h"

#include "Drivers/USB_Communication/USBCDCD.h"
#include "MessageContainer.pb-c.h"

#include "Stub_Status.h"
#include "drivers/FPGA/FPGA.h"
#include "drivers/adc_sampling/adc.h"
#include <driverlib/adc.h>

void Stub_IntADCReadRequest(MessageContainer* requestContainer)
{
    uint32_t status = FAILED;

    unsigned short data;

    MessageContainer responseContainer;

    StubIntADCReadRequest* request = stub_int_adcread_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);
    status = PASSED;

    StubIntADCReadResponse response =   STUB_INT_ADCREAD_RESPONSE__INIT;

    status_response(status,&response.status, &response.statusword ,&response.has_statusword);

    uint32_t temp2 = request->adc_device;

    if((temp2 & 0x8000) == 0x8000)
    {
        request->adc_device  = (request->adc_device & 0x1F) ;//the max id is 0x14 (remove the 0x8000)

        if(request->adc_device == 0x02)//CHAN_DISPENSE_PRESSURE_1
        {
            response.sampling_in_bits    = (ADC_GetReading(ADC_CTL_CH2)<<16) | ADC_GetReading(ADC_CTL_CH3); //ISPENSE_PRESSURE_1 <<16 | DISPENSE_PRESSURE_2
            response.voltage_sampling_mv = (ADC_GetReading(ADC_CTL_CH4)<<16) | ADC_GetReading(ADC_CTL_CH5); //ISPENSE_PRESSURE_3 <<16 | DISPENSE_PRESSURE_4
        }
        else
        if(request->adc_device == 0x06)//CHAN_DISPENSE_PRESSURE_5
        {
            response.sampling_in_bits    = (ADC_GetReading(ADC_CTL_CH6)<<16) | ADC_GetReading(ADC_CTL_CH7); //ISPENSE_PRESSURE_5 <<16 | DISPENSE_PRESSURE_6
            response.voltage_sampling_mv = (ADC_GetReading(ADC_CTL_CH8)<<16) | ADC_GetReading(ADC_CTL_CH9); //ISPENSE_PRESSURE_7 <<16 | DISPENSE_PRESSURE_8
        }

        response.adc_device = request->adc_device;
        response.has_adc_device = true;
        response.has_sampling_in_bits = true;
        response.has_voltage_sampling_mv = true;
    }
    else
    {
        #ifndef RUN_AS_MAIN_JIG
                ADCAcquireInit();
                //SysCtlDelay(10000);
                delayUs(500);
                ADCAcquireStart(0,1);
                //SysCtlDelay(10000);
                delayUs(500);
                ADC_TriggerCollection();
                //SysCtlDelay(10000);
                delayUs(500);

                ADC0SS0Handler();
                //SysCtlDelay(100000);
                delayms(1);
        #endif

        request->adc_device  = (request->adc_device & 0x1F) ;//the max id is 0x14

        data = ADC_GetReading((ADC_TYPE) request->adc_device);


        response.adc_device = request->adc_device;
        response.has_adc_device = true;
        response.sampling_in_bits = data;
        response.has_sampling_in_bits = true;

        float temp= 0;
        temp = 4096 - data;
        temp *= 3;
        temp *= 1000; //move to mv
        temp /= 4096;
        response.voltage_sampling_mv = 3000 - temp;
        response.has_voltage_sampling_mv = true;

    }
    responseContainer = createContainer(MESSAGE_TYPE__StubIntADCReadResponse, requestContainer->token, true, &response, &stub_int_adcread_response__pack, &stub_int_adcread_response__get_packed_size);

    stub_int_adcread_request__free_unpacked(request,NULL);
    //-------------------------------------------------------------------------------------------
    uint8_t* container_buffer = malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    free(responseContainer.data.data);
    SendChars((char*)container_buffer, container_size);
}