1 /**
2   *********************************************************************************
3   *
4   * @file    ald_utils.h
5   * @brief   This file contains the Utilities functions/types for the driver.
6   *
7   * @version V1.0
8   * @date    8 Feb. 2023
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          8 Feb. 2023     Lisq            The first version
14   *
15   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
16   *
17   * SPDX-License-Identifier: Apache-2.0
18   *
19   * Licensed under the Apache License, Version 2.0 (the License); you may
20   * not use this file except in compliance with the License.
21   * You may obtain a copy of the License at
22   *
23   * www.apache.org/licenses/LICENSE-2.0
24   *
25   * Unless required by applicable law or agreed to in writing, software
26   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
27   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28   * See the License for the specific language governing permissions and
29   * limitations under the License.
30   **********************************************************************************
31   */
32 
33 #ifndef __ALD_UTILS_H__
34 #define __ALD_UTILS_H__
35 
36 #ifdef __cplusplus
37  extern "C" {
38 #endif /* __cplusplus */
39 
40 #include <stdlib.h>
41 #include "es32vf2264.h"
42 #include "type.h"
43 
44 /** @addtogroup ALD
45   * @{
46   */
47 
48 /** @addtogroup ALD_UTILS
49   * @{
50   */
51 
52 /** @defgroup ALD_Public_Types Public Types
53   * @{
54   */
55 /**
56   * @brief  SysTick interval
57   */
58 extern uint32_t __systick_interval;
59 
60 /**
61   * @brief  ALD Status structures definition
62   */
63 typedef enum {
64     ALD_OK      = 0x0U, /**< OK */
65     ALD_ERROR   = 0x1U, /**< ERROR */
66     ALD_BUSY    = 0x2U, /**< BUSY */
67     ALD_TIMEOUT = 0x3U, /**< TIMEOUT */
68 } ald_status_t;
69 
70 /**
71   * @brief  SysTick interval definition
72   */
73 typedef enum {
74     ALD_SYSTICK_INTERVAL_1MS    = 1000U,    /**< Interval is 1ms */
75     ALD_SYSTICK_INTERVAL_10MS   = 100U,     /**< Interval is 10ms */
76     ALD_SYSTICK_INTERVAL_100MS  = 10U,      /**< Interval is 100ms */
77     ALD_SYSTICK_INTERVAL_1000MS = 1U,       /**< Interval is 1s */
78 } ald_systick_interval_t;
79 /**
80   * @}
81   */
82 
83 /** @defgroup ALD_Public_Macros Public Macros
84   * @{
85   */
86 #define ALD_MAX_DELAY   0xFFFFFFFFU
87 #define IS_BIT_SET(reg, bit)    (((reg) & (bit)) != RESET)
88 #define IS_BIT_CLR(reg, bit)    (((reg) & (bit)) == RESET)
89 #define ALD_RESET_HANDLE_STATE(x)   ((x)->state = 0)
90 #define __LOCK(x)           \
91 do {                    \
92     if ((x)->lock == LOCK) {    \
93         return ALD_BUSY;        \
94     }               \
95     else {              \
96         (x)->lock = LOCK;   \
97     }               \
98 } while (0)
99 #define __UNLOCK(x)         \
100 do {                    \
101     (x)->lock = UNLOCK;     \
102 } while (0)
103 #define ALD_PANIC()     \
104 do {            \
105     while (1)   \
106         ;   \
107 } while (0)
108 
109 /**
110   * @brief  Nested IRQ start : Save CSR and enable global interrupt.
111   */
112 #define ALD_NEST_INT_START() \
113     volatile uint32_t val_mcause, val_mstatus, val_mepc; \
114     __ASM volatile("csrr %0, mcause" : "=r"(val_mcause)); \
115     __ASM volatile("csrr %0, mepc" : "=r"(val_mepc)); \
116     __ASM volatile("csrr %0, mstatus" : "=r"(val_mstatus)); \
117     __enable_irq(); \
118 
119 /**
120   * @brief  Nested IRQ end : Restore CSR and disable global interrupt.
121   */
122 #define ALD_NEST_INT_END() \
123     __disable_irq(); \
124     __ASM volatile("csrw mstatus, %0" : : "r"(val_mstatus));\
125     __ASM volatile("csrw mepc, %0" : : "r"(val_mepc));\
126     __ASM volatile("csrw mcause, %0" : : "r"(val_mcause));\
127 
128 /**
129   * @}
130   */
131 
132 /** @defgroup ALD_Private_Macros Private Macros
133   * @{
134   */
135 #define IS_SYSTICK_INTERVAL(x)  (((x) == ALD_SYSTICK_INTERVAL_1MS)   || \
136                                  ((x) == ALD_SYSTICK_INTERVAL_10MS)  || \
137                                  ((x) == ALD_SYSTICK_INTERVAL_100MS) || \
138                                  ((x) == ALD_SYSTICK_INTERVAL_1000MS))
139 /**
140   * @}
141   */
142 
143 /** @addtogroup ALD_Public_Functions
144   * @{
145   */
146 
147 /** @addtogroup ALD_Public_Functions_Group1
148   * @{
149   */
150 
151 /* Initialization functions */
152 void ald_cmu_init(void);
153 void ald_tick_init(uint32_t prio);
154 void ald_systick_interval_select(ald_systick_interval_t value);
155 
156 /**
157   * @}
158   */
159 
160 /** @addtogroup ALD_Public_Functions_Group2
161   * @{
162   */
163 /* Peripheral Control functions */
164 void ald_inc_tick(void);
165 void ald_systick_irq_cbk(void);
166 void ald_delay_1ms(__IO uint32_t delay);
167 void ald_delay_1us(__IO uint32_t delay);
168 uint32_t ald_get_tick(void);
169 uint32_t ald_get_ald_version(void);
170 void ald_flash_wait_config(uint8_t cycle);
171 ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout);
172 void ald_mcu_irq_config(IRQn_Type irq, uint8_t prio, type_func_t status);
173 /**
174   * @}
175   */
176 
177 /**
178   * @}
179   */
180 
181 /**
182   * @}
183   */
184 
185 /**
186   * @}
187   */
188 
189 #ifdef __cplusplus
190 }
191 #endif /* __cplusplus */
192 
193 #endif /* __ALD_UTILS_H__ */
194