/**********************************************************************/
/*                                                                    */
/* File name: demo.c                                                  */
/*                                                                    */
/* Since:     2007-02-25                                              */
/*                                                                    */
/* Version:   uip Kernel v1.09                                        */
/*            Copyright (C) 2006..2018 Wang Baojin.                   */
/*                                                                    */
/* Author:    Designed by Wang Baojin(王保进)                         */
/*                                                                    */
/*                                                                    */
/* Purpose:   demo application(基于uip kernel的应用程序)              */
/*                                                                    */
/* Distribution: This file is puip of uip kernel.                     */
/*            uip Kernel is free software; you can redistribute it    */
/*            and/or modify it under the terms of the GNU General     */
/*            Public License as published by the Free Software        */
/*            Foundation; either version 2, or (at your option)       */
/*            any later version.                                      */
/*                                                                    */
/*            uip Kernel is distributed in the hope that it will be   */
/*            useful, but WITHOUT ANY WARRANTY; without even the      */
/*            implied warranty of MERCHANTABILITY or FITNESS FOR A    */
/*            PuipICULAR PURPOSE.  See the GNU General Public         */
/*            License for more details.                               */
/*                                                                    */
/*            You should have received a copy of the GNU General      */
/*            Public License along with gpsim; see the file           */
/*            COPYING.txt. If not, write to the Free Software         */
/*            Foundation, 59 Temple Place - Suite 330,                */
/*            Boston, MA 02111-1307, USA.                             */
/*                                                                    */
/*          > A special exception to the GPL can be applied should    */
/*            you wish to distribute a combined work that includes    */
/*            uip Kernel, without being obliged to provide the        */
/*            source code for any proprietary components.             */
/*                                                                    */
/* History:                                                           */
/*   2016/06/10  [WBJ] Create this file.                              */
/*                                                                    */
/**********************************************************************/

#include "uip_kernel_api.h"
#include "demo.h"
#include "hardware_init.h"
#include "chip_uip_interrupt.h"
#include "hhd32f0xx.h"
#include "hhd_sys.h"
#include "hhd_uart.h"
#include "hhd_gpio.h"

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    uipAppInitialize                                    1.09        */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    Application Initialization function.                            */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
VOID uipAppInitialize(VOID)
{	
    uipTaskInitialize((uipTaskPtr)&AppTask[0],  AppTaskNumber);
    uipAlarmInitialize((uipAlarmPtr)&AppAlarm[0], AppAlarmNumber);
    uipResInitialize((uipResPtr)&AppRes[0], AppResNumber);
    uipMsgInitialize((uipMsgPtr)&AppMsg[0], AppMsgNumber);
	  
    /* 与处理器平台特征有关的初始化代码 */
    /* 设置 PendSV 是最低优先级的中断 */
    *(UIP_UINT *)(0xE000ED20) |= 0x00FF0000;
	  
    /* initialize system timer */
    Kernel_System_Tick_Init();
}

unsigned char AppResetSymbol = 0;

TASK(TASK_0)
{
    /* --------- System initialization ------------------*/
	SYS_SystemInitial();	
	
    /* --------- GPIOA Periph clock enable --------------*/
    ENABLEGPIOACLK;
    ENABLEGPIOCCLK;
		
    /* --------- Configure PA0 as output pushpull mode --*/	
	GPIO_ConfigPinsAsOutput(GPIOC,PIN0 | PIN1);
    
    while(1) {
	    //if(ActivateTask(TASK_1_ID) == E_OK)
        if(WaitEvent(APP_EVENT_TASK_0) == E_OK) {  	  	 
            ClearEvent(APP_EVENT_TASK_0);			 
            if(AppResetSymbol == 0){
		        /* -------- Set GPIO bit to 1 ---------------------*/
		        GPIO_SetPin(GPIOC,PIN0);
		        GPIO_ResetPin(GPIOC,PIN1);
				AppResetSymbol = 1;
		    } else {
		        /* -------- Clear GPIO bit to 0 -------------------*/
    	        GPIO_ResetPin(GPIOC,PIN0);	
		        GPIO_SetPin(GPIOC,PIN1);
				AppResetSymbol = 0;
            }				
        }
	}
}

