1 /********************************** (C) COPYRIGHT *******************************
2  * File Name          : ch32v10x_misc.c
3  * Author             : WCH
4  * Version            : V1.0.0
5  * Date               : 2020/04/30
6  * Description        : This file provides all the miscellaneous firmware functions .
7  * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
8  * SPDX-License-Identifier: Apache-2.0
9  *********************************************************************************/
10 #include "ch32v10x_misc.h"
11 
12 __IO uint32_t NVIC_Priority_Group = 0;
13 
14 /*********************************************************************
15  * @fn      NVIC_PriorityGroupConfig
16  *
17  * @brief   Configures the priority grouping - pre-emption priority and subpriority.
18  *
19  * @param   NVIC_PriorityGroup - specifies the priority grouping bits length.
20  *            NVIC_PriorityGroup_0 - 0 bits for pre-emption priority
21  *                                   4 bits for subpriority
22  *            NVIC_PriorityGroup_1 - 1 bits for pre-emption priority
23  *                                   3 bits for subpriority
24  *            NVIC_PriorityGroup_2 - 2 bits for pre-emption priority
25  *                                   2 bits for subpriority
26  *            NVIC_PriorityGroup_3 - 3 bits for pre-emption priority
27  *                                   1 bits for subpriority
28  *            NVIC_PriorityGroup_4 - 4 bits for pre-emption priority
29  *                                   0 bits for subpriority
30  *
31  * @return  none
32  */
NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)33 void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)
34 {
35     NVIC_Priority_Group = NVIC_PriorityGroup;
36 }
37 
38 /*********************************************************************
39  * @fn      NVIC_Init
40  *
41  * @brief   Initializes the NVIC peripheral according to the specified parameters in
42  *        the NVIC_InitStruct.
43  *
44  * @param   NVIC_InitStruct - pointer to a NVIC_InitTypeDef structure that contains the
45  *        configuration information for the specified NVIC peripheral.
46  *
47  * @return  none
48  */
NVIC_Init(NVIC_InitTypeDef * NVIC_InitStruct)49 void NVIC_Init(NVIC_InitTypeDef *NVIC_InitStruct)
50 {
51     uint8_t tmppre = 0;
52 
53     if(NVIC_Priority_Group == NVIC_PriorityGroup_0)
54     {
55         NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4);
56     }
57     else if(NVIC_Priority_Group == NVIC_PriorityGroup_1)
58     {
59         if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority == 1)
60         {
61             NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (1 << 7) | (NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4));
62         }
63         else
64         {
65             NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (0 << 7) | (NVIC_InitStruct->NVIC_IRQChannelSubPriority << 4));
66         }
67     }
68     else if(NVIC_Priority_Group == NVIC_PriorityGroup_2)
69     {
70         if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority <= 1)
71         {
72             tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (4 * NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority);
73             NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (0 << 7) | (tmppre << 4));
74         }
75         else
76         {
77             tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (4 * (NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority - 2));
78             NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (1 << 7) | (tmppre << 4));
79         }
80     }
81     else if(NVIC_Priority_Group == NVIC_PriorityGroup_3)
82     {
83         if(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority <= 3)
84         {
85             tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (2 * NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority);
86             NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (0 << 7) | (tmppre << 4));
87         }
88         else
89         {
90             tmppre = NVIC_InitStruct->NVIC_IRQChannelSubPriority + (2 * (NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority - 4));
91             NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, (1 << 7) | (tmppre << 4));
92         }
93     }
94     else if(NVIC_Priority_Group == NVIC_PriorityGroup_4)
95     {
96         NVIC_SetPriority(NVIC_InitStruct->NVIC_IRQChannel, NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << 4);
97     }
98 
99     if(NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
100     {
101         NVIC_EnableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
102     }
103     else
104     {
105         NVIC_DisableIRQ(NVIC_InitStruct->NVIC_IRQChannel);
106     }
107 }
108