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 2017
9   * @author  AE Team
10   * @note
11   *
12   * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
13   *
14   * SPDX-License-Identifier: Apache-2.0
15   *
16   * Licensed under the Apache License, Version 2.0 (the License); you may
17   * not use this file except in compliance with the License.
18   * You may obtain a copy of the License at
19   *
20   * www.apache.org/licenses/LICENSE-2.0
21   *
22   * Unless required by applicable law or agreed to in writing, software
23   * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25   * See the License for the specific language governing permissions and
26   * limitations under the License.
27   *
28   *********************************************************************************
29   */
30 
31 #ifndef __UTILS_H__
32 #define __UTILS_H__
33 
34 #ifdef __cplusplus
35  extern "C" {
36 #endif
37 
38 #include <stdlib.h>
39 #include "ald_conf.h"
40 #include "type.h"
41 #include "es32f065x.h"
42 
43 
44 /** @addtogroup ES32FXXX_ALD
45   * @{
46   */
47 
48 /** @addtogroup 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 	OK      = 0x0U,	/**< Status: OK */
65 	ERROR   = 0x1U,	/**< Status: ERROR */
66 	BUSY    = 0x2U,	/**< Status: BUSY */
67 	TIMEOUT = 0x3U,	/**< Status: TIMEOUT */
68 } ald_status_t;
69 
70 /**
71   * @brief  SysTick interval definition
72   */
73 typedef enum {
74 	SYSTICK_INTERVAL_1MS    = 1000U,	/**< Interval is 1ms */
75 	SYSTICK_INTERVAL_10MS   = 100U,		/**< Interval is 10ms */
76 	SYSTICK_INTERVAL_100MS  = 10U,		/**< Interval is 100ms */
77 	SYSTICK_INTERVAL_1000MS = 1U,		/**< Interval is 1s */
78 } 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 RESET_HANDLE_STATE(x)	((x)->state = 0)
90 #define __LOCK(x)				\
91 	do {					\
92 		if ((x)->lock == LOCK) {	\
93 			return BUSY;		\
94 		}				\
95 		else {				\
96 			(x)->lock = LOCK;	\
97 		}				\
98 	} while (0)
99 
100 #define __UNLOCK(x)				\
101 	do {					\
102 		(x)->lock = UNLOCK;		\
103 	} while (0)
104 
105 /**
106   * @}
107   */
108 
109 /** @defgroup ALD_Private_Macros Private Macros
110   * @{
111   */
112 #define MCU_UID0_ADDR	0x000403E0U
113 #define MCU_UID1_ADDR	0x000403E8U
114 #define MCU_UID2_ADDR	0x000403F0U
115 #define MCU_CHIPID_ADDR	0x000403F8U
116 #define IS_PRIO(x)	((x) < 4)
117 #define IS_SYSTICK_INTERVAL(x)	(((x) == SYSTICK_INTERVAL_1MS)   || \
118                                  ((x) == SYSTICK_INTERVAL_10MS)  || \
119                                  ((x) == SYSTICK_INTERVAL_100MS) || \
120                                  ((x) == SYSTICK_INTERVAL_1000MS))
121 /**
122   * @}
123   */
124 
125 /** @addtogroup ALD_Public_Functions
126   * @{
127   */
128 
129 /** @addtogroup ALD_Public_Functions_Group1
130   * @{
131   */
132 
133 /* Initialization functions */
134 void ald_cmu_init(void);
135 void ald_tick_init(uint32_t prio);
136 void ald_systick_interval_select(systick_interval_t value);
137 
138 /**
139   * @}
140   */
141 
142 /** @addtogroup ALD_Public_Functions_Group2
143   * @{
144   */
145 /* Peripheral Control functions */
146 void ald_inc_tick_weak(void);
147 void ald_delay_ms(__IO uint32_t delay);
148 uint32_t ald_get_tick(void);
149 void ald_suspend_tick(void);
150 void ald_resume_tick(void);
151 void ald_systick_irq_cbk(void);
152 void ald_inc_tick(void);
153 uint32_t ald_get_ald_version(void);
154 ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout);
155 void ald_mcu_irq_config(IRQn_Type irq, uint8_t prio, type_func_t status);
156 uint32_t ald_mcu_get_tick(void);
157 uint32_t ald_mcu_get_cpu_id(void);
158 void ald_mcu_get_uid(uint8_t *buf);
159 uint32_t ald_mcu_get_chipid(void);
160 /**
161   * @}
162   */
163 
164 /**
165   * @}
166   */
167 
168 /**
169   * @}
170   */
171 
172 /**
173   * @}
174   */
175 
176 #ifdef __cplusplus
177 }
178 #endif
179 
180 #endif /* __UTILS_H__ */
181