1 /*!
2 * @file apm32e10x_misc.c
3 *
4 * @brief This file provides all the miscellaneous firmware functions.
5 * Include NVIC,SystemTick and Power management.
6 *
7 * @version V1.0.2
8 *
9 * @date 2022-12-31
10 *
11 * @attention
12 *
13 * Copyright (C) 2021-2023 Geehy Semiconductor
14 *
15 * You may not use this file except in compliance with the
16 * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
17 *
18 * The program is only for reference, which is distributed in the hope
19 * that it will be useful and instructional for customers to develop
20 * their software. Unless required by applicable law or agreed to in
21 * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
22 * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
23 * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
24 * and limitations under the License.
25 */
26
27 #include "apm32e10x_misc.h"
28
29 /** @addtogroup APM32E10x_StdPeriphDriver
30 @{
31 */
32
33 /** @addtogroup MISC_Driver
34 * @brief MISC driver modules
35 @{
36 */
37
38 /** @defgroup MISC_Macros Macros
39 @{
40 */
41
42 #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
43
44 /**@} end of group MISC_Macros */
45
46
47 /** @defgroup MISC_Functions Functions
48 @{
49 */
50
51 /*!
52 * @brief Configures the priority grouping: pre-emption priority and subpriority.
53 *
54 * @param priorityGroup : specifies the priority grouping bits length.
55 * This parameter can be one of the following values:
56 * @arg NVIC_PRIORITY_GROUP_0
57 * @arg NVIC_PRIORITY_GROUP_1
58 * @arg NVIC_PRIORITY_GROUP_2
59 * @arg NVIC_PRIORITY_GROUP_3
60 * @arg NVIC_PRIORITY_GROUP_4
61 *
62 * @retval None
63 */
NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_T priorityGroup)64 void NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_T priorityGroup)
65 {
66 SCB->AIRCR = AIRCR_VECTKEY_MASK | priorityGroup;
67 }
68
69 /*!
70 * @brief Enable NVIC request
71 *
72 * @param irq: the NVIC interrupt request, detailed in IRQn_Type
73 * For the complete APM32 Devices IRQ Channels list,please refer to apm32e10x.h file
74 *
75 * @param preemptionPriority: the pre-emption priority needed to set
76 *
77 * @param subPriority: the subpriority needed to set
78 *
79 * @retval None
80 */
NVIC_EnableIRQRequest(IRQn_Type irq,uint8_t preemptionPriority,uint8_t subPriority)81 void NVIC_EnableIRQRequest(IRQn_Type irq, uint8_t preemptionPriority, uint8_t subPriority)
82 {
83 uint32_t tempPriority, tempPrePri, tempSubPri;
84 uint32_t priorityGrp;
85
86 /** Get priority group */
87 priorityGrp = (SCB->AIRCR) & (uint32_t)0x700U;
88
89 /** get pre-emption priority and subpriority */
90 switch(priorityGrp)
91 {
92 case NVIC_PRIORITY_GROUP_0:
93 tempPrePri = 0;
94 tempSubPri = 4;
95 break;
96
97 case NVIC_PRIORITY_GROUP_1:
98 tempPrePri = 1;
99 tempSubPri = 3;
100 break;
101
102 case NVIC_PRIORITY_GROUP_2:
103 tempPrePri = 2;
104 tempSubPri = 2;
105 break;
106
107 case NVIC_PRIORITY_GROUP_3:
108 tempPrePri = 3;
109 tempSubPri = 1;
110 break;
111
112 case NVIC_PRIORITY_GROUP_4:
113 tempPrePri = 4;
114 tempSubPri = 0;
115 break;
116
117 default:
118 NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_0);
119 tempPrePri = 0;
120 tempSubPri = 4;
121 break;
122 }
123
124 tempPrePri = 4 - tempPrePri;
125 tempSubPri = 4 - tempSubPri;
126 tempPriority = preemptionPriority << tempPrePri;
127 tempPriority |= subPriority & (0x0f >> tempSubPri);
128 tempPriority <<= 4;
129 NVIC->IP[irq] = (uint8_t)tempPriority;
130
131 /* enable the selected IRQ */
132 NVIC->ISER[irq >> 0x05U] = (uint32_t)0x01U << (irq & (uint8_t)0x1FU);
133 }
134
135 /*!
136 * @brief Disable NVIC request
137 *
138 * @param irq: the NVIC interrupt request, detailed in IRQn_Type
139 *
140 * @retval None
141 */
NVIC_DisableIRQRequest(IRQn_Type irq)142 void NVIC_DisableIRQRequest(IRQn_Type irq)
143 {
144 /* disable the selected IRQ.*/
145 NVIC->ICER[irq >> 0x05U] = (uint32_t)0x01U << (irq & (uint8_t)0x1FU);
146 }
147
148 /*!
149 * @brief Configs the vector table location and Offset.
150 *
151 * @param vectTab: specifies if the vector table is in RAM or FLASH memory
152 * This parameter can be one of the following values:
153 * @arg NVIC_VECT_TAB_RAM
154 * @arg NVIC_VECT_TAB_FLASH
155 *
156 * @param Offset Vector Table base offset field. This value must be a multiple of 0x200
157 *
158 * @retval None
159 */
NVIC_ConfigVectorTable(NVIC_VECT_TAB_T vectTab,uint32_t offset)160 void NVIC_ConfigVectorTable(NVIC_VECT_TAB_T vectTab, uint32_t offset)
161 {
162 SCB->VTOR = vectTab | (offset & (uint32_t)0x1FFFFF80);
163 }
164
165 /*!
166 * @brief set the state of the low power mode
167 *
168 * @param lowPowerMode: the low power mode state
169 * This parameter can be one of the following values:
170 * @arg NVIC_LOWPOWER_SEVONPEND
171 * @arg NVIC_LOWPOWER_SLEEPDEEP
172 * @arg NVIC_LOWPOWER_SLEEPONEXIT
173 *
174 * @retval None
175 */
NVIC_SetSystemLowPower(NVIC_LOWPOWER_T lowPowerMode)176 void NVIC_SetSystemLowPower(NVIC_LOWPOWER_T lowPowerMode)
177 {
178 SCB->SCR |= lowPowerMode;
179 }
180
181
182 /*!
183 * @brief reset the state of the low power mode
184 *
185 * @param lowPowerMode: the low power mode state
186 * This parameter can be one of the following values:
187 * @arg NVIC_LOWPOWER_SEVONPEND
188 * @arg NVIC_LOWPOWER_SLEEPDEEP
189 * @arg NVIC_LOWPOWER_SLEEPONEXIT
190 *
191 * @retval None
192 */
NVIC_ResetystemLowPower(NVIC_LOWPOWER_T lowPowerMode)193 void NVIC_ResetystemLowPower(NVIC_LOWPOWER_T lowPowerMode)
194 {
195 SCB->SCR &= (uint32_t)(~(uint32_t)lowPowerMode);
196 }
197
198 /*!
199 * @brief Configures the SysTick clock source
200 *
201 * @param clkSource: specifies the SysTick clock source
202 * This parameter can be one of the following values:
203 * @arg SYSTICK_CLK_SOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
204 * @arg SYSTICK_CLK_SOURCE_HCLK: AHB clock selected as SysTick clock source.
205 *
206 * @retval None
207 */
SysTick_ConfigCLKSource(SYSTICK_CLK_SOURCE_T clkSource)208 void SysTick_ConfigCLKSource(SYSTICK_CLK_SOURCE_T clkSource)
209 {
210 if (clkSource == SYSTICK_CLK_SOURCE_HCLK)
211 {
212 SysTick->CTRL |= (uint32_t)BIT2;
213 }
214 else
215 {
216 SysTick->CTRL &= (uint32_t)(~BIT2);
217 }
218 }
219
220 /**@} end of group MISC_Functions */
221 /**@} end of group MISC_Driver */
222 /**@} end of group APM32E10x_StdPeriphDriver */
223