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