bl_wifi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #undef ARCH_RISCV
  31. #include <rtthread.h>
  32. #include <stdint.h>
  33. #include <string.h>
  34. #include <stdio.h>
  35. //FIXME no BL808/BL606p header file including is Allowed here
  36. #ifdef BL808
  37. #include <bl808.h>
  38. #include <bl808_glb.h>
  39. #elif defined(BL606P)
  40. #include <bl606p.h>
  41. #include <bl606p_glb.h>
  42. #else
  43. #error "Use CHIP BL808/BL606P for this module"
  44. #endif
  45. #include "bl_wifi.h"
  46. // #include "bl_irq.h"
  47. typedef struct _bl_wifi_env {
  48. uint8_t sta_mac_addr_board[6];
  49. uint8_t sta_mac_addr_usr[6];
  50. uint8_t ap_mac_addr_board[6];
  51. uint8_t ap_mac_addr_usr[6];
  52. uint8_t country_code;
  53. bl_wifi_ap_info_t ap_info;
  54. uint8_t ap_info_en;
  55. bl_wifi_ap_info_t sta_info;
  56. uint8_t sta_info_en;
  57. } bl_wifi_env_t;
  58. bl_wifi_env_t wifi_env = {
  59. .sta_mac_addr_board = {0x18, 0xb9, 0x05, 0x88, 0x88, 0x88}
  60. };
  61. /****************************************************************************/ /**
  62. * @brief set wifi core clock divider
  63. *
  64. * @param clkDiv: divider
  65. *
  66. * @return SUCCESS or ERROR
  67. *
  68. *******************************************************************************/
  69. /* 0x3B0 : wifi_cfg0 */
  70. #define GLB_WIFI_CFG0_OFFSET (0x3B0)
  71. #define GLB_WIFI_MAC_CORE_DIV GLB_WIFI_MAC_CORE_DIV
  72. #define GLB_WIFI_MAC_CORE_DIV_POS (0U)
  73. #define GLB_WIFI_MAC_CORE_DIV_LEN (4U)
  74. #define GLB_WIFI_MAC_CORE_DIV_MSK (((1U<<GLB_WIFI_MAC_CORE_DIV_LEN)-1)<<GLB_WIFI_MAC_CORE_DIV_POS)
  75. #define GLB_WIFI_MAC_CORE_DIV_UMSK (~(((1U<<GLB_WIFI_MAC_CORE_DIV_LEN)-1)<<GLB_WIFI_MAC_CORE_DIV_POS))
  76. #define GLB_WIFI_MAC_WT_DIV GLB_WIFI_MAC_WT_DIV
  77. #define GLB_WIFI_MAC_WT_DIV_POS (4U)
  78. #define GLB_WIFI_MAC_WT_DIV_LEN (4U)
  79. #define GLB_WIFI_MAC_WT_DIV_MSK (((1U<<GLB_WIFI_MAC_WT_DIV_LEN)-1)<<GLB_WIFI_MAC_WT_DIV_POS)
  80. #define GLB_WIFI_MAC_WT_DIV_UMSK (~(((1U<<GLB_WIFI_MAC_WT_DIV_LEN)-1)<<GLB_WIFI_MAC_WT_DIV_POS))
  81. BL_Err_Type GLB_Set_WiFi_Core_CLK(uint8_t clkDiv)
  82. {
  83. uint32_t tmpVal = 0;
  84. CHECK_PARAM((clkDiv <= 0xF));
  85. tmpVal = BL_RD_REG(GLB_BASE, GLB_WIFI_CFG0);
  86. tmpVal = BL_SET_REG_BITS_VAL(tmpVal, GLB_WIFI_MAC_CORE_DIV, clkDiv);
  87. BL_WR_REG(GLB_BASE, GLB_WIFI_CFG0, tmpVal);
  88. return SUCCESS;
  89. }
  90. int bl_wifi_clock_enable(void)
  91. {
  92. static int called = 0;
  93. if (0 == called) {
  94. called = 1;
  95. //GLB_Set_System_CLK(PLL_XTAL_38P4M, GLB_PLL_CLK_160M);
  96. GLB_Set_WiFi_Core_CLK(1);//0: 80MHZ, 1: 40MHZ
  97. }
  98. return 0;
  99. }
  100. void mac_irq(void);
  101. void bl_irq_handler(void);
  102. void mac_irq_callback(int irq, void *arg)
  103. {
  104. mac_irq();
  105. }
  106. void bl_irq_callback(int irq, void *arg)
  107. {
  108. bl_irq_handler();
  109. }
  110. int bl_wifi_enable_irq(void)
  111. {
  112. // bl_irq_register(WIFI_IRQn, mac_irq);
  113. // bl_irq_register(WIFI_IPC_PUBLIC_IRQn, bl_irq_handler);
  114. // bl_irq_enable(WIFI_IRQn);
  115. // bl_irq_enable(WIFI_IPC_PUBLIC_IRQn);
  116. bflb_irq_attach(WIFI_IRQn, mac_irq_callback, NULL);
  117. bflb_irq_attach(WIFI_IPC_PUBLIC_IRQn, bl_irq_callback, NULL);
  118. bflb_irq_enable(WIFI_IRQn);
  119. bflb_irq_enable(WIFI_IPC_PUBLIC_IRQn);
  120. //NVIC_SetPriority((IRQn_Type)5, 0);
  121. //NVIC_EnableIRQ((IRQn_Type)5);
  122. puts("Enable BMX IRQ\r\n");
  123. //NVIC_EnableIRQ((IRQn_Type)0);
  124. //NVIC_EnableIRQ((IRQn_Type)1);
  125. //NVIC_EnableIRQ((IRQn_Type)2);
  126. //NVIC_EnableIRQ((IRQn_Type)3);
  127. //NVIC_EnableIRQ((IRQn_Type)4);
  128. //*(uint32_t*)0x40000050 = ((0xF << 28) | (1 << 24));
  129. return 0;
  130. }
  131. int bl_wifi_sta_mac_addr_set(uint8_t mac[6])
  132. {
  133. memcpy(wifi_env.sta_mac_addr_board, mac, 6);
  134. return 0;
  135. }
  136. int bl_wifi_ap_mac_addr_set(uint8_t mac[6])
  137. {
  138. memcpy(wifi_env.ap_mac_addr_board, mac, 6);
  139. return 0;
  140. }
  141. int bl_wifi_mac_addr_set(uint8_t mac[6])
  142. {
  143. memcpy(wifi_env.sta_mac_addr_usr, mac, 6);
  144. return 0;
  145. }
  146. int bl_wifi_mac_addr_get(uint8_t mac[6])
  147. {
  148. memcpy(mac, wifi_env.sta_mac_addr_board, 6);
  149. return 0;
  150. }
  151. int bl_wifi_country_code_set(uint8_t country_code)
  152. {
  153. wifi_env.country_code = country_code;
  154. return 0;
  155. }
  156. #if 0
  157. int bl_wifi_power_table_set(bl_tx_pwr_tbl_t* tx_pwr_tbl)
  158. {
  159. bl60x_fw_rf_tx_power_table_set(tx_pwr_tbl);
  160. return 0;
  161. }
  162. #endif
  163. int bl_wifi_ap_info_set(uint8_t* ssid, uint8_t ssid_len,
  164. uint8_t* psk, uint8_t psk_len,
  165. uint8_t chan)
  166. {
  167. memset(&wifi_env.ap_info, 0, sizeof(bl_wifi_ap_info_t));
  168. memcpy(wifi_env.ap_info.ssid, ssid, ssid_len);
  169. memcpy(wifi_env.ap_info.psk, psk, psk_len);
  170. wifi_env.ap_info.chan = chan;
  171. wifi_env.ap_info_en = 1;
  172. return 0;
  173. }
  174. int bl_wifi_ap_info_get(bl_wifi_ap_info_t* ap_info)
  175. {
  176. if (wifi_env.ap_info_en != 1) {
  177. return -1;
  178. }
  179. memcpy(ap_info, &wifi_env.ap_info, sizeof(bl_wifi_ap_info_t));
  180. return 0;
  181. }
  182. int bl_wifi_sta_info_set(uint8_t* ssid, uint8_t ssid_len, uint8_t* psk, uint8_t psk_len, int autoconnect)
  183. {
  184. memset(&wifi_env.sta_info, 0, sizeof(bl_wifi_ap_info_t));
  185. memcpy(wifi_env.sta_info.ssid, ssid, ssid_len);
  186. memcpy(wifi_env.sta_info.psk, psk, psk_len);
  187. wifi_env.sta_info_en = autoconnect;
  188. return 0;
  189. }
  190. int bl_wifi_sta_info_get(bl_wifi_ap_info_t* sta_info)
  191. {
  192. if (wifi_env.sta_info_en != 1) {
  193. return -1;
  194. }
  195. memcpy(sta_info, &wifi_env.sta_info, sizeof(bl_wifi_ap_info_t));
  196. return 0;
  197. }
  198. #if 0
  199. int bl_wifi_netif_init(void)
  200. {
  201. struct netif *netif = &wifi_netif;
  202. ip4_addr_t ipaddr;
  203. ip4_addr_t netmask;
  204. ip4_addr_t gw;
  205. ipaddr.addr = 0;
  206. netmask.addr = 0;
  207. gw.addr = 0;
  208. netif->hwaddr[0] = 0x18;
  209. netif->hwaddr[1] = 0xB9;
  210. netif->hwaddr[2] = 0x05;
  211. netif->hwaddr[3] = 0x88;
  212. netif->hwaddr[4] = 0x88;
  213. netif->hwaddr[5] = 0x88;
  214. /* - netif_add(struct netif *netif, struct ip_addr *ipaddr,
  215. * struct ip_addr *netmask, struct ip_addr *gw,
  216. * void *state, err_t (* init)(struct netif *netif),
  217. * err_t (* input)(struct pbuf *p, struct netif *netif))
  218. *
  219. * Adds your network interface to the netif_list. Allocate a struct
  220. * netif and pass a pointer to this structure as the first argument.
  221. * Give pointers to cleared ip_addr structures when using DHCP,
  222. * or fill them with sane numbers otherwise. The state pointer may be NULL.
  223. *
  224. * The init function pointer must point to a initialization function for
  225. * your ethernet netif interface. The following code illustrates it's use.*/
  226. netif_add(netif, &ipaddr, &netmask, &gw, NULL, &bl606a0_wifi_netif_init, &tcpip_input);
  227. netif->name[0] = 's';
  228. netif->name[1] = 't';
  229. netif->flags |= NETIF_FLAG_LINK_UP;
  230. netif_set_default(netif);
  231. netif_set_up(netif);
  232. netifapi_dhcp_start(netif);
  233. return 0;
  234. }
  235. #endif