TT12-MCU/applications/w25q.c

185 lines
4.8 KiB
C
Raw Normal View History

/*
* 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>
#define DBG_TAG "w25q"
#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"
int rt_hw_spi_flash_init(void)
{
2023-06-18 01:34:09 +00:00
// rt_kprintf("SW Version: %s\n","1.4(temp)");
__HAL_RCC_GPIOB_CLK_ENABLE();
// __HAL_RCC_GPIOD_CLK_ENABLE();
// rt_kprintf("sfud success.\n");
// rt_tick_t t= rt_tick_get_millisecond();
rt_hw_spi_device_attach("spi2", "spi10", GPIOB, GPIO_PIN_12);
// rt_kprintf("sfud success.\n");
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
{
LOG_E("sfud error.");
return -RT_ERROR;
};
// rt_kprintf("sfud success.\n");
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){
// rt_kprintf("spi_flash mount to spi!\n");
mkdir("sd",0);
} else {
LOG_E("spi_flash mount to spi failed!");
}
}
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(w25q128_mount);
//1.41版本之前挂载并未出现问题。
//1.41版出现flash能挂载而sd卡正确识别后却不能挂载的问题
//后发现是SD卡的挂载目录"sd"丢失,原因未知。
2023-06-19 07:56:45 +00:00
void sdmnt_init(void)
{
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
{
LOG_E("dfs mount failed");
// mkfs("elm","sd0");
2023-06-19 07:56:45 +00:00
}
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(sdmnt_init);
/**
* flash是否损坏
*/
void checkFlash()
{
//间接判断。文件存在但大小为零则认为flash已损坏
if (isFileExit("cfg.ini") && getFileSize("cfg.ini")==0 && isFileExit("map.geojson") && getFileSize("map.geojson")==0)
{
LOG_E("the flash is damaged, try formatting to fix.");
dfs_mkfs("elm", "W25Q128");
rt_hw_cpu_reset();
}
}
/* 导出到自动初始化 */
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());
// 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);