1 /**
2 ******************************************************************************
3 * @file misc.c
4 * @author MCD Application Team
5 * @version V1.8.1
6 * @date 27-January-2022
7 * @brief This file provides all the miscellaneous firmware functions (add-on
8 * to CMSIS functions).
9 *
10 * @verbatim
11 *
12 * ===================================================================
13 * How to configure Interrupts using driver
14 * ===================================================================
15 *
16 * This section provide functions allowing to configure the NVIC interrupts (IRQ).
17 * The Cortex-M4 exceptions are managed by CMSIS functions.
18 *
19 * 1. Configure the NVIC Priority Grouping using NVIC_PriorityGroupConfig()
20 * function according to the following table.
21
22 * The table below gives the allowed values of the pre-emption priority and subpriority according
23 * to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function
24 * ==========================================================================================================================
25 * NVIC_PriorityGroup | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority | Description
26 * ==========================================================================================================================
27 * NVIC_PriorityGroup_0 | 0 | 0-15 | 0 bits for pre-emption priority
28 * | | | 4 bits for subpriority
29 * --------------------------------------------------------------------------------------------------------------------------
30 * NVIC_PriorityGroup_1 | 0-1 | 0-7 | 1 bits for pre-emption priority
31 * | | | 3 bits for subpriority
32 * --------------------------------------------------------------------------------------------------------------------------
33 * NVIC_PriorityGroup_2 | 0-3 | 0-3 | 2 bits for pre-emption priority
34 * | | | 2 bits for subpriority
35 * --------------------------------------------------------------------------------------------------------------------------
36 * NVIC_PriorityGroup_3 | 0-7 | 0-1 | 3 bits for pre-emption priority
37 * | | | 1 bits for subpriority
38 * --------------------------------------------------------------------------------------------------------------------------
39 * NVIC_PriorityGroup_4 | 0-15 | 0 | 4 bits for pre-emption priority
40 * | | | 0 bits for subpriority
41 * ==========================================================================================================================
42 *
43 * 2. Enable and Configure the priority of the selected IRQ Channels using NVIC_Init()
44 *
45 * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
46 * The pending IRQ priority will be managed only by the subpriority.
47 *
48 * @note IRQ priority order (sorted by highest to lowest priority):
49 * - Lowest pre-emption priority
50 * - Lowest subpriority
51 * - Lowest hardware priority (IRQ number)
52 *
53 * @endverbatim
54 *
55 ******************************************************************************
56 * @attention
57 *
58 * Copyright (c) 2016 STMicroelectronics.
59 * All rights reserved.
60 *
61 * This software is licensed under terms that can be found in the LICENSE file
62 * in the root directory of this software component.
63 * If no LICENSE file comes with this software, it is provided AS-IS.
64 *
65 ******************************************************************************
66 */
67
68 /* Includes ------------------------------------------------------------------*/
69 #include "misc.h"
70
71 /** @addtogroup STM32F4xx_StdPeriph_Driver
72 * @{
73 */
74
75 /** @defgroup MISC
76 * @brief MISC driver modules
77 * @{
78 */
79
80 /* Private typedef -----------------------------------------------------------*/
81 /* Private define ------------------------------------------------------------*/
82 #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
83
84 /* Private macro -------------------------------------------------------------*/
85 /* Private variables ---------------------------------------------------------*/
86 /* Private function prototypes -----------------------------------------------*/
87 /* Private functions ---------------------------------------------------------*/
88
89 /** @defgroup MISC_Private_Functions
90 * @{
91 */
92
93 /**
94 * @brief Configures the priority grouping: pre-emption priority and subpriority.
95 * @param NVIC_PriorityGroup: specifies the priority grouping bits length.
96 * This parameter can be one of the following values:
97 * @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
98 * 4 bits for subpriority
99 * @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
100 * 3 bits for subpriority
101 * @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
102 * 2 bits for subpriority
103 * @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
104 * 1 bits for subpriority
105 * @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
106 * 0 bits for subpriority
107 * @note When the NVIC_PriorityGroup_0 is selected, IRQ pre-emption is no more possible.
108 * The pending IRQ priority will be managed only by the subpriority.
109 * @retval None
110 */
NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)111 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
112 {
113 /* Check the parameters */
114 assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
115
116 /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
117 SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
118 }
119
120 /**
121 * @brief Initializes the NVIC peripheral according to the specified
122 * parameters in the NVIC_InitStruct.
123 * @note To configure interrupts priority correctly, the NVIC_PriorityGroupConfig()
124 * function should be called before.
125 * @param NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
126 * the configuration information for the specified NVIC peripheral.
127 * @retval None
128 */
NVIC_Init(NVIC_InitTypeDef * NVIC_InitStruct)129 void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
130 {
131 uint8_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
132
133 /* Check the parameters */
134 assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
135 assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
136 assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
137
138 if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
139 {
140 /* Compute the Corresponding IRQ Priority --------------------------------*/
141 tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
142 tmppre = (0x4 - tmppriority);
143 tmpsub = tmpsub >> tmppriority;
144
145 tmppriority = NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
146 tmppriority |= (uint8_t)(NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub);
147
148 tmppriority = tmppriority << 0x04;
149
150 NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
151
152 /* Enable the Selected IRQ Channels --------------------------------------*/
153 NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
154 (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
155 }
156 else
157 {
158 /* Disable the Selected IRQ Channels -------------------------------------*/
159 NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
160 (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
161 }
162 }
163
164 /**
165 * @brief Sets the vector table location and Offset.
166 * @param NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.
167 * This parameter can be one of the following values:
168 * @arg NVIC_VectTab_RAM: Vector Table in internal SRAM.
169 * @arg NVIC_VectTab_FLASH: Vector Table in internal FLASH.
170 * @param Offset: Vector Table base offset field. This value must be a multiple of 0x200.
171 * @retval None
172 */
NVIC_SetVectorTable(uint32_t NVIC_VectTab,uint32_t Offset)173 void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
174 {
175 /* Check the parameters */
176 assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
177 assert_param(IS_NVIC_OFFSET(Offset));
178
179 SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
180 }
181
182 /**
183 * @brief Selects the condition for the system to enter low power mode.
184 * @param LowPowerMode: Specifies the new mode for the system to enter low power mode.
185 * This parameter can be one of the following values:
186 * @arg NVIC_LP_SEVONPEND: Low Power SEV on Pend.
187 * @arg NVIC_LP_SLEEPDEEP: Low Power DEEPSLEEP request.
188 * @arg NVIC_LP_SLEEPONEXIT: Low Power Sleep on Exit.
189 * @param NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.
190 * @retval None
191 */
NVIC_SystemLPConfig(uint8_t LowPowerMode,FunctionalState NewState)192 void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)
193 {
194 /* Check the parameters */
195 assert_param(IS_NVIC_LP(LowPowerMode));
196 assert_param(IS_FUNCTIONAL_STATE(NewState));
197
198 if (NewState != DISABLE)
199 {
200 SCB->SCR |= LowPowerMode;
201 }
202 else
203 {
204 SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
205 }
206 }
207
208 /**
209 * @brief Configures the SysTick clock source.
210 * @param SysTick_CLKSource: specifies the SysTick clock source.
211 * This parameter can be one of the following values:
212 * @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
213 * @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
214 * @retval None
215 */
SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)216 void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
217 {
218 /* Check the parameters */
219 assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
220 if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
221 {
222 SysTick->CTRL |= SysTick_CLKSource_HCLK;
223 }
224 else
225 {
226 SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
227 }
228 }
229
230 /**
231 * @}
232 */
233
234 /**
235 * @}
236 */
237
238 /**
239 * @}
240 */
241
242