[STM32F4] 软件中断问题,附stm32f4 的IAR工程

[复制链接]
2500|9
 楼主| zchong 发表于 2016-2-24 20:12 | 显示全部楼层 |阅读模式
本帖最后由 zchong 于 2016-2-29 09:13 编辑

设计意图:由systick定时器产生每秒25600个中断,然后定时器中断中每256次通过软件中断产生一个中断,每5120次产生另外一个中断;
测试结果:
1、IAR,JLINK仿真状态下,5120点的那个软件中断产生的次数明显不对,256点的是正常的;
2、脱离仿真器,直接运行,结果是正确的;
搞不清状况了,大伙可以测试一下,代码如下:



  1. static void  systick_init(void)
  2. {
  3.     RCC_ClocksTypeDef   rcc_clocks;
  4.         uint32_t    cnts;

  5.     SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  6.    
  7.         RCC_GetClocksFreq(&rcc_clocks);

  8.         cnts = (uint32_t)rcc_clocks.HCLK_Frequency / 25600;

  9.     if ((cnts - 1UL) > SysTick_LOAD_RELOAD_Msk) { while(1); }   /* Reload value impossible */

  10.     SysTick->LOAD  = (uint32_t)(cnts - 1UL);                    /* set reload register */
  11.     NVIC_SetPriority (SysTick_IRQn, 0);                         /* set Priority highest */
  12.     SysTick->VAL   = 0;                                         /* Load the SysTick Counter Value */
  13.     SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
  14.                      SysTick_CTRL_TICKINT_Msk   |
  15.                      SysTick_CTRL_ENABLE_Msk;                    /* Enable SysTick IRQ and SysTick Timer */
  16.         
  17. }



  18. static void EXTILine0_Config(void)
  19. {
  20.     EXTI_InitTypeDef   EXTI_InitStructure;
  21.     NVIC_InitTypeDef   NVIC_InitStructure;

  22.     /* Enable SYSCFG clock */
  23.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);

  24.     /* Configure EXTI Line0 */
  25.     EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  26.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  27.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  28.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  29.     EXTI_Init(&EXTI_InitStructure);

  30.     /* Enable and set EXTI Line0 Interrupt to the lowest priority */
  31.     NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  32.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x1;
  33.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  34.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  35.     NVIC_Init(&NVIC_InitStructure);
  36. }

  37. /**
  38.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Configures EXTI Line15 (connected to PG15 pin) in interrupt mode
  39.   * @param  None
  40.   * @retval None
  41.   */
  42. static void EXTILine13_15_Config(void)
  43. {
  44.     EXTI_InitTypeDef   EXTI_InitStructure;
  45.     NVIC_InitTypeDef   NVIC_InitStructure;

  46.     /* Enable SYSCFG clock */
  47.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  48.   
  49.     /* Configure EXTI Line13 */
  50.     EXTI_InitStructure.EXTI_Line = EXTI_Line13;
  51.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  52.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  53.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  54.     EXTI_Init(&EXTI_InitStructure);
  55.   
  56.     /* Enable and set EXTI15_10 Interrupt to the lowest priority */
  57.     NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
  58.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
  59.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  60.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  61.     NVIC_Init(&NVIC_InitStructure);
  62. }

  63. static void exti_init(void)
  64. {
  65.     EXTILine0_Config();
  66.    
  67.     EXTILine13_15_Config();   
  68. }


  69. void period_init(void)
  70. {
  71.     /* Configures the priority grouping: pre-emption priority and subpriority */
  72.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  73.    
  74.     /* Sample periodic interrupt config */
  75.     systick_init();
  76.    
  77.    
  78.     /* Externel interrupt init */
  79.     exti_init();
  80. }

  81. /**
  82.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Main program
  83.   * @param  None
  84.   * @retval None
  85.   */
  86. int main(void)
  87. {   
  88.     /* Timer, cycle sampling init */
  89.     period_init();
  90.    
  91.     /* Infinite loop */
  92.     while (1)
  93.     {        

  94.     }
  95. }


  96. int32_t short_interrupt_trig_cnt = 0;
  97. int32_t short_interrupt_cnt= 0;
  98. int32_t long_interrupt_trig_cnt = 0;
  99. int32_t long_interrupt_cnt= 0;

  100. /**
  101.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Period sampling handler,512 pionts every cycle.
  102.   * @param  None
  103.   * @retval None
  104.   */

  105. void SysTick_Handler(void)
  106. {
  107.     static uint16_t counter = 0;
  108.         
  109.     counter++;
  110.    
  111.     /* 256 points,half cycle */
  112.     if ((counter&0x00FF) == 0x00FF)
  113.     {
  114.         /* Generate software interrupt */
  115.         EXTI_GenerateSWInterrupt(EXTI_Line0);
  116.         short_interrupt_trig_cnt++;
  117.     }  
  118.    
  119.     /* 5120 points,ten cycles */
  120.     if (counter >= 5119)
  121.     {
  122.         counter = 0;
  123.         /* Generate software interrupt */
  124.         EXTI_GenerateSWInterrupt(EXTI_Line13);
  125.         long_interrupt_trig_cnt++;
  126.     }
  127.    
  128. }


  129. /**
  130.   * [url=home.php?mod=space&uid=247401]@brief[/url]  This function handles External line 0 interrupt request.
  131.   * @param  None
  132.   * @retval None
  133.   */
  134. void EXTI0_IRQHandler(void)
  135. {
  136.     if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  137.     {
  138.         /* Clear the EXTI line 0 pending bit */
  139.         EXTI_ClearITPendingBit(EXTI_Line0);
  140.         
  141.         short_interrupt_cnt++;
  142.     }
  143. }

  144. /**
  145.   * [url=home.php?mod=space&uid=247401]@brief[/url]  This function handles External lines 15 to 10 interrupt request.
  146.   * @param  None
  147.   * @retval None
  148.   */
  149. void EXTI15_10_IRQHandler(void)
  150. {  
  151.     if(EXTI_GetITStatus(EXTI_Line13) != RESET)
  152.     {  
  153.         /* Clear the EXTI line 13 pending bit */
  154.         EXTI_ClearITPendingBit(EXTI_Line13);
  155.         //delay(0x1);   
  156.         long_interrupt_cnt++;
  157.     }
  158. }



   附上IAR工程,很方便移植到其它的STM32处理器上,感兴趣的可以测试一下!!  

