summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/standalone/drivers/spi/fspim/fspim.c
blob: b2694483ad07a88749cbac5e37d15270aa553b47 (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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/*
 * Copyright : (C) 2022 Phytium Information Technology, Inc.
 * All Rights Reserved.
 *
 * This program is OPEN SOURCE software: you can redistribute it and/or modify it
 * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
 * either version 1.0 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the Phytium Public License for more details.
 *
 *
 * FilePath: fspim.c
 * Date: 2022-02-10 14:53:42
 * LastEditTime: 2022-02-18 09:08:32
 * Description:  This files is for spim api implementation
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 * 1.0   zhugengyu  2021-12-3   init commit
 * 1.1   zhugengyu  2022-4-15   support test mode
 * 1.2   zhugengyu  2022-5-13   support spi dma
 */


/***************************** Include Files *********************************/

#include <string.h>
#include "fio.h"
#include "ferror_code.h"
#include "ftypes.h"
#include "fdebug.h"
#include "fspim_hw.h"
#include "fspim.h"

/************************** Constant Definitions *****************************/

/**************************** Type Definitions *******************************/

/***************** Macros (Inline Functions) Definitions *********************/
#define FSPIM_DEBUG_TAG "SPIM"
#define FSPIM_ERROR(format, ...)   FT_DEBUG_PRINT_E(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_WARN(format, ...)   FT_DEBUG_PRINT_W(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_INFO(format, ...)    FT_DEBUG_PRINT_I(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)
#define FSPIM_DEBUG(format, ...)   FT_DEBUG_PRINT_D(FSPIM_DEBUG_TAG, format, ##__VA_ARGS__)

/************************** Function Prototypes ******************************/
FError FSpimReset(FSpim *instance_p);

/************************** Variable Definitions *****************************/
static const char *FSPIM_ERROR_CODE_MSG[FSPIM_NUM_OF_ERR_CODE] =
{
    "FSPIM_SUCCESS : fspim success",
    "FSPIM_ERR_INVAL_STATE : fspim invalid state",
    "FSPIM_ERR_NOT_READY : fspim driver not ready",
    "FSPIM_ERR_INVAL_PARAM : fspim invalid input parameters",
    "FSPIM_ERR_BUS_BUSY : fspim bus is busy",
    "FSPIM_ERR_NOT_SUPPORT : fspim not support operation",
    "FSPIM_ERR_TIMEOUT : fspim wait timeout",
    "FSPIM_ERR_TRANS_FAIL : fspim data transfer failed",
};


/*****************************************************************************/

/* 此文件主要为了完成用户对外接口,用户可以使用这些接口直接开始工作 */

/* - 包括用户API的定义和实现
   - 同时包含必要的OPTION方法,方便用户进行配置
   - 如果驱动可以直接进行I/O操作,在此源文件下可以将API 进行实现 */

/*
 * @name: FSpimCfgInitialize
 * @msg:  Initializes a specific instance such that it is ready to be used.
 * @param {FSpim} *instance_p FSPIM驱动控制数据
 * @param {FSpimConfig} *config_p FSPIM驱动配置数据
 * @return 驱动初始化的错误码信息,FSPIM_SUCCESS 表示初始化成功,其它返回值表示初始化失败
 */
FError FSpimCfgInitialize(FSpim *instance_p, const FSpimConfig *input_config_p)
{
    FASSERT(instance_p && input_config_p);
    uintptr base_addr = instance_p->config.base_addr;

    FError ret = FSPIM_SUCCESS;
    /*
    * If the device is started, disallow the initialize and return a Status
    * indicating it is started.  This allows the user to de-initialize the device
    * and reinitialize, but prevents a user from inadvertently
    * initializing.
    */
    if (FT_COMPONENT_IS_READY == instance_p->is_ready)
    {
        FSPIM_WARN("device is already initialized!!!");
    }

    /*
    * Set default values and configuration data, including setting the
    * callback handlers to stubs  so the system will not crash should the
    * application not assign its own callbacks.
     */
    FSpimDeInitialize(instance_p);
    instance_p->config = *input_config_p;

    /*
    * Reset the device.
    */
    ret = FSpimReset(instance_p);
    if (FSPIM_SUCCESS == ret)
    {
        instance_p->is_ready = FT_COMPONENT_IS_READY;
    }

    return ret;
}

/**
 * @name: FSpimDeInitialize
 * @msg: DeInitialization function for the device instance
 * @return {无}
 * @param {FSpim} *instance_p FSPIM驱动控制数据
 */
void FSpimDeInitialize(FSpim *instance_p)
{
    FASSERT(instance_p);

    instance_p->is_ready = 0;
    memset(instance_p, 0, sizeof(*instance_p));

    return;
}

/**
 * @name: FSpimReset
 * @msg: 重置FSPIM控制器
 * @return {FError} FSPIM_SUCCESS表示重置成功,其它返回值表示重置失败
 * @param {FSpim} *instance_p
 */
FError FSpimReset(FSpim *instance_p)
{
    FASSERT(instance_p);
    uintptr base_addr = instance_p->config.base_addr;
    FError ret = FSPIM_SUCCESS;
    u32 reg_val;
    u32 fifo;

    /* 禁用SPI控制器 */
    FSpimSetEnable(base_addr, FALSE);

    /* 选择数据长度和帧格式 */
    reg_val = FSPIM_CTRL_R0_DFS(FSPIM_DEFAULT_DFS) |
              FSPIM_CTRL_R0_FRF(FSPIM_DEFAULT_FRF) |
              FSPIM_CTRL_R0_CFS(FSPIM_DEFAULT_CFS);

    if (instance_p->config.en_test)
    {
        reg_val |= FSPIM_CTRL_R0_SLV_SRL(FSPIM_SRL_TEST); /* 设置测试模式,TX Fifo和RX Fifo内部短接 */
    }
    else
    {
        reg_val |= FSPIM_CTRL_R0_SLV_SRL(FSPIM_SRL_NORAML); /* 设置为正常模式 */
    }

    FSpimSetCtrlR0(base_addr, reg_val);

    /* 选择串行时钟极性和相位 */
    FSpimSetCpha(base_addr, instance_p->config.cpha);
    FSpimSetCpol(base_addr, instance_p->config.cpol);

    /* 设置传输模式 */
    FSpimSetTransMode(base_addr, FSPIM_TRANS_MODE_RX_TX);

    /* 禁用slave */
    FSpimSetSlaveEnable(base_addr, FALSE);

    /* 禁用SPI 中断,设置slave设备 */
    FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS);
    FSpimSelSlaveDev(base_addr, instance_p->config.slave_dev_id);

    /* 获取SPI RX/TX FIFO 深度 */
    if (0 == instance_p->tx_fifo_len)
    {
        fifo = FSpimGetTxFifoDepth(base_addr);
        instance_p->tx_fifo_len = ((fifo == 1) ? 0 : fifo);
        FSPIM_INFO("fifo depth %d tx_fifo_len %d", fifo, instance_p->tx_fifo_len);
    }

    if (0 == instance_p->rx_fifo_len)
    {
        fifo = FSpimGetRxFifoDepth(base_addr);
        instance_p->rx_fifo_len = ((fifo == 1) ? 0 : fifo);
        FSPIM_INFO("fifo depth %d tx_fifo_len %d", fifo, instance_p->tx_fifo_len);
    }

    FSPIM_WRITE_REG32(base_addr, FSPIM_DMA_CR_OFFSET, 0x0); /* disable ddma */

    if (instance_p->config.en_dma)
    {
        /* recv data in continuous way */
        FSpimSetCtrlR1(base_addr, FSPIM_CTRL_R1_NDF_64KB);

        /* setup fifo threshold */
        FSpimSetRxFifoThreshold(base_addr, instance_p->rx_fifo_len);
        FSpimSetTxFifoThreshold(base_addr, instance_p->tx_fifo_len);

        /* setup fifo DMA level to trigger interrupt */
        FSpimSetRxDMALevel(base_addr, FSPIM_RX_DMA_LEVEL);
        FSpimSetTxDMALevel(base_addr, FSPIM_TX_DMA_LEVEL);
    }
    else
    {
        FSpimSetCtrlR1(base_addr, 0);

        FSpimSetRxFifoThreshold(base_addr, 0);
        FSpimSetTxFifoThreshold(base_addr, 0);
        FSpimSetRxDMALevel(base_addr, 0);
        FSpimSetTxDMALevel(base_addr, 0);
    }


    ret = FSpimSetSpeed(base_addr, instance_p->config.max_freq_hz);
    if (FSPIM_SUCCESS != ret)
        return ret;

    FSPIM_WRITE_REG32(base_addr, FSPIM_RX_SAMPLE_DLY_OFFSET, FSPIM_DEFAULT_RSD);

    /* 使能SPI控制器 */
    FSpimSetEnable(base_addr, TRUE);

    return ret;
}

