 //*****************************************************************************
//
// main.c - FreeRTOS port for EK-LM3S8962 Evaluation Board and CCS v5.1.
//
//*****************************************************************************

// funkcni comand z css b build steps
// "${CG_TOOL_ROOT}/bin/hex${CG_TOOL_SUFFIX}.exe" -i "${BuildArtifactFileName}" -o "${BuildArtifactFileBaseName}.hex" -order MS -romwidth 16

#include "mybool.h"
#include "priorities.h"
#include "packet.h"
#include <stdio.h>

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "timers.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/* Standard Stellaris includes */
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_sysctl.h"
#include "inc/hw_types.h"

#include "driverlib/udma.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/systick.h"
#include "driverlib/rom.h"
#include "drivers/set_pinout.h"		// nastavenie pinov dosky
#include "drivers/sound.h"

// vsetko pre pracu s displejom
#include "grlib/grlib.h"
#include "grlib/widget.h"

// moje 
#include "MySound.h"
#include "MainTask.h"
#include "com_RS485.h"
#include "display_task.h"

#include "displayScreens/MenuScreen.h"
#include "displayScreens/MesageWindow.h"

// Takto sa vypisuje do console v CCS
	//	ROM_IntMasterDisable(); 
	//	printf("test\n");
	//	ROM_IntMasterEnable(); 

//*****************************************************************************
//
// The speed of the processor clock, which is therefore the speed of the clock
// that is fed to the peripherals.
//
//*****************************************************************************
unsigned long g_ulSystemClock;

//*****************************************************************************
//
// The error routine that is called if the driver library encounters an error.
//
//*****************************************************************************
#ifdef DEBUG
void
__error__(char *pcFilename, unsigned long ulLine)
{
}
#endif

// hlavicky taskov
extern	xTaskHandle	pvDisplayTask;
extern	xTaskHandle	xHComTask;
extern	xTaskHandle xHMainTask;

/*
 * Hook functions that can get called by the kernel.
 */
void vApplicationIdleHook( void );
void vApplicationTickHook( void );
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName );
void vApplicationTickHook( void );

/*-----------------------------------------------------------*/

void vTask1( void *pvParameters )
{
    volatile unsigned char i;
    static long toggle = 0x00;
//	char sprava[LENGHT_CONS_MES];    
	i= 0;
    for( ;; )
    {	
//		if( uxTaskGetStackHighWaterMark(pvDisplayTask) > StMaxDispT ) StMaxDispT= uxTaskGetStackHighWaterMark(pvDisplayTask);
//		if( uxTaskGetStackHighWaterMark(xHComTask) > StMaxRs485T ) StMaxRs485T= uxTaskGetStackHighWaterMark(xHComTask);
//		if( uxTaskGetStackHighWaterMark(xHMainTask) > StMaxMainT ) StMaxMainT= uxTaskGetStackHighWaterMark(xHMainTask);
        toggle ^= GPIO_PIN_2;
        if (toggle)
        {
//    			snprintf((char *)sprava, (LENGHT_CONS_MES - 1), "Disptask task: %d",  uxTaskGetStackHighWaterMark(pvDisplayTask)); 
//    			NewMasageToConsole(sprava, 3);
//    			snprintf((char *)sprava, (LENGHT_CONS_MES - 1), "RS485 task: %d", uxTaskGetStackHighWaterMark(xHComTask) ); 
//    			NewMasageToConsole(sprava, 3);
//    			snprintf((char *)sprava, (LENGHT_CONS_MES - 1), "Main task: %d", uxTaskGetStackHighWaterMark(xHMainTask) ); 
//    			NewMasageToConsole(sprava, 3);
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);
        }
        else
        {
            GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);
        }
        vTaskDelay( 1000 / portTICK_RATE_MS);
    }
    
}

int main(void)
{
    //
    // Set the system clock to run at 50MHz from the PLL
    // na 80
    ROM_SysCtlClockSet(/*SYSCTL_SYSDIV_2_5*/SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN |
                       SYSCTL_XTAL_16MHZ);

    // Get the system clock speed.
    g_ulSystemClock = ROM_SysCtlClockGet();

    //
    // Set the part pinout appropriately for this board.
    //
    PinoutSet();
    
	InitMySound();
    //
    // Enable Interrupts
    //
    ROM_IntMasterEnable();   
    
    //
    // Configure GPIO Pin used for the LED.
    //
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2|GPIO_PIN_3);
    
    InitMesageWindow();
 	DisplayTaskInit();  
 	MainTaskInit(); 
    ComRS485Init();

    /* Create task */
    xTaskCreate(    vTask1,     /* Pointer to the function that implements the task. */
                    "Task 1",   /* Text name for the task.  This is to facilitate debugging only. */
                    240,        /* Stack depth in words. */
                    NULL,       /* We are not using the task parameter. */
                    PRIORITY_VTASK_1,          /* This task will run at priority 1. */
                    NULL );     /* We are not using the task handle. */
                    
	
    /* Start the scheduler so our tasks start executing. */
    vTaskStartScheduler();

    /* If all is well we will never reach here as the scheduler will now be
    running.  If we do reach here then it is likely that there was insufficient
    heap available for the idle task to be created. */
    while (1)
    {
    }
}

/*-----------------------------------------------------------*/

void vApplicationIdleHook( void )
{
}

/*-----------------------------------------------------------*/

void vApplicationTickHook( void )
{
}

/*-----------------------------------------------------------*/

void vApplicationMallocFailedHook( void )
{
    /* This function will only be called if an API call to create a task, queue
    or semaphore fails because there is too little heap RAM remaining. */
    for( ;; );
}

/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
    /* This function will only be called if a task overflows its stack.  Note
    that stack overflow checking does slow down the context switch
    implementation. */
    for( ;; );
}



