application.c 6.9 KB
Newer Older
O
onelife.real 已提交
1 2
/***************************************************************************//**
 * @file 	application.c
O
onelife.real 已提交
3
 * @brief 	application tasks
O
onelife.real@gmail.com 已提交
4
 * 	COPYRIGHT (C) 2011, RT-Thread Development Team
O
onelife.real 已提交
5
 * @author 	Bernard, onelife
O
onelife.real 已提交
6 7
 * @version 0.4 beta
 *******************************************************************************
O
onelife.real 已提交
8
 * @section License
O
onelife.real 已提交
9 10 11
 * 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
 *******************************************************************************
O
onelife.real 已提交
12 13
 * @section Change Logs
 * Date			Author		Notes
O
onelife.real 已提交
14
 * 2009-01-05 	Bernard 	first version
O
onelife.real 已提交
15
 * 2010-12-29	onelife		Modify for EFM32
O
onelife.real 已提交
16
 * 2011-05-06	onelife		Add SPI Flash DEMO
O
onelife.real 已提交
17 18
 * 2011-07-15	onelife		Add accelerometer DEMO
 * 2011-07-27	onelife		Modify Ethernet DEMO
O
onelife.real 已提交
19 20
 * 2011-08-23	onelife		Modify Ethernet DEMO according to the changes of 
 *  lwIP API in reversion 1668 
O
onelife.real 已提交
21
 ******************************************************************************/
O
onelife.real 已提交
22
 
O
onelife.real 已提交
23 24 25 26
/***************************************************************************//**
 * @addtogroup efm32
 * @{
 ******************************************************************************/
O
onelife.real 已提交
27

O
onelife.real 已提交
28
/* Includes ------------------------------------------------------------------*/
O
onelife.real 已提交
29 30
#include <board.h>

O
onelife.real 已提交
31
#if defined(RT_USING_DFS)
O
onelife.real 已提交
32 33 34 35 36 37 38 39
/* dfs init */
#include <dfs_init.h>
/* dfs filesystem:ELM filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
#include <dfs_fs.h>
#endif

O
onelife.real 已提交
40
#include "dev_led.h"
O
onelife.real 已提交
41 42 43
#if defined(EFM32_USING_ACCEL)
#include "dev_accel.h"
#endif
O
onelife.real 已提交
44
#if defined(EFM32_USING_SFLASH)
O
onelife.real 已提交
45
#include "dev_sflash.h"
O
onelife.real 已提交
46
#endif
O
onelife.real 已提交
47
#if defined(EFM32_USING_SPISD)
O
onelife.real 已提交
48 49
#include "drv_sdcard.h"
#endif
O
onelife.real 已提交
50 51 52
#if defined(EFM32_USING_ETHERNET)
#include "drv_ethernet.h"
#endif
O
onelife.real 已提交
53

O
onelife.real 已提交
54 55 56 57
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
O
onelife.real 已提交
58
volatile rt_uint32_t	rt_system_status = 0;
O
onelife.real 已提交
59

O
onelife.real 已提交
60 61
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
O
onelife.real 已提交
62 63
void rt_demo_thread_entry(void* parameter)
{
O
onelife.real 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#if 0 //defined(EFM32_USING_ACCEL)
	struct efm32_accel_result_t result;

	rt_kprintf(">>> waiting\n");
	rt_thread_sleep(6000);
	rt_kprintf(">>> start\n");
	while(1)
	{
		efm_accel_get_data(&result);
		rt_kprintf("Accel x: %x\n", result.x);
		rt_kprintf("Accel y: %x\n", result.y);
		rt_kprintf("Accel z: %x\n\n", result.z);
		rt_thread_sleep(200);
	}
#endif

O
onelife.real 已提交
80 81 82 83
#if defined(RT_USING_DFS)
	/* Filesystem Initialization */
	dfs_init();

O
onelife.real 已提交
84
 #if defined(RT_USING_DFS_ELMFAT)
O
onelife.real 已提交
85 86 87
	/* init the elm chan FatFs filesystam*/
	elm_init();

O
onelife.real 已提交
88
  #if defined(EFM32_USING_SPISD)
