aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Main.c
blob: a5ddb694bdebba23523125f9c6fbaa0180bb3b80 (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
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 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.PPC.UI.Notifications
{
    /// <summary>
    /// Represents a pending notification item.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    public class PendingNotification<T, TResult>
    {
        /// <summary>
        /// Gets or sets the item.
        /// </summary>
        public T Item { get; set; }

        /// <summary>
        /// Gets or sets the completion source.
        /// </summary>
        public TaskCompletionSource<TResult> CompletionSource { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="PendingNotification{T, TResult}"/> class.
        /// </summary>
        public PendingNotification()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PendingNotification{T, TResult}"/> class.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="completionSource">The completion source.</param>
        public PendingNotification(T item, TaskCompletionSource<TResult> completionSource) :<
#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 "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 <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_Memory/Flash_Memory.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/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 "Modules/Waste/Waste.h"
#include <Drivers/I2C_Communication/Dispenser_Card/IO_Ports/Dispenser_IO.h>
#include "Drivers/I2C_Communication/Main_Board_EEPROM/Main_EEPROM.h"
#include "delay.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>

//*****************************************************************************
//
// 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;

// 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;

F2_CTRL_REG F2_CTRL_Reg;
F1_GPO_REG F1_GPO_Reg;

F3_GPO_01_REG F3_GPO_01_Reg;

Types_Site *site;
Error_Id eid;
int eCode;
char ErrorPath[50] = "0://ErrFile.txt";
bool UpdateFlag = false;
/* ======== errHook ======== */
Void errHook(Error_Block *eb)
{
    /*FRESULT Fresult = FR_OK;
    FIL *FileHandle = 0;  //the system supports a single active file
    uint32_t Bytes = 0;
    char File[50] = "";
    char message[300];
    int len;*/

    STATUS_GREEN_LED_OFF;
    ACTIVITY_GREEN_LED_OFF;
    COMM_GREEN_LED_OFF;

    STATUS_RED_LED_ON;
    ACTIVITY_RED_LED_ON;
    COMM_RED_LED_ON;

    site = Error_getSite(eb);
    eid = Error_getId(eb);
    eCode = Error_getCode(eb);
    //CloseLogFile();
    if (UpdateFlag == true)
        return;
    /*FileHandle = my_malloc(sizeof(FIL));
    if (FileHandle == 0)
        Fresult = FR_DENIED;
    else
    {
        Fresult = f_open(FileHandle,ErrorPath,FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
        if (Fresult == FR_OK)
        {
            f_lseek(FileHandle, FileHandle->fsize);
            strncpy(File,site->file,49);
            len = usnprintf(message, 80, "\r\n%s %s",__DATE__, __TIME__);
            Fresult = f_write(FileHandle,message,len,&Bytes );
            // 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);
            Fresult = f_write(FileHandle,message,len,&Bytes );

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

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

            // perform default error output
            f_close(FileHandle);
        }
        Task_sleep (200);
        my_free(FileHandle);
    }*/
#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()
{

    InitPinOutPullUps();

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

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

    #endif

}

int main(void)
{
    // 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();
   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();
   }

#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);
    }
    //OpenLogFile();

   Init_All_I2C();
   Block_Main_MUX_TCA9548A_0xE4();//unused, same i2c as head and whs cards
   Check_Head_Type_Via_EEPROM();
   //Trigger_Check_WHS_Type_Via_EEPROM();
   Check_WHS_Type_Via_EEPROM();
   if ((Head_Type >= HEAD_TYPE_UNKNOWN_WITH_CARD)&&(WHS_Type == WHS_TYPE_NEW))
   {
       PP_Machine = true;
   }
   #ifndef EVALUATION_BOARD
   STATUS_RED_LED_ON;
   STATUS_GREEN_LED_ON;
   FPGA_Init();
   #endif

	MAP_FPULazyStackingEnable();

    SysCtlUSBPLLEnable();

    if (Head_Type > HEAD_TYPE_SYLKO_WITHOUT_CARD)
        Head_IO_Init();

#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();
	utilsInit(ui32SysClock);

#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 ((Head_Type > HEAD_TYPE_SYLKO_WITHOUT_CARD)||(WHS_Type == WHS_TYPE_NEW))
        I2C_ReadingTask_Init();
	//EMAC_initEMAC();
    ControlInit();

    AlarmHandlingInit();

    MillisecInit();

    ControlStart();
    Heaters_Init();
    MotorsInit();
//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){};
}