summaryrefslogtreecommitdiff
path: root/bsp/phytium/libraries/standalone/board/d2000/fioctrl.c
blob: ae1ff0d3a2ae5cf4d035ad8d698cc954bf8c6648 (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
/*
 * 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: fioctrl.c
 * Date: 2022-02-10 14:53:42
 * LastEditTime: 2022-02-18 08:25:29
 * Description:  This files is for io-ctrl function implementation (io-mux/io-config/io-delay)
 *
 * Modify History:
 *  Ver   Who        Date         Changes
 * ----- ------     --------    --------------------------------------
 * 1.0   zhugengyu  2022/2/22   init commit
 */


/***************************** Include Files *********************************/
#include "fparameters.h"
#include "fio.h"
#include "fkernel.h"
#include "fassert.h"
#include "fdebug.h"

#include "fioctrl.h"
#include "fpinctrl.h"

/************************** Constant Definitions *****************************/
/* Bit[0] : 输入延迟功能使能  */
#define FIOCTRL_DELAY_EN(delay_beg)                      BIT(delay_beg)
#define FIOCTRL_INPUT_DELAY_OFF                          0

/* Bit[3:1] : 输入延迟精调档位选择 */
#define FIOCTRL_DELICATE_DELAY_MASK(delay_beg)             GENMASK((delay_beg + 3), (delay_beg + 1))
#define FIOCTRL_DELICATE_DELAY_GET(reg_val, delay_beg)     GET_REG32_BITS((reg_val), (delay_beg + 3), (delay_beg + 1))
#define FIOCTRL_DELICATE_DELAY_SET(val, delay_beg)         SET_REG32_BITS((val), (delay_beg + 3), (delay_beg + 1))

/* Bit[6:4] : 输入延迟粗调档位选择 */
#define FIOCTRL_ROUGH_DELAY_MASK(delay_beg)               GENMASK((delay_beg + 6), (delay_beg + 4))
#define FIOCTRL_ROUGH_DELAY_GET(reg_val, delay_beg)       GET_REG32_BITS((reg_val), (delay_beg + 6), (delay_beg + 4))
#define FIOCTRL_ROUGH_DELAY_SET(val, delay_beg)           SET_REG32_BITS((val), (delay_beg + 6), (delay_beg + 4))

/* Bit[7] : 保留 */
/* Bit[8] : 输出延迟功能使能 */

/* Bit[11:9] : 输出延迟精调档位选择 */
/* Bit [14:12] : 输出延迟粗调档位选择  */
/* Bit [15] : 保留 */

#define FIOCTRL_FUNC_BEG_OFF(reg_bit)   ((reg_bit) + 0)
#define FIOCTRL_FUNC_END_OFF(reg_bit)   ((reg_bit) + 1) /* bit[1:0] 复用功能占2个位 */
#define FIOCTRL_PULL_BEG_OFF(reg_bit)   ((reg_bit) + 2)
#define FIOCTRL_PULL_END_OFF(reg_bit)   ((reg_bit) + 3) /* bit[3:2] 上下拉功能占2个位 */

#define FIOCTRL_DELAY_IN_BEG_OFF(reg_bit)    ((reg_bit) + 0)
#define FIOCTRL_DELAY_IN_END_OFF(reg_bit)    ((reg_bit) + 7) /* bit[8:1] 输入延时占7个位 */
#define FIOCTRL_DELAY_OUT_BEG_OFF(reg_bit)   ((reg_bit) + 8)
#define FIOCTRL_DELAY_OUT_END_OFF(reg_bit)   ((reg_bit) + 15) /* bit[15:9] 输出延时占7个位 */

/* 芯片引脚控制寄存器的起止位置 */
#define FIOCTRL_REG_OFFSET_MIN                           0x200
#define FIOCTRL_REG_OFFSET_MAX                           0x22c

/* 芯片引脚延时寄存器的起止位置 */
#define FIOCTRL_DELAY_REG_OFFSET_MIN                     0x400
#define FIOCTRL_DELAY_REG_OFFSET_MAX                     0x404

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

/***************** Macros (Inline Functions) Definitions *********************/
#define FIOCTRL_DEBUG_TAG "FIOCTRL"
#define FIOCTRL_ERROR(format, ...) FT_DEBUG_PRINT_E(FIOCTRL_DEBUG_TAG, format, ##__VA_ARGS__)
#define FIOCTRL_WARN(format, ...)  FT_DEBUG_PRINT_W(FIOCTRL_DEBUG_TAG, format, ##__VA_ARGS__)
#define FIOCTRL_INFO(format, ...)  FT_DEBUG_PRINT_I(FIOCTRL_DEBUG_TAG, format, ##__VA_ARGS__)
#define FIOCTRL_DEBUG(format, ...) FT_DEBUG_PRINT_D(FIOCTRL_DEBUG_TAG, format, ##__VA_ARGS__)

#define FIOCTRL_ASSERT_REG_OFF(pin) FASSERT_MSG(((pin.reg_off >= FIOCTRL_REG_OFFSET_MIN) && (pin.reg_off <= FIOCTRL_REG_OFFSET_MAX)), "invalid pin register off @%d", (pin.reg_off))
#define FIOCTRL_ASSERT_FUNC(func) FASSERT_MSG((func < FPIN_NUM_OF_FUNC), "invalid func as %d", (func))
#define FIOCTRL_ASSERT_PULL(pull) FASSERT_MSG((pull < FPIN_NUM_OF_PULL), "invalid pull as %d", (pull))

#define FIOCTRL_ASSERT_DELAY_REG_OFF(pin) FASSERT_MSG(((pin.reg_off >= FIOCTRL_DELAY_REG_OFFSET_MIN) && (pin.reg_off <= FIOCTRL_DELAY_REG_OFFSET_MAX)), "invalid delay pin register off @%d", (pin.reg_off))
#define FIOCTRL_ASSERT_DELAY(delay) FASSERT_MSG(((delay) < FPIN_NUM_OF_DELAY), "invalid delay as %d", (delay));
/************************** Function Prototypes ******************************/

/************************** Variable Definitions *****************************/

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

/**
 * @name: FPinGetFunc
 * @msg: 获取IO引脚当前的复用功能
 * @return {FPinFunc} 当前的复用功能
 * @param {FPinIndex} pin IO引脚索引
 * @note 参考编程手册,使用 FIOCTRL_INDEX 宏定义index的值
 */
FPinFunc FPinGetFunc(const FPinIndex pin)
{
    FIOCTRL_ASSERT_REG_OFF(pin);

    u32 func_beg = FIOCTRL_FUNC_BEG_OFF(pin.reg_bit);
    u32 func_end = FIOCTRL_FUNC_END_OFF(pin.reg_bit);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);
    u32 func = GET_REG32_BITS(reg_val, func_end, func_beg);
    FIOCTRL_ASSERT_FUNC(func);

    return (FPinFunc)GET_REG32_BITS(reg_val, func_end, func_beg);
}

