summaryrefslogtreecommitdiff
path: root/bsp/hc32/tests/test_sdmmc.c
blob: cf461d1b71201c8e37414b01538e02daff6bf2c6 (plain)
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
/*
 * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-30     CDT          first version
 */

/*
 * 程序清单:SD/MMC卡 设备使用例程
 * 例程导出了 sample_sdmmc 命令到控制终端
 * 命令调用格式:sdmmc_sample
 * 程序功能:对整个SD/MMC卡进行写和读操作,比较数据是否一致
 *
 * 注意: 修改函数SystemClock_Config下面参数,
 *     stcPLLHInit.PLLCFGR_f.PLLN = 120UL - 1UL;
 *                    改为
 *     stcPLLHInit.PLLCFGR_f.PLLN = 100UL - 1UL;
 *
 * menuconfig:
 *     Hardware Drivers Config  --->  Onboard Peripheral Drivers  ---->  Enable SDIO  ---->  Enable SDIOx(x:测试板硬件决定)
 *
 *     RT-Thread Components ---> Device Drivers ---> Using SD/MMC device drivers ---> (1024) The stack size for mmcsd thread
 */

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

#if defined(BSP_USING_SDIO)

#define SDMMC_DEVICE_NAME "sd"
#define SDMMC_SECTOR_SIZE 512UL

#define SDMMC_TEST_SECTORS_PER_TIME 100UL
#define SDMMC_TEST_TIME             10UL

#define SDMMC_TEST_SECTORS  (SDMMC_TEST_TIME * SDMMC_TEST_SECTORS_PER_TIME)
#define SDMMC_TEST_BUF_SIZE (SDMMC_SECTOR_SIZE * SDMMC_TEST_SECTORS_PER_TIME)

static void sdmmc_thread_entry(void *parameter)
{
    rt_ssize_t size;
    rt_uint32_t err_count = 0;
    rt_device_t sd_device;
    rt_uint32_t sector_start;
    rt_uint32_t sector_end;
    ;
    rt_uint32_t sector_cur_start;
    rt_uint32_t sector_cur_end;
    rt_uint8_t *sector_rbuf;
    rt_uint8_t *sector_wbuf;

    sd_device = rt_device_find(SDMMC_DEVICE_NAME);
    if (sd_device == RT_NULL)
    {
        rt_kprintf("no nand device found!\n");
        return;
    }

    if (rt_device_open(sd_device, RT_DEVICE_FLAG_RDWR) != RT_EOK)
    {
        rt_kprintf("fail to open!\n");
        return;
    }

    sector_rbuf = rt_malloc(SDMMC_TEST_BUF_SIZE);
    if (sector_rbuf == RT_NULL)
    {
        rt_kprintf("out of memory!");
        return;
    }

    sector_wbuf = rt_malloc(SDMMC_TEST_BUF_SIZE);
    if (sector_wbuf == RT_NULL)
    {
        rt_free(sector_rbuf);
        rt_kprintf("out of memory!");
        return;
    }

    sector_start = ((rt_uint32_t)rand() & 0x00000FFFUL);
    sector_end = (sector_start + SDMMC_TEST_SECTORS - 1);

    rt_kprintf("sector=[%d, %d]: ......test start...... !\r\n", sector_start, sector_end);

    for (sector_cur_start = sector_start; sector_cur_start <= sector_end; sector_cur_start += SDMMC_TEST_SECTORS_PER_TIME)
    {
        sector_cur_end = sector_cur_start + SDMMC_TEST_SECTORS_PER_TIME - 1UL;

        /* initialize buffer data */
        rt_memset(sector_rbuf, 0, SDMMC_TEST_BUF_SIZE);
        rt_memset(sector_wbuf, (rt_uint8_t)rand(), SDMMC_TEST_BUF_SIZE);

        /* write sdmmc */
        size = rt_device_write(sd_device, sector_cur_start, sector_wbuf, SDMMC_TEST_SECTORS_PER_TIME);
        if (size == SDMMC_TEST_SECTORS_PER_TIME)
        {
            rt_kprintf("sector=[%d, %d]: ok wr !\r\n", sector_cur_start, sector_cur_end);
        }
        else
        {
            err_count++;
            rt_kprintf("sector=[%d, %d]: error wr !\r\n", sector_cur_start, sector_cur_end);
            continue;
        }

        /* read sdmmc */
        size = rt_device_read(sd_device, sector_cur_start, sector_rbuf, SDMMC_TEST_SECTORS_PER_TIME);
        if (size == SDMMC_TEST_SECTORS_PER_TIME)
        {
            rt_kprintf("sector=[%d, %d]: ok rd !\r\n", sector_cur_start, sector_cur_end);
        }
        else
        {
            err_count++;
            rt_kprintf("sector=[%d, %d]: error rd !\r\n", sector_cur_start, sector_cur_end);
            continue;
        }

        /* compare data */
        if (rt_memcmp(sector_wbuf, sector_rbuf, SDMMC_TEST_BUF_SIZE) == 0)
        {
            rt_kprintf("sector=[%d, %d]: ok cmp  !\r\n", sector_cur_start, sector_cur_end);
        }
        else
        {
            err_count++;
            rt_kprintf("sector=[%d, %d]: error cmp  !\r\n", sector_cur_start, sector_cur_end);
        }
    }

    if (rt_device_close(sd_device) != RT_EOK)
    {
        rt_kprintf("fail to close!\n");
    }

    rt_free(sector_rbuf);
    rt_free(sector_wbuf);

    if (err_count == 0)
    {
        rt_kprintf("sector=[%d, %d]: ...... test ok...... !\r\n\r\n", sector_start, sector_end);
    }
    else
    {
        rt_kprintf("sector=[%d, %d]: ...... test error...... !\r\n\r\n", sector_start, sector_end);
    }
}

static void sdmmc_sample(int argc, char *argv[])
{
    rt_thread_t thread = rt_thread_create("sdmmc", sdmmc_thread_entry, RT_NULL, 2048, 15, 10);
    if (thread != RT_NULL)
    {
        rt_thread_startup(thread);
    }
}
MSH_CMD_EXPORT(sdmmc_sample, sdmmc sample);

#endif