drv_wlan.c 6.9 KB
Newer Older
R
Rbb666 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
/*
 * File      : drv_wlan.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2015, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-08-30     ZeroFree     the first version
 */

#include <rtthread.h>
#include <rtdevice.h>

#ifdef RT_USING_WIFI

#include <drv_common.h>
#include <wlan_mgnt.h>
#include <wlan_prot.h>
#include <wlan_cfg.h>
#include <string.h>
#include <fal.h>

#define DBG_ENABLE
#define DBG_SECTION_NAME    "WLAN"
#define DBG_COLOR
#define DBG_LEVEL           DBG_LOG
#include <rtdbg.h>

#define NVRAM_GENERATED_MAC_ADDRESS      "macaddr=02:0A:F7:fe:86:1c"
#define WIFI_IMAGE_PARTITION_NAME        "wifi_image"
#define WIFI_INIT_THREAD_STACK_SIZE      (1024 * 4)
#define WIFI_INIT_THREAD_PRIORITY        (RT_THREAD_PRIORITY_MAX/2)
#define WIFI_INIT_WAIT_TIME              (rt_tick_from_millisecond(1000))

extern int wifi_hw_init(void);
static rt_bool_t init_flag = 0;
static const struct fal_partition *partition = RT_NULL;

ALIGN(64)
static const char wifi_nvram_image[] =
    // # The following parameter values are just placeholders, need to be updated.
    "manfid=0x2d0"                                                       "\x00"
    "prodid=0x0726"                                                      "\x00"
    "vendid=0x14e4"                                                      "\x00"
    "devid=0x43e2"                                                       "\x00"
    "boardtype=0x0726"                                                   "\x00"
    "boardrev=0x1101"                                                    "\x00"
    "boardnum=22"                                                        "\x00"
    "xtalfreq=26000"                                                     "\x00"
    "sromrev=11"                                                         "\x00"
    "boardflags=0x00404201"                                              "\x00"
    "boardflags3=0x08000000"                                             "\x00"
    NVRAM_GENERATED_MAC_ADDRESS                                          "\x00"
    "nocrc=1"                                                            "\x00"
    "ag0=255"                                                            "\x00"
    "aa2g=1"                                                             "\x00"
    "ccode=ALL"
    "\x00"
    "swdiv_en=1"                                                         "\x00"
    "swdiv_gpio=2"                                                       "\x00"

    "pa0itssit=0x20"                                                     "\x00"
    "extpagain2g=0"                                                      "\x00"
    "pa2ga0=-215,5267,-656"                                              "\x00"
    "AvVmid_c0=0x0,0xc8"                                                 "\x00"
    "cckpwroffset0=5"                                                    "\x00"
    "maxp2ga0=80"                                                        "\x00"
    "txpwrbckof=6"                                                       "\x00"
    "cckbw202gpo=0x6666"                                                 "\x00"
    "legofdmbw202gpo=0xaaaaaaaa"                                         "\x00"
    "mcsbw202gpo=0xbbbbbbbb"                                             "\x00"
    "propbw202gpo=0xdd"                                                  "\x00"
    "ofdmdigfilttype=18"                                                 "\x00"
    "ofdmdigfilttypebe=18"                                               "\x00"
    "papdmode=1"                                                         "\x00"
    "papdvalidtest=1"                                                    "\x00"
    "pacalidx2g=32"                                                      "\x00"
    "papdepsoffset=-36"                                                  "\x00"
    "papdendidx=61"                                                      "\x00"
    "wl0id=0x431b"                                                       "\x00"
    "deadman_to=0xffffffff"                                              "\x00"
    "muxenab=0x11"                                                        "\x00"
    "spurconfig=0x3"                                                     "\x00"
    "\x00\x00";

int wiced_platform_resource_size(int resource)
{
    int size = 0;

    /* Download firmware */
    if (resource == 0)
    {
        size = 355159;
    }
    else if (resource == 1)
    {
        size = sizeof(wifi_nvram_image);
    }

    return size;
}

int wiced_platform_resource_read(int resource, uint32_t offset, void* buffer, uint32_t buffer_size)
{
    if (resource == 0)
    {
        /* read RF firmware from partition */
        fal_partition_read(partition, offset, buffer, buffer_size);
    }
    else if (resource == 1)
    {
        memcpy(buffer, &wifi_nvram_image[offset], buffer_size);
    }

    return buffer_size;
}

/**
 * wait milliseconds for wifi low level initialize complete
 *
 * time_ms: timeout in milliseconds
 */
int rt_hw_wlan_wait_init_done(rt_uint32_t time_ms)
{
    rt_uint32_t time_cnt = 0;

    /* wait wifi low level initialize complete */
    while (time_cnt <= (time_ms / 100))
    {
        time_cnt++;
        rt_thread_mdelay(100);

        if (init_flag == 1)
        {
            break;
        }
    }

    if (time_cnt > (time_ms / 100))
    {
        return -RT_ETIMEOUT;
    }

    return RT_EOK;
}


static void wifi_init_thread_entry(void *parameter)
{
    /* initialize fal */
    fal_init();
    partition = fal_partition_find(WIFI_IMAGE_PARTITION_NAME);

    if (partition == RT_NULL)
    {
        LOG_E("%s partition is not exist, please check your configuration!", WIFI_IMAGE_PARTITION_NAME);
        return;
    }

    /* initialize low level wifi(ap6212) library */
    wifi_hw_init();

    /* waiting for sdio bus stability */
    rt_thread_delay(WIFI_INIT_WAIT_TIME);

    /* set wifi work mode */
    rt_wlan_set_mode(RT_WLAN_DEVICE_STA_NAME, RT_WLAN_STATION);
    /* NEED TODO !!! */
    /* rt_wlan_set_mode(RT_WLAN_DEVICE_AP_NAME, RT_WLAN_AP); */

    init_flag = 1;
}

int rt_hw_wlan_init(void)
{
    if (init_flag == 1)
    {
        return RT_EOK;
    }

    rt_thread_t tid = RT_NULL;

    tid = rt_thread_create("wifi_init", wifi_init_thread_entry, RT_NULL, WIFI_INIT_THREAD_STACK_SIZE, WIFI_INIT_THREAD_PRIORITY, 20);

    if (tid)
    {
        rt_thread_startup(tid);
    }
    else
    {
        LOG_E("Create wifi initialization thread fail!");
        return -RT_ERROR;
    }

    return RT_EOK;
}
INIT_APP_EXPORT(rt_hw_wlan_init);

static int ap6212_init(void)
{
#define AP6212_WL_REG_ON    GET_PIN(C, 13)

    /* enable the WLAN REG pin */
    rt_pin_mode(AP6212_WL_REG_ON, PIN_MODE_OUTPUT);
    rt_pin_write(AP6212_WL_REG_ON, PIN_HIGH);

    return 0;
}
INIT_DEVICE_EXPORT(ap6212_init);

#endif /* RT_USING_WIFI */