drv_wlan.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * File : drv_wlan.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2018-08-30 ZeroFree the first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #ifdef RT_USING_WIFI
  17. #include <drv_common.h>
  18. #include <wlan_mgnt.h>
  19. #include <wlan_prot.h>
  20. #include <wlan_cfg.h>
  21. #include <string.h>
  22. #include <fal.h>
  23. #define DBG_ENABLE
  24. #define DBG_SECTION_NAME "WLAN"
  25. #define DBG_COLOR
  26. #define DBG_LEVEL DBG_LOG
  27. #include <rtdbg.h>
  28. #define NVRAM_GENERATED_MAC_ADDRESS "macaddr=02:0A:F7:fe:86:1c"
  29. #define WIFI_IMAGE_PARTITION_NAME "wifi_image"
  30. #define WIFI_INIT_THREAD_STACK_SIZE (1024 * 4)
  31. #define WIFI_INIT_THREAD_PRIORITY (RT_THREAD_PRIORITY_MAX/2)
  32. #define WIFI_INIT_WAIT_TIME (rt_tick_from_millisecond(1000))
  33. extern int wifi_hw_init(void);
  34. static rt_bool_t init_flag = 0;
  35. static const struct fal_partition *partition = RT_NULL;
  36. ALIGN(64)
  37. static const char wifi_nvram_image[] =
  38. // # The following parameter values are just placeholders, need to be updated.
  39. "manfid=0x2d0" "\x00"
  40. "prodid=0x0726" "\x00"
  41. "vendid=0x14e4" "\x00"
  42. "devid=0x43e2" "\x00"
  43. "boardtype=0x0726" "\x00"
  44. "boardrev=0x1101" "\x00"
  45. "boardnum=22" "\x00"
  46. "xtalfreq=26000" "\x00"
  47. "sromrev=11" "\x00"
  48. "boardflags=0x00404201" "\x00"
  49. "boardflags3=0x08000000" "\x00"
  50. NVRAM_GENERATED_MAC_ADDRESS "\x00"
  51. "nocrc=1" "\x00"
  52. "ag0=255" "\x00"
  53. "aa2g=1" "\x00"
  54. "ccode=ALL"
  55. "\x00"
  56. "swdiv_en=1" "\x00"
  57. "swdiv_gpio=2" "\x00"
  58. "pa0itssit=0x20" "\x00"
  59. "extpagain2g=0" "\x00"
  60. "pa2ga0=-215,5267,-656" "\x00"
  61. "AvVmid_c0=0x0,0xc8" "\x00"
  62. "cckpwroffset0=5" "\x00"
  63. "maxp2ga0=80" "\x00"
  64. "txpwrbckof=6" "\x00"
  65. "cckbw202gpo=0x6666" "\x00"
  66. "legofdmbw202gpo=0xaaaaaaaa" "\x00"
  67. "mcsbw202gpo=0xbbbbbbbb" "\x00"
  68. "propbw202gpo=0xdd" "\x00"
  69. "ofdmdigfilttype=18" "\x00"
  70. "ofdmdigfilttypebe=18" "\x00"
  71. "papdmode=1" "\x00"
  72. "papdvalidtest=1" "\x00"
  73. "pacalidx2g=32" "\x00"
  74. "papdepsoffset=-36" "\x00"
  75. "papdendidx=61" "\x00"
  76. "wl0id=0x431b" "\x00"
  77. "deadman_to=0xffffffff" "\x00"
  78. "muxenab=0x11" "\x00"
  79. "spurconfig=0x3" "\x00"
  80. "\x00\x00";
  81. int wiced_platform_resource_size(int resource)
  82. {
  83. int size = 0;
  84. /* Download firmware */
  85. if (resource == 0)
  86. {
  87. size = 355159;
  88. }
  89. else if (resource == 1)
  90. {
  91. size = sizeof(wifi_nvram_image);
  92. }
  93. return size;
  94. }
  95. int wiced_platform_resource_read(int resource, uint32_t offset, void* buffer, uint32_t buffer_size)
  96. {
  97. if (resource == 0)
  98. {
  99. /* read RF firmware from partition */
  100. fal_partition_read(partition, offset, buffer, buffer_size);
  101. }
  102. else if (resource == 1)
  103. {
  104. memcpy(buffer, &wifi_nvram_image[offset], buffer_size);
  105. }
  106. return buffer_size;
  107. }
  108. /**
  109. * wait milliseconds for wifi low level initialize complete
  110. *
  111. * time_ms: timeout in milliseconds
  112. */
  113. int rt_hw_wlan_wait_init_done(rt_uint32_t time_ms)
  114. {
  115. rt_uint32_t time_cnt = 0;
  116. /* wait wifi low level initialize complete */
  117. while (time_cnt <= (time_ms / 100))
  118. {
  119. time_cnt++;
  120. rt_thread_mdelay(100);
  121. if (init_flag == 1)
  122. {
  123. break;
  124. }
  125. }
  126. if (time_cnt > (time_ms / 100))
  127. {
  128. return -RT_ETIMEOUT;
  129. }
  130. return RT_EOK;
  131. }
  132. static void wifi_init_thread_entry(void *parameter)
  133. {
  134. /* initialize fal */
  135. fal_init();
  136. partition = fal_partition_find(WIFI_IMAGE_PARTITION_NAME);
  137. if (partition == RT_NULL)
  138. {
  139. LOG_E("%s partition is not exist, please check your configuration!", WIFI_IMAGE_PARTITION_NAME);
  140. return;
  141. }
  142. /* initialize low level wifi(ap6212) library */
  143. wifi_hw_init();
  144. /* waiting for sdio bus stability */
  145. rt_thread_delay(WIFI_INIT_WAIT_TIME);
  146. /* set wifi work mode */
  147. rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
  148. /* NEED TODO !!! */
  149. /* rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP); */
  150. init_flag = 1;
  151. }
  152. int rt_hw_wlan_init(void)
  153. {
  154. if (init_flag == 1)
  155. {
  156. return RT_EOK;
  157. }
  158. rt_thread_t tid = RT_NULL;
  159. tid = rt_thread_create("wifi_init", wifi_init_thread_entry, RT_NULL, WIFI_INIT_THREAD_STACK_SIZE, WIFI_INIT_THREAD_PRIORITY, 20);
  160. if (tid)
  161. {
  162. rt_thread_startup(tid);
  163. }
  164. else
  165. {
  166. LOG_E("Create wifi initialization thread fail!");
  167. return -RT_ERROR;
  168. }
  169. return RT_EOK;
  170. }
  171. INIT_APP_EXPORT(rt_hw_wlan_init);
  172. static int ap6212_init(void)
  173. {
  174. #define AP6212_WL_REG_ON GET_PIN(C, 13)
  175. /* enable the WLAN REG pin */
  176. rt_pin_mode(AP6212_WL_REG_ON, PIN_MODE_OUTPUT);
  177. rt_pin_write(AP6212_WL_REG_ON, PIN_HIGH);
  178. return 0;
  179. }
  180. INIT_DEVICE_EXPORT(ap6212_init);
  181. #endif /* RT_USING_WIFI */