summaryrefslogtreecommitdiff
path: root/bsp/hc32/tests/test_qspi.c
blob: 66bd0106d771b2b8fdf2b067ced751461b765466 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*
 * 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
 */

/*
 * 程序清单:这是一个 QSPI 设备使用例程
 * 例程导出了 qspi_w25q_sample 命令到控制终端
 * 命令调用格式:qspi_w25q_sample qspi10
 * 命令解释:命令第二个参数是要使用的QSPI设备名称,为空则使用默认的QSPI设备
 * 程序功能:通过QSPI设备读取 w25q 的 ID 数据
*/

#include <rtthread.h>
#include <rtdevice.h>
#include "board_config.h"


#if defined(BSP_USING_QSPI)
#include "drv_qspi.h"

#define W25Q_QSPI_DEVICE_NAME           "qspi10"

#define W25Q_FLAG_BUSY                  (0x01)
#define W25Q_WR_ENABLE                  (0x06)
#define W25Q_SECTOR_ERASE               (0x20)
#define W25Q_RD_STATUS_REG1             (0x05)
#define W25Q_PAGE_PROGRAM               (0x02)
#define W25Q64_QUAD_INPUT_PAGE_PROGRAM  (0x32)

#define W25Q_STD_RD                     (0x03)
#define W25Q_FAST_RD                    (0x0B)
#define W25Q_FAST_RD_DUAL_OUTPUT        (0x3B)
#define W25Q_FAST_RD_DUAL_IO            (0xBB)
#define W25Q_FAST_RD_QUAD_OUTPUT        (0x6B)
#define W25Q_FAST_RD_QUAD_IO            (0xEB)

#define W25Q64_RD_STATUS_REG1           (0x05)
#define W25Q64_WR_STATUS_REG1           (0x01)
#define W25Q64_RD_STATUS_REG2           (0x35)
#define W25Q64_WR_STATUS_REG2           (0x31)
#define W25Q64_RD_STATUS_REG3           (0x15)
#define W25Q64_WR_STATUS_REG3           (0x11)

#define W25Q_PAGE_SIZE                  (256UL)
#define W25Q_SECTOR_SIZE                (1024UL * 4UL)
#define W25Q_PAGE_PER_SECTOR            (W25Q_SECTOR_SIZE / W25Q_PAGE_SIZE)
#define W25Q_MAX_ADDR                   (0x800000UL)

#define W25Q_QSPI_DATA_LINE_WIDTH       1
#define W25Q_QSPI_RD_MD                 (W25Q_FAST_RD_QUAD_IO)

#define W25Q_QSPI_WR_RD_ADDR            0x4000
#define W25Q_QSPI_DATA_BUF_LEN          0x2000
#define W25Q_QSPI_WR_CMD                W25Q64_QUAD_INPUT_PAGE_PROGRAM

#if defined (HC32F460) || defined (HC32F4A0) || defined (HC32F472)
    #ifndef BSP_QSPI_USING_SOFT_CS
        #if (W25Q_QSPI_WR_CMD == W25Q64_QUAD_INPUT_PAGE_PROGRAM)
            #error "QUAD PAGE PROGRAM must use soft CS pin!!"
        #endif
    #endif
#endif

#if W25Q_QSPI_RD_MD == W25Q_STD_RD
    #define W25Q_QSPI_RD_DUMMY_CYCLE    0
#elif W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_IO
    #define W25Q_QSPI_RD_DUMMY_CYCLE    4
#elif W25Q_QSPI_RD_MD == W25Q_FAST_RD_QUAD_IO
    #define W25Q_QSPI_RD_DUMMY_CYCLE    6
#else
    #define W25Q_QSPI_RD_DUMMY_CYCLE    8
#endif

#if (W25Q_QSPI_RD_MD == W25Q_FAST_RD_QUAD_IO)
    #define W25Q_QSPI_ADDR_LINE         4
#elif (W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_IO)
    #define W25Q_QSPI_ADDR_LINE         2
#else
    #define W25Q_QSPI_ADDR_LINE         1
#endif

