找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 6852|回复: 0
打印 上一主题 下一主题
收起左侧

misc.c中断优先级的设置

[复制链接]
跳转到指定楼层
楼主
ID:72519 发表于 2015-1-23 03:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. /**
  2.   ******************************************************************************
  3.   * @file    misc.c
  4.   * @author  MCD Application Team
  5.   * @version V3.5.0
  6.   * @date    11-March-2011
  7.   * @brief   This file provides all the miscellaneous firmware functions (add-on
  8.   *          to CMSIS functions).
  9. 1.优先级分组设置。
  10. 2.中断号大于0的中断的优先级的设置。
  11. 3.中断向量表的地址的偏移的设置。
  12. 4.低功耗模式的设置。
  13. 5.SysTick的时钟源配置。
  14. ******************************************************************************
  15.   * @attention
  16.   *
  17.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  18.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  19.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  20.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  21.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  22.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  23.   *
  24.   * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
  25.   ******************************************************************************
  26.   */
  27. /* Includes ------------------------------------------------------------------*/
  28. #include "misc.h"
  29. /** @addtogroup STM32F10x_StdPeriph_Driver
  30.   * @{
  31.   */
  32. /** @defgroup MISC
  33.   * @brief MISC driver modules
  34.   * @{
  35.   */
  36. /** @defgroup MISC_Private_TypesDefinitions
  37.   * @{
  38.   */
  39. /**
  40.   * @}
  41.   */
  42. /** @defgroup MISC_Private_Defines
  43.   * @{
  44.   */
  45. #define AIRCR_VECTKEY_MASK    ((uint32_t)0x05FA0000)
  46. /**
  47.   * @}
  48.   */
  49. /** @defgroup MISC_Private_Macros
  50.   * @{
  51.   */
  52. /**
  53.   * @}
  54.   */
  55. /** @defgroup MISC_Private_Variables
  56.   * @{
  57.   */
  58. /**
  59.   * @}
  60.   */
  61. /** @defgroup MISC_Private_FunctionPrototypes
  62.   * @{
  63.   */
  64. /**
  65.   * @}
  66.   */
  67. /** @defgroup MISC_Private_Functions
  68.   * @{
  69.   */
  70. /**
  71.   * @brief  Configures the priority grouping: pre-emption priority and subpriority.
  72.   * @param  NVIC_PriorityGroup: specifies the priority grouping bits length.
  73.   *   This parameter can be one of the following values:
  74.   *     @arg NVIC_PriorityGroup_0: 0 bits for pre-emption priority
  75.   *                                4 bits for subpriority
  76.   *     @arg NVIC_PriorityGroup_1: 1 bits for pre-emption priority
  77.   *                                3 bits for subpriority
  78.   *     @arg NVIC_PriorityGroup_2: 2 bits for pre-emption priority
  79.   *                                2 bits for subpriority
  80.   *     @arg NVIC_PriorityGroup_3: 3 bits for pre-emption priority
  81.   *                                1 bits for subpriority
  82.   *     @arg NVIC_PriorityGroup_4: 4 bits for pre-emption priority
  83.   *                                0 bits for subpriority
  84.   * @retval None
  85.   */
  86. void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup)//优先级组配置,抢占式优先级和响应优先级所占位的个数的配置
  87. {
  88.   /* Check the parameters */
  89.   assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup));

  90.   /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */
  91.   SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup;
  92. }
  93. /**
  94.   * @brief  Initializes the NVIC peripheral according to the specified
  95.   *         parameters in the NVIC_InitStruct.
  96.   * @param  NVIC_InitStruct: pointer to a NVIC_InitTypeDef structure that contains
  97.   *         the configuration information for the specified NVIC peripheral.
  98.   * @retval None
  99.   */
  100. void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct)//根据结构体的指定参数初始化NVIC,但是只能设置中断号大于0的中断向量
  101. {
  102.   uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F;

  103.   /* Check the parameters */
  104.   assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd));
  105.   assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority));
  106.   assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority));
  107.    
  108.   if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE)
  109.   {
  110.     /* Compute the Corresponding IRQ Priority --------------------------------*/   
  111.     tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08;
  112.     tmppre = (0x4 - tmppriority);
  113.     tmpsub = tmpsub >> tmppriority;
  114.     tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre;
  115.     tmppriority |=  NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub;
  116.     tmppriority = tmppriority << 0x04;
  117.       
  118.     NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority;
  119.    
  120.     /* Enable the Selected IRQ Channels --------------------------------------*/
  121.     NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  122.       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  123.   }
  124.   else
  125.   {
  126.     /* Disable the Selected IRQ Channels -------------------------------------*/
  127.     NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] =
  128.       (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
  129.   }
  130. }
  131. /**
  132.   * @brief  Sets the vector table location and Offset.
  133.   * @param  NVIC_VectTab: specifies if the vector table is in RAM or FLASH memory.
  134.   *   This parameter can be one of the following values:
  135.   *     @arg NVIC_VectTab_RAM
  136.   *     @arg NVIC_VectTab_FLASH
  137.   * @param  Offset: Vector Table base offset field. This value must be a multiple
  138.   *         of 0x200.
  139.   * @retval None
  140.   */
  141. void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset)//设置向量表的位置(RAM或FLASH中)和偏移
  142. {
  143.   /* Check the parameters */
  144.   assert_param(IS_NVIC_VECTTAB(NVIC_VectTab));
  145.   assert_param(IS_NVIC_OFFSET(Offset));
  146.   
  147.   SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80);
  148. }
  149. /**
  150.   * @brief  Selects the condition for the system to enter low power mode.
  151.   * @param  LowPowerMode: Specifies the new mode for the system to enter low power mode.
  152.   *   This parameter can be one of the following values:
  153.   *     @arg NVIC_LP_SEVONPEND
  154.   *     @arg NVIC_LP_SLEEPDEEP
  155.   *     @arg NVIC_LP_SLEEPONEXIT
  156.   * @param  NewState: new state of LP condition. This parameter can be: ENABLE or DISABLE.
  157.   * @retval None
  158.   */
  159. void NVIC_SystemLPConfig(uint8_t LowPowerMode, FunctionalState NewState)//系统低功耗模式配置
  160. {
  161.   /* Check the parameters */
  162.   assert_param(IS_NVIC_LP(LowPowerMode));
  163.   assert_param(IS_FUNCTIONAL_STATE(NewState));

  164.   if (NewState != DISABLE)
  165.   {
  166.     SCB->SCR |= LowPowerMode;
  167.   }
  168.   else
  169.   {
  170.     SCB->SCR &= (uint32_t)(~(uint32_t)LowPowerMode);
  171.   }
  172. }
  173. /**
  174.   * @brief  Configures the SysTick clock source.
  175.   * @param  SysTick_CLKSource: specifies the SysTick clock source.
  176.   *   This parameter can be one of the following values:
  177.   *     @arg SysTick_CLKSource_HCLK_Div8: AHB clock divided by 8 selected as SysTick clock source.
  178.   *     @arg SysTick_CLKSource_HCLK: AHB clock selected as SysTick clock source.
  179.   * @retval None
  180.   */
  181. void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)//SysTick的时钟源配置
  182. {
  183.   /* Check the parameters */
  184.   assert_param(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  185.   if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  186.   {
  187.     SysTick->CTRL |= SysTick_CLKSource_HCLK;
  188.   }
  189.   else
  190.   {
  191.     SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  192.   }
  193. }
  194. /**
  195.   * @}
  196.   */
  197. /**
  198.   * @}
  199.   */
  200. /**
  201.   * @}
  202.   */
  203. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表