/**
 * @name: FPinSetFunc
 * @msg: 设置IO引脚复用功能
 * @return {*}
 * @param {FPinIndex} pin IO引脚索引
 * @param {FPinFunc} func IO复用功能
 * @note 参考编程手册,使用 FIOCTRL_INDEX 宏定义index的值
 */
void FPinSetFunc(const FPinIndex pin, FPinFunc func)
{
    FIOCTRL_ASSERT_REG_OFF(pin);
    FIOCTRL_ASSERT_FUNC(func);

    u32 func_beg = FIOCTRL_FUNC_BEG_OFF(pin.reg_bit);
    u32 func_end = FIOCTRL_FUNC_END_OFF(pin.reg_bit);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);

    reg_val &= ~GENMASK(func_end, func_beg);
    reg_val |= SET_REG32_BITS(func, func_end, func_beg);

    FtOut32(FIOCTRL_REG_BASE_ADDR + pin.reg_off, reg_val);
    return;
}

/**
 * @name: FPinGetPull
 * @msg: 获取IO引脚当前的上下拉设置
 * @return {*}
 * @param {FPinIndex} pin IO引脚索引
 * @note 参考编程手册,使用 FIOCTRL_INDEX 宏定义index的值
 */
FPinPull FPinGetPull(const FPinIndex pin)
{
    FIOCTRL_ASSERT_REG_OFF(pin);

    u32 pull_beg = FIOCTRL_PULL_BEG_OFF(pin.reg_bit);
    u32 pull_end = FIOCTRL_PULL_END_OFF(pin.reg_bit);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);
    u32 pull = GET_REG32_BITS(reg_val, pull_end, pull_beg);

    FIOCTRL_ASSERT_PULL(pull);
    return (FPinPull)pull;
}

