Author Topic: CH32V003 Timer issue during Sleep  (Read 2889 times)

0 Members and 1 Guest are viewing this topic.

Offline corgonTopic starter

  • Contributor
  • Posts: 46
  • Country: sk
CH32V003 Timer issue during Sleep
« on: May 02, 2024, 03:25:15 pm »
Hi,

I'm struggling with Tim2 not running during Sleep (WFI).
If I have activated the sleep mode, TIM2 in interrupt mode does not run.

Can you please take a look at what's wrong or what I've overlooked?

I really appreciate your advice.

Here is the complete compilable code:

Code: [Select]
#include "debug.h"
//#define SYSCLK_FREQ_8MHz_HSI 8000000 //Internal clock 8MHz

// Global Variables
u_int counter = 0;

// GPIO Settings
void GPIO_INIT(void){
    GPIO_InitTypeDef GPIO_InitStructure = {0};
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
}

// Timer TIM2 Setting and Interrupt setting                                            *
void TIM_INT_Init(u16 arr, u16 psc){
    TIM_TimeBaseInitTypeDef TIMBase_InitStruct;
    NVIC_InitTypeDef NVIC_InitStruct;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    TIMBase_InitStruct.TIM_Period = arr;
    TIMBase_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
    TIMBase_InitStruct.TIM_Prescaler = psc;
    TIMBase_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseInit(TIM2, &TIMBase_InitStruct);
    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 5;
    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 5;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);
    TIM_Cmd(TIM2, ENABLE);
}

// Main
    int main(void){
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
        SystemCoreClockUpdate();
        GPIO_INIT();
        Delay_Init();
        TIM_INT_Init(1000,10000); //(period, prescaller)

// Infinite loop
   while(1){
        Delay_Ms(10);
        PWR_EnterSTANDBYMode(PWR_STANDBYEntry_WFI); // Enter to Sleep Mode
        }
 }

// Timer TIM2 Interrupt Handler
void TIM2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

void TIM2_IRQHandler(void){
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET){
        counter++;
        if (counter >=3) {
         GPIO_WriteBit(GPIOD, GPIO_Pin_5, 1);
         Delay_Ms(500);
         GPIO_WriteBit(GPIOD, GPIO_Pin_5, 0);
         counter =0;
        }
       }
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}

Thanks,

Corgon
 

Offline bikeNomad

  • Contributor
  • Posts: 35
  • Country: us
Re: CH32V003 Timer issue during Sleep
« Reply #1 on: February 09, 2025, 07:24:36 pm »
Have you tried not using the fast interrupt mode?
I'm an autodidact who believes in Sturgeon's Law and wants to continue contributing to the creation and improvement of the other 10% of everything.
 

Online eTobey

  • Super Contributor
  • ***
  • Posts: 1422
  • Country: de
  • Virtual Features for the SDS800XHD -> My website
    • Virtual feature script
Re: CH32V003 Timer issue during Sleep
« Reply #2 on: February 10, 2025, 07:48:35 am »
Is the timer meant to be running in this sleepmode?
"Sometimes, after talking with a person, you want to pet a dog, wave at a monkey, and take off your hat to an elephant."(Maxim Gorki)

SDS800X HD bugs/issues/workarounds (Updated 17. Oct 2025)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf