utils.h 5.1 KB

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