/**
 * @name: FSpimGetTxRound
 * @msg: 计算当前FIFO支持的发送字节数
 * @return {fsize_t} 当前TX FIFO可以容纳的字节数
 * @param {FSpim} *instance_p
 */
static fsize_t FSpimGetTxRound(FSpim *instance_p)
{
    fsize_t data_width = instance_p->config.n_bytes;
    uintptr base_addr = instance_p->config.base_addr;
    fsize_t tx_left_round, tx_fifo_room, rx_tx_gap;

    tx_left_round = (fsize_t)(instance_p->tx_buff_end - instance_p->tx_buff) / data_width;
    tx_fifo_room = instance_p->tx_fifo_len -
                   FSpimGetTxFifoLevel(base_addr);
    rx_tx_gap = ((fsize_t)(instance_p->rx_buff_end - instance_p->rx_buff) -
                 (fsize_t)(instance_p->tx_buff_end - instance_p->tx_buff)) / data_width;

    FSPIM_DEBUG("tx_left_round: %d, tx_fifo_room: %d, gap: %d",
                tx_left_round,
                tx_fifo_room,
                ((fsize_t)(instance_p->tx_fifo_len) - rx_tx_gap));
    return min3(tx_left_round,
                tx_fifo_room,
                ((fsize_t)(instance_p->tx_fifo_len) - rx_tx_gap));
}

