#include <project.h>
#include <FreeRTOS.h>
#include <task.h>

// Define Prototype
void prvHardwareSetup();

/* ---------------------------------------------------------------------------
 * FreeRTOS support and configuration functions
 * --------------------------------------------------------------------------- */

void prvHardwareSetup( void )
{
	/* Port layer functions that need to be copied into the vector table. */
	extern void xPortPendSVHandler( void );
	extern void xPortSysTickHandler( void );
	extern void vPortSVCHandler( void );
	extern cyisraddress CyRamVectors[];

	/* Install the OS Interrupt Handlers. */
	CyRamVectors[ 11 ] = ( cyisraddress ) vPortSVCHandler;
	CyRamVectors[ 14 ] = ( cyisraddress ) xPortPendSVHandler;
	CyRamVectors[ 15 ] = ( cyisraddress ) xPortSysTickHandler;
}

void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )
{
	/* The stack space has been execeeded for a task, considering allocating more. */
	taskDISABLE_INTERRUPTS();
	for( ;; );
}

void vApplicationMallocFailedHook( void )
{
	/* The heap space has been execeeded. */
	taskDISABLE_INTERRUPTS();
	for( ;; );
}

// define user function defines and prototypes
#define	TASK_STACK_LED 128
#define	TASK_PRIORITY_LED (tskIDLE_PRIORITY +1)

void Task_LED(void *pvParameters);

int main()
{	
	prvHardwareSetup();
	CyGlobalIntEnable;

	xTaskCreate(Task_LED, 			\
		(signed portCHAR *) "LED", 	\
		TASK_STACK_LED , 			\
		NULL, 						\
		TASK_PRIORITY_LED, 			\
		NULL);
	
	vTaskStartScheduler();
	
	while(1) continue;
	return 1; // We should never reach this
}

// Task : LED
void Task_LED(void *pvParameters)
{
	const portTickType xDelay = 500 / portTICK_RATE_MS;	// 500 msの時間定義
	while(1) {
		LED_Write(~LED_Read());
		vTaskDelay(xDelay);
	}
}