TASK(TASK_1)
{    
    while(1) {
	  	//if(ChainTask(TASK_0_ID) == E_OK)
        if(WaitEvent(APP_EVENT_TASK_1) == E_OK) {
            ClearEvent(APP_EVENT_TASK_1);
        }
    }	
}

TASK(TASK_2)
{		
    while(1) {
        if(WaitEvent(APP_EVENT_0) == E_OK) {
            ClearEvent(APP_EVENT_0);           
            if(GetResource(RESOURCE_ID_0) == E_OK) {      
                ReleaseResource(RESOURCE_ID_0);
            }
        }
    }	
}

TASK(TASK_3)
{	   
    while(1) {
        if(GetResource(RESOURCE_ID_0) == E_OK) {
            SetEvent(TASK_2_ID, APP_EVENT_0);
            //SendMessage(MSG_ID_0, &msgBuf[0]);
            //SetEvent(TASK_0_ID, APP_EVENT_TASK_0);
            //SetEvent(TASK_1_ID, APP_EVENT_TASK_1);
	        ReleaseResource(RESOURCE_ID_0);
        }
    }	
}

TASK(TASK_4)
{
    UIP_UCHAR appBuf[APP_MSG_SIZE];

    /* 被uip中断处理程序激活，接收数据，并发送回串口 */
    ReceiveMessage(MSG_ID_0, &appBuf[0]);

    /* 处理接收到的串口数据，例如：可以回送到该串口 */
	//UART_Write(UART0, &appBuf[0], APP_MSG_SIZE);
	
	//Send Test String
	//UART_Send(UART0, "This is UART0 Test String!", 26);
    
    UIP_TerminateTask();	
}

TASK(AppIdleTask)
{
    UIP_INT32  rcount;

    while(1) {
        for (rcount = 0; rcount < 50000; rcount++);
        for (rcount = 0; rcount < 50000; rcount++);
        for (rcount = 0; rcount < 50000; rcount++);
        /* 添加其它系统空闲时运行的逻辑代码 */
        
    }
}

StatusType uipCOMInit(VOID)
{
    return 0;
}

/**********************************************************************/ 
/*                                                                    */ 
/*  Function name                                       Version       */ 
/*                                                                    */ 
/*    Main                                                1.0         */ 
/*                                                                    */ 
/*  Author                                                            */ 
/*                                                                    */ 
/*    Wang Baojin                                                     */ 
/*                                                                    */ 
/*  Description                                                       */ 
/*                                                                    */ 
/*    Application main function.                                      */
/*                                                                    */ 
/*  Parameter(In)                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Parameter(Out)                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Return Status                                                     */ 
/*                                                                    */ 
/*    None                                                            */
/*                                                                    */ 
/*  Call functions                                                    */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Called by                                                         */ 
/*                                                                    */ 
/*    None                                                            */ 
/*                                                                    */ 
/*  Release                                                           */ 
/*                                                                    */ 
/*  author name         release date              Description         */ 
/*                                                                    */ 
/*  Wang Baojin          2007.02.25            Initial Version 1.0    */ 
/*                                                                    */ 
/**********************************************************************/ 
//int main(void)
//{
//    int     mode;

//    Hardware_Init();

//    /* Start RTOS */
//    mode          = uip_KERNEL_RUNNING;
//    uipIdleTaskID = TASK_App_Idle_ID;

//    /* Start RTOS */
//    while (1) {
//        StartOS(mode);
//    }

//    /* Return to exit (never happens) */
//    //return 1;
//}


void dly1us(int dlytime) 
{
    while(dlytime--);
}

/***************************************************************************************/
/*                                 End of File : demo.c                                */
/***************************************************************************************/