/**
 * @name: FSpimFifoTx
 * @msg: 利用Fifo进行发送
 * @return {无}
 * @param {FSpim} *instance_p
 */
void FSpimFifoTx(FSpim *instance_p)
{
    FASSERT(instance_p);
    fsize_t tx_round = FSpimGetTxRound(instance_p);
    FSPIM_DEBUG("tx round: %d", tx_round);
    uintptr base_addr = instance_p->config.base_addr;
    u32 data_width = instance_p->config.n_bytes;
    u16 data = 0xff;

    while (tx_round)
    {
        if (instance_p->tx_buff_end - instance_p->length)
        {
            if (FSPIM_1_BYTE == data_width)
            {
                /*
                * Data Transfer Width is Byte (8 bit).
                */
                data = *(u8 *)(instance_p->tx_buff);
            }
            else if (FSPIM_2_BYTE == data_width)
            {
                /*
                * Data Transfer Width is Half Word (16 bit).
                */
                data = *(u16 *)(instance_p->tx_buff);
            }
            else
            {
                FASSERT(0);
            }
        }

        FSpimWriteData(base_addr, data);
        FSPIM_DEBUG("  send 0x%x", data);
        instance_p->tx_buff += data_width;
        tx_round--;
    }
}

/**
 * @name: FSpimGetRxRound
 * @msg: 获取当前Fifo支持的接收字节数
 * @return {fsize_t} 当前RX FIFO可以容纳的字节数
 * @param {FSpim} *instance_p
 */