/**
 * @name: FPinSetPull
 * @msg: 设置IO引脚当前的上下拉
 * @return {*}
 * @param {FPinIndex} pin IO引脚索引
 * @param {FPinPull} pull 上下拉设置
 */
void FPinSetPull(const FPinIndex pin, FPinPull pull)
{
    FIOCTRL_ASSERT_REG_OFF(pin);
    FIOCTRL_ASSERT_PULL(pull);

    u32 pull_beg = FIOCTRL_PULL_BEG_OFF(pin.reg_bit);
    u32 pull_end = FIOCTRL_PULL_END_OFF(pin.reg_bit);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);

    reg_val &= ~GENMASK(pull_end, pull_beg);
    reg_val |= SET_REG32_BITS(pull, pull_end, pull_beg);

    FtOut32(FIOCTRL_REG_BASE_ADDR + pin.reg_off, reg_val);
    return;
}

/**
 * @name: FPinGetConfig
 * @msg: 获取IO引脚的复用、上下拉和驱动能力设置
 * @return {*}
 * @param {FPinIndex} pin IO引脚索引
 * @param {FPinFunc} *func IO复用功能
 * @param {FPinPull} *pull pull 上下拉设置
 */
void FPinGetConfig(const FPinIndex pin, FPinFunc *func, FPinPull *pull)
{
    FIOCTRL_ASSERT_REG_OFF(pin);

    u32 func_beg = FIOCTRL_FUNC_BEG_OFF(pin.reg_bit);
    u32 func_end = FIOCTRL_FUNC_END_OFF(pin.reg_bit);
    u32 pull_beg = FIOCTRL_PULL_BEG_OFF(pin.reg_bit);
    u32 pull_end = FIOCTRL_PULL_END_OFF(pin.reg_bit);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);

    if (func)
    {
        *func = GET_REG32_BITS(reg_val, func_end, func_beg);
    }

    if (pull)
    {
        *pull = GET_REG32_BITS(reg_val, pull_end, pull_beg);
    }

    return;
}

/**
 * @name: FPinSetConfig
 * @msg: 设置IO引脚的复用、上下拉和驱动能力
 * @return {*}
 * @param {FPinIndex} pin IO引脚索引
 * @param {FPinFunc} func IO复用功能
 * @param {FPinPull} pull pull 上下拉设置
 */
void FPinSetConfig(const FPinIndex pin, FPinFunc func, FPinPull pull)
{
    FIOCTRL_ASSERT_REG_OFF(pin);
    u32 func_beg = FIOCTRL_FUNC_BEG_OFF(pin.reg_bit);
    u32 func_end = FIOCTRL_FUNC_END_OFF(pin.reg_bit);
    u32 pull_beg = FIOCTRL_PULL_BEG_OFF(pin.reg_bit);
    u32 pull_end = FIOCTRL_PULL_END_OFF(pin.reg_bit);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);

    reg_val &= ~GENMASK(func_end, func_beg);
    reg_val |= SET_REG32_BITS(func, func_end, func_beg);

    reg_val &= ~GENMASK(pull_end, pull_beg);
    reg_val |= SET_REG32_BITS(pull, pull_end, pull_beg);

    FtOut32(FIOCTRL_REG_BASE_ADDR + pin.reg_off, reg_val);
    return;
}

/**
 * @name: FPinGetDelay
 * @msg: 获取IO引脚当前的延时设置
 * @return {FPinDelay} 当前的延时设置
 * @param {FPinIndex} pin IO引脚延时设置索引
 * @param {FPinDelayDir} dir 输入/输出延时
 * @param {FPinDelayType} type 精调/粗调延时
 */
FPinDelay FPinGetDelay(const FPinIndex pin, FPinDelayDir dir, FPinDelayType type)
{
    FIOCTRL_ASSERT_DELAY_REG_OFF(pin);
    u8 delay = 0;
    const u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);
    u32 delay_beg = 0, delay_end = 0;

    if (FPIN_OUTPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_OUT_BEG_OFF(pin.reg_off);
    }
    else if (FPIN_INPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_IN_BEG_OFF(pin.reg_off);
    }
    else
    {
        FASSERT(0);
    }

    if (FPIN_DELAY_FINE_TUNING == type)
    {
        delay = FIOCTRL_DELICATE_DELAY_GET(reg_val, delay_beg);
    }
    else if (FPIN_DELAY_COARSE_TUNING == type)
    {
        delay = FIOCTRL_ROUGH_DELAY_GET(reg_val, delay_beg);
    }
    else
    {
        FASSERT(0);
    }

    FIOCTRL_ASSERT_DELAY(delay);
    return (FPinDelay)delay;
}