O
onelife.real 已提交
89 90 91 92 93 94 95 96 97
	/* mount sd card fat partition 1 as root directory */
	if (dfs_mount(SPISD_DEVICE_NAME, "/", "elm", 0, 0) == 0)
	{
		rt_kprintf("FatFs init OK\n");
	}
	else
	{
		rt_kprintf("FatFs init failed!\n");
	}
O
onelife.real 已提交
98 99
  #endif
 #endif
O
onelife.real 已提交
100 101
#endif

O
onelife.real 已提交
102
#ifdef EFM32_USING_SFLASH
O
onelife.real 已提交
103
{
O
onelife.real 已提交
104 105 106 107
	rt_uint8_t i;
	rt_uint8_t test[] = "123456789ABCDEF";
	rt_uint8_t buf[30], buf2[30];

O
onelife.real 已提交
108
	efm_spiFlash_cmd(sflash_inst_rdid_l, EFM32_NO_DATA, buf, sizeof(buf));
O
onelife.real 已提交
109 110 111 112 113 114
	rt_kprintf("Manuf ID: %x\n", buf[0]);
	rt_kprintf("Memory type: %x\n", buf[1]);
	rt_kprintf("Memory capacity: %x\n", buf[2]);
	rt_kprintf("CFD length: %x\n", buf[3]);
	rt_kprintf("CFD: %x%x%x...%x%x\n", buf[4], buf[5], buf[6], buf[18], buf[19]);

O
onelife.real 已提交
115
	efm_spiFlash_cmd(sflash_inst_wren, EFM32_NO_DATA, EFM32_NO_POINTER, EFM32_NO_DATA);
O
onelife.real 已提交
116 117
	do
	{
O
onelife.real 已提交
118
		efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
O
onelife.real 已提交
119 120 121 122 123 124
		rt_kprintf("Status: %x\n", buf2[0]);
	} while (buf2[0] == 0xFF);
	rt_kprintf("Status: %x\n", buf2[0]);

	//efm_spiFash_cmd(sflash_inst_pp, 0x000003F8, test, sizeof(test) - 1);

O
onelife.real 已提交
125
	efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
O
onelife.real 已提交
126 127
	rt_kprintf("Status: %x\n", buf2[0]);
	
O
onelife.real 已提交
128
	efm_spiFlash_cmd(sflash_inst_read, 0x00000300, buf, sizeof(buf));
O
onelife.real 已提交
129 130 131 132 133 134
	rt_kprintf("READ: \n");
	for (i = 0; i < sizeof(buf); i++)
	{
		rt_kprintf("%c\n", buf[i]);
	}

O
onelife.real 已提交
135 136
	//efm_spiFlash_deinit();
}
O
onelife.real 已提交
137 138 139
#endif

#if defined(EFM32_USING_ETHERNET)
O
onelife.real 已提交
140
	extern void lwip_sys_init(void);
O
onelife.real 已提交
141 142 143
	/* init lwip system */
	lwip_sys_init();
	rt_kprintf("TCP/IP stack init OK!\n");
O
onelife.real 已提交
144

O
onelife.real 已提交
145 146 147 148 149 150
 #if defined(EFM32_USING_ETH_HTTPD)
	extern void httpd_init(void);
	/* init http server */
	httpd_init();
	rt_kprintf("Http service init OK!\n");
 #endif /* defined(EFM32_USING_ETH_HTTPD) */
O
onelife.real 已提交
151
#endif /* defined(EFM32_USING_ETHERNET) */
O
onelife.real 已提交
152
	rt_kprintf("Demo End\n");
O
onelife.real 已提交
153 154 155 156 157

	while(1)
	{
		rt_thread_sleep(10);
	}
O
onelife.real 已提交
158 159
}