static fsize_t FSpimGetRxRound(FSpim *instance_p)
{
    fsize_t data_width = instance_p->config.n_bytes;
    uintptr base_addr = instance_p->config.base_addr;

    fsize_t rx_left_round = (fsize_t)(instance_p->rx_buff_end - instance_p->rx_buff) / data_width;

    FSPIM_DEBUG("left round %d, rx level %d", rx_left_round, FSpimGetRxFifoLevel(base_addr));
    return min(rx_left_round, (fsize_t)FSpimGetRxFifoLevel(base_addr));
}

/**
 * @name: FSpimFifoRx
 * @msg: 利用Fifo进行接收
 * @return {无}
 * @param {FSpim} *instance_p
 */
void FSpimFifoRx(FSpim *instance_p)
{
    FASSERT(instance_p);
    fsize_t rx_round = FSpimGetRxRound(instance_p);
    FSPIM_DEBUG("rx round: %d", rx_round);
    uintptr base_addr = instance_p->config.base_addr;
    u32 data_width = instance_p->config.n_bytes;
    u16 data;

    while (rx_round)
    {
        data = FSpimReadData(base_addr);
        if ((fsize_t)(instance_p->rx_buff_end - instance_p->length))
        {
            if (FSPIM_1_BYTE == data_width)
            {
                /*
                * Data Transfer Width is Byte (8 bit).
                */
                *(u8 *)(instance_p->rx_buff) = (u8)data;
                FSPIM_DEBUG("  recv 0x%x", *(u8 *)(instance_p->rx_buff));
            }
            else if (FSPIM_2_BYTE == data_width)
            {
                /*
                * Data Transfer Width is Half Word (16 bit).
                */
                *(u16 *)(instance_p->rx_buff) = (u16)data;
                FSPIM_DEBUG("  recv 0x%x", *(u16 *)(instance_p->rx_buff));
            }
            else
            {
                FASSERT(0);
            }

        }

        instance_p->rx_buff += data_width;
        rx_round--;
    }

    return;
}

/**
 * @name: FSpimTransferPollFifo
 * @msg: 先发送后接收数据 (阻塞处理),利用Fifo进行处理
 * @return {FError} FSPIM_SUCCESS表示处理成功,其它返回值表示处理失败
 * @param {FSpim} *instance_p 驱动控制数据
 * @param {void} *tx_buf 写缓冲区,可以为空,为空时表示只关注读数据,此时驱动会发送0xff读数据
 * @param {void} *rx_buf 读缓冲区, 可以为空,为空时表示值关注写数据,此时SPI总线上返回的数据会被抛弃
 * @param {fsize_t} len 进行传输的长度,如果tx_buf或者rx_buf不为空,则两个buf的长度必须为len
    - 使用此函数前需要确保FSPIM驱动初始化成功
    - 从函数不会使用中断,会按照TX FIFO的深度进行传输,每次发送填满TX FIFO后触发发送/接收动作
 */
FError FSpimTransferPollFifo(FSpim *instance_p, const void *tx_buf, void *rx_buf, fsize_t len)
{
    FASSERT(instance_p);
    u32 reg_val;
    uintptr base_addr = instance_p->config.base_addr;
    u32 data_width = instance_p->config.n_bytes;
    u32 tx_level;
    FError ret = FSPIM_SUCCESS;

    if (FT_COMPONENT_IS_READY != instance_p->is_ready)
    {
        FSPIM_ERROR("device is already initialized!!!");
        return FSPIM_ERR_NOT_READY;
    }

    FSpimSetEnable(base_addr, FALSE);

    reg_val = FSpimGetCtrlR0(base_addr);

    reg_val &= ~FSPIM_CTRL_R0_DFS_MASK;
    reg_val |= FSPIM_CTRL_R0_DFS((data_width << 3) - 1);

    reg_val &= ~FSPIM_CTRL_R0_TMOD_MASK;
    if (tx_buf && rx_buf)
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_TX);
    else if (rx_buf)
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_ONLY);
    else
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_TX);

    FSpimSetCtrlR0(base_addr, reg_val);

    FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS);

    instance_p->length = len;
    instance_p->tx_buff = tx_buf;
    instance_p->tx_buff_end = tx_buf + len;
    instance_p->rx_buff = rx_buf;
    instance_p->rx_buff_end = rx_buf + len;
    FSPIM_DEBUG("tx buff@%p-%d, rx buff@%p-%d",
                instance_p->tx_buff, len,
                instance_p->rx_buff, len);

    FSpimSetEnable(base_addr, TRUE);

    do
    {
        FSpimFifoTx(instance_p);
        FSpimFifoRx(instance_p);
    }
    while (instance_p->rx_buff_end > instance_p->rx_buff);

    return ret;
}