cpu.zip

943.08 KB, 下载次数: 9

 楼主| zchong 发表于 2016-2-25 12:26 | 显示全部楼层
求助啊
airwill 发表于 2016-2-25 13:07 | 显示全部楼层
仿真, 不应该有这样的问题哪
我怀疑你的观察和检查方法是不是有问题?
 楼主| zchong 发表于 2016-2-25 18:03 | 显示全部楼层
airwill 发表于 2016-2-25 13:07
仿真, 不应该有这样的问题哪
我怀疑你的观察和检查方法是不是有问题?

看上面的代码就可以发现问题了
真正的中断产生次数远大于软件触发中断的次数
我用示波器也测试过io,的确是这样
E-Kaia 发表于 2016-2-25 18:20 | 显示全部楼层
仿真器下面调试中断程序是不是不太靠谱?
Bermanrep 发表于 2016-2-26 09:11 | 显示全部楼层
楼主能否说一下你是如何仿真的,如何确定256点是正确的啊
 楼主| zchong 发表于 2016-2-26 10:18 | 显示全部楼层
Bermanrep 发表于 2016-2-26 09:11
楼主能否说一下你是如何仿真的,如何确定256点是正确的啊

触发次数与实际中断产生次数一致就可认为是正确的
500days 发表于 2016-2-26 22:44 | 显示全部楼层
如果这款芯片没有软中断,就加不了操作系统了
lwsn 发表于 2016-2-27 20:40 | 显示全部楼层
楼主是怎么检查的,具体怎么操作的
 楼主| zchong 发表于 2016-2-28 22:04 | 显示全部楼层
lwsn 发表于 2016-2-27 20:40
楼主是怎么检查的,具体怎么操作的

两种方式:
1、软件中断触发的地方使用一个计数器,软件中断服务函数中使用一个计数器,看这两个计数器是否一致;
2、在方法1的计数器位置翻转IO,使用示波器测量。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

53

主题

1820

帖子

4

粉丝
快速回复 在线客服 返回列表 返回顶部