新手上路
- 积分
- 25
- 金钱
- 25
- 注册时间
- 2017-10-20
- 在线时间
- 7 小时
|
1金钱
本帖最后由 Recca 于 2021-7-7 17:31 编辑
这里我大体说一下我的思路:
1、SPI FLASH 是使用 SFUD 通用驱动的
2、SPI FLASH 分区是使用 FAL 分区,然后根目录 / 挂载到 filesystem 分区,文件系统类型是 elm FAT
3、使用了 SD 卡,SD 卡挂载到 /mnt,文件系统是 elm FAT
4、wifi 固件的烧写我是参考例程写的,将 wifi 固件烧写到 wifi_image 分区
5、wifi 驱动也修改了,把 OTA 部分屏蔽掉了。
报错界面:
报错界面
wifi 固件的烧写代码:
- void wifi_firmware_upgrade(void)
- {
- const struct fal_partition *wifi_image_part = RT_NULL;
- struct dfs_fd file_fd;
-
- rt_uint32_t file_size;
- int len;
- int read_len;
- int write_len = 0;
- char *read_buf = RT_NULL;
- /* 获取 wifi 固件的大小 */
- if (dfs_file_open(&file_fd, "/mnt/wifi_image_1.0.rbl", O_RDONLY) != 0)
- {
- rt_kprintf("dfs file open failed!");
- goto ERROR;
- }
- file_size = file_fd.size;
- rt_kprintf("wifi firmware size:%d.\n", file_size);
-
- /* 找到 wifi_image 分区 */
- wifi_image_part = fal_partition_find("wifi_image");
- if(wifi_image_part == RT_NULL)
- {
- rt_kprintf("wifi_image find failed.\n");
- goto ERROR2;
- }
- /* 根据 wifi 固件大小擦除 wifi_image 分区 */
- if (fal_partition_erase(wifi_image_part, 0, file_size) < 0)
- {
- rt_kprintf("wifi_image erase failed.\n");
- goto ERROR2;
- }
- /* 下面就是循环写操作了 */
- read_buf = rt_malloc(1024);
- if (read_buf == RT_NULL)
- {
- goto ERROR2;
- }
- rt_memset(read_buf, 0x0, 1024);
- do
- {
- if (file_size >= 1024)
- {
- read_len = 1024;
- }
- else
- {
- read_len = file_size;
- }
- len = dfs_file_read(&file_fd, read_buf, read_len);
- if (len != read_len )
- {
- rt_kprintf("File read failed.\n");
- goto ERROR3;
- }
- file_size -= read_len;
- len = fal_partition_write(wifi_image_part, 0 + write_len, (const uint8_t *)read_buf, read_len);
- if (len < 0)
- {
- rt_kprintf("fal write failed!");
- goto ERROR3;
- }
- write_len += read_len;
- rt_memset(read_buf, 0x0, 1024);
- } while(write_len != file_fd.size);
- if (write_len == file_fd.size)
- rt_kprintf("wifi firmware upgrade success.\n");
- ERROR3:
- free(read_buf);
- ERROR2:
- dfs_file_close(&file_fd);
- ERROR:
- return;
- }
- MSH_CMD_EXPORT(wifi_firmware_upgrade, Upgrade wifi firmware.);
复制代码
wifi 驱动的代码:
- 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(100))
- #define PIN_WIFI_IRQ GET_PIN(C, 5)
- extern int wifi_hw_init(void);
- extern void wwd_thread_notify_irq(void);
- static const struct fal_partition *partition = RT_NULL;
- static rt_uint32_t init_flag = 0;
- struct rt_wlan_device *bcm_hw_wlan_dev_alloc(void)
- {
- struct rt_wlan_device *wlan;
- wlan = rt_malloc(sizeof(struct rt_wlan_device));
- return wlan;
- }
- /**
- * 获得 Flash wifi_image 分区存储的 WLAN 固件所占空间的大小 size
- * 就是 wifi_image_1.0.rbl
- **/
- int wiced_platform_resource_size(int resource)
- {
- int size = 0;
- /* Download firmware */
- if (resource == 0)
- {
- partition = fal_partition_find(WIFI_IMAGE_PARTITION_NAME);
- if (partition == RT_NULL)
- {
- LOG_E("%s partition is not exist, please check your configuration!");
- return size;
- }
- }
- return size;
- }
- /**
- * 从 Flash wifi_image 分区读取 size 大小的数据(实际就是 WLAN 固件代码)
- * 并保存到指针 buffer 所指向的内存空间
- **/
- int wiced_platform_resource_read(int resource, uint32_t offset, void *buffer, uint32_t buffer_size)
- {
- int transfer_size = 0;
- if (partition == RT_NULL)
- {
- return 0;
- }
- /* read RF firmware from partition */
- transfer_size = fal_partition_read(partition, offset, buffer, buffer_size);
- return transfer_size;
- }
- #ifdef RT_USING_PM
- void wiced_platform_keep_awake(void)
- {
- rt_pm_request(PM_SLEEP_MODE_NONE);
- }
- void wiced_platform_let_sleep(void)
- {
- rt_pm_release(PM_SLEEP_MODE_NONE);
- }
- #endif
- /**
- * return:1 initialize done
- * 0 not initialize
- */
- int rt_hw_wlan_get_initialize_status(void)
- {
- return init_flag;
- }
- /**
- * 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 (rt_hw_wlan_get_initialize_status() == 1)
- {
- break;
- }
- }
- if (time_cnt > (time_ms / 100))
- {
- return -RT_ETIMEOUT;
- }
- return RT_EOK;
- }
- static void _wiced_irq_handler(void *param)
- {
- wwd_thread_notify_irq();
- }
- static void wifi_init_thread_entry(void *parameter)
- {
- /* set wifi irq handle, must be initialized first */
- rt_pin_mode(PIN_WIFI_IRQ, PIN_MODE_INPUT_PULLUP);
- rt_pin_attach_irq(PIN_WIFI_IRQ, PIN_IRQ_MODE_RISING_FALLING, _wiced_irq_handler, RT_NULL);
- rt_pin_irq_enable(PIN_WIFI_IRQ, PIN_IRQ_ENABLE);
- /* initialize low level wifi(ap6181) 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);
- init_flag = 1;
- }
- int rt_hw_wlan_init(void)
- {
- if (init_flag == 1)
- {
- return RT_EOK;
- }
- #ifdef BSP_USING_WIFI_THREAD_INIT
- 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;
- }
- #else
- wifi_init_thread_entry(RT_NULL);
- init_flag = 1;
- #endif
- return RT_EOK;
- }
- #ifdef BSP_USING_WIFI_AUTO_INIT
- INIT_APP_EXPORT(rt_hw_wlan_init);
- #endif
- #endif
复制代码
主代码:
- void mnt_init(void)
- {
- struct rt_device *rootfs = RT_NULL;
- /* FAL 初始化 */
- fal_init();
- /* 将 elm fat 文件系统挂载 W25Q128 的 filesystem 分区 */
- rootfs = fal_blk_device_create("filesystem");
- if(rootfs == RT_NULL)
- return;
- if (dfs_mount("filesystem", "/", "elm", 0, 0) == 0)
- {
- rt_kprintf("file system initialization done!\n");
- }
- else
- {
- if(dfs_mkfs("elm", "filesystem") == 0)
- {
- if (dfs_mount("filesystem", "/", "elm", 0, 0) == 0)
- {
- rt_kprintf("file system initialization done!\n");
- }
- else
- {
- rt_kprintf("file system initialization failed!\n");
- }
- }
- }
- /* easyflash 的初始化 */
- easyflash_init();
- /* 创建 /mnt 目录,用于挂载 SD 卡 */
- if (opendir("/mnt") == RT_NULL)
- {
- if (mkdir("mnt", 0x777) == -1)
- return;
- }
-
- if(rt_device_find("sd0") == RT_NULL)
- {
- rt_kprintf("failed to find sd card device.\n");
- return;
- }
- if (dfs_mount("sd0", "/mnt", "elm", 0, 0) == RT_EOK)
- {
- rt_kprintf("sd card mount to '/mnt' success.\n");
- }
- else
- {
- rt_kprintf("sd card mount to '/mnt' failed!\n");
- }
- }
- int main(void)
- {
- mnt_init();
- rt_hw_wlan_init();
- /* 等待 500 ms 以便 wifi 完成初始化 */
- rt_hw_wlan_wait_init_done(500);
- return RT_EOK;
- }
复制代码
|
|