/**
 * @name: FSpimTransferByInterrupt
 * @msg: 先发送后接收数据 (中断处理),利用Fifo进行处理
 * @return {FError} FSPIM_SUCCESS表示处理成功,其它返回值表示处理失败
 * @param {FSpim} *instance_p 驱动控制数据
 * @param {void} *tx_buf 写缓冲区
 * @param {void} *rx_buf 读缓冲区
 * @param {fsize_t} len 读写缓冲区长度 (必须相等)
 */
FError FSpimTransferByInterrupt(FSpim *instance_p, const void *tx_buf, void *rx_buf, fsize_t len)
{
    FASSERT(instance_p);
    u32 reg_val;
    uintptr base_addr = instance_p->config.base_addr;
    u32 data_width = instance_p->config.n_bytes;
    u32 tx_level;

    if (FT_COMPONENT_IS_READY != instance_p->is_ready)
    {
        FSPIM_ERROR("device is already initialized!!!");
        return FSPIM_ERR_NOT_READY;
    }

    FSpimSetEnable(base_addr, FALSE);

    reg_val = FSpimGetCtrlR0(base_addr);

    reg_val &= ~FSPIM_CTRL_R0_DFS_MASK;
    reg_val |= FSPIM_CTRL_R0_DFS((data_width << 3) - 1);

    reg_val &= ~FSPIM_CTRL_R0_TMOD_MASK;
    if (tx_buf && rx_buf)
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_TX);
    else if (rx_buf)
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_ONLY);
    else
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_TX);

    FSpimSetCtrlR0(base_addr, reg_val);

    FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS);

    instance_p->length = len;
    instance_p->tx_buff = tx_buf;
    instance_p->tx_buff_end = instance_p->tx_buff + len;
    instance_p->rx_buff = rx_buf;
    instance_p->rx_buff_end = instance_p->rx_buff + len;

    /* 设置中断触发的时机,fifo填满一半,或者所有的数据填完 */
    tx_level = min(instance_p->tx_fifo_len / 2, instance_p->length / data_width);
    FSpimSetTxFifoThreshold(base_addr, tx_level);
    FSpimUmaskIrq(base_addr, FSPIM_IMR_TXEIS | FSPIM_IMR_TXOIS | FSPIM_IMR_RXUIS | FSPIM_IMR_RXOIS);

    FSpimSetEnable(base_addr, TRUE);

    return FSPIM_SUCCESS;
}

#ifdef FSPIM_VERSION_2 /* E2000 */

/**
 * @name: FSpimTransferDMA
 * @msg: 启动SPIM DMA数据传输
 * @return {FError} FSPIM_SUCCESS表示启动DMA传输成功,其它值表示失败
 * @param {FSpim} *instance_p, 驱动控制数据
 * @param {boolean} tx, TRUE: 启动发送DMA
 * @param {boolean} rx, TRUE: 启动接收DMA
 */
