1 /*****************************************************************************
2  * Copyright (c) 2022, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file misc.c
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #include "misc.h"
36 
37 /** @addtogroup n32l43x_StdPeriph_Driver
38  * @{
39  */
40 
41 /** @addtogroup MISC
42  * @brief MISC driver modules
43  * @{
44  */
45 
46 /** @addtogroup MISC_Private_TypesDefinitions
47  * @{
48  */
49 
50 /**
51  * @}
52  */
53 
54 /** @addtogroup MISC_Private_Defines
55  * @{
56  */
57 
58 #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000)
59 /**
60  * @}
61  */
62 
63 /** @addtogroup MISC_Private_Macros
64  * @{
65  */
66 
67 /**
68  * @}
69  */
70 
71 /** @addtogroup MISC_Private_Variables
72  * @{
73  */
74 
75 /**
76  * @}
77  */
78 
79 /** @addtogroup MISC_Private_FunctionPrototypes
80  * @{
81  */
82 
83 /**
84  * @}
85  */
86 
87 /** @addtogroup MISC_Private_Functions
88  * @{
89  */
90 
91 /**
92  * @brief  Configures the priority grouping: pre-emption priority and subpriority.
93  * @param NVIC_PriorityGroup specifies the priority grouping bits length.
94  *   This parameter can be one of the following values:
95  *     @arg NVIC_PriorityGroup_0 0 bits for pre-emption priority
96  *                                4 bits for subpriority
97  *     @arg NVIC_PriorityGroup_1 1 bits for pre-emption priority
98  *                                3 bits for subpriority
99  *     @arg NVIC_PriorityGroup_2 2 bits for pre-emption priority
100  *                                2 bits for subpriority
101  *     @arg NVIC_PriorityGroup_3 3 bits for pre-emption priority
102  *                                1 bits for subpriority
103  *     @arg NVIC_PriorityGroup_4 4 bits for pre-emption priority
104  *                                0 bits for subpriority
105  */
NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)106 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
107 {
108     /* Check the parameters */
109     assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));
110 
111     /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
112     SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
113 }
114 
115 /**
116  * @brief  Initializes the NVIC peripheral according to the specified
117  *         parameters in the NVIC_InitStruct.
118  * @param NVIC_InitStruct pointer to a NVIC_InitType structure that contains
119  *         the configuration information for the specified NVIC peripheral.
120  */
NVIC_Init(NVIC_InitType * NVIC_InitStruct)121 void NVIC_Init(NVIC_InitType* NVIC_InitStruct)
122 {
123     uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
124 
125     /* Check the parameters */
126     assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
127     assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
128     assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
129 
130     if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
131     {
132         /* Compute the Corresponding IRQ Priority --------------------------------*/
133         tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700)) >> 0x08;
134         tmppre      = (0x4 - tmppriority);
135         tmpsub      = tmpsub >> tmppriority;
136 
137         tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
138         tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
139         tmppriority = tmppriority << 0x04;
140 
141         NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
142 
143         /* Enable the Selected IRQ Channels --------------------------------------*/
144         NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01
145                                                                << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
146     }
147     else
148     {
149         /* Disable the Selected IRQ Channels -------------------------------------*/
150         NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01
151                                                                << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
152     }
153 }
154 
155 /**
156  * @brief  Sets the vector table location and Offset.
157  * @param NVIC_VectTab specifies if the vector table is in RAM or FLASH memory.
158  *   This parameter can be one of the following values:
159  *     @arg NVIC_VectTab_RAM
160  *     @arg NVIC_VectTab_FLASH
161  * @param Offset Vector Table base offset field. This value must be a multiple
162  *         of 0x200.
163  */
NVIC_SetVectorTable(uint32_t NVIC_VectTab,uint32_t Offset)164 void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
165 {
166     /* Check the parameters */
167     assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
168     assert_param(IS_NVIC_OFFSET(Offset));
169 
170     SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
171 }
172 
173 /**
174  * @brief  Selects the condition for the system to enter low power mode.
175  * @param LowPowerMode Specifies the new mode for the system to enter low power mode.
176  *   This parameter can be one of the following values:
177  *     @arg NVIC_LP_SEVONPEND
178  *     @arg NVIC_LP_SLEEPDEEP
179  *     @arg NVIC_LP_SLEEPONEXIT
180  * @param Cmd new state of LP condition. This parameter can be: ENABLE or DISABLE.
181  */
NVIC_SystemLPConfig(uint8_t LowPowerMode,FunctionalState Cmd)182 void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState Cmd)
183 {
184     /* Check the parameters */
185     assert_param(IS_NVIC_LP(LowPowerMode));
186     assert_param(IS_FUNCTIONAL_STATE(Cmd));
187 
188     if (Cmd != DISABLE)
189     {
190         SCB->SCR |= LowPowerMode;
191     }
192     else
193     {
194         SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
195     }
196 }
197 
198 /**
199  * @brief  Configures the SysTick clock source.
200  * @param SysTick_CLKSource specifies the SysTick clock source.
201  *   This parameter can be one of the following values:
202  *     @arg SysTick_CLKSource_HCLK_Div8 AHB clock divided by 8 selected as SysTick clock source.
203  *     @arg SysTick_CLKSource_HCLK AHB clock selected as SysTick clock source.
204  */
SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)205 void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
206 {
207     /* Check the parameters */
208     assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
209     if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
210     {
211         SysTick->CTRL |= SysTick_CLKSource_HCLK;
212     }
213     else
214     {
215         SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
216     }
217 }
218 
219 /**
220  * @}
221  */
222 
223 /**
224  * @}
225  */
226 
227 /**
228  * @}
229  */
230