application.c 6.9 KB

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