FError FSpimTransferDMA(FSpim *instance_p, boolean tx, boolean rx)
{
    FASSERT(instance_p);
    u32 reg_val;
    uintptr base_addr = instance_p->config.base_addr;
    u32 data_width = instance_p->config.n_bytes;

    if (FT_COMPONENT_IS_READY != instance_p->is_ready)
    {
        FSPIM_ERROR("device is not yet initialized!!!");
        return FSPIM_ERR_NOT_READY;
    }

    FSpimSetEnable(base_addr, FALSE);

    /* set up spim transfer mode */
    reg_val = FSpimGetCtrlR0(base_addr);
    reg_val &= ~FSPIM_CTRL_R0_DFS_MASK;
    reg_val |= FSPIM_CTRL_R0_DFS((data_width << 3) - 1);

    reg_val &= ~FSPIM_CTRL_R0_TMOD_MASK;
    if (tx && rx)
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_TX);
    else if (rx)
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_ONLY);
    else
        reg_val |= FSPIM_CTRL_R0_TMOD(FSPIM_TMOD_RX_TX);

    FSpimSetCtrlR0(base_addr, reg_val);

    FSpimMaskIrq(base_addr, FSPIM_IMR_ALL_BITS); /* mask all interrupts */

    FSpimSetEnable(base_addr, TRUE);

    /* enable DMA tx / rx */
    reg_val = FSPIM_READ_REG32(base_addr, FSPIM_DMA_CR_OFFSET);
    if (tx)
        reg_val |= FSPIM_DMA_CR_TDMAE;
    else
        reg_val &= ~FSPIM_DMA_CR_TDMAE;

    if (rx)
        reg_val |= FSPIM_DMA_CR_RDMAE;
    else
        reg_val &= ~FSPIM_DMA_CR_RDMAE;
    FSPIM_WRITE_REG32(base_addr, FSPIM_DMA_CR_OFFSET, reg_val);

    FSpimSelSlaveDev(base_addr, instance_p->config.slave_dev_id);

    return FSPIM_SUCCESS;
}

/**
 * @name: FSpimSetChipSelection
 * @msg: 设置片选信号
 * @return {NONE}
 * @param {FSpim} *instance_p, 驱动控制数据
 * @param {boolean} on, TRUE: 片选打开, FALSE: 片选关闭
 */
void FSpimSetChipSelection(FSpim *instance_p, boolean on)
{
    FASSERT(instance_p);
    u32 reg_val;
    FSpimSlaveDevice cs_n = instance_p->config.slave_dev_id;
    uintptr base_addr = instance_p->config.base_addr;
    if (FT_COMPONENT_IS_READY != instance_p->is_ready)
    {
        FSPIM_ERROR("device is not yet initialized!!!");
        return;
    }

    reg_val = FSPIM_READ_REG32(base_addr, FSPIM_CS_OFFSET);

    if (on)
    {
        reg_val |= FSPIM_CHIP_SEL_EN((u32)cs_n);
        reg_val |= FSPIM_CHIP_SEL((u32)cs_n);
    }
    else
    {
        reg_val &= ~FSPIM_CHIP_SEL_EN((u32)cs_n);
        reg_val &= ~FSPIM_CHIP_SEL((u32)cs_n);
    }

    FSPIM_WRITE_REG32(base_addr, FSPIM_CS_OFFSET, reg_val);

    return;
}
#endif

/**
 * @name: FSpimErrorToMessage
 * @msg: 获取FSPIM模块错误码对应的错误信息
 * @return {const char *}, 错误码信息,NULL表示失败
 * @param {FError} error, FSPIM输入错误码
 */
const char *FSpimErrorToMessage(FError error)
{
    const char *msg = NULL;
    if (FSPIM_SUCCESS != error && (FSPIM_ERR_CODE_PREFIX != error & (FT_ERRCODE_SYS_MODULE_MASK | FT_ERRCODE_SUB_MODULE_MASK)))
    {
        /* if input error do not belong to this module */
        return msg;
    }
    u32 index = error & FT_ERRCODE_TAIL_VALUE_MASK;

    if (index < FSPIM_NUM_OF_ERR_CODE)
    {
        msg = FSPIM_ERROR_CODE_MSG[index];
    }

    return msg;
}