ald_utils.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. #ifndef __ALD_UTILS_H__
  33. #define __ALD_UTILS_H__
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif /* __cplusplus */
  37. #include <stdlib.h>
  38. #include "es32vf2264.h"
  39. #include "type.h"
  40. /** @addtogroup ALD
  41. * @{
  42. */
  43. /** @addtogroup ALD_UTILS
  44. * @{
  45. */
  46. /** @defgroup ALD_Public_Types Public Types
  47. * @{
  48. */
  49. /**
  50. * @brief SysTick interval
  51. */
  52. extern uint32_t __systick_interval;
  53. /**
  54. * @brief ALD Status structures definition
  55. */
  56. typedef enum {
  57. ALD_OK = 0x0U, /**< OK */
  58. ALD_ERROR = 0x1U, /**< ERROR */
  59. ALD_BUSY = 0x2U, /**< BUSY */
  60. ALD_TIMEOUT = 0x3U, /**< TIMEOUT */
  61. } ald_status_t;
  62. /**
  63. * @brief SysTick interval definition
  64. */
  65. typedef enum {
  66. ALD_SYSTICK_INTERVAL_1MS = 1000U, /**< Interval is 1ms */
  67. ALD_SYSTICK_INTERVAL_10MS = 100U, /**< Interval is 10ms */
  68. ALD_SYSTICK_INTERVAL_100MS = 10U, /**< Interval is 100ms */
  69. ALD_SYSTICK_INTERVAL_1000MS = 1U, /**< Interval is 1s */
  70. } ald_systick_interval_t;
  71. /**
  72. * @}
  73. */
  74. /** @defgroup ALD_Public_Macros Public Macros
  75. * @{
  76. */
  77. #define ALD_MAX_DELAY 0xFFFFFFFFU
  78. #define IS_BIT_SET(reg, bit) (((reg) & (bit)) != RESET)
  79. #define IS_BIT_CLR(reg, bit) (((reg) & (bit)) == RESET)
  80. #define ALD_RESET_HANDLE_STATE(x) ((x)->state = 0)
  81. #define __LOCK(x) \
  82. do { \
  83. if ((x)->lock == LOCK) { \
  84. return ALD_BUSY; \
  85. } \
  86. else { \
  87. (x)->lock = LOCK; \
  88. } \
  89. } while (0)
  90. #define __UNLOCK(x) \
  91. do { \
  92. (x)->lock = UNLOCK; \
  93. } while (0)
  94. #define ALD_PANIC() \
  95. do { \
  96. while (1) \
  97. ; \
  98. } while (0)
  99. /**
  100. * @brief Nested IRQ start : Save CSR and enable global interrupt.
  101. */
  102. #define ALD_NEST_INT_START() \
  103. volatile uint32_t val_mcause, val_mstatus, val_mepc; \
  104. __ASM volatile("csrr %0, mcause" : "=r"(val_mcause)); \
  105. __ASM volatile("csrr %0, mepc" : "=r"(val_mepc)); \
  106. __ASM volatile("csrr %0, mstatus" : "=r"(val_mstatus)); \
  107. __enable_irq(); \
  108. /**
  109. * @brief Nested IRQ end : Restore CSR and disable global interrupt.
  110. */
  111. #define ALD_NEST_INT_END() \
  112. __disable_irq(); \
  113. __ASM volatile("csrw mstatus, %0" : : "r"(val_mstatus));\
  114. __ASM volatile("csrw mepc, %0" : : "r"(val_mepc));\
  115. __ASM volatile("csrw mcause, %0" : : "r"(val_mcause));\
  116. /**
  117. * @}
  118. */
  119. /** @defgroup ALD_Private_Macros Private Macros
  120. * @{
  121. */
  122. #define IS_SYSTICK_INTERVAL(x) (((x) == ALD_SYSTICK_INTERVAL_1MS) || \
  123. ((x) == ALD_SYSTICK_INTERVAL_10MS) || \
  124. ((x) == ALD_SYSTICK_INTERVAL_100MS) || \
  125. ((x) == ALD_SYSTICK_INTERVAL_1000MS))
  126. /**
  127. * @}
  128. */
  129. /** @addtogroup ALD_Public_Functions
  130. * @{
  131. */
  132. /** @addtogroup ALD_Public_Functions_Group1
  133. * @{
  134. */
  135. /* Initialization functions */
  136. void ald_cmu_init(void);
  137. void ald_tick_init(uint32_t prio);
  138. void ald_systick_interval_select(ald_systick_interval_t value);
  139. /**
  140. * @}
  141. */
  142. /** @addtogroup ALD_Public_Functions_Group2
  143. * @{
  144. */
  145. /* Peripheral Control functions */
  146. void ald_inc_tick(void);
  147. void ald_systick_irq_cbk(void);
  148. void ald_delay_1ms(__IO uint32_t delay);
  149. void ald_delay_1us(__IO uint32_t delay);
  150. uint32_t ald_get_tick(void);
  151. uint32_t ald_get_ald_version(void);
  152. void ald_flash_wait_config(uint8_t cycle);
  153. ald_status_t ald_wait_flag(uint32_t *reg, uint32_t bit, flag_status_t status, uint32_t timeout);
  154. void ald_mcu_irq_config(IRQn_Type irq, uint8_t prio, type_func_t status);
  155. /**
  156. * @}
  157. */
  158. /**
  159. * @}
  160. */
  161. /**
  162. * @}
  163. */
  164. /**
  165. * @}
  166. */
  167. #ifdef __cplusplus
  168. }
  169. #endif /* __cplusplus */
  170. #endif /* __ALD_UTILS_H__ */