2023-04-27 08:54:45 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* Change Logs:
|
|
|
|
|
* Date Author Notes
|
|
|
|
|
* 2023-04-14 murmur the first version
|
|
|
|
|
*/
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
|
|
|
|
//#define LOG_TAG "flash"
|
|
|
|
|
//#define LOG_LVL LOG_LVL_DBG
|
|
|
|
|
//#include <ulog.h>
|
|
|
|
|
|
2023-05-23 06:59:35 +00:00
|
|
|
|
#define DBG_TAG "w25q"
|
2023-04-27 08:54:45 +00:00
|
|
|
|
#define DBG_LVL DBG_LOG
|
|
|
|
|
#include <rtdbg.h>
|
|
|
|
|
|
|
|
|
|
#include "board.h"
|
|
|
|
|
#include <rtdevice.h>
|
2023-08-31 09:14:48 +00:00
|
|
|
|
#include "cfg/cfg.h"
|
2023-04-27 08:54:45 +00:00
|
|
|
|
|
|
|
|
|
int rt_hw_spi_flash_init(void)
|
|
|
|
|
{
|
2023-06-18 01:34:09 +00:00
|
|
|
|
// rt_kprintf("SW Version: %s\n","1.4(temp)");
|
2023-04-27 08:54:45 +00:00
|
|
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
2023-05-23 06:59:35 +00:00
|
|
|
|
// __HAL_RCC_GPIOD_CLK_ENABLE();
|
2023-06-01 02:23:08 +00:00
|
|
|
|
// rt_kprintf("sfud success.\n");
|
|
|
|
|
// rt_tick_t t= rt_tick_get_millisecond();
|
2023-05-23 06:59:35 +00:00
|
|
|
|
rt_hw_spi_device_attach("spi2", "spi10", GPIOB, GPIO_PIN_12);
|
2023-06-01 02:23:08 +00:00
|
|
|
|
// rt_kprintf("sfud success.\n");
|
2023-04-27 08:54:45 +00:00
|
|
|
|
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
|
|
|
|
|
{
|
2023-09-02 08:39:08 +00:00
|
|
|
|
LOG_E("sfud error.");
|
2023-04-27 08:54:45 +00:00
|
|
|
|
return -RT_ERROR;
|
|
|
|
|
};
|
2023-06-17 10:26:40 +00:00
|
|
|
|
// rt_kprintf("sfud success.\n");
|
2023-04-27 08:54:45 +00:00
|
|
|
|
return RT_EOK;
|
|
|
|
|
}
|
|
|
|
|
/* 导出到自动初始化 */
|
|
|
|
|
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void w25q128_mount(void)
|
|
|
|
|
{
|
|
|
|
|
// dfs_mkfs("elm", "W25Q128");
|
|
|
|
|
rt_device_t dev;
|
|
|
|
|
dev = rt_device_find("W25Q128");
|
|
|
|
|
if(dev != RT_NULL) {
|
|
|
|
|
if(dfs_mount("W25Q128", "/", "elm", 0, 0) == 0){
|
2023-06-17 10:26:40 +00:00
|
|
|
|
// rt_kprintf("spi_flash mount to spi!\n");
|
2023-07-18 11:23:48 +00:00
|
|
|
|
mkdir("sd",0);
|
2023-04-27 08:54:45 +00:00
|
|
|
|
} else {
|
2023-09-02 08:39:08 +00:00
|
|
|
|
LOG_E("spi_flash mount to spi failed!");
|
2023-04-27 08:54:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 导出到自动初始化 */
|
|
|
|
|
INIT_COMPONENT_EXPORT(w25q128_mount);
|
2023-07-18 11:23:48 +00:00
|
|
|
|
//1.41版本之前挂载并未出现问题。
|
|
|
|
|
//1.41版出现flash能挂载而sd卡正确识别后却不能挂载的问题
|
|
|
|
|
//后发现是SD卡的挂载目录"sd"丢失,原因未知。
|
2023-04-27 08:54:45 +00:00
|
|
|
|
|
|
|
|
|
|
2023-06-19 07:56:45 +00:00
|
|
|
|
void sdmnt_init(void)
|
|
|
|
|
{
|
2023-09-05 10:53:01 +00:00
|
|
|
|
rt_thread_mdelay(300);//这段延时必须加上,系统上电过程中存在延时,否则会出现先挂载后注册块设备sd0的情况
|
2023-06-19 07:56:45 +00:00
|
|
|
|
// mkfs("elm","sd0");//挂在前需格式化
|
|
|
|
|
if(dfs_mount("sd0","/sd","elm",0,0)==0) //挂载文件系统,参数:块设备名称、挂载目录、文件系统类型、读写标志、私有数据0
|
|
|
|
|
{
|
|
|
|
|
// rt_kprintf("dfs mount success\r\n");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-09-05 10:53:01 +00:00
|
|
|
|
LOG_E("dfs mount failed");
|
|
|
|
|
// mkfs("elm","sd0");
|
2023-06-19 07:56:45 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 导出到自动初始化 */
|
|
|
|
|
INIT_COMPONENT_EXPORT(sdmnt_init);
|
2023-09-06 01:51:08 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断flash是否损坏
|
|
|
|
|
*/
|
2023-09-02 08:39:08 +00:00
|
|
|
|
void checkFlash()
|
|
|
|
|
{
|
2023-09-06 01:51:08 +00:00
|
|
|
|
//间接判断。文件存在但大小为零则认为flash已损坏
|
|
|
|
|
if (isFileExit("cfg.ini") && getFileSize("cfg.ini")==0 && isFileExit("map.geojson") && getFileSize("map.geojson")==0)
|
2023-09-02 08:39:08 +00:00
|
|
|
|
{
|
|
|
|
|
LOG_E("the flash is damaged, try formatting to fix.");
|
|
|
|
|
dfs_mkfs("elm", "W25Q128");
|
|
|
|
|
rt_hw_cpu_reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/* 导出到自动初始化 */
|
2023-09-06 01:51:08 +00:00
|
|
|
|
INIT_COMPONENT_EXPORT(checkFlash);
|
2023-06-19 07:56:45 +00:00
|
|
|
|
|
|
|
|
|
void bootinfo()
|
|
|
|
|
{
|
|
|
|
|
rt_kprintf("%d ms since boot.\n", rt_tick_get_millisecond());
|
2023-09-02 08:39:08 +00:00
|
|
|
|
// add_val("bootCnt");
|
2023-06-19 07:56:45 +00:00
|
|
|
|
}
|
|
|
|
|
/* 导出到自动初始化 */
|
2023-08-25 02:01:16 +00:00
|
|
|
|
INIT_APP_EXPORT(bootinfo);
|
|
|
|
|
|
|
|
|
|
void sram_init(void)
|
|
|
|
|
{
|
|
|
|
|
MX_FSMC_Init();
|
|
|
|
|
}
|
|
|
|
|
/* 导出到自动初始化 */
|
|
|
|
|
INIT_COMPONENT_EXPORT(sram_init);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int sram_test(void)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
uint32_t start_time = 0, time_cast = 0;
|
|
|
|
|
#if SRAM_DATA_WIDTH == 8
|
|
|
|
|
char data_width = 1;
|
|
|
|
|
uint8_t data = 0;
|
|
|
|
|
#elif SRAM_DATA_WIDTH == 16
|
|
|
|
|
char data_width = 2;
|
|
|
|
|
uint16_t data = 0;
|
|
|
|
|
#else
|
|
|
|
|
char data_width = 4;
|
|
|
|
|
uint32_t data = 0;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* write data */
|
|
|
|
|
LOG_D("Writing the %ld bytes data, waiting....", SRAM_SIZE);
|
|
|
|
|
start_time = rt_tick_get();
|
|
|
|
|
for (i = 0; i < SRAM_SIZE / data_width; i++)
|
|
|
|
|
{
|
|
|
|
|
#if SRAM_DATA_WIDTH == 8
|
|
|
|
|
*(__IO uint8_t *)(SRAM_BANK_ADDR + i * data_width) = (uint8_t)0x55;
|
|
|
|
|
#elif SRAM_DATA_WIDTH == 16
|
|
|
|
|
*(__IO uint16_t *)(SRAM_BANK_ADDR + i * data_width) = (uint16_t)0x5555;
|
|
|
|
|
#else
|
|
|
|
|
*(__IO uint32_t *)(SRAM_BANK_ADDR + i * data_width) = (uint32_t)0x55555555;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
time_cast = rt_tick_get() - start_time;
|
|
|
|
|
LOG_D("Write data success, total time: %d.%03dS.", time_cast / RT_TICK_PER_SECOND,
|
|
|
|
|
time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
|
|
|
|
|
|
|
|
|
|
/* read data */
|
|
|
|
|
LOG_D("start Reading and verifying data, waiting....");
|
|
|
|
|
for (i = 0; i < SRAM_SIZE / data_width; i++)
|
|
|
|
|
{
|
|
|
|
|
#if SRAM_DATA_WIDTH == 8
|
|
|
|
|
data = *(__IO uint8_t *)(SRAM_BANK_ADDR + i * data_width);
|
|
|
|
|
if (data != 0x55)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("SRAM test failed!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#elif SRAM_DATA_WIDTH == 16
|
|
|
|
|
data = *(__IO uint16_t *)(SRAM_BANK_ADDR + i * data_width);
|
|
|
|
|
if (data != 0x5555)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("SRAM test failed!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
data = *(__IO uint32_t *)(SRAM_BANK_ADDR + i * data_width);
|
|
|
|
|
if (data != 0x55555555)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("SRAM test failed!");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i >= SRAM_SIZE / data_width)
|
|
|
|
|
{
|
|
|
|
|
LOG_D("SRAM test success!");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
|
}
|
|
|
|
|
MSH_CMD_EXPORT(sram_test, sram test);
|