O
onelife.real 已提交
160 161
void rt_led_thread_entry(void* parameter)
{
O
onelife.real 已提交
162 163
	rt_uint8_t n = 0;

O
onelife.real 已提交
164 165 166 167
	rt_hw_led_on(0);
	rt_hw_led_on(1);
	rt_hw_led_on(2);
	rt_hw_led_on(3);
O
onelife.real 已提交
168 169 170 171 172 173 174 175 176 177 178

	while(1)
	{
		/* Toggle a led per second */
		rt_hw_led_toggle(n++);
		if (n == LEDS_MAX_NUMBER)
		{
			n =0;
		}
		rt_thread_delay(100);
	}
O
onelife.real 已提交
179 180 181 182
}

int rt_application_init()
{
O
onelife.real 已提交
183 184
	rt_thread_t demo_thread, led_thread;

O
onelife.real 已提交
185 186 187 188 189 190 191 192
#if defined(EFM32_USING_ACCEL)
	if (efm_accel_init() != RT_EOK)
	{
		rt_kprintf("*** Init accelerometer driver failed!");
		while(1); //Or do something?
	}
#endif

O
onelife.real 已提交
193
#if defined(EFM32_USING_SFLASH)
O
onelife.real 已提交
194 195 196 197 198
	if (efm_spiFlash_init() != RT_EOK)
	{
		rt_kprintf("*** Init SPI Flash driver failed!");
		while(1); //Or do something?
	}
O
onelife.real 已提交
199 200 201
#endif

#if defined(EFM32_USING_SPISD)
O
onelife.real 已提交
202 203 204 205 206
	if (efm_spiSd_init() != RT_EOK)
	{
		rt_kprintf("*** Init SD card driver failed!");
		while(1); //Or do something?
	}
O
onelife.real 已提交
207
#endif
O
onelife.real@gmail.com 已提交
208 209 210 211

	/* Initialize all device drivers (dev_?.c) */
	if (rt_hw_led_init() != RT_EOK)
	{
O
onelife.real 已提交
212
		rt_kprintf("*** Init LED driver failed!");
O
onelife.real@gmail.com 已提交
213 214
		while(1); //Or do something?
	}
O
onelife.real 已提交
215
#if defined(RT_USING_MISC)
O
onelife.real@gmail.com 已提交
216 217
	if (rt_hw_misc_init() != RT_EOK)
	{
O
onelife.real 已提交
218
		rt_kprintf("*** Init miscellaneous driver failed!");
O
onelife.real@gmail.com 已提交
219 220
		while(1); //Or do something?
	}
O
onelife.real 已提交
221
#endif
O
onelife.real 已提交
222

O
onelife.real 已提交
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
#if defined(RT_USING_LWIP)
	{
		/* Create Ethernet Threads */
		if (eth_system_device_init() != RT_EOK)
		{
			rt_kprintf("*** Create Ethernet threads failed!");
			while(1); //Or do something?
		}
 #if defined(EFM32_USING_ETHERNET)
		if (efm_hw_eth_init() != RT_EOK)
		{
			rt_kprintf("*** Init Ethernet driver failed!");
			while(1); //Or do something?
		}
 #endif
	}
#endif

O
onelife.real 已提交
241
#if (RT_THREAD_PRIORITY_MAX == 32)
O
onelife.real 已提交
242 243 244 245
	demo_thread = rt_thread_create(
		"demo",
		rt_demo_thread_entry, 
		RT_NULL,
O
onelife.real 已提交
246
		1024, 
O
onelife.real 已提交
247 248 249
		3, 
		20);

O
onelife.real 已提交
250 251 252 253
	led_thread = rt_thread_create(
		"led",
		rt_led_thread_entry, 
		RT_NULL,
O
onelife.real 已提交
254
		256,
O
onelife.real 已提交
255 256 257 258 259
		3, 
		20);
#else
#endif

O
onelife.real 已提交
260 261 262 263 264 265
	if(demo_thread != RT_NULL)
	{
		rt_kprintf("demo sp:%x\n", demo_thread->sp);
		rt_thread_startup(demo_thread);
	}

O
onelife.real 已提交
266 267 268 269 270 271 272 273
	if(led_thread != RT_NULL)
	{
		rt_thread_startup(led_thread);
	}

	return 0;
}

O
onelife.real 已提交
274
/***************************************************************************//**
O
onelife.real 已提交
275
 * @}
O
onelife.real 已提交
276
 ******************************************************************************/