application.c 3.9 KB
Newer Older
M
Ming, Bai 已提交
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
/*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2009, 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
 * 2010-05-02     Aozima       add led function
 */

#include <rtthread.h>

#include <board.h>

#ifdef RT_USING_DFS
/* dfs init */
#include <dfs_init.h>
/* dfs filesystem:ELM FatFs 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/driver.h>
#endif

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

        /* init the elm FAT 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 init failed!\n");
    }
#endif

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

        eth_system_device_init();

        /* register ethernetif device */
        lpc17xx_emac_hw_init();

        /* 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 application_init(void);
		
		rt_device_t lcd;

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

		/* find lcd device */
		lcd = rt_device_find("lcd");
		if (lcd != RT_NULL)
		{
			/* set lcd device as rtgui graphic driver */
			rtgui_graphic_set_device(lcd);

			/* init rtgui system server */
			rtgui_system_server_init();

			/* startup rtgui in demo of RT-Thread/GUI examples */
			application_init();
		}
    }
#endif
}

// init led
#define rt_hw_led_init()   LPC_GPIO2->DIR |= 1<<25;
// trun on led n
#define rt_hw_led_on(n)    LPC_GPIO2->CLR |= 1<<25;
// trun off led n
#define rt_hw_led_off(n)   LPC_GPIO2->SET |= 1<<25;

ALIGN(RT_ALIGN_SIZE)
static char thread_led_stack[1024];
struct rt_thread thread_led;
static void rt_thread_entry_led(void* parameter)
{
    unsigned int count=0;

    rt_hw_led_init();

    while (1)
    {
        /* led on */
#ifndef RT_USING_FINSH
        rt_kprintf("led on,count : %d\r\n",count);
#endif
        count++;
        rt_hw_led_on(1);
        /* sleep 0.5 second and switch to other thread */
        rt_thread_delay(RT_TICK_PER_SECOND/2);

        /* led off */
#ifndef RT_USING_FINSH
        rt_kprintf("led off\r\n");
#endif
        rt_hw_led_off(1);
        rt_thread_delay(RT_TICK_PER_SECOND/2);
    }
}

int rt_application_init(void)
{
	rt_thread_t tid;

	rt_thread_init(&thread_led,
			"led",
			rt_thread_entry_led,
			RT_NULL,
			&thread_led_stack[0],
			sizeof(thread_led_stack),11,5);
	rt_thread_startup(&thread_led);

	tid = rt_thread_create("init",
			rt_init_thread_entry, RT_NULL,
			2048, RT_THREAD_PRIORITY_MAX/3, 20);
	if (tid != RT_NULL) rt_thread_startup(tid);

	return 0;
}

#if defined(RT_USING_RTGUI) && defined(RT_USING_FINSH)
#include <rtgui/rtgui_server.h>
#include <rtgui/event.h>
#include <rtgui/kbddef.h>

#include <finsh.h>

void key(rt_uint32_t key)
{
	struct rtgui_event_kbd ekbd;

	RTGUI_EVENT_KBD_INIT(&ekbd);
	ekbd.mod  = RTGUI_KMOD_NONE;
	ekbd.unicode = 0;
	ekbd.key = key;

	ekbd.type = RTGUI_KEYDOWN;
	rtgui_server_post_event((struct rtgui_event*)&ekbd, sizeof(ekbd));

	rt_thread_delay(2);

	ekbd.type = RTGUI_KEYUP;
	rtgui_server_post_event((struct rtgui_event*)&ekbd, sizeof(ekbd));
}
FINSH_FUNCTION_EXPORT(key, send a key to gui server);
#endif