1 /********************************** (C) COPYRIGHT *******************************
2 * File Name          : ch32f20x_misc.c
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2021/08/08
6 * Description        : This file provides all the miscellaneous firmware functions .
7 *********************************************************************************/
8 #include "ch32f20x_misc.h"
9 
10 #define AIRCR_VECTKEY_MASK    ((uint32_t)0x05FA0000)
11 
12 
13 /******************************************************************************************
14 * Function Name  : NVIC_PriorityGroupConfig
15 * Description    : Configures the priority grouping: pre-emption priority and subpriority.
16 * Input          : NVIC_PriorityGroup: specifies the priority grouping bits length.
17 *                    NVIC_PriorityGroup_0: 0 bits for pre-emption priority
18 *                                          4 bits for subpriority
19 *										 NVIC_PriorityGroup_1: 1 bits for pre-emption priority
20 *                                          3 bits for subpriority
21 *                    NVIC_PriorityGroup_2: 2 bits for pre-emption priority
22 *                                          2 bits for subpriority
23 *                    NVIC_PriorityGroup_3: 3 bits for pre-emption priority
24 *                                          1 bits for subpriority
25 *                    NVIC_PriorityGroup_4: 4 bits for pre-emption priority
26 *                                          0 bits for subpriority
27 * Return         : None
28 *******************************************************************************************/
NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)29 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
30 {
31   SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
32 }
33 
34 /******************************************************************************************
35 * Function Name  : NVIC_Init
36 * Description    : Initializes the NVIC peripheral according to the specified parameters in
37 *                  the NVIC_InitStruct.
38 * Input          : NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains the
39 *                                   configuration information for the specified NVIC peripheral.
40 * Return         : None
41 *******************************************************************************************/
NVIC_Init(NVIC_InitTypeDef * NVIC_InitStruct)42 void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)
43 {
44   uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;
45 
46   if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
47   {
48     /* Compute the Corresponding IRQ Priority */
49     tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
50     tmppre = (0x4 - tmppriority);
51     tmpsub = tmpsub >> tmppriority;
52 
53     tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
54     tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
55     tmppriority = tmppriority << 0x04;
56 
57     NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
58 
59     /* Enable the Selected IRQ Channels */
60     NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
61       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
62   }
63   else
64   {
65     /* Disable the Selected IRQ Channels */
66     NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
67       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
68   }
69 }
70 
71 /******************************************************************************************
72 * Function Name  : NVIC_SetVectorTable
73 * Description    : Sets the vector table location and Offset.
74 * Input          : NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.The
75 *                                value can be NVIC_VectTab_RAM or NVIC_VectTab_FLASH.
76 *                  Offset      : Vector Table base offset field. This value must be a multiple
77 *                                of 0x200.
78 * Return         : None
79 *******************************************************************************************/
NVIC_SetVectorTable(uint32_t NVIC_VectTab,uint32_t Offset)80 void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)
81 {
82   SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
83 }
84 
85 /******************************************************************************************
86 * Function Name  : NVIC_SystemLPConfig
87 * Description    : Selects the condition for the system to enter low power mode.
88 * Input          : LowPowerMode: Specifies the new mode for the system to enter low power mode.
89 *                      NVIC_LP_SEVONPEND
90 *                      NVIC_LP_SLEEPDEEP
91 *                      NVIC_LP_SLEEPONEXIT
92 *                  NewState    : new state of LP condition. This parameter can be: ENABLE or DISABLE.
93 * Return         : None
94 *******************************************************************************************/
NVIC_SystemLPConfig(uint8_t LowPowerMode,FunctionalState NewState)95 void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)
96 {
97   if (NewState != DISABLE)
98   {
99     SCB->SCR |= LowPowerMode;
100   }
101   else
102   {
103     SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
104   }
105 }
106 
107 /******************************************************************************************
108 * Function Name  : SysTick_CLKSourceConfig
109 * Description    : Configures the SysTick clock source.
110 * Input          : SysTick_CLKSource: Specifies the new mode for the system to enter low power mode.
111 *                     ysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
112 *                     SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
113 * Return         : None
114 *******************************************************************************************/
SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)115 void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
116 {
117   if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
118   {
119     SysTick->CTRL |= SysTick_CLKSource_HCLK;
120   }
121   else
122   {
123     SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
124   }
125 }
126 
127 
128