1 /*
2  * Copyright 2024 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef FSL_COMMON_RISCV_H_
9 #define FSL_COMMON_RISCV_H_
10 
11 /*!
12  * @addtogroup ksdk_common
13  * @{
14  */
15 
16 /*******************************************************************************
17  * Definitions
18  ******************************************************************************/
19 
20 /*! @name Timer utilities */
21 /*! @{ */
22 /*! Macro to convert a microsecond period to raw count value */
23 #define USEC_TO_COUNT(us, clockFreqInHz) (uint64_t)(((uint64_t)(us) * (clockFreqInHz)) / 1000000U)
24 /*! Macro to convert a raw count value to microsecond */
25 #define COUNT_TO_USEC(count, clockFreqInHz) (uint64_t)((uint64_t)(count) * 1000000U / (clockFreqInHz))
26 
27 /*! Macro to convert a millisecond period to raw count value */
28 #define MSEC_TO_COUNT(ms, clockFreqInHz) (uint64_t)((uint64_t)(ms) * (clockFreqInHz) / 1000U)
29 /*! Macro to convert a raw count value to millisecond */
30 #define COUNT_TO_MSEC(count, clockFreqInHz) (uint64_t)((uint64_t)(count) * 1000U / (clockFreqInHz))
31 /*! @} */
32 
33 #define SDK_ISR_EXIT_BARRIER
34 
35 /*! @name Alignment variable definition macros */
36 /*! @{ */
37 /*! Macro to define a variable with alignbytes alignment */
38 #define SDK_ALIGN(var, alignbytes) var __attribute__((aligned(alignbytes)))
39 
40 /*! Macro to change a value to a given size aligned value */
41 #define SDK_SIZEALIGN(var, alignbytes) \
42     ((unsigned int)((var) + ((alignbytes)-1U)) & (unsigned int)(~(unsigned int)((alignbytes)-1U)))
43 /*! @} */
44 
45 /*!
46  * @def MSDK_REG_SECURE_ADDR(x)
47  * Convert the register address to the one used in secure mode.
48  *
49  * @def MSDK_REG_NONSECURE_ADDR(x)
50  * Convert the register address to the one used in unsecure mode.
51  */
52 #define MSDK_REG_SECURE_ADDR(x) ((typeof(x))((uintptr_t)(x) | (0x1UL << 28)))
53 #define MSDK_REG_NONSECURE_ADDR(x) ((typeof(x))((uintptr_t)(x) & ~(0x1UL << 28)))
54 
55 /*
56  * The fsl_clock.h is included here because it needs MAKE_VERSION/MAKE_STATUS/status_t
57  * defined in previous of this file.
58  */
59 #include "fsl_clock.h"
60 
61 /*******************************************************************************
62  * API
63  ******************************************************************************/
64 
65 #if defined(__cplusplus)
66 extern "C" {
67 #endif
68 
69 /*!
70  * @brief Enable specific interrupt.
71  *
72  * Empty function for build compatibility.
73  *
74  * @param interrupt The IRQ number.
75  * @return Always return kStatus_Success.
76  */
EnableIRQ(IRQn_Type interrupt)77 static inline status_t EnableIRQ(IRQn_Type interrupt)
78 {
79     return kStatus_Success;
80 }
81 
82 /*!
83  * @brief Disable specific interrupt.
84  *
85  * Empty function for build compatibility.
86  *
87  * @param interrupt The IRQ number.
88  * @return Always return kStatus_Success.
89  */
DisableIRQ(IRQn_Type interrupt)90 static inline status_t DisableIRQ(IRQn_Type interrupt)
91 {
92     return kStatus_Success;
93 }
94 
95 /*!
96  * @brief Disable the global IRQ
97  *
98  * Disable the global IRQ.
99  *
100  * @return The global IRQ control register value before been disabled.
101  */
DisableGlobalIRQ(void)102 static inline uint32_t DisableGlobalIRQ(void)
103 {
104     return csr_read_clear(CSR_MSTATUS, CSR_MSTATUS_MIE);
105 }
106 
107 /*!
108  * @brief Enable the global IRQ
109  *
110  * Enable the global interrupt by setting the global IRQ control register.
111  * Generally this value is the return value of DisableGlobalIRQ.
112  *
113  * @param intCtrl The global interrupt control register value.
114  */
EnableGlobalIRQ(uint32_t intCtrl)115 static inline void EnableGlobalIRQ(uint32_t intCtrl)
116 {
117     csr_set(CSR_MSTATUS, intCtrl);
118 }
119 
120 #if defined(__cplusplus)
121 }
122 #endif
123 
124 /*! @} */
125 
126 #endif
127