1 /**
2   *********************************************************************************
3   *
4   * @file    utils.h
5   * @brief   This file contains the Utilities functions/types for the driver.
6   *
7   * @version V1.0
8   * @date    07 Nov 2019
9   * @author  AE Team
10   * @note
11   *          Change Logs:
12   *          Date            Author          Notes
13   *          07 Nov 2019     AE Team         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 __UTILS_H__
34 #define __UTILS_H__
35 
36 #ifdef __cplusplus
37  extern "C" {
38 #endif
39 
40 #include <stdlib.h>
41 #include "type.h"
42 #ifdef ES32F36xx
43 #include "es32f36xx.h"
44 #elif ES32F39xx
45 #include "es32f39xx.h"
46 #elif ES32F336x
47 #include "es32f336x.h"
48 #endif
49 
50 /** @addtogroup ES32FXXX_ALD
51   * @{
52   */
53 
54 /** @addtogroup UTILS
55   * @{
56   */
57 
58 /** @defgroup ALD_Public_Types Public Types
59   * @{
60   */
61 /**
62   * @brief  SysTick interval
63   */
64 extern uint32_t __systick_interval;
65 
66 /**
67   * @brief  ALD Status structures definition
68   */
69 typedef enum {
70 	OK      = 0x0U,	/**< OK */
71 	ERROR   = 0x1U,	/**< ERROR */
72 	BUSY    = 0x2U,	/**< BUSY */
73 	TIMEOUT = 0x3U,	/**< TIMEOUT */
74 } ald_status_t;
75 
76 /**
77   * @brief NVIC Preemption Priority Group
78   */
79 typedef enum {
80 	NVIC_PRIORITY_GROUP_0 = 0x7U,	/**< 0-bits for pre-emption priority 4-bits for subpriority */
81 	NVIC_PRIORITY_GROUP_1 = 0x6U,	/**< 1-bits for pre-emption priority 3-bits for subpriority */
82 	NVIC_PRIORITY_GROUP_2 = 0x5U,	/**< 2-bits for pre-emption priority 2-bits for subpriority */
83 	NVIC_PRIORITY_GROUP_3 = 0x4U,	/**< 3-bits for pre-emption priority 1-bits for subpriority */
84 	NVIC_PRIORITY_GROUP_4 = 0x3U,	/**< 4-bits for pre-emption priority 0-bits for subpriority */
85 } nvic_priority_group_t;
86 
87 /**
88   * @brief  SysTick interval definition
89   */
90 typedef enum {
91 	SYSTICK_INTERVAL_1MS    = 1000U,	/**< Interval is 1ms */
92 	SYSTICK_INTERVAL_10MS   = 100U,		/**< Interval is 10ms */
93 	SYSTICK_INTERVAL_100MS  = 10U,		/**< Interval is 100ms */
94 	SYSTICK_INTERVAL_1000MS = 1U,		/**< Interval is 1s */
95 } systick_interval_t;
96 /**
97   * @}
98   */
99 
100 /** @defgroup ALD_Public_Macros Public Macros
101   * @{
102   */
103 #define ALD_MAX_DELAY	0xFFFFFFFFU
104 #define IS_BIT_SET(reg, bit)	(((reg) & (bit)) != RESET)
105 #define IS_BIT_CLR(reg, bit)	(((reg) & (bit)) == RESET)
106 #define RESET_HANDLE_STATE(x)	((x)->state = 0)
107 #define __LOCK(x)			\
108 do {					\
109 	if ((x)->lock == LOCK) {	\
110 		return BUSY;		\
111 	}				\
112 	else {				\
113 		(x)->lock = LOCK;	\
114 	}				\
115 } while (0)
116 #define __UNLOCK(x)			\
117 do {					\
118 	(x)->lock = UNLOCK;		\
119 } while (0)
120 #define ALD_PANIC() 	\
121 do {			\
122 	while (1)	\
123 		;	\
124 } while (0)
125 /**
126   * @}
127   */
128 
129 /** @defgroup ALD_Private_Macros Private Macros
130   * @{
131   */
132 #define MCU_UID0_ADDR	0x000803E0U
133 #define MCU_UID1_ADDR	0x000803E8U
134 #define MCU_UID2_ADDR	0x000803ECU
135 #define MCU_CHIPID_ADDR	0x000803F8U
136 #define DWT_CR		*(uint32_t *)0xE0001000U
137 #define DWT_CYCCNT	*(volatile uint32_t *)0xE0001004U
138 #define DEM_CR		*(uint32_t *)0xE000EDFCU
139 #define DEM_CR_TRCENA	(1U << 24)
140 #define DWT_CR_CYCCNTEA	(1U << 0)
141 #define IS_PREEMPT_PRIO(x)	((x) < 16)
142 #define IS_SUB_PRIO(x)		((x) < 16)
143 #define IS_SYSTICK_INTERVAL(x)	(((x) == SYSTICK_INTERVAL_1MS)   || \
144                                  ((x) == SYSTICK_INTERVAL_10MS)  || \
145                                  ((x) == SYSTICK_INTERVAL_100MS) || \
146                                  ((x) == SYSTICK_INTERVAL_1000MS))
147 /**
148   * @}
149   */
150 
151 /** @addtogroup ALD_Public_Functions
152   * @{
153   */
154 
155 /** @addtogroup ALD_Public_Functions_Group1
156   * @{
157   */
158 
159 /* Initialization functions */
160 void ald_cmu_init(void);
161 void ald_tick_init(uint32_t prio);
162 void ald_systick_interval_select(systick_interval_t value);
163 
164 /**
165   * @}
166   */
167 
168 /** @addtogroup ALD_Public_Functions_Group2
169   * @{
170   */
171 /* Peripheral Control functions */
172 void ald_inc_tick(void);
173 void ald_systick_irq_cbk(void);
174 void ald_delay_ms(__IO uint32_t delay);
175 void ald_delay_us(__IO uint32_t delay);
176 uint32_t ald_get_tick(void);
177 void ald_suspend_tick(void);
178 void ald_resume_tick(void);
179 uint32_t ald_get_ald_version(void);
180 void ald_flash_wait_config(uint8_t cycle);
181 ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout);
182 void ald_mcu_irq_config(IRQn_Type irq, uint8_t preempt_prio, uint8_t sub_prio, type_func_t status);
183 void ald_mcu_timestamp_init(void);
184 uint32_t ald_mcu_get_timestamp(void);
185 uint32_t ald_mcu_get_cpu_id(void);
186 void ald_mcu_get_uid(uint8_t *buf);
187 uint32_t ald_mcu_get_chipid(void);
188 /**
189   * @}
190   */
191 
192 /**
193   * @}
194   */
195 
196 /**
197   * @}
198   */
199 
200 /**
201   * @}
202   */
203 
204 #ifdef __cplusplus
205 }
206 #endif
207 
208 #endif /* __UTILS_H__ */
209