143 lines
3.0 KiB
C
143 lines
3.0 KiB
C
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <string.h>
|
|||
|
|
|||
|
#include "ql_log.h"
|
|||
|
#include "osi_api.h"
|
|||
|
#include "ql_sdmmc.h"
|
|||
|
#include "ql_api_osi.h"
|
|||
|
#include "ql_fs.h"
|
|||
|
#include "ql_gpio.h"
|
|||
|
#include "esp32_iap_sdmmc.h"
|
|||
|
|
|||
|
|
|||
|
#define SDMMC_CMD_PIN 48
|
|||
|
#define SDMMC_D0_PIN 39
|
|||
|
#define SDMMC_D1_PIN 40
|
|||
|
#define SDMMC_D2_PIN 49
|
|||
|
#define SDMMC_D3_PIN 50
|
|||
|
// sdmmc clk 是pin134的固定功能,不用配置
|
|||
|
|
|||
|
#define SDMMC_CMD_FUN 0
|
|||
|
#define SDMMC_D0_FUN 0
|
|||
|
#define SDMMC_D1_FUN 0
|
|||
|
#define SDMMC_D2_FUN 0
|
|||
|
#define SDMMC_D3_FUN 0
|
|||
|
|
|||
|
#define QL_FM_FAT32 0x02
|
|||
|
|
|||
|
|
|||
|
#define QL_SDMMC_FILE_PATH "SD:test.txt"
|
|||
|
#define QL_SDMMC_TEST_STR "1234567890abcdefg"
|
|||
|
|
|||
|
#define QL_GPIODEMO_LOG_LEVEL QL_LOG_LEVEL_INFO
|
|||
|
#define LOGI(msg, ...) QL_LOG(QL_GPIODEMO_LOG_LEVEL, "ql_esp_iap_sdmmc_EMO", msg, ##__VA_ARGS__)
|
|||
|
|
|||
|
|
|||
|
static void sdmmc_pin_init(void)
|
|||
|
{
|
|||
|
ql_pin_set_func(SDMMC_CMD_PIN, SDMMC_CMD_FUN);
|
|||
|
ql_pin_set_func(SDMMC_D0_PIN , SDMMC_D0_FUN);
|
|||
|
ql_pin_set_func(SDMMC_D1_PIN , SDMMC_D1_FUN);
|
|||
|
ql_pin_set_func(SDMMC_D2_PIN , SDMMC_D2_FUN);
|
|||
|
ql_pin_set_func(SDMMC_D3_PIN , SDMMC_D3_FUN);
|
|||
|
|
|||
|
ql_sdmmc_cfg_t cfg = {
|
|||
|
.dev = QL_SDMMC_SD_CARD_ONLY, //只以sd卡方式进行初始化
|
|||
|
.sd_mv = 0, //SD卡默认电压域3.2v
|
|||
|
.emmc_mv = 0, //emmc默认电压域1.8v
|
|||
|
};
|
|||
|
ql_sdmmc_set_dev_cfg(cfg);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static int64 ql_sdmmc_fs_test(char* path_name)
|
|||
|
{
|
|||
|
int fd = 0;
|
|||
|
int64 err = 0;
|
|||
|
char buffer[100];
|
|||
|
char *str = QL_SDMMC_TEST_STR;
|
|||
|
|
|||
|
fd = ql_fopen(path_name, "wb+");
|
|||
|
if(fd < 0)
|
|||
|
{
|
|||
|
LOGI("open file failed");
|
|||
|
err = fd;
|
|||
|
goto exit;
|
|||
|
}
|
|||
|
|
|||
|
err = ql_fwrite(str, strlen(str) + 1, 1, fd); //strlen not include '\0'
|
|||
|
if(err < 0)
|
|||
|
{
|
|||
|
LOGI("write file failed");
|
|||
|
ql_fclose(fd);
|
|||
|
goto exit;
|
|||
|
}
|
|||
|
|
|||
|
err = ql_frewind(fd);
|
|||
|
if(err < 0)
|
|||
|
{
|
|||
|
LOGI("rewind file failed");
|
|||
|
ql_fclose(fd);
|
|||
|
goto exit;
|
|||
|
}
|
|||
|
|
|||
|
err = ql_fread(buffer, ql_fsize(fd), 1, fd);
|
|||
|
if(err < 0)
|
|||
|
{
|
|||
|
LOGI("read file failed");
|
|||
|
ql_fclose(fd);
|
|||
|
goto exit;
|
|||
|
}
|
|||
|
|
|||
|
LOGI("file read result is %s", buffer);
|
|||
|
ql_fclose(fd);
|
|||
|
exit:
|
|||
|
return err;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void sdmmc_fs_thread(void *ctx)
|
|||
|
{
|
|||
|
while (1)
|
|||
|
{
|
|||
|
ql_sdmmc_fs_test(QL_SDMMC_FILE_PATH);
|
|||
|
ql_rtos_task_sleep_s(5);
|
|||
|
}
|
|||
|
|
|||
|
exit:
|
|||
|
ql_rtos_task_delete(NULL);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
int ql_sdmmc_app_init(void)
|
|||
|
{
|
|||
|
QlOSStatus err = QL_SDMMC_SUCCESS;
|
|||
|
|
|||
|
sdmmc_pin_init();
|
|||
|
|
|||
|
if(QL_SDMMC_SUCCESS != ql_sdmmc_mount())
|
|||
|
{
|
|||
|
LOGI("Mount failed");
|
|||
|
return -1;
|
|||
|
}
|
|||
|
else LOGI("Mount succeed");
|
|||
|
|
|||
|
// /* 格式化为一个分区 */
|
|||
|
// if(QL_SDMMC_SUCCESS != ql_sdmmc_mkfs(QL_FM_FAT32))
|
|||
|
// {
|
|||
|
// LOGI("mkfs failed");
|
|||
|
// return -1;
|
|||
|
// }
|
|||
|
// else LOGI("mkfs succeed");
|
|||
|
|
|||
|
/* 测试用 */
|
|||
|
ql_task_t sdmmc_task_fs = NULL;
|
|||
|
err = ql_rtos_task_create(&sdmmc_task_fs, 4096, APP_PRIORITY_NORMAL, "sdDEMO_fs", sdmmc_fs_thread, NULL, 5);
|
|||
|
if(err != QL_OSI_SUCCESS)
|
|||
|
{
|
|||
|
LOGI("creat sd task fs failed err = %d", err);
|
|||
|
}
|
|||
|
return 0;
|
|||
|
}
|