
/*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 */

/**
 * @addtogroup STM32
 */
/*@{*/

#include <board.h>
#include <rtthread.h>

#ifdef RT_USING_DFS
/* dfs init */
#include <dfs_init.h>
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif

#ifdef RT_USING_LWIP
#include <lwip/sys.h>
#include <lwip/api.h>
#include <netif/ethernetif.h>
#endif

#ifdef RT_USING_RTGUI
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_server.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/driver.h>
#endif

/*
*事件例程，用来演示RTT的事件
*创建三个线程，两静态线程用于控制LED，一动态线程用于控制KEY
*按KEY0，红灯闪烁5次；按KEY1，绿灯闪烁5次；WK_UP无效！
*RTT事件是一个32位无符号整形变量
*/

ALIGN(RT_ALIGN_SIZE)
static rt_uint8_t led_stack[ 256 ];//静态线程栈
static struct rt_thread ledR_thread;//控制块

static rt_uint8_t ledG_stack[ 256 ];
static struct rt_thread ledG_thread;

static struct rt_event key_event;//静态事件控制块

static void ledR_thread_entry(void* parameter)//静态线程，红灯
{
	rt_uint8_t count;
	rt_uint32_t e;
    rt_hw_led_init();
	while(1)
	{
		if (rt_event_recv(&key_event, (1<<0),//接收事件，若不存在则挂起 PS：单事件，仍然需要用OR（或）
			RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&e) == RT_EOK)//0号事件有效
		{
			for(count=5;count>0;count--)
			{
				/* led1 on */
				rt_hw_led_on(0);
				rt_thread_delay(25);//延时25个时间片，0.25s

				/* led1 off */
				rt_hw_led_off(0);
				rt_thread_delay(25);
			}
		}	
	}
}

static void ledG_thread_entry(void* parameter)//静态线程，绿灯
{
	rt_uint8_t count;
	rt_uint32_t e;
    rt_hw_led_init();
	while(1)
	{
		if(rt_event_recv(&key_event,(1<<1),//1号事件有效
			RT_EVENT_FLAG_OR|RT_EVENT_FLAG_CLEAR,RT_WAITING_FOREVER,&e) == RT_EOK)
		{
			for(count=5;count>0;count--)
			{
				/* led1 on */
				rt_hw_led_on(1);
				rt_thread_delay(30);//延时30个时间片，0.3s

				/* led1 off */
				rt_hw_led_off(1);
				rt_thread_delay(30);
			}
		}
	}
}

static void KEYPr_thread_entry(void* parameter)//动态线程
{
	rt_uint32_t key = 0;
	KEY_Init();
	while(1)
	{	
		key = KEY_Scan(0);
		switch(key)
		{
			case 1:{rt_event_send(&key_event,1<<0);}break;//置位0号事件
			case 2:{rt_event_send(&key_event,1<<1);}break;//置位1号事件
			default:break;
		}

		rt_thread_delay(5);
	}
}

void rt_init_thread_entry(void* parameter)
{
/* Filesystem Initialization */
#ifdef RT_USING_DFS
	{
		/* init the device filesystem */
		dfs_init();

#ifdef RT_USING_DFS_ELMFAT
		/* init the elm chan FatFs filesystam*/
		elm_init();

		/* mount sd card fat partition 1 as root directory */
		if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
		{
			rt_kprintf("File System initialized!\n");
		}
		else
			rt_kprintf("File System initialzation failed!\n");
#endif
	}
#endif

/* LwIP Initialization */
#ifdef RT_USING_LWIP
	{
		extern void lwip_sys_init(void);

		/* register ethernetif device */
		eth_system_device_init();

#ifdef STM32F10X_CL
		rt_hw_stm32_eth_init();
#else
	/* STM32F103 */
	#if STM32_ETH_IF == 0
			rt_hw_enc28j60_init();
	#elif STM32_ETH_IF == 1
			rt_hw_dm9000_init();
	#endif
#endif

		/* re-init device driver */
		rt_device_init_all();

		/* init lwip system */
		lwip_sys_init();
		rt_kprintf("TCP/IP initialized!\n");
	}
#endif

#ifdef RT_USING_RTGUI
	{
	    extern void rtgui_system_server_init(void);
	    extern void rt_hw_lcd_init();
	    extern void rtgui_touch_hw_init(void);

		rt_device_t lcd;

		/* init lcd */
		rt_hw_lcd_init();

		/* init touch panel */
		rtgui_touch_hw_init();

		/* re-init device driver */
		rt_device_init_all();

		/* find lcd device */
		lcd = rt_device_find("lcd");

		/* set lcd device as rtgui graphic driver */
		rtgui_graphic_set_device(lcd);

		/* init rtgui system server */
		rtgui_system_server_init();
	}
#endif /* #ifdef RT_USING_RTGUI */
}

int rt_application_init()
{
	rt_err_t int_result;
	rt_thread_t create_result;
	
	rt_event_init(&key_event, "key_event", RT_IPC_FLAG_FIFO);//初始化事件对象
	
	create_result = rt_thread_create("KEYSc",KEYPr_thread_entry,  //创建一个动态线程
	RT_NULL,512,18,5);
	if (create_result !=RT_NULL) rt_thread_startup(create_result);//启动线程
	
	int_result = rt_thread_init(&ledR_thread,                     //创建一个静态线程
		"ledR",ledR_thread_entry, RT_NULL,
		(rt_uint8_t*)&led_stack[0], sizeof(led_stack), 20, 5);
	if (int_result == RT_EOK) rt_thread_startup(&ledR_thread);    //启动线程

	int_result = rt_thread_init(&ledG_thread,                     //创建一个静态线程
		"ledG",ledG_thread_entry, RT_NULL,
		(rt_uint8_t*)&ledG_stack[0], sizeof(ledG_stack), 20, 5);
	if (int_result == RT_EOK) rt_thread_startup(&ledG_thread);    //启动线程
	
#if (RT_THREAD_PRIORITY_MAX == 32)
	create_result = rt_thread_create("init",
								rt_init_thread_entry, RT_NULL,
								2048, 8, 20);
#else
	create_result = rt_thread_create("init",
								rt_init_thread_entry, RT_NULL,
								2048, 80, 20);
#endif

	if (create_result != RT_NULL) rt_thread_startup(create_result);

	return 0;
}

/*@}*/
