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
|
/**
*********************************************************************************
*
* @file ald_utils.c
* @brief This file contains the Utilities functions/types for the driver.
*
* @version V1.0
* @date 8 Feb. 2023
* @author AE Team
* @note
* Change Logs:
* Date Author Notes
* 8 Feb. 2023 Lisq The first version
*
* Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the License); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**********************************************************************************
*/
#include <string.h>
#include "ald_conf.h"
/** @defgroup EASTSOFT ES32VF2264 ALD
* @brief Shanghai Eastsoft Microelectronics E902 Chip Abstraction Layer Driver(ALD)
* @{
*/
/** @defgroup UTILS Utils
* @brief Utils module driver
* @{
*/
/** @defgroup ALD_Private_Constants Private Constants
* @brief ALD Private Constants
* @{
*/
/**
* @brief ALD version number
*/
#define __ALD_VERSION_MAIN (0x01) /**< [31:24] main version */
#define __ALD_VERSION_SUB1 (0x00) /**< [23:16] sub1 version */
#define __ALD_VERSION_SUB2 (0x00) /**< [15:8] sub2 version */
#define __ALD_VERSION_RC (0x00) /**< [7:0] release candidate */
#define __ALD_VERSION ((__ALD_VERSION_MAIN << 24) | \
(__ALD_VERSION_SUB1 << 16) | \
(__ALD_VERSION_SUB2 << 8 ) | \
(__ALD_VERSION_RC))
/**
* @}
*/
/** @defgroup ALD_Private_Variables Private Variables
* @{
*/
/** @brief lib_tick: Increase by one millisecond
*/
__IO uint32_t lib_tick;
uint32_t __systick_interval = ALD_SYSTICK_INTERVAL_1MS;
/**
* @}
*/
/** @defgroup ALD_Public_Functions Public Functions
* @{
*/
/** @defgroup ALD_Public_Functions_Group1 Initialization Function
* @brief Initialization functions
*
* @verbatim
===============================================================================
##### Initialization functions #####
===============================================================================
[..] This section provides functions allowing to:
(+) Initializes interface, the NVIC allocation and initial clock
configuration. It initializes the source of time base also when timeout
is needed and the backup domain when enabled.
(+) Configure The time base source to have 1ms time base with a dedicated
Tick interrupt priority.
(++) Systick timer is used by default as source of time base, but user
can eventually implement his proper time base source (a general purpose
timer for example or other time source), keeping in mind that Time base
duration should be kept 1ms.
(++) Time base configuration function (ald_tick_init()) is called automatically
at the beginning of the program after reset by ald_cmu_init() or at
any time when clock is configured.
(++) Source of time base is configured to generate interrupts at regular
time intervals. Care must be taken if ald_delay_ms() is called from a
peripheral ISR process, the Tick interrupt line must have higher priority
(numerically lower) than the peripheral interrupt. Otherwise the caller
ISR process will be blocked.
(++) functions affecting time base configurations are declared as __weak
to make override possible in case of other implementations in user file.
(+) Configure the interval of Systick interrupt.
@endverbatim
* @{
*/
/**
* @brief This function Configures time base source, CLINT and DMA.
* @note This function is called at the beginning of program after reset and before
* the clock configuration.
* @note The time base configuration is based on MSI clock when exiting from Reset.
* Once done, time base tick start incrementing.
* In the default implementation, Systick is used as source of time base.
* The tick variable is incremented each 1ms in its ISR.
* @retval None
*/
void ald_cmu_init(void)
{
ald_cmu_clock_config_default();
ald_tick_init(TICK_INT_PRIORITY);
#ifdef ALD_DMA
ald_cmu_perh_clock_config(ALD_CMU_PERH_DMA, ENABLE);
ald_dma_init();
#endif
return;
}
/**
* @brief This function configures the source of the time base.
* The time source is configured to have 1ms time base with a dedicated
* Tick interrupt priority.
* @note In the default implementation, SysTick timer is the source of time base.
* It is used to generate interrupts at regular time intervals.
* Care must be taken if ald_delay_ms() is called from a peripheral ISR process,
* The SysTick interrupt must have higher priority (numerically lower)
* than the peripheral interrupt. Otherwise the caller ISR process will be blocked.
* The function is declared as __weak to be overwritten in case of other
* implementation in user file.
* @param prio: Tick interrupt priority.
* @retval None
*/
__weak void ald_tick_init(uint32_t prio)
{
/* Configure the SysTick IRQ */
csi_coret_config(ald_cmu_get_sys_clock() / ALD_SYSTICK_INTERVAL_1MS, CLINT_IRQn);
csi_vic_set_prio(CLINT_IRQn, prio);
csi_vic_enable_sirq(CLINT_IRQn);
return;
}
/**
* @brief Selects the interval of systick interrupt.
* @param value: The value of interval:
* @arg @ref SYSTICK_INTERVAL_1MS 1 millisecond
* @arg @ref SYSTICK_INTERVAL_10MS 10 milliseconds
* @arg @ref SYSTICK_INTERVAL_100MS 100 milliseconds
* @arg @ref SYSTICK_INTERVAL_1000MS 1 second
* @retval None
*/
void ald_systick_interval_select(ald_systick_interval_t value)
{
assert_param(IS_SYSTICK_INTERVAL(value));
if (value == 0)
return;
csi_coret_config(ald_cmu_get_sys_clock() / value, CLINT_IRQn);
__systick_interval = value;
if (TICK_INT_PRIORITY != 15)
csi_vic_set_prio(CLINT_IRQn, TICK_INT_PRIORITY);
return;
}
/**
* @}
*/
/** @defgroup ALD_Public_Functions_Group2 Control functions
* @brief Control functions
*
* @verbatim
===============================================================================
##### Control functions #####
===============================================================================
[..] This section provides functions allowing to:
(+) Provide a tick value in millisecond
(+) Provide a blocking delay in millisecond
(+) Suspend the time base source interrupt
(+) Resume the time base source interrupt
(+) Get the ALD version
(+) Waiting for flag
(+) Configure the interrupt
(+) Provide system tick value
(+) Initialize core timestamp
(+) Get core timestamp
(+) Get CPU ID
(+) Get UID
(+) Get CHIPID
@endverbatim
* @{
*/
/**
* @brief This function invoked by Systick ISR.
* @note This function is declared as __weak to be overwritten in case of
* other implementations in user file.
* @retval None
*/
__weak void ald_systick_irq_cbk(void)
{
/* do nothing */
return;
}
/**
* @brief This function is called to increment a global variable "lib_tick"
* used as application time base.
* @note In the default implementation, this variable is incremented each 1ms
* in Systick ISR.
* @note This function is declared as __weak to be overwritten in case of other
* implementations in user file.
* @retval None
*/
__weak void ald_inc_tick(void)
{
++lib_tick;
ald_systick_irq_cbk();
}
/**
* @brief Provides a tick value in millisecond.
* @note This function is declared as __weak to be overwritten in case of other
* implementations in user file.
* @retval tick value
*/
__weak uint32_t ald_get_tick(void)
{
return lib_tick;
}
/**
* @brief This function provides accurate delay (in milliseconds) based
* on variable incremented.
* @note In the default implementation, SysTick timer is the source of time base.
* It is used to generate interrupts at regular time intervals where lib_tick
* is incremented.
* @note This function is declared as __weak to be overwritten in case of other
* implementations in user file.
* @param delay: specifies the delay time length, in milliseconds.
* @retval None
*/
__weak void ald_delay_1ms(__IO uint32_t delay)
{
uint32_t tick, __delay;
switch (__systick_interval) {
case ALD_SYSTICK_INTERVAL_1MS:
__delay = delay;
break;
case ALD_SYSTICK_INTERVAL_10MS:
__delay = delay / 10;
break;
case ALD_SYSTICK_INTERVAL_100MS:
__delay = delay / 100;
break;
case ALD_SYSTICK_INTERVAL_1000MS:
__delay = delay / 1000;
break;
default:
__delay = delay;
break;
}
tick = ald_get_tick();
__delay = __delay == 0 ? 1 : __delay;
while ((ald_get_tick() - tick) < __delay)
;
}
/**
* @brief This function provides accurate delay (in microseconds) based
* on variable incremented.
* @note In the default implementation, SysTick timer is the source of time base.
* It is used to generate interrupts at regular time intervals where lib_tick
* is incremented.
* @note This function is declared as __weak to be overwritten in case of other
* implementations in user file.
* @param delay: specifies the delay time length, in microseconds(us).
* @retval None
*/
__weak void ald_delay_1us(__IO uint32_t delay)
{
uint32_t start, now, delta, us_tick;
start = CORET->MTIME;
us_tick = ald_cmu_get_sys_clock() / 1000000UL;
do
{
now = CORET->MTIME;
delta = now - start;
}
while (delta < (us_tick * delay));
}
/**
* @brief This method returns the ALD revision
* @retval version: 0xXYZR (8bits for each decimal, R for RC)
*/
uint32_t ald_get_ald_version(void)
{
return __ALD_VERSION;
}
/**
* @brief Configure the flash wait period.
* @param cycle: The period.
* @retval None
*/
void ald_flash_wait_config(uint8_t cycle)
{
uint32_t tmp;
tmp = MSC->MEMWAIT;
MODIFY_REG(tmp, MSC_MEMWAIT_FLASH_W_MSK, (0x30U | cycle) << MSC_MEMWAIT_FLASH_W_POSS);
MSC->MEMWAIT = tmp;
return;
}
/**
* @brief Waiting the specified bit in the register change to SET/RESET.
* @param reg: The register address.
* @param bit: The specified bit.
* @param status: The status for waiting.
* @param timeout: Timeout duration.
* @retval Status, see @ref ald_status_t.
*/
ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout)
{
uint32_t tick = ald_get_tick();
assert_param(timeout > 0);
if (status == SET) {
while (!(IS_BIT_SET(*reg, bit))) {
if (((ald_get_tick()) - tick) > timeout)
return ALD_TIMEOUT;
}
}
else {
while ((IS_BIT_SET(*reg, bit))) {
if (((ald_get_tick()) - tick) > timeout)
return ALD_TIMEOUT;
}
}
return ALD_OK;
}
/**
* @brief Configure interrupt.
* @param irq: Interrunpt type.
* @param prio: preempt priority(0-7). The larger the prio value, the higher the priority.
* @param status: Status.
* @arg ENABLE
* @arg DISABLE
* @retval None
*/
void ald_mcu_irq_config(IRQn_Type irq, uint8_t prio, type_func_t status)
{
assert_param(IS_FUNC_STATE(status));
if (status == ENABLE)
{
csi_vic_set_prio(irq, prio);
csi_vic_enable_sirq(irq);
}
else
{
csi_vic_disable_sirq(irq);
}
return;
}
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
|