Advertisement
petrdynin

CH32V003_nesting_priority_interrupt

Jan 22nd, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.09 KB | Source Code | 0 0
  1. #include "ch32v00x.h"
  2.  
  3. volatile uint32_t cnt = 0;
  4. uint32_t result;
  5.  
  6. //#define  NESTING
  7.  
  8.  
  9. #ifdef NESTING
  10.  
  11. void __attribute__((interrupt("WCH-Interrupt-fast"))) SW_Handler(void){  // #14
  12.  
  13.     cnt++;
  14.  
  15.     EXTI->INTENR |= EXTI_INTENR_MR0;
  16.     EXTI->SWIEVR |= EXTI_SWIEVR_SWIEVR0;
  17.     while(1){
  18.          if(!(EXTI->SWIEVR & EXTI_SWIEVR_SWIEVR0))
  19.              EXTI->SWIEVR |= EXTI_SWIEVR_SWIEVR0;
  20.       }
  21. }
  22.  
  23. void __attribute__((interrupt("WCH-Interrupt-fast"))) EXTI7_0_IRQHandler(void){ // #20
  24.  
  25.    EXTI->INTFR = EXTI_INTF_INTF0;
  26.  
  27. }
  28.  
  29. void main(void){
  30.  
  31.   __asm volatile("csrr %0,0x804" : "=r"(result)); // что в настройках стартапа?
  32.  
  33.   NVIC_SetPriority(Software_IRQn, 0b10 << 6); //#14  IPRIOR14 [7:6] <- 0x10
  34.   NVIC_SetPriority(EXTI7_0_IRQn,  0b00 << 6); //#20  IPRIOR20 [7:6] <- 0x01
  35.  
  36.   NVIC_EnableIRQ(Software_IRQn);
  37.   NVIC_EnableIRQ(EXTI7_0_IRQn);
  38.  
  39.   SysTick->CTLR |= (uint32_t)(1 << 0);    // вкл SysTick
  40.   SysTick->CTLR |= (uint32_t)(1 << 31);   // вкл SWI
  41.  
  42.   while(1){
  43.  
  44.   }
  45. }
  46.  
  47. #else
  48.  
  49. void __attribute__((interrupt("WCH-Interrupt-fast"))) SW_Handler(void){  // #14
  50.  
  51.     cnt++;
  52.  
  53. }
  54.  
  55. void __attribute__((interrupt("WCH-Interrupt-fast"))) EXTI7_0_IRQHandler(void){ // #20
  56.  
  57.    EXTI->INTFR = EXTI_INTF_INTF0;
  58.  
  59. }
  60.  
  61. void main(void){
  62.  
  63.   __asm volatile("csrr %0,0x804" : "=r"(result));                 // чтение
  64.   __asm volatile("fence");
  65.   __asm volatile("csrrw %0, 0x804, %1" : "=r"(result) :"r"(0x1)); //чтение - запись
  66.   __asm volatile("csrr %0,0x804" : "=r"(result));                 // что в настройках стартапа?
  67.  
  68.   __disable_irq();
  69.  
  70.  
  71.   EXTI->INTENR |= EXTI_INTENR_MR0;
  72.   EXTI->SWIEVR |= EXTI_SWIEVR_SWIEVR0;
  73.  
  74.   SysTick->CTLR |= (uint32_t)(1 << 0);    // вкл SysTick
  75.   SysTick->CTLR |= (uint32_t)(1 << 31);   // вкл SWI
  76.  
  77.   NVIC_SetPriority(Software_IRQn, 0b10 << 6); //#14  IPRIOR14 [7:6] <- 0x10
  78.   NVIC_SetPriority(EXTI7_0_IRQn,  0b01 << 6); //#20  IPRIOR20 [7:6] <- 0x01
  79.  
  80.   NVIC_EnableIRQ(Software_IRQn);
  81.   NVIC_EnableIRQ(EXTI7_0_IRQn);
  82.  
  83.   __enable_irq();
  84.  
  85.   while(1){
  86.  
  87.   }
  88. }
  89.  
  90. #endif
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement