1 /*!
2 * @file apm32f4xx_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-06-23
10 *
11 * @attention
12 *
13 * Copyright (C) 2021-2022 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 usefull 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 "apm32f4xx_misc.h"
28
29 /** @addtogroup APM32F4xx_StdPeriphDriver
30 @{
31 */
32
33 /** @defgroup MISC_Driver
34 * @brief MISC driver modules
35 @{
36 */
37
38 /** @defgroup MISC_Functions
39 @{
40 */
41
42 /** @defgroup MISC_Macros Macros
43 @{
44 */
45
46 #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
47
48 /**@} end of group MISC_Macros*/
49
50
51 /** @addtogroup MISC_Functions
52 @{
53 */
54
55 /*!
56 * @brief Configures the priority grouping: pre-emption priority and subpriority.
57 *
58 * @param priorityGroup : specifies the priority grouping bits length.
59 * This parameter can be one of the following values:
60 * @arg NVIC_PRIORITY_GROUP_0 : 0 bits for pre-emption priority,4 bits for subpriority
61 * @arg NVIC_PRIORITY_GROUP_1 : 1 bits for pre-emption priority,3 bits for subpriority
62 * @arg NVIC_PRIORITY_GROUP_2 : 2 bits for pre-emption priority,2 bits for subpriority
63 * @arg NVIC_PRIORITY_GROUP_3 : 3 bits for pre-emption priority,1 bits for subpriority
64 * @arg NVIC_PRIORITY_GROUP_4 : 4 bits for pre-emption priority,0 bits for subpriority
65 *
66 * @retval None
67 */
NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_T priorityGroup)68 void NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_T priorityGroup)
69 {
70 SCB->AIRCR = AIRCR_VECTKEY_MASK | priorityGroup;
71 }
72
73 /*!
74 * @brief Enable NVIC request
75 *
76 * @param irq: the NVIC interrupt request, detailed in IRQn_Type
77 * For the complete APM32 Devices IRQ Channels list,please refer to apm32f4xx.h file
78 *
79 * @param preemptionPriority: the pre-emption priority needed to set
80 *
81 * @param subPriority: the subpriority needed to set
82 *
83 * @retval None
84 */
NVIC_EnableIRQRequest(IRQn_Type irq,uint8_t preemptionPriority,uint8_t subPriority)85 void NVIC_EnableIRQRequest(IRQn_Type irq, uint8_t preemptionPriority, uint8_t subPriority)
86 {
87 uint32_t tempPriority, tempPrePri, tempSubPri;
88 uint32_t priorityGrp;
89
90 /** Get priority group */
91 priorityGrp = (SCB->AIRCR) & (uint32_t)0x700U;
92
93 /** get pre-emption priority and subpriority */
94 switch (priorityGrp)
95 {
96 case NVIC_PRIORITY_GROUP_0:
97 tempPrePri = 0;
98 tempSubPri = 4;
99 break;
100
101 case NVIC_PRIORITY_GROUP_1:
102 tempPrePri = 1;
103 tempSubPri = 3;
104 break;
105
106 case NVIC_PRIORITY_GROUP_2:
107 tempPrePri = 2;
108 tempSubPri = 2;
109 break;
110
111 case NVIC_PRIORITY_GROUP_3:
112 tempPrePri = 3;
113 tempSubPri = 1;
114 break;
115
116 case NVIC_PRIORITY_GROUP_4:
117 tempPrePri = 4;
118 tempSubPri = 0;
119 break;
120
121 default:
122 NVIC_ConfigPriorityGroup(NVIC_PRIORITY_GROUP_0);
123 tempPrePri = 0;
124 tempSubPri = 4;
125 break;
126 }
127
128 tempPrePri = 4 - tempPrePri;
129 tempSubPri = 4 - tempSubPri;
130 tempPriority = preemptionPriority << tempPrePri;
131 tempPriority |= subPriority & (0x0f >> tempSubPri);
132 tempPriority <<= 4;
133 NVIC->IP[irq] = (uint8_t)tempPriority;
134
135 /** enable the selected IRQ */
136 NVIC->ISER[irq >> 0x05U] = (uint32_t)0x01U << (irq & (uint8_t)0x1FU);
137 }
138
139 /*!
140 * @brief Disable NVIC request
141 *
142 * @param irq: the NVIC interrupt request, detailed in IRQn_Type
143 *
144 * @retval None
145 */
NVIC_DisableIRQRequest(IRQn_Type irq)146 void NVIC_DisableIRQRequest(IRQn_Type irq)
147 {
148 /** disable the selected IRQ.*/
149 NVIC->ICER[irq >> 0x05U] = (uint32_t)0x01U << (irq & (uint8_t)0x1FU);
150 }
151
152 /*!
153 * @brief Configs the vector table location and Offset.
154 *
155 * @param vectTab: specifies if the vector table is in RAM or FLASH memory
156 * This parameter can be one of the following values:
157 * @arg NVIC_VECT_TAB_RAM : NVIC vector table base address launch from RAM
158 * @arg NVIC_VECT_TAB_FLASH : NVIC vector table base address launch from flash
159 *
160 * @param Offset Vector Table base offset field. This value must be a multiple of 0x200
161 *
162 * @retval None
163 */
NVIC_ConfigVectorTable(NVIC_VECT_TAB_T vectTab,uint32_t offset)164 void NVIC_ConfigVectorTable(NVIC_VECT_TAB_T vectTab, uint32_t offset)
165 {
166 SCB->VTOR = vectTab | (offset & (uint32_t)0x1FFFFF80);
167 }
168
169 /*!
170 * @brief set the state of the low power mode
171 *
172 * @param lowPowerMode: the low power mode state
173 * This parameter can be one of the following values:
174 * @arg NVIC_LOWPOWER_SEVONPEND : Wake up according to pending request
175 * @arg NVIC_LOWPOWER_SLEEPDEEP : Enable sleep deep
176 * @arg NVIC_LOWPOWER_SLEEPONEXIT : Sleep after exit ISR
177 *
178 * @retval None
179 */
NVIC_SetSystemLowPower(NVIC_LOWPOWER_T lowPowerMode)180 void NVIC_SetSystemLowPower(NVIC_LOWPOWER_T lowPowerMode)
181 {
182 SCB->SCR |= lowPowerMode;
183 }
184
185 /*!
186 * @brief reset the state of the low power mode
187 *
188 * @param lowPowerMode: the low power mode state
189 * This parameter can be one of the following values:
190 * @arg NVIC_LOWPOWER_SEVONPEND : Wake up according to pending request
191 * @arg NVIC_LOWPOWER_SLEEPDEEP : Enable sleep deep
192 * @arg NVIC_LOWPOWER_SLEEPONEXIT : Sleep after exit ISR
193 *
194 * @retval None
195 */
NVIC_ResetystemLowPower(NVIC_LOWPOWER_T lowPowerMode)196 void NVIC_ResetystemLowPower(NVIC_LOWPOWER_T lowPowerMode)
197 {
198 SCB->SCR &= (uint32_t)(~(uint32_t)lowPowerMode);
199 }
200
201 /*!
202 * @brief Configures the SysTick clock source
203 *
204 * @param clkSource: specifies the SysTick clock source
205 * This parameter can be one of the following values:
206 * @arg SYSTICK_CLK_SOURCE_HCLK_DIV8 : AHB clock divided by 8 selected as SysTick clock source.
207 * @arg SYSTICK_CLK_SOURCE_HCLK : AHB clock selected as SysTick clock source.
208 *
209 * @retval None
210 */
SysTick_ConfigCLKSource(SYSTICK_CLK_SOURCE_T clkSource)211 void SysTick_ConfigCLKSource(SYSTICK_CLK_SOURCE_T clkSource)
212 {
213 if (clkSource == SYSTICK_CLK_SOURCE_HCLK)
214 {
215 SysTick->CTRL |= (uint32_t)BIT2;
216 }
217 else
218 {
219 SysTick->CTRL &= (uint32_t)(~BIT2);
220 }
221 }
222
223 /**@} end of group MISC_Functions */
224 /**@} end of group MISC_Driver */
225 /**@} end of group APM32F4xx_StdPeriphDriver */
226