aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Main.c
blob: 131638d343b020b0bc98756c7448a2a1fd51a5e9 (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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <Container.h>
#include "include.h"
/*#include <stdbool.h>
#include <stdio.h>
#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
//#include <ti/drivers/USBMSCHFatFs.h>
#include <driverlib/sysctl.h>
#include <driverlib/interrupt.h>
#include <driverlib/rom_map.h>
#include <ti/sysbios/knl/Semaphore.h>
*/
#include <driverlib/sysctl.h>
#include "driverlib/eeprom.h"
#include "driverlib/hibernate.h"

#include "Drivers/USB_Communication/USBCDCD.h"
#include "Drivers/I2C_Communication/I2C.h"
#include "PMR/common/MessageContainer.pb-c.h"
#include "Common/Sys_PinOut_Config/Pin.h"
#include "Common/Sys_PinOut_Config/Pin_config.h"
#include "Common/utilities/idle_task.h"
#include "Drivers/ADC_Sampling/ADC.h"

#include "drivers/Heater/Heater.h"
#include "drivers/Motors/Motor.h"

#include "Communication/CommunicationTask.h"
#include "Drivers/I2C_Communication/Head_Card/IO_Ports/Head_IO.h"

#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Types.h>

#include <xdc/runtime/Main.h>
#include <xdc/runtime/Assert.h>


#include "drivers/Flash_ram/FlashProgram.h"
#include "drivers/Flash_ram/MCU_E2Prom.h"

#include <DataDef.h>
#include "Sys_PinOut_Config/MCU_MAIN_pinout.h"
#include <Drivers/I2C_Communication/I2C.h>
#include "drivers/Uart_Comm/WHS_Controller_Comm/WHS_UART/WHS_Uart.h"
#include <Drivers/I2C_Communication/WHS_Card/IO_Extender_Ports_TCA9555/WHS_IO.h>
#include "Drivers/Uart_Comm/BTSR/BTSR.h"

/*
#include "Drivers/SPI_SDCARD/SDSPI.h"
#include "Drivers/SPI_SDCARD/SDCardSPI.h"
#include "Drivers/Ethernet_Config/EMACCommon.h"
#include "Drivers/USBStick_FileSys/USBstick.h"
#include "Drivers/USBStick_FileSys/USBMSCHFatFs.h"
*/
#include "Common/SW_Info/SW_Info.h"

#include "Modules/Control/control.h"
#include "Modules/Control/MillisecTask.h"
#include "Modules/Heaters/Heaters_ex.h"
#include "Modules/Ids/Ids_ex.h"
#include "Modules/AlarmHandling/AlarmHandling.h"

#include "StateMachines/Printing/PrintingSTM.h"
#include "StateMachines/Initialization/InitSequence.h"
#include "StateMachines/Initialization/PowerIdle.h"
#include <Drivers/SSI_Comm/SSI_Comm.h>
#include "drivers/SPI/SPI_Comm.h"
#include "drivers/Uart_Comm/Uart.h"
#include "drivers/valves/valve.h"

#include "drivers/FPGA/FPGA.h"
#include <drivers/FPGA/FPGA_Comm.h>
#include <drivers/FPGA/FPGA_INTERRUPTS/fpga_interrupts.h>

#include "Drivers/I2C_Communication/I2C.h"

#include "Drivers/I2C_Communication/Dispenser_Card/EEPROM/Dispenser_EEPROM.h"

#include "drivers/FPGA/FPGA_GPIO/FPGA_GPIO.h"

#include "drivers/Flash_Memory/FATFS/ff.h"
#include "drivers/Flash_Memory/FATFS/Control_File_System.h"

#include "Modules/General/buttons.h"
#include <Drivers/I2C_Communication/Dispenser_Card/IO_Ports/Dispenser_IO.h>
#include "Drivers/I2C_Communication/Main_Board_EEPROM/Main_EEPROM.h"
#include "Drivers/I2C_Communication/Head_Card/IO_Ports/Head_IO.h"
#include "Drivers/I2C_Communication/Head_Card/EEPROM/Head_EEPROM.h"
#include "modules/AlarmHandling/AlarmHandling.h"
#include <Drivers/I2C_Communication/I2C_Task.h>
#include <Drivers/I2C_Communication/WHS_Card/EEPROM/WHS_EEPROM.h>
#include <Drivers/I2C_Communication/RFID_NFC/I2C_IFS_Mux.h>

#include <Drivers/I2C_Communication/RFID_NFC/NFC_MainBaord.h>
#include <Drivers/I2C_Communication/RFID_NFC/logi-tag/LT_RFID.h>
#include <Drivers/I2C_Communication/RFID_NFC/RFIDTagInfo.h>
#include <Drivers/I2C_Communication/RFID_NFC/NFC.h>

#include <Drivers/I2C_Communication/RFID_NFC/NFC_MainBaord.h>
#include <Drivers/I2C_Communication/I2C_RFID_Dispenser_Task/I2C_RFID_Disp_Task.h>
#include <Utilities/delay.h>
//*****************************************************************************
//
// The system tick rate expressed both as ticks per second and a millisecond
// period.
//
//*****************************************************************************
#define TICKS_PER_SECOND        1000


//*****************************************************************************
//
// Global system tick counter
//
//*****************************************************************************
static volatile uint32_t g_ui32SysTickCount = 0;

unsigned char Brd_ID = 0, Assy_ID = 0;

// Flags used to pass commands from interrupt context to the main loop.
static volatile uint32_t g_ui32Flags;

bool Machine_Idle_Mode = false;

extern  bool PP_Machine;

Types_Site *site;
Error_Id eid;
int eCode;
char ErrorPath[50] = "0://ErrFile.txt";
bool UpdateFlag = false;
bool FirstErrorFlag = true;
Task_Handle PrevTask,NextTask;
extern FIL *LogFileHandle;
/* ======== Function switchHook ======== */
Void MyswitchFxn(Task_Handle prev, Task_Handle next)
{
    PrevTask = prev;
    NextTask = next;
}
/* ======== errHook ======== */
Void errHook(Error_Block *eb)
{
    //FRESULT Fresult = FR_OK;

    uint32_t Bytes = 0;
    //char File[50] = "";*/
    char message[300];
    int len;
    MCU_E2PromUpdateResetReason(InitFailures);

    //STATUS_GREEN_LED_OFF;
    ACTIVITY_GREEN_LED_OFF;
    COMM_GREEN_LED_OFF;
    Head_I2C_EXP1_0x40.ushort = 0;
    Head_I2C_EXP2_0x42.ushort = 0;
    Head_Write_IO_Reg(0x40,LOW_AND_HIGH);
    Head_Write_IO_Reg(0x42,LOW);

    ReportResetReason();

    STATUS_RED_LED_ON;
    ACTIVITY_RED_LED_ON;
    COMM_RED_LED_ON;
    ReportStopReporting();
    if (UpdateFlag == true)
        return;
    if (FirstErrorFlag == true)
    {
        FirstErrorFlag = false;
        MCU_E2PromProgram(EEPROM_INIT_FAILURE_COUNTER,InitFailures+1);
		utilsStoreLocalTime();

        len = usnprintf(message, 300, "\r\nerror task 0x%x %s context prev task 0x%x,%s", PrevTask,Task_Handle_name(PrevTask),NextTask,Task_Handle_name(NextTask));
        f_write(LogFileHandle,message,len,&Bytes );
        site = Error_getSite(eb);
        eid = Error_getId(eb);
        eCode = Error_getCode(eb);
        // print user supplied error code
        len = usnprintf(message, 300, "\r\nerror %d, eid %d  mod %d file %s line %d",
                        eCode, eid,site->mod,
                        site->file,site->line);
        f_write(LogFileHandle,message,len,&Bytes );

        //LOG_ERROR (eCode, "error # ");
        // check originator's mod id against known mods
        if (site->mod == Main_Module_id()) {
            f_write(LogFileHandle,"\r\napp error: ",12,&Bytes );
        }

        // check error id against known errors
        if (eid == Error_E_generic) {
            f_write(LogFileHandle,"generic: ",10,&Bytes );
        }
        else if (eid == Assert_E_assertFailed) {
            System_printf("assertion violation: ");
            f_write(LogFileHandle,"\r\nassertion violation: ",22,&Bytes );
        }
        CloseLogFile();
    }


#ifdef WATCHDOG
    //Power_Reset();
    SysCtlReset();
#endif
}

//*****************************************************************************
//
// Interrupt handler for the system tick counter.
//
//*****************************************************************************
void SysTickHandler(void)
{
    // Update our system time.
    g_ui32SysTickCount++;
}

void Init_EVB()
{
#ifdef EVALUATION_BOARD
    InitPinOutPullUps();

    #ifdef Stub_Mode
    //MOTOR Driver L6470 SPI
    SPI2_Init();
    init_BUSY_Pin();

    //Danser_ENC:
    SSI1_Init(); // Read dancer ENC SSI

    #endif
#endif
}

int main(void)
{
    FRESULT Fresult = FR_OK;
    time_t time_store = 0;

    // Enable interrupts to the processor.
    //
    ROM_IntMasterDisable();
    //Set the clock 120MHz
	uint32_t ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), SYS_CLK_FREQ);

    // Enable the system tick.
    ROM_SysTickPeriodSet(ui32SysClock / TICKS_PER_SECOND);
    ROM_SysTickIntEnable();
    ROM_SysTickEnable();

    //Calculateinit();//moved to HWConfiguration after reading the dispenser EEPROM

	PortFunctionInit();
	//FlashInit();

