application.c 6.1 KB

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