‎2019-01-08 02:56 AM
Greetings ,
I am running STM32F030R8 at 48 MHz. I want to generate timer interrupt every 10 uSec or say 50 uSec. The PSC is 16bit wide which means i cannot go less then 1 ms delay for timer interrupt. Is there any other method with which i can achieve 50 uSec or 10 uSec timer interrupt ?
‎2019-01-08 03:30 AM
Nonsense, you could easily set the timer to interrupt every micro second, problem is the thing would saturate with work much earlier.
Measure time with a free running counter you inspect periodically. ​
‎2019-01-08 12:20 PM
I used a low power timer to generate delays in usec units with ISR callbacks.
Let me see some code extracts for this:
//=================== LPTIM2 to replace the NOPs() by interrupt events (master mode only)
extern LPTIM_HandleTypeDef hlptim2;
void SPIP_LPTIM2_Init(void) {
LPTIM_HandleTypeDef* hlptim = &hlptim2;
// Set the LPTIM2 clock to PCLK because HSI16 is off
// RCC->CCIPR &= ~(3<<20);
// RCC->CCIPR |= (0<<20); 48 MHz = MSI = APB
HAL_NVIC_SetPriority(LPTIM2_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(LPTIM2_IRQn);
/* Set WAVE bit to enable the set once mode */
hlptim->Instance->CFGR |= LPTIM_CFGR_WAVE;
/* Enable Autoreload match interrupt */
hlptim->Instance->ICR |= 0x0002; // ARRMCF clear flag
__HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
/* Enable Compare match interrupt */
// __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
/* Enable the Peripheral */
__HAL_LPTIM_ENABLE(hlptim);
}
void SPIP_LPTIM2_SetDelay_us(uint32_t delay_us) {
LPTIM_HandleTypeDef* hlptim = &hlptim2;
if(delay_us*48>0xFFFF) TrapError(); // change the clock prescaler if looking at more than one milisecond
if(delay_us==0) return; // done
/* Enable Autoreload match interrupt */
__HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_ARRM);
/* Enable Compare match interrupt */
// __HAL_LPTIM_ENABLE_IT(hlptim, LPTIM_IT_CMPM);
/* Enable the Peripheral */
__HAL_LPTIM_ENABLE(hlptim);
/* Load the period value in the autoreload register */
__HAL_LPTIM_AUTORELOAD_SET(hlptim, delay_us * 48);
// BriskDimTimedLED(1,100);
/* Start timer in single shot mode */
__HAL_LPTIM_START_SINGLE(hlptim);
}
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) {
// Timer is one shot mode, so it should trigger only once after kitstarted
__HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
SPIP_InterruptBasedStateMachine(0);
// BriskDimTimedLED(1,0);
NOPs(1);
}
void LPTIM2_IRQHandler(void) {
HAL_LPTIM_IRQHandler(&hlptim2); // ==> HAL_LPTIM_AutoReloadMatchCallback(&hlptim2);
}
‎2019-01-08 04:39 PM
Actually You can't get larger (not smaller) periods than 1.365 ms and can get smaller ones down to core clock period 20.8 ns. But, as PSC is prescaler, this is only a period for timer tick. The timer current value itself is in CNT register and target/reload (You can get interrupt on that) value in ARR register. So there is no problem in getting any period in microseconds range, but, as Clive said - don't saturate the device with too frequent interrupts.
‎2019-01-09 01:07 AM
Thanks everybody for your valuable feedback.
@Clive - Well I need to measure time delay between input pulse. This wont saturate as after measuring the time delay between two pulse i am supposed to wait for 1 to 2 seconds and then again start the same thing. All i know is that delay between two pulse will be in micro-seconds.
Any guidance on ticking the timer every micro-seconds using cube ??
‎2019-01-09 01:57 AM
Hello everybody....
First of all i would like to apologize for my mistake. Actually i didn't understand the concept of PSC in STMCube. I don't have much experience of timers in STM32 ...
Again, thanks for your comments to make me realize my mistake... =)