2020-10-21 01:08 PM
Please help. What do I miss? I can get timer2 to generate interrupt every 5us, but not 1us. I use sample code from stm8s discovery board. Scope capture (on pin LED2_Y, attached) shows interrupt period of 5us. Captures of Registers are also attached. See my code below:
#define LED1_Gn GPIO_PIN_0
#define LED2_G GPIO_PIN_5
#define LED2_Y GPIO_PIN_6
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER3, ENABLE);
GPIO_DeInit(GPIOA);
GPIO_DeInit(GPIOB);
GPIO_DeInit(GPIOC);
GPIO_DeInit(GPIOD);
GPIO_DeInit(GPIOF);
GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_OUT_PP_HIGH_FAST);
GPIO_Init(GPIOC, GPIO_PIN_2, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOC, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_FAST);
GPIO_Init(GPIOD, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_SLOW); //LED2_G
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_OUT_PP_LOW_FAST); //LED2_Y
TIM2IT_Init();
TIM3IT_Init();
enableInterrupts();
while (1)
{ nop();
}
//==========================================================
void TIM2IT_Init(void)
{
//TIM2 interrupt every 1us ------------------
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_1, 1);
TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
TIM2_Cmd(ENABLE);
}
/////////////////////////////////////////////////////////////////////////
INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
{
GPIO_WriteReverse(GPIOD, LED2_Y);
TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
}
2020-10-21 01:21 PM
If you have to interrupt at 1MHz you're approaching the problem from the wrong direction..
Do whatever you need to do in hardware, and find a way to decimate the interrupt loading.
Can't the TIM toggle the pin by itself?
2020-10-21 01:28 PM
Yes, TIM2 can toggle the pin at every 5us or slower. See attached scope capture. I think it's STM8S105 limitation. However, its clock is 16MHz. With the prescale and period described in data sheet, 1us should be within it capacity.
2020-10-21 02:22 PM
I know what the problem is. From Prog Manual, CPU needs 18 cycles for IRQ overhead. So 1MHz interrupt rate is too fast for CPU.
2020-10-21 02:58 PM
Exactly, and all the cycles for the code you're executing..
The TIM HW should be able to toggle it's pins without any software intervention or interrupts. Use that mode, and actually get a 500 KHz square wave