application.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /***************************************************************************//**
  2. * @file application.c
  3. * @brief application tasks
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author Bernard, onelife
  6. * @version 0.4 beta
  7. *******************************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file
  10. * LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
  11. *******************************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2009-01-05 Bernard first version
  15. * 2010-12-29 onelife Modify for EFM32
  16. * 2011-05-06 onelife Add SPI Flash DEMO
  17. * 2011-07-15 onelife Add accelerometer DEMO
  18. * 2011-07-27 onelife Modify Ethernet DEMO
  19. ******************************************************************************/
  20. /***************************************************************************//**
  21. * @addtogroup efm32
  22. * @{
  23. ******************************************************************************/
  24. /* Includes ------------------------------------------------------------------*/
  25. #include <board.h>
  26. #if defined(RT_USING_DFS)
  27. /* dfs init */
  28. #include <dfs_init.h>
  29. /* dfs filesystem:ELM filesystem init */
  30. #include <dfs_elm.h>
  31. /* dfs Filesystem APIs */
  32. #include <dfs_fs.h>
  33. #endif
  34. #include "dev_led.h"
  35. #if defined(EFM32_USING_ACCEL)
  36. #include "dev_accel.h"
  37. #endif
  38. #if defined(EFM32_USING_SFLASH)
  39. #include "dev_sflash.h"
  40. #endif
  41. #if defined(EFM32_USING_SPISD)
  42. #include "drv_sdcard.h"
  43. #endif
  44. #if defined(EFM32_USING_ETHERNET)
  45. #include "drv_ethernet.h"
  46. #endif
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* Private define ------------------------------------------------------------*/
  49. /* Private macro -------------------------------------------------------------*/
  50. /* Private variables ---------------------------------------------------------*/
  51. volatile rt_uint32_t rt_system_status = 0;
  52. /* Private function prototypes -----------------------------------------------*/
  53. /* Private functions ---------------------------------------------------------*/
  54. void rt_demo_thread_entry(void* parameter)
  55. {
  56. #if 0 //defined(EFM32_USING_ACCEL)
  57. struct efm32_accel_result_t result;
  58. rt_kprintf(">>> waiting\n");
  59. rt_thread_sleep(6000);
  60. rt_kprintf(">>> start\n");
  61. while(1)
  62. {
  63. efm_accel_get_data(&result);
  64. rt_kprintf("Accel x: %x\n", result.x);
  65. rt_kprintf("Accel y: %x\n", result.y);
  66. rt_kprintf("Accel z: %x\n\n", result.z);
  67. rt_thread_sleep(200);
  68. }
  69. #endif
  70. #if defined(RT_USING_DFS)
  71. /* Filesystem Initialization */
  72. dfs_init();
  73. #if defined(RT_USING_DFS_ELMFAT)
  74. /* init the elm chan FatFs filesystam*/
  75. elm_init();
  76. #if defined(EFM32_USING_SPISD)
  77. /* mount sd card fat partition 1 as root directory */
  78. if (dfs_mount(SPISD_DEVICE_NAME, "/", "elm", 0, 0) == 0)
  79. {
  80. rt_kprintf("FatFs init OK\n");
  81. }
  82. else
  83. {
  84. rt_kprintf("FatFs init failed!\n");
  85. }
  86. #endif
  87. #endif
  88. #endif
  89. #ifdef EFM32_USING_SFLASH
  90. {
  91. rt_uint8_t i;
  92. rt_uint8_t test[] = "123456789ABCDEF";
  93. rt_uint8_t buf[30], buf2[30];
  94. efm_spiFlash_cmd(sflash_inst_rdid_l, EFM32_NO_DATA, buf, sizeof(buf));
  95. rt_kprintf("Manuf ID: %x\n", buf[0]);
  96. rt_kprintf("Memory type: %x\n", buf[1]);
  97. rt_kprintf("Memory capacity: %x\n", buf[2]);
  98. rt_kprintf("CFD length: %x\n", buf[3]);
  99. rt_kprintf("CFD: %x%x%x...%x%x\n", buf[4], buf[5], buf[6], buf[18], buf[19]);
  100. efm_spiFlash_cmd(sflash_inst_wren, EFM32_NO_DATA, EFM32_NO_POINTER, EFM32_NO_DATA);
  101. do
  102. {
  103. efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
  104. rt_kprintf("Status: %x\n", buf2[0]);
  105. } while (buf2[0] == 0xFF);
  106. rt_kprintf("Status: %x\n", buf2[0]);
  107. //efm_spiFash_cmd(sflash_inst_pp, 0x000003F8, test, sizeof(test) - 1);
  108. efm_spiFlash_cmd(sflash_inst_rdsr, EFM32_NO_DATA, buf2, sizeof(buf2));
  109. rt_kprintf("Status: %x\n", buf2[0]);
  110. efm_spiFlash_cmd(sflash_inst_read, 0x00000300, buf, sizeof(buf));
  111. rt_kprintf("READ: \n");
  112. for (i = 0; i < sizeof(buf); i++)
  113. {
  114. rt_kprintf("%c\n", buf[i]);
  115. }
  116. //efm_spiFlash_deinit();
  117. }
  118. #endif
  119. #if defined(EFM32_USING_ETHERNET)
  120. extern void lwip_sys_init(void);
  121. #if defined(EFM32_USING_ETH_HTTPD)
  122. extern void httpd_init(void);
  123. #endif
  124. rt_device_t eth = RT_NULL;
  125. /* find Ethernet device */
  126. eth = rt_device_find(ETH_DEVICE_NAME);
  127. if (eth != RT_NULL)
  128. {
  129. /* init Ethernet device */
  130. eth->init(eth);
  131. rt_kprintf("Ethernet init OK!\n");
  132. /* init lwip system */
  133. lwip_sys_init();
  134. rt_kprintf("TCP/IP stack init OK!\n");
  135. #if defined(EFM32_USING_ETH_HTTPD)
  136. /* init http server */
  137. httpd_init();
  138. rt_kprintf("Http service init OK!\n");
  139. #endif
  140. }
  141. else
  142. {
  143. rt_kprintf("%s is not found\n"), ETH_DEVICE_NAME;
  144. }
  145. #endif /* defined(EFM32_USING_ETHERNET) */
  146. rt_kprintf("Demo End\n");
  147. while(1)
  148. {
  149. rt_thread_sleep(10);
  150. }
  151. }
  152. void rt_led_thread_entry(void* parameter)
  153. {
  154. rt_uint8_t n = 0;
  155. rt_hw_led_on(0);
  156. rt_hw_led_on(1);
  157. rt_hw_led_on(2);
  158. rt_hw_led_on(3);
  159. while(1)
  160. {
  161. /* Toggle a led per second */
  162. rt_hw_led_toggle(n++);
  163. if (n == LEDS_MAX_NUMBER)
  164. {
  165. n =0;
  166. }
  167. rt_thread_delay(100);
  168. }
  169. }
  170. int rt_application_init()
  171. {
  172. rt_thread_t demo_thread, led_thread;
  173. #if defined(EFM32_USING_ACCEL)
  174. if (efm_accel_init() != RT_EOK)
  175. {
  176. rt_kprintf("*** Init accelerometer driver failed!");
  177. while(1); //Or do something?
  178. }
  179. #endif
  180. #if defined(EFM32_USING_SFLASH)
  181. if (efm_spiFlash_init() != RT_EOK)
  182. {
  183. rt_kprintf("*** Init SPI Flash driver failed!");
  184. while(1); //Or do something?
  185. }
  186. #endif
  187. #if defined(EFM32_USING_SPISD)
  188. if (efm_spiSd_init() != RT_EOK)
  189. {
  190. rt_kprintf("*** Init SD card driver failed!");
  191. while(1); //Or do something?
  192. }
  193. #endif
  194. /* Initialize all device drivers (dev_?.c) */
  195. if (rt_hw_led_init() != RT_EOK)
  196. {
  197. rt_kprintf("*** Init LED driver failed!");
  198. while(1); //Or do something?
  199. }
  200. #if defined(RT_USING_MISC)
  201. if (rt_hw_misc_init() != RT_EOK)
  202. {
  203. rt_kprintf("*** Init miscellaneous driver failed!");
  204. while(1); //Or do something?
  205. }
  206. #endif
  207. #if defined(RT_USING_LWIP)
  208. {
  209. /* Create Ethernet Threads */
  210. if (eth_system_device_init() != RT_EOK)
  211. {
  212. rt_kprintf("*** Create Ethernet threads failed!");
  213. while(1); //Or do something?
  214. }
  215. #if defined(EFM32_USING_ETHERNET)
  216. if (efm_hw_eth_init() != RT_EOK)
  217. {
  218. rt_kprintf("*** Init Ethernet driver failed!");
  219. while(1); //Or do something?
  220. }
  221. #endif
  222. }
  223. #endif
  224. #if (RT_THREAD_PRIORITY_MAX == 32)
  225. demo_thread = rt_thread_create(
  226. "demo",
  227. rt_demo_thread_entry,
  228. RT_NULL,
  229. 1024,
  230. 3,
  231. 20);
  232. led_thread = rt_thread_create(
  233. "led",
  234. rt_led_thread_entry,
  235. RT_NULL,
  236. 256,
  237. 3,
  238. 20);
  239. #else
  240. #endif
  241. if(demo_thread != RT_NULL)
  242. {
  243. rt_kprintf("demo sp:%x\n", demo_thread->sp);
  244. rt_thread_startup(demo_thread);
  245. }
  246. if(led_thread != RT_NULL)
  247. {
  248. rt_thread_startup(led_thread);
  249. }
  250. return 0;
  251. }
  252. /***************************************************************************//**
  253. * @}
  254. ******************************************************************************/