2019-07-30 04:27 AM
2019-07-30 06:00 AM
Look into HAL_SYSTICK_Callback... If I remember correctly this function is called once every ms, you can use this to implement a counter.
2019-07-30 06:09 AM
Put a TIM in a free running mode, ie 16-bit, maximal, 1 MHz
Then delta the TIM->CNT to measure if given number of micro-seconds have elapsed.
uint16_t a, b;
a = TIM2->CNT;
do
{
b = TIM2->CNT;
} while((b-a) < 123);
2019-07-31 10:46 AM
Use a low power timer for setting a min delay in microsecond.
extern LPTIM_HandleTypeDef hlptim1;
void LPTIM1_Init(void) {
LPTIM_HandleTypeDef* hlptim = &hlptim1;
// 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(LPTIM1_IRQn, 3/*0*/, 0);
HAL_NVIC_EnableIRQ(LPTIM1_IRQn);
hlptim->Instance->CFGR |= LPTIM_PRESCALER_DIV16; // 3 ticks = 1 us
/* Set WAVE bit to enable the set once mode */
hlptim->Instance->CFGR |= LPTIM_CFGR_PRELOAD;// 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 LPTIM1_SetTick_us(uint32_t delay_us) {
LPTIM_HandleTypeDef* hlptim = &hlptim1;
if((delay_us/3)>0xFFFF) TrapError(); // change the clock prescaler if looking at more than one milisecond
if(delay_us==0) {
// turn the timer event off
__HAL_LPTIM_DISABLE_IT(hlptim, LPTIM_IT_ARRM);
__HAL_LPTIM_DISABLE(hlptim);
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 * 3);
/* Start timer in freerun mode */
__HAL_LPTIM_START_CONTINUOUS(hlptim);
}
/*
void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) {
// Timer is one shot mode, so it should trigger only once after kitstarted
NOPs(1);
}
*/
void LPTIM1_IRQHandler(void) {
// HAL_LPTIM_IRQHandler(&hlptim1); // ==> HAL_LPTIM_AutoReloadMatchCallback(&hlptim2);
/* Autoreload match interrupt */
if(__HAL_LPTIM_GET_FLAG(&hlptim1, LPTIM_FLAG_ARRM) != RESET)
{
if(__HAL_LPTIM_GET_IT_SOURCE(&hlptim1, LPTIM_IT_ARRM) != RESET)
{
/* Clear Autoreload match flag */
__HAL_LPTIM_CLEAR_FLAG(&hlptim1, LPTIM_FLAG_ARRM);
/* Autoreload match Callback */
Add_on_board_LPTIM1_Tick(&STModADD_On);
}
}
}
STM32L4R5 with SYSCLK = 48MHz