#if (W25Q_QSPI_RD_MD == W25Q_STD_RD) || (W25Q_QSPI_RD_MD == W25Q_FAST_RD)
    #define W25Q_QSPI_DATA_LINE         1
#elif (W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_OUTPUT) || (W25Q_QSPI_RD_MD == W25Q_FAST_RD_DUAL_IO)
    #define W25Q_QSPI_DATA_LINE         2
#else
    #define W25Q_QSPI_DATA_LINE         4
#endif


struct rt_qspi_device *qspi_dev_w25q;     /* QSPI 设备句柄 */

static uint8_t u8WrBuf[W25Q_QSPI_DATA_BUF_LEN];
static uint8_t u8RdBuf[W25Q_QSPI_DATA_BUF_LEN];


static int rt_hw_qspi_flash_init(void)
{
#ifndef BSP_QSPI_USING_SOFT_CS
    if (RT_EOK != rt_hw_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, W25Q_QSPI_DATA_LINE_WIDTH, RT_NULL, RT_NULL))
#else
#if defined (HC32F472)
    if (RT_EOK != rt_hw_qspi_bus_attach_device("qspi1", "qspi10", GET_PIN(B, 12), W25Q_QSPI_DATA_LINE_WIDTH, RT_NULL, RT_NULL))
#elif defined (HC32F4A0) || defined (HC32F460) || defined (HC32F448) || defined (HC32F4A8)
    if (RT_EOK != rt_hw_qspi_bus_attach_device("qspi1", "qspi10", GET_PIN(C, 7), W25Q_QSPI_DATA_LINE_WIDTH, RT_NULL, RT_NULL))
#endif
#endif
    {
        rt_kprintf("Failed to attach the qspi device.");
        return -RT_ERROR;
    }

    return RT_EOK;
}
/* 导出到自动初始化 */
INIT_COMPONENT_EXPORT(rt_hw_qspi_flash_init);


void w25q_read_uid(struct rt_qspi_device *device)
{
    rt_uint8_t w25x_read_uid = 0x4B;    /* 命令 */
    rt_uint8_t u8UID[8] = {0};
    rt_uint8_t txBuf[5] = {0};

    rt_memset(txBuf, 0xFF, 5);
    txBuf[0] = w25x_read_uid;

    if (8 != rt_qspi_send_then_recv(device, txBuf, 5, u8UID, 8))
    {
        rt_kprintf("qspi get uid failed!\n");
    }
    else
    {
        rt_kprintf("w25q UID is: %02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n",
                   u8UID[0], u8UID[1], u8UID[2], u8UID[3], u8UID[4], u8UID[5], u8UID[6], u8UID[7]);
    }
}


int32_t w25q_check_process_done(struct rt_qspi_device *device, uint32_t u32Timeout)
{
    __IO uint32_t u32Count = 0U;
    int32_t i32Ret = LL_ERR_TIMEOUT;
    rt_uint8_t rxBuf[5] = {0};
    rt_uint8_t txBuf[5] = {0};

    txBuf[0] = W25Q_RD_STATUS_REG1;
    while (u32Count < u32Timeout)
    {
        if (1 != rt_qspi_send_then_recv(device, txBuf, 1, rxBuf, 1))
        {
            rt_kprintf("qspi get SR failed!\n");
        }
        else
        {
            if (W25Q_FLAG_BUSY != (rxBuf[0] & W25Q_FLAG_BUSY))
            {
                i32Ret = LL_OK;
                break;
            }
        }
        rt_thread_mdelay(1);
        u32Count++;
    }

    return i32Ret;
}

