aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Modules/Control/MillisecTask.c
blob: 6f3a5985253be0ee97ea0b21f13ef8f2962b5fba (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
/************************************************************************************************************************
 * Millisec.c
 * Millisec module
 *
 * The millisec task is called once every 1 millisecond to gather data from the FPGA crucial modules,
 * so it will be ready for the Millisec operations. it is called 300 (TBD, configurable)
 * microsecond before the Millisec task, so that the data will be ready for the Millisec handling.
 * the ,millisecond task holds the pointer to a double buffer of results, so that the Millisec will handle the
 * most updated data, without disturbing data gathering
 *
 **************************************************************************************************************************/

////////////////////////////////State machine operation////////////////////////////////////
//the state machine operation is used to operate in runtime correct profile flow execution
//by recieved esign flow of the user from the UI
///////////////////////////////////////////////////////////////////////////////////////////
#include "include.h"
#include "Modules/General/GeneralHardware.h"

#include <driverlib/timer.h>
#include <inc/hw_ints.h>

#include "drivers/adc_sampling/adc.h"
#include "control.h"
/******************** Definitions  ********************************************/
#define INVALID_MSG_ID    0xFFFF
#define MAX_TANGO_CONTROL_DEVICES 200
/******************** STRUCTURES AND ENUMs  ********************************************/

typedef enum
{
    OneMillisec,
}nillisecMessages;

typedef struct MillisecMessage{
    uint16_t messageId;
    uint16_t msglen;
    uint32_t tick;
    uint8_t messageData[20];
}MillisecMessageStruc;
/******************** GLOBAL PARAMETERS  ********************************************/
Mailbox_Handle          MillisecMsgQ = NULL;
bool                    MillisecRestart;
static GateMutex_Handle gateMillisecDB;

uint32_t                MillisecDatalog[MAX_TANGO_CONTROL_DEVICES];
uint32_t        Millisec_timerBase = TIMER1_BASE;        //Timer handle
/******************** Functions  ********************************************/

//**********************************************************************
/******************** CODE  ********************************************/
//**********************************************************************

void MillisecInit(void)
{
    Error_Block eb;

    MillisecMsgQ = Mailbox_create(sizeof(MillisecMessageStruc), 20, NULL,NULL);

    MillisecRestart = false;

    memset(MillisecDatalog,0,sizeof(uint32_t)*MAX_TANGO_CONTROL_DEVICES);


    gateMillisecDB = GateMutex_create(NULL, &eb);
    if (gateMillisecDB == NULL)
    {
        System_abort("Could not create USB Wait gate");
    }

    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    ROM_TimerConfigure(Millisec_timerBase, TIMER_CFG_PERIODIC);   // 32 bits Timer
    //TimerIntRegister(Millisec_timerBase, TIMER_A, Timer0Isr);    // Registering  isr
    ROM_TimerEnable(Millisec_timerBase, TIMER_A);
    ROM_IntEnable(INT_TIMER1A);
    ROM_TimerIntEnable(Millisec_timerBase, TIMER_TIMA_TIMEOUT);

    ADCAcquireInit();

    return;
}
void MillisecStop(void)
{
    MillisecRestart = false;
    ADCAcquireStop();
}

void MillisecStart(void)
{
    MillisecRestart = true;
    ROM_TimerLoadSet(Millisec_timerBase, TIMER_A,120000/*one millisecond*/);
    ADCAcquireStart(0,1);
}


void OneMilliSecondMillisecInterrupt(UArg arg0)
{
    MillisecMessageStruc Message;
    bool retcode = false;
    ROM_IntMasterDisable();
    if (MillisecRestart == true)
    {
        ROM_TimerLoadSet(Millisec_timerBase, TIMER_A,120000/*one millisecond*/);
    }
    else
        ROM_TimerDisable(Millisec_timerBase,TIMER_A);

    //trigger the ADC collection - check and set priorities to make sure handling timing is correct.
    //we might want to call it from the task, afetr execution of other taks!!!
    ADC_TriggerCollection();
    //send message to the Millisec task
    Message.messageId = OneMillisec;
    Message.tick = UsersysTickGet();
    Message.msglen = sizeof(MillisecMessageStruc);
    if (MillisecMsgQ != NULL)
        retcode = Mailbox_post(MillisecMsgQ , &Message, BIOS_NO_WAIT);

    ROM_TimerIntClear(Millisec_timerBase, TIMER_TIMA_TIMEOUT);  // Clear the timer interrupt
    //
    // Enable all interrupts.
    //
    ROM_IntMasterEnable();
    return ;
}

uint32_t MillisecLoop(uint32_t tick)
{
    //call all modules Millisec functions
    //test dancers and speed encoders
    //check all callback units (state machine waiting for completion of a change)
    bool Ten_msTick, Hundred_msTick, Onesecond_Tick;
    Ten_msTick      =   (tick%eTenMilliSecond == 0)      ?true:false;
    Hundred_msTick  =   (tick%eHunderdMillisecond == 0)  ?true:false;
    Onesecond_Tick  =   (tick%eOneSecond == 0)           ?true:false;

    //gather data from FPGA
    return OK;
}
/******************************************************************************
 *  ======== messageTsk ========
 *  Task for this function is created statically. See the project's .cfg file.
 *  this message task is created statically in system initialization,
 ******************************************************************************/
void MillisecTask(UArg arg0, UArg arg1)
{
    MillisecMessageStruc Message;
    //char str[60];
    //uint16_t length;
    //Clock_setTimeout(HostKAClock, 1000);
    //Clock_start(HostKAClock);
    MillisecInit();

    while(1)
    {
        Mailbox_pend(MillisecMsgQ , &Message, BIOS_WAIT_FOREVER);
        switch (Message.messageId)
        {
            case OneMillisec:
                MillisecLoop(Message.tick);
                break;
            default:
                break;
        }
    }
}