1 /**
2 ******************************************************************************
3 * @file    HAL_misc.c
4 * @author  AE Team
5 * @version V1.1.0
6 * @date    28/08/2019
7 * @brief   This file provides all the miscellaneous firmware functions (add-on
8 *          to CMSIS functions).
9 ******************************************************************************
10 * @attention
11 *
12 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14 * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *
19 * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
20 ******************************************************************************
21 */
22 
23 /* Includes ------------------------------------------------------------------*/
24 #include "HAL_misc.h"
25 
26 /** @addtogroup StdPeriph_Driver
27 * @{
28 */
29 
30 /** @defgroup MISC
31 * @brief MISC driver modules
32 * @{
33 */
34 
35 /** @defgroup MISC_Private_TypesDefinitions
36 * @{
37 */
38 
39 /**
40 * @}
41 */
42 
43 /** @defgroup MISC_Private_Defines
44 * @{
45 */
46 
47 #define AIRCR_VECTKEY_MASK    ((uint32_t)0x05FA0000)
48 /**
49 * @}
50 */
51 
52 /** @defgroup MISC_Private_Macros
53 * @{
54 */
55 
56 /**
57 * @}
58 */
59 
60 /** @defgroup MISC_Private_Variables
61 * @{
62 */
63 
64 /**
65 * @}
66 */
67 
68 /** @defgroup MISC_Private_FunctionPrototypes
69 * @{
70 */
71 
72 /**
73 * @}
74 */
75 
76 /** @defgroup MISC_Private_Functions
77 * @{
78 */
79 
80 /**
81 * @brief  Configures the priority grouping: pre-emption priority and subpriority.
82 * @param  NVIC_PriorityGroup: specifies the priority grouping bits length.
83 *   This parameter can be one of the following values:
84 *     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
85 *                                4 bits for subpriority
86 *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
87 *                                3 bits for subpriority
88 *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
89 *                                2 bits for subpriority
90 *     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
91 *                                1 bits for subpriority
92 *     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
93 *                                0 bits for subpriority
94 * @retval None
95 */
NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)96 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
97 {
98 
99 
100     /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
101     SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
102 }
103 
104 /**
105 * @brief  Initializes the NVIC peripheral according to the specified
106 *         parameters in the NVIC_InitStruct.
107 * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
108 *         the configuration information for the specified NVIC peripheral.
109 * @retval None
110 */
NVIC_Init(NVIC_InitTypeDef * NVIC_InitStruct)111 void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
112 {
113     uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
114 
115 
116 
117     if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
118     {
119         /* Compute the Corresponding IRQ Priority --------------------------------*/
120         tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700)) >> 0x08;
121         tmppre = (0x4 - tmppriority);
122         tmpsub = tmpsub >> tmppriority;
123 
124         tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
125         tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
126         tmppriority = tmppriority << 0x04;
127 
128         NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
129 
130         /* Enable the Selected IRQ Channels --------------------------------------*/
131         NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
132             (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
133     }
134     else
135     {
136         /* Disable the Selected IRQ Channels -------------------------------------*/
137         NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
138             (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
139     }
140 
141     tmppre = NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05];
142 }
143 
144 /**
145 * @brief  Sets the vector table location and Offset.
146 * @param  NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.
147 *   This parameter can be one of the following values:
148 *     @arg NVIC_VectTab_RAM
149 *     @arg NVIC_VectTab_FLASH
150 * @param  Offset: Vector Table base offset field. This value must be a multiple
151 *         of 0x200.
152 * @retval None
153 */
NVIC_SetVectorTable(uint32_t NVIC_VectTab,uint32_t Offset)154 void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
155 {
156     SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
157 }
158 
159 /**
160 * @brief  Selects the condition for the system to enter low power mode.
161 * @param  LowPowerMode: Specifies the new mode for the system to enter low power mode.
162 *   This parameter can be one of the following values:
163 *     @arg NVIC_LP_SEVONPEND
164 *     @arg NVIC_LP_SLEEPDEEP
165 *     @arg NVIC_LP_SLEEPONEXIT
166 * @param  NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.
167 * @retval None
168 */
NVIC_SystemLPConfig(uint8_t LowPowerMode,FunctionalState NewState)169 void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)
170 {
171 
172 
173     if (NewState != DISABLE)
174     {
175         SCB->SCR |= LowPowerMode;
176     }
177     else
178     {
179         SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
180     }
181 }
182 
183 /**
184 * @brief  Configures the SysTick clock source.
185 * @param  SysTick_CLKSource: specifies the SysTick clock source.
186 *   This parameter can be one of the following values:
187 *     @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
188 *     @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
189 * @retval None
190 */
SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)191 void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
192 {
193     /* Check the parameters */
194 
195     if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
196     {
197         SysTick->CTRL |= SysTick_CLKSource_HCLK;
198     }
199     else
200     {
201         SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
202     }
203 }
204 
205 /**
206 * @}
207 */
208 
209 /**
210 * @}
211 */
212 
213 /**
214 * @}
215 */
216 
217 /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/
218