rt_err_t bsp_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length, void *recv_buf, rt_size_t recv_length)
{
    RT_ASSERT(send_buf);
    RT_ASSERT(recv_buf);
    RT_ASSERT(send_length != 0);

    struct rt_qspi_message message;
    unsigned char *ptr = (unsigned char *)send_buf;
    rt_size_t count = 0;
    rt_err_t result = 0;

    message.instruction.content = ptr[0];
    message.instruction.qspi_lines = 1;
    count++;

    /* get address */
    if (send_length > 1)
    {
        if (send_length >= 4)
        {
            /* address size is 3 Byte */
            message.address.content = (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
            message.address.size = 24;
            count += 3;
        }
        else
        {
            return -RT_ERROR;
        }
        message.address.qspi_lines = W25Q_QSPI_ADDR_LINE;
    }
    else
    {
        /* no address stage */
        message.address.content = 0 ;
        message.address.qspi_lines = 0;
        message.address.size = 0;
    }

    message.alternate_bytes.content = 0;
    message.alternate_bytes.size = 0;
    message.alternate_bytes.qspi_lines = 0;

    /* set dummy cycles */
    message.dummy_cycles = W25Q_QSPI_RD_DUMMY_CYCLE;

    /* set recv buf and recv size */
    message.parent.recv_buf = recv_buf;
    message.parent.send_buf = RT_NULL;
    message.parent.length = recv_length;
    message.parent.cs_take = 1;
    message.parent.cs_release = 1;
    message.qspi_data_lines = W25Q_QSPI_DATA_LINE;

    result = rt_qspi_transfer_message(device, &message);
    if (result == 0)
    {
        result = -RT_EIO;
    }
    else
    {
        result = recv_length;
    }

    return result;
}


rt_err_t bsp_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length, uint8_t dataLine)
{
    RT_ASSERT(send_buf);
    RT_ASSERT(length != 0);

    struct rt_qspi_message message;
    unsigned char *ptr = (unsigned char *)send_buf;
    rt_size_t  count = 0;
    rt_err_t result = 0;

    message.instruction.content = ptr[0];
    message.instruction.qspi_lines = 1;
    count++;

    /* get address */
    if (length > 1)
    {
        if (device->config.medium_size > 0x1000000 && length >= 5)
        {
            /* medium size greater than 16Mb, address size is 4 Byte */
            message.address.content = (ptr[1] << 24) | (ptr[2] << 16) | (ptr[3] << 8) | (ptr[4]);
            message.address.size = 32;
            message.address.qspi_lines = 1;
            count += 4;
        }
        else if (length >= 4)
        {
            /* address size is 3 Byte */
            message.address.content = (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
            message.address.size = 24;
            message.address.qspi_lines = 1;
            count += 3;
        }
        else
        {
            /* no address stage */
            message.address.content = 0 ;
            message.address.qspi_lines = 0;
            message.address.size = 0;
        }

    }
    else
    {
        /* no address stage */
        message.address.content = 0 ;
        message.address.qspi_lines = 0;
        message.address.size = 0;
    }

    message.alternate_bytes.content = 0;
    message.alternate_bytes.size = 0;
    message.alternate_bytes.qspi_lines = 0;

    message.dummy_cycles = 0;

    /* determine if there is data to send */
    if (length - count > 0)
    {
        message.qspi_data_lines = dataLine;
    }
    else
    {
        message.qspi_data_lines = 0;
    }

    /* set send buf and send size */
    message.parent.send_buf = ptr + count;
    message.parent.recv_buf = RT_NULL;
    message.parent.length = length - count;
    message.parent.cs_take = 1;
    message.parent.cs_release = 1;

    result = rt_qspi_transfer_message(device, &message);
    if (result == 0)
    {
        result = -RT_EIO;
    }
    else
    {
        result = length;
    }

    return result;
}



void w25q_write_sr(struct rt_qspi_device *device, uint8_t reg, uint8_t value)
{
    rt_uint8_t txBuf[5] = {0};

    txBuf[0] = W25Q_WR_ENABLE;
    if (1 != rt_qspi_send(device, txBuf, 1))
    {
        rt_kprintf("qspi send cmd failed!\n");
    }
    txBuf[0] = reg;
    txBuf[1] = value;
    if (2 != bsp_qspi_send(device, txBuf, 2, 1))
    {
        rt_kprintf("qspi send addr failed!\n");
    }
    if (LL_OK != w25q_check_process_done(device, 500U))
    {
        rt_kprintf("qspi wait busy failed!\n");
    }
}


int32_t w25q_read_data(struct rt_qspi_device *device, uint32_t u32Addr, uint8_t *pu8ReadBuf, uint32_t u32Size)
{
    int32_t i32Ret = LL_OK;
    rt_uint8_t txBuf[5] = {0};

    txBuf[0] = W25Q_QSPI_RD_MD;
    txBuf[1] = (u32Addr >> 16) & 0xFFU;
    txBuf[2] = (u32Addr >> 8) & 0xFFU;
    txBuf[3] = u32Addr & 0xFFU;
    if (u32Size != bsp_qspi_send_then_recv(device, txBuf, 4, pu8ReadBuf, u32Size))
    {
        i32Ret = LL_ERR;
    }

    return i32Ret;
}


int32_t w25q_write_data(struct rt_qspi_device *device, uint32_t u32Addr, uint8_t *pu8WriteBuf, uint32_t u32Size)
{
    int32_t i32Ret = LL_OK;
    uint32_t u32TempSize, u32AddrOffset = 0U;
    uint8_t w25q_txBuf[W25Q_PAGE_SIZE + 10];

    if ((u32Addr % W25Q_PAGE_SIZE) != 0U)
    {
        return LL_ERR_INVD_PARAM;
    }
    while (u32Size != 0UL)
    {
        if (u32Size >= W25Q_PAGE_SIZE)
        {
            u32TempSize = W25Q_PAGE_SIZE;
        }
        else
        {
            u32TempSize = u32Size;
        }

        w25q_txBuf[0] = W25Q_WR_ENABLE;
        if (1 != rt_qspi_send(device, w25q_txBuf, 1))
        {
            rt_kprintf("qspi send cmd failed!\n");
        }
        w25q_txBuf[0] = W25Q_QSPI_WR_CMD;
        w25q_txBuf[1] = (u32Addr >> 16) & 0xFFU;
        w25q_txBuf[2] = (u32Addr >> 8) & 0xFFU;
        w25q_txBuf[3] = u32Addr & 0xFFU;
        rt_memcpy(&w25q_txBuf[4], &pu8WriteBuf[u32AddrOffset], u32TempSize);
        if (W25Q64_QUAD_INPUT_PAGE_PROGRAM == w25q_txBuf[0])
        {
            if ((u32TempSize + 4) != bsp_qspi_send(device, w25q_txBuf, u32TempSize + 4, 4))
            {
                rt_kprintf("qspi send addr failed!\n");
            }
        }
        else
        {
            if ((u32TempSize + 4) != bsp_qspi_send(device, w25q_txBuf, u32TempSize + 4, 1))
            {
                rt_kprintf("qspi send addr failed!\n");
            }
        }
        i32Ret = w25q_check_process_done(device, 500U);
        if (i32Ret != LL_OK)
        {
            break;
        }

        u32Addr       += u32TempSize;
        u32AddrOffset += u32TempSize;
        u32Size       -= u32TempSize;
    }

    return i32Ret;
}

int32_t w25q_erase_sector(struct rt_qspi_device *device, uint32_t u32Addr, uint32_t u32Size)
{
    uint8_t txBuf[10];
    uint32_t u32SectorNum, u32Cnt;
    int32_t i32Ret = LL_OK;

    if ((u32Addr % W25Q_SECTOR_SIZE) != 0U)
    {
        return LL_ERR_INVD_PARAM;
    }
    u32SectorNum = u32Size / W25Q_SECTOR_SIZE;
    if ((u32Size % W25Q_SECTOR_SIZE) != 0U)
    {
        u32SectorNum += 1;
    }
    for (u32Cnt = 0; u32Cnt < u32SectorNum; u32Cnt++)
    {
        txBuf[0] = W25Q_WR_ENABLE;
        if (1 != rt_qspi_send(device, txBuf, 1))
        {
            rt_kprintf("qspi send cmd failed!\n");
        }
        txBuf[0] = W25Q_SECTOR_ERASE;
        txBuf[1] = (u32Addr >> 16) & 0xFFU;
        txBuf[2] = (u32Addr >> 8) & 0xFFU;
        txBuf[3] = u32Addr & 0xFFU;
        if (4 != rt_qspi_send(device, txBuf, 4))
        {
            rt_kprintf("qspi send addr failed!\n");
        }
        if (LL_OK != w25q_check_process_done(device, 500U))
        {
            i32Ret = LL_ERR;
            break;
        }
        u32Addr += W25Q_SECTOR_SIZE;
    }

    return i32Ret;
}

void w25q_write_read_data(struct rt_qspi_device *device, uint32_t u32Addr)
{
    uint32_t u32Cnt;

    for (u32Cnt = 0; u32Cnt < W25Q_QSPI_DATA_BUF_LEN; u32Cnt++)
    {
        u8WrBuf[u32Cnt] = u32Cnt & 0xFFUL;
        u8RdBuf[u32Cnt] = 0U;
    }
    if (LL_OK != w25q_erase_sector(device, u32Addr, W25Q_QSPI_DATA_BUF_LEN))
    {
        rt_kprintf("qspi erase sector failed!\n");
    }
    if (LL_OK !=  w25q_write_data(device, u32Addr, u8WrBuf, W25Q_QSPI_DATA_BUF_LEN))
    {
        rt_kprintf("qspi write data failed!\n");
    }
    if (LL_OK !=  w25q_read_data(device, u32Addr, u8RdBuf, W25Q_QSPI_DATA_BUF_LEN))
    {
        rt_kprintf("qspi read data failed!\n");
    }
    if (rt_memcmp(u8WrBuf, u8RdBuf, W25Q_QSPI_DATA_BUF_LEN) == 0)
    {
        rt_kprintf("qspi write and read test ok: addr=0x%06X\n", u32Addr);
    }
    else
    {
        rt_kprintf("qspi write and read failed!\n");
    }
}

static void qspi_thread_entry(void *parameter)
{
    rt_err_t ret;
    struct rt_qspi_configuration qcfg = {0};
    uint32_t u32Addr = W25Q_QSPI_WR_RD_ADDR;

    qcfg.medium_size        = W25Q_MAX_ADDR;
    qcfg.qspi_dl_width      = W25Q_QSPI_DATA_LINE_WIDTH;
    qcfg.parent.mode        = RT_SPI_MODE_0;
    qcfg.parent.data_width  = 8;
    qcfg.parent.max_hz      = 10000000UL;
    ret = rt_qspi_configure(qspi_dev_w25q, &qcfg);
    if ((RT_EOK != ret) && (-RT_EBUSY != ret))
    {
        rt_kprintf("qspi config failed!\n");
    }

    /* 读取UID */
    w25q_read_uid(qspi_dev_w25q);
    /* Set QE = 1 */
    w25q_write_sr(qspi_dev_w25q, W25Q64_WR_STATUS_REG2, 0x02);

    while (1)
    {
        /* 读写对比数据 */
        w25q_write_read_data(qspi_dev_w25q, u32Addr);
        u32Addr += W25Q_QSPI_DATA_BUF_LEN;
        if (u32Addr >= (W25Q_MAX_ADDR - W25Q_QSPI_DATA_BUF_LEN))
        {
            u32Addr = W25Q_QSPI_WR_RD_ADDR;
        }
        rt_thread_mdelay(500);
    }
}


void qspi_w25q_sample(int argc, char *argv[])
{
    char name[RT_NAME_MAX];

    if (argc == 2)
    {
        rt_strncpy(name, argv[1], RT_NAME_MAX);
    }
    else
    {
        rt_strncpy(name, W25Q_QSPI_DEVICE_NAME, RT_NAME_MAX);
    }

    /* 查找 qspi 设备获取设备句柄 */
    qspi_dev_w25q = (struct rt_qspi_device *)rt_device_find(name);
    if (!qspi_dev_w25q)
    {
        rt_kprintf("qspi sample run failed! can't find %s device!\n", name);
    }
    else
    {
        /* 创建 线程 */
        rt_thread_t thread = rt_thread_create("qspi", qspi_thread_entry, RT_NULL, 2048, 15, 10);
        /* 创建成功则启动线程 */
        if (thread != RT_NULL)
        {
            rt_thread_startup(thread);
        }
    }
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(qspi_w25q_sample, qspi w25q sample);

#endif