/**
 * @name: FPinGetDelayEn
 * @msg: 获取IO引脚当前的延时使能标志位
 * @return {*}
 * @param {FPinIndex} pin IO引脚延时设置索引
 * @param {FPinDelayDir} dir 输入/输出延时
 */
boolean FPinGetDelayEn(const FPinIndex pin, FPinDelayDir dir)
{
    FIOCTRL_ASSERT_DELAY_REG_OFF(pin);
    boolean enabled = FALSE;
    const u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);
    u32 delay_beg = 0, delay_end = 0;

    if (FPIN_OUTPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_OUT_BEG_OFF(pin.reg_off);
    }
    else if (FPIN_INPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_IN_BEG_OFF(pin.reg_off);
    }
    else
    {
        FASSERT(0);
    }

    if (FIOCTRL_DELAY_EN(delay_beg) & reg_val)
    {
        enabled = TRUE;
    }

    return enabled;
}

/**
 * @name: FPinSetDelay
 * @msg: 设置IO引脚延时
 * @return {*}
 * @param {FPinIndex} pin IO引脚延时设置索引
 * @param {FPinDelayDir} dir 输入/输出延时
 * @param {FPinDelayType} type 精调/粗调延时
 * @param {FPinDelay} delay 延时档位设置 0 ~ 8 档可用
 */
void FPinSetDelay(const FPinIndex pin, FPinDelayDir dir, FPinDelayType type, FPinDelay delay)
{
    FIOCTRL_ASSERT_DELAY_REG_OFF(pin);
    FIOCTRL_ASSERT_DELAY(delay);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);
    u32 delay_beg = 0, delay_end = 0;

    if (FPIN_OUTPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_OUT_BEG_OFF(pin.reg_off);
    }
    else if (FPIN_INPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_IN_BEG_OFF(pin.reg_off);
    }
    else
    {
        FASSERT(0);
    }

    if (FPIN_DELAY_FINE_TUNING == type)
    {
        reg_val &= ~FIOCTRL_DELICATE_DELAY_MASK(delay_beg);
        delay = FIOCTRL_DELICATE_DELAY_GET(reg_val, delay_beg);
    }
    else if (FPIN_DELAY_COARSE_TUNING == type)
    {
        reg_val &= ~FIOCTRL_ROUGH_DELAY_MASK(delay_beg);
        delay = FIOCTRL_ROUGH_DELAY_GET(reg_val, delay_beg);
    }
    else
    {
        FASSERT(0);
    }

    FtOut32(FIOCTRL_REG_BASE_ADDR + pin.reg_off, reg_val);
    return;
}

/**
 * @name: FPinSetDelayEn
 * @msg: 使能/去使能IO引脚延时
 * @return {*}
 * @param {FPinIndex} pin IO引脚延时设置索引
 * @param {FPinDelayDir} dir 输入/输出延时
 * @param {boolean} enable TRUE: 使能, FALSE: 去使能
 */
void FPinSetDelayEn(const FPinIndex pin, FPinDelayDir dir, boolean enable)
{
    FIOCTRL_ASSERT_DELAY_REG_OFF(pin);
    u32 reg_val = FtIn32(FIOCTRL_REG_BASE_ADDR + pin.reg_off);
    u32 delay_beg = 0, delay_end = 0;

    if (FPIN_OUTPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_OUT_BEG_OFF(pin.reg_off);
    }
    else if (FPIN_INPUT_DELAY == dir)
    {
        delay_beg = FIOCTRL_DELAY_IN_BEG_OFF(pin.reg_off);
    }
    else
    {
        FASSERT(0);
    }

    reg_val &= ~FIOCTRL_DELAY_EN(delay_beg);
    if (enable)
    {
        reg_val |= FIOCTRL_DELAY_EN(delay_beg);
    }

    FtOut32(FIOCTRL_REG_BASE_ADDR + pin.reg_off, reg_val);
    return;
}