/**************************************************************************//**
 * @file     misc.h
 * @brief    miscellaneous functions header file.
 * @version  VXX.XX.XX
 * @date     XX-XXX-XXXX
 *
 * @note
 * Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd.. All rights reserved.
 *
 * @attention
 * THIS SOFTWARE JUST PROVIDES CUSTOMERS WITH CODING INFORMATION REGARDING 
 * THEIR PRODUCTS, WHICH AIMS AT SAVING TIME FOR THEM. SPINTROL SHALL NOT BE
 * LIABLE FOR THE USE OF THE SOFTWARE. SPINTROL DOES NOT GUARANTEE THE 
 * CORRECTNESS OF THIS SOFTWARE AND RESERVES THE RIGHT TO MODIFY THE SOFTWARE 
 * WITHOUT NOTIFICATION.
 *
 ******************************************************************************/


#ifndef MISC_H
#define MISC_H

#ifdef __cplusplus
 extern "C" {
#endif


#include "spc1068.h"




/**
 *  @brief  NVIC Init Structure definition
 */
typedef struct
{
  uint8_t NVIC_IRQChannel;                    /*!< Specifies the IRQ channel to be enabled or disabled.
                                                   This parameter can be a value of @ref IRQn_Type 
                                                   (For the complete STM32 Devices IRQ Channels list, please
                                                    refer to stm32f10x.h file) */

  uint8_t NVIC_IRQChannelPreemptionPriority;  /*!< Specifies the pre-emption priority for the IRQ channel
                                                   specified in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  uint8_t NVIC_IRQChannelSubPriority;         /*!< Specifies the subpriority level for the IRQ channel specified
                                                   in NVIC_IRQChannel. This parameter can be a value
                                                   between 0 and 15 as described in the table @ref NVIC_Priority_Table */

  FunctionalState NVIC_IRQChannelCmd;         /*!< Specifies whether the IRQ channel defined in NVIC_IRQChannel
                                                   will be enabled or disabled. 
                                                   This parameter can be set either to ENABLE or DISABLE */
} NVIC_InitTypeDef;




/**
 *  @brief  NVIC_Priority_Table
 *          The table below gives the allowed values of the pre-emption priority and subpriority according
 *          to the Priority Grouping configuration performed by NVIC_PriorityGroupConfig function
 */
/*
  ============================================================================================================================
    NVIC_PriorityGroup   | NVIC_IRQChannelPreemptionPriority | NVIC_IRQChannelSubPriority  | Description
  ============================================================================================================================
   NVIC_PriorityGroup_0  |                0                  |            0-15             | 0 bits for pre-emption priority
                         |                                   |                             | 4 bits for subpriority
  ----------------------------------------------------------------------------------------------------------------------------
   NVIC_PriorityGroup_1  |                0-1                |            0-7              | 1 bits for pre-emption priority
                         |                                   |                             | 3 bits for subpriority
  ----------------------------------------------------------------------------------------------------------------------------
   NVIC_PriorityGroup_2  |                0-3                |            0-3              | 2 bits for pre-emption priority
                         |                                   |                             | 2 bits for subpriority
  ----------------------------------------------------------------------------------------------------------------------------
   NVIC_PriorityGroup_3  |                0-7                |            0-1              | 3 bits for pre-emption priority
                         |                                   |                             | 1 bits for subpriority
  ----------------------------------------------------------------------------------------------------------------------------
   NVIC_PriorityGroup_4  |                0-15               |            0                | 4 bits for pre-emption priority
                         |                                   |                             | 0 bits for subpriority
  ============================================================================================================================
*/




/**
 *  @brief  Vector_Table_Base
 */
#define NVIC_VectTab_RAM                ((uint32_t)0x20000000)
#define NVIC_VectTab_FLASH              ((uint32_t)0x1FFF8000)
#define IS_NVIC_VECTTAB(VECTTAB)        (((VECTTAB) == NVIC_VectTab_RAM) || \
                                         ((VECTTAB) == NVIC_VectTab_FLASH))




/**
 *  @brief  Preemption_Priority_Group
 */
#define NVIC_PriorityGroup_0            ((uint32_t)0x700)       /*!< 0 bits for pre-emption priority
                                                                     4 bits for subpriority */
#define NVIC_PriorityGroup_1            ((uint32_t)0x600)       /*!< 1 bits for pre-emption priority
                                                                     3 bits for subpriority */
#define NVIC_PriorityGroup_2            ((uint32_t)0x500)       /*!< 2 bits for pre-emption priority
                                                                     2 bits for subpriority */
#define NVIC_PriorityGroup_3            ((uint32_t)0x400)       /*!< 3 bits for pre-emption priority
                                                                     1 bits for subpriority */
#define NVIC_PriorityGroup_4            ((uint32_t)0x300)       /*!< 4 bits for pre-emption priority
                                                                     0 bits for subpriority */

#define IS_NVIC_PRIORITY_GROUP(GROUP)   (((GROUP) == NVIC_PriorityGroup_0) || \
                                         ((GROUP) == NVIC_PriorityGroup_1) || \
                                         ((GROUP) == NVIC_PriorityGroup_2) || \
                                         ((GROUP) == NVIC_PriorityGroup_3) || \
                                         ((GROUP) == NVIC_PriorityGroup_4))

#define IS_NVIC_PREEMPTION_PRIORITY(PRIORITY)  ((PRIORITY) < 0x10)

#define IS_NVIC_SUB_PRIORITY(PRIORITY)  ((PRIORITY) < 0x10)

#define IS_NVIC_OFFSET(OFFSET)  ((OFFSET) < 0x0007FFF)




/**
 *  @brief  MISC Public Function Declaration
 */
void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup);
void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);
void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset);


#ifdef __cplusplus
}
#endif /* extern "C" */

#endif /* MISC_H */

/******************* Copyright (C) 2022 Spintrol Electronic Technology (Shanghai) Co., Ltd. ***** END OF FILE ****/
