1 /*!
2  * @file        apm32f10x_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.4
8  *
9  * @date        2022-12-01
10  *
11  * @attention
12  *
13  *  Copyright (C) 2020-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 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 /* Includes */
28 #include "apm32f10x_misc.h"
29 
30 /** @addtogroup APM32F10x_StdPeriphDriver
31   @{
32 */
33 
34 /** @addtogroup MISC_Driver MISC Driver
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 apm32f10x.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  * @brief     reset the state of the low power mode
183  *
184  * @param     lowPowerMode: the low power mode state
185  *                          This parameter can be one of the following values:
186  *                          @arg NVIC_LOWPOWER_SEVONPEND
187  *                          @arg NVIC_LOWPOWER_SLEEPDEEP
188  *                          @arg NVIC_LOWPOWER_SLEEPONEXIT
189  *
190  * @retval    None
191  */
NVIC_ResetystemLowPower(NVIC_LOWPOWER_T lowPowerMode)192 void NVIC_ResetystemLowPower(NVIC_LOWPOWER_T lowPowerMode)
193 {
194     SCB->SCR &= (uint32_t)(~(uint32_t)lowPowerMode);
195 }
196 
197 /*!
198  * @brief     Configures the SysTick clock source
199  *
200  * @param     clkSource: specifies the SysTick clock source
201  *                       This parameter can be one of the following values:
202  *                       @arg SYSTICK_CLK_SOURCE_HCLK_DIV8: AHB clock divided by 8 selected as SysTick clock source.
203  *                       @arg SYSTICK_CLK_SOURCE_HCLK: AHB clock selected as SysTick clock source.
204  *
205  * @retval    None
206  */
SysTick_ConfigCLKSource(SYSTICK_CLK_SOURCE_T clkSource)207 void SysTick_ConfigCLKSource(SYSTICK_CLK_SOURCE_T clkSource)
208 {
209     if (clkSource == SYSTICK_CLK_SOURCE_HCLK)
210     {
211         SysTick->CTRL |= (uint32_t)BIT2;
212     }
213     else
214     {
215         SysTick->CTRL &= (uint32_t)(~BIT2);
216     }
217 }
218 
219 /**@} end of group MISC_Functions*/
220 /**@} end of group MISC_Driver */
221 /**@} end of group APM32F10x_StdPeriphDriver*/
222