drv_wlan.c 6.7 KB

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