#ifndef EVALUATION_BOARD
    TangoVersion_t Version;
    uint32_t Data,i;

   MCU_E2PromInit();
   MCU_E2PromUpdateResetReason(0);

#endif
    #ifdef EVALUATION_BOARD
        Init_EVB();
    #endif

    FRESULT iFResult = Init_Flash_File_System(false);// true
    if(iFResult != FR_OK)
    {
        LOG_ERROR (iFResult, "Error during init Flash File System");
        assert(iFResult);
    }
    Fresult = f_mkdir("0://SysInfo");
    if ((Fresult != FR_OK)&&(Fresult != FR_EXIST))
    {
#ifdef WATCHDOG
        ROM_WatchdogResetDisable(WATCHDOG0_BASE);
        uint32_t timeout = 0xFFFFFFFFU;
        ROM_WatchdogReloadSet(WATCHDOG0_BASE, timeout);
#endif
        STATUS_GREEN_LED_ON;
        ACTIVITY_GREEN_LED_ON;
        COMM_GREEN_LED_ON;

        STATUS_RED_LED_ON;
        ACTIVITY_RED_LED_ON;
        COMM_RED_LED_ON;
        //FFS error
        Report("FFS error - rebuilding", __FILE__,__LINE__,Fresult, RpMessage,0, 0);
        FRESULT iFResult = Init_Flash_File_System(true);// true
        Fresult = f_mkdir("0://SysInfo");

        ACTIVITY_GREEN_LED_OFF;
        COMM_GREEN_LED_OFF;

        STATUS_RED_LED_OFF;
        ACTIVITY_RED_LED_OFF;
        COMM_RED_LED_OFF;
#ifdef WATCHDOG
        ROM_WatchdogResetEnable(WATCHDOG0_BASE);
        timeout = 120000000*3;
        ROM_WatchdogReloadSet(WATCHDOG0_BASE, timeout);
#endif
    }

    utilsInit(ui32SysClock);
    MCU_E2PromRead(EEPROM_LOCAL_TIME,&time_store);
    if (time_store)
    {
        ulocaltime(time_store+5,&LocalTime);
        HibernateCalendarSet(&LocalTime);
    }
    OpenLogFile();
#ifndef EVALUATION_BOARD
    Data = MCU_E2PromEmbeddedVersionRead();
    memcpy (&Version,&Data,4);
    i = memcmp(&Version,&_gTangoVersion,4);
    if (i)
    {
        LOG_ERROR(Version.m_minor,"Version change, do stuff");
        FlashInit();
        MCU_E2PromEmbeddedVersionProgram();
        MCU_E2PromProgram(EEPROM_INIT_FAILURE_COUNTER,0);
    }
    MCU_E2PromRead(EEPROM_INIT_FAILURE_COUNTER,&InitFailures);
#endif
   Init_All_I2C();
#ifndef EVALUATION_BOARD
   Block_Main_MUX_TCA9548A_0xE4();//unused, same i2c as head and whs cards

   Check_Head_Type_Via_EEPROM();//without trigger
   Check_WHS_Type_Via_EEPROM();//without trigger
   I2C_2_3_BUSY_DELAY  = 100;  //I2C2_BASE, I2C3_BASE

   //WHS_Type = WHS_TYPE_NEW; //for testing
   //Head_Type = HEAD_TYPE_ARC; //for testing

   if ((Head_Type >= HEAD_TYPE_UNKNOWN_WITH_CARD)&&(WHS_Type == WHS_TYPE_NEW))
   {
       PP_Machine = true;
   }
   if ((Head_Type > HEAD_TYPE_FLAT_WITHOUT_CARD)||(WHS_Type == WHS_TYPE_NEW))
   {
       I2C_ReadingTask_Init();//I2C Task for HEAD + WHS
   }
   if (Head_Type > HEAD_TYPE_FLAT_WITHOUT_CARD)
   {
       Trigger_Head_io_Init();//Head_IO_Init();//moved to set 24 to pressure sensor in arc earlier
   }

   STATUS_RED_LED_ON;
   STATUS_GREEN_LED_ON;
   FPGA_Init();

   I2C_DispRFIDTask_Init();//I2C Task for dispensers & RFID

    #if defined(USE_RFID_LOGIC) || defined(USE_RFID_STUB)
       //Trigger_RFID_Init();  //Init_IFS();//must be done after FPGA_Init
       Init_IFS();
    #else
       Check_IFS_Availability();
    #endif

#endif

	MAP_FPULazyStackingEnable();

    SysCtlUSBPLLEnable();

#ifndef EVALUATION_BOARD
    DeActivateAllSSR();
    Control_Dryer_Fan(STOP,0);//Preventing unwanted movement at power up, since the default from FPGA is "0" (= START)
#endif

#ifdef WATCHDOG
	InitWatchdog(ui32SysClock*3);
#endif

	CommunicationTaskInit();

#ifndef EVALUATION_BOARD
	//Turn_the_Blower_On();//Turn on with the Default_Voltage
	ActivateChiller();//WHS Cooler SSR9
	//ActivateHeadMagnet();
    Set_Speed_Sensor_TypeII_Registers(10,5);//set default values
#endif

    if (WHS_Type == WHS_TYPE_NEW)
    {
        InitConsole_WHS_UART3(ui32SysClock);//Init WHS UART Shinko RS485 Communication
        Trigger_RS485_Direction_Init();//init i/o
    }

    Read_HW_Version(&Brd_ID, &Assy_ID);

    if(Assy_ID > 4)//Bit2 = 1 for BTSR
     {
        // Configure the GPIO Pin PR0 for BTSR RS485 UART  - Set as output to change to uart4 RS485 Direction:
        ROM_GPIOPinTypeGPIOOutput(GPIO_PORTR_BASE, GPIO_PIN_0);
        ROM_GPIOPinWrite(GPIO_PORTR_BASE, GPIO_PIN_0, 1);//RX 1 (remove the error CAB ERR on the BTSR screen?)
        InitConsole_BTSR_UART4(ui32SysClock);

         #ifdef USE_UART4_FOR_BTSR
             BTSR_Init();
         #endif
     }
     else
     {
         #ifdef FORCE_BTSR_CARD_0023//0014

             // Configure the GPIO Pin PR0 for BTSR RS485 UART  - Set as output to change to uart4 RS485 Direction:
             ROM_GPIOPinTypeGPIOOutput(GPIO_PORTR_BASE, GPIO_PIN_0);
             ROM_GPIOPinWrite(GPIO_PORTR_BASE, GPIO_PIN_0, 1);//RX 1 (remove the error CAB ERR on the BTSR screen?)
             InitConsole_BTSR_UART4(ui32SysClock);

             #ifdef USE_UART4_FOR_BTSR
                BTSR_Init();
             #endif
         #endif

         ReportWithPackageFilter(ThreadFilter,"HW does NOT support BTSR",__FILE__,__LINE__,(int)Brd_ID,RpError,(int)Assy_ID,0);
     }

	//EMAC_initEMAC();
    ControlInit();

    AlarmHandlingInit();

    MillisecInit();

    ControlStart();
    Heaters_Init();
#ifndef EVALUATION_BOARD
    MotorsInit();
#endif
    //Shai    WHS_HW_test(); // for WHS sensor test

    #ifndef EVALUATION_BOARD
        Init_Machine_Leds();
    #endif

#ifndef EVALUATION_BOARD
   //WHS_init();
    //IDS_ModuleInit();
    Valve_Set(VALVE_MIXCHIP_WASTECH, Mixer_Waste);
#endif

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



    //FPGA_Read_limit_Switches_Registers();

    //calibration(); //test vme communication

    //GetFiles(); - to start jtag
    //GetVmeByte();

    //I2C_NFC_Test();
    //
    //test_avi(); //example for shai

    //Read_Speed_Sensor_TypeII();//must be delay between Set_Speed_Sensor_TypeII_Registers to Read_Speed_Sensor_TypeII

    //Test_ADS120_Internal_Temperature_Sensor(0);

#ifndef EVALUATION_BOARD
    Main_EEPROM_Read_Main_Card_SN();
#endif

    // Enable interrupts to the processor.
    //
    ROM_IntMasterEnable();


    IDLE_TASK_package_init();
    Start_InitSequence();
    //PowerIdleInit();
    BIOS_start();

    return (0);
    //while(1){};
}