void timer_irq_handler(void) { // Channel 1 for mbed timeout if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); us_ticker_irq_handler(); } } // Channel 2 for HAL tick if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) { __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle); if ((val - PreviousVal) >= HAL_TICK_DELAY) { // Increment HAL variable HAL_IncTick(); // Prepare next interrupt __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); PreviousVal = val; #if 0 // For DEBUG only HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); #endif } } } }
// Timer 17 update and capture compare interrupt void TIM1_TRG_COM_TIM17_IRQHandler(void) { if(__HAL_TIM_GET_FLAG(&TimHandle[0], TIM_FLAG_CC1) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[0], TIM_IT_CC1) !=RESET) { { __HAL_TIM_CLEAR_IT(&TimHandle[0], TIM_IT_CC1); encoderSpeed[0] = HAL_TIM_ReadCapturedValue(&TimHandle[0], TIM_CHANNEL_1); TIM17->CNT = 0; } } } if(__HAL_TIM_GET_FLAG(&TimHandle[0], TIM_FLAG_UPDATE) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[0], TIM_IT_UPDATE) !=RESET) { __HAL_TIM_CLEAR_IT(&TimHandle[0], TIM_IT_UPDATE); encoderSpeed[0] = PERIOD; // If timer expires we have got no pulse during measurment period, set max time } } }
/// \method read_timed(buf, freq) /// Read analog values into the given buffer at the given frequency. Buffer /// can be bytearray or array.array for example. If a buffer with 8-bit elements /// is used, sample resolution will be reduced to 8 bits. /// /// Example: /// /// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19 /// buf = bytearray(100) # create a buffer of 100 bytes /// adc.read_timed(buf, 10) # read analog values into buf at 10Hz /// # this will take 10 seconds to finish /// for val in buf: # loop over all values /// print(val) # print the value out /// /// This function does not allocate any memory. STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) { pyb_obj_adc_t *self = self_in; mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); int typesize = mp_binary_get_size('@', bufinfo.typecode, NULL); // Init TIM6 at the required frequency (in Hz) timer_tim6_init(mp_obj_get_int(freq_in)); // Start timer HAL_TIM_Base_Start(&TIM6_Handle); // This uses the timer in polling mode to do the sampling // We could use DMA, but then we can't convert the values correctly for the buffer adc_config_channel(self); for (uint index = 0; index < bufinfo.len; index++) { // Wait for the timer to trigger while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) { } __HAL_TIM_CLEAR_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE); uint value = adc_read_channel(&self->handle); if (typesize == 1) { value >>= 4; } mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value); }
/// \method read_timed(buf, timer) /// /// Read analog values into `buf` at a rate set by the `timer` object. /// /// `buf` can be bytearray or array.array for example. The ADC values have /// 12-bit resolution and are stored directly into `buf` if its element size is /// 16 bits or greater. If `buf` has only 8-bit elements (eg a bytearray) then /// the sample resolution will be reduced to 8 bits. /// /// `timer` should be a Timer object, and a sample is read each time the timer /// triggers. The timer must already be initialised and running at the desired /// sampling frequency. /// /// To support previous behaviour of this function, `timer` can also be an /// integer which specifies the frequency (in Hz) to sample at. In this case /// Timer(6) will be automatically configured to run at the given frequency. /// /// Example using a Timer object (preferred way): /// /// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19 /// tim = pyb.Timer(6, freq=10) # create a timer running at 10Hz /// buf = bytearray(100) # creat a buffer to store the samples /// adc.read_timed(buf, tim) # sample 100 values, taking 10s /// /// Example using an integer for the frequency: /// /// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19 /// buf = bytearray(100) # create a buffer of 100 bytes /// adc.read_timed(buf, 10) # read analog values into buf at 10Hz /// # this will take 10 seconds to finish /// for val in buf: # loop over all values /// print(val) # print the value out /// /// This function does not allocate any memory. STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) { pyb_obj_adc_t *self = self_in; mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); size_t typesize = mp_binary_get_size('@', bufinfo.typecode, NULL); TIM_HandleTypeDef *tim; #if defined(TIM6) if (mp_obj_is_integer(freq_in)) { // freq in Hz given so init TIM6 (legacy behaviour) tim = timer_tim6_init(mp_obj_get_int(freq_in)); HAL_TIM_Base_Start(tim); } else #endif { // use the supplied timer object as the sampling time base tim = pyb_timer_get_handle(freq_in); } // configure the ADC channel adc_config_channel(&self->handle, self->channel); // This uses the timer in polling mode to do the sampling // TODO use DMA uint nelems = bufinfo.len / typesize; for (uint index = 0; index < nelems; index++) { // Wait for the timer to trigger so we sample at the correct frequency while (__HAL_TIM_GET_FLAG(tim, TIM_FLAG_UPDATE) == RESET) { } __HAL_TIM_CLEAR_FLAG(tim, TIM_FLAG_UPDATE); if (index == 0) { // for the first sample we need to turn the ADC on HAL_ADC_Start(&self->handle); } else { // for subsequent samples we can just set the "start sample" bit #if defined(STM32F4) || defined(STM32F7) ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART; #elif defined(STM32L4) SET_BIT(ADCx->CR, ADC_CR_ADSTART); #else #error Unsupported processor #endif } // wait for sample to complete #define READ_TIMED_TIMEOUT (10) // in ms adc_wait_for_eoc_or_timeout(READ_TIMED_TIMEOUT); // read value uint value = ADCx->DR; // store value in buffer if (typesize == 1) { value >>= 4; } mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value); }
void us_ticker_clear_interrupt(void) { TimMasterHandle.Instance = TIM_MST; if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); } }
void timer_irq_handler(void) { uint16_t cnt_val = TIM_MST->CNT; TimMasterHandle.Instance = TIM_MST; // Clear Update interrupt flag if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) { if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_UPDATE) == SET) { __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_UPDATE); SlaveCounter++; } } // Channel 1 for mbed timeout if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC1) == SET) { __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC1); if (oc_rem_part > 0) { set_compare(oc_rem_part); // Finish the remaining time left oc_rem_part = 0; } else { if (oc_int_part > 0) { set_compare(0xFFFF); oc_rem_part = cnt_val; // To finish the counter loop the next time oc_int_part--; } else { us_ticker_irq_handler(); } } } } // Channel 2 for HAL tick if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { if (__HAL_TIM_GET_IT_SOURCE(&TimMasterHandle, TIM_IT_CC2) == SET) { __HAL_TIM_CLEAR_IT(&TimMasterHandle, TIM_IT_CC2); uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle); if ((val - PreviousVal) >= HAL_TICK_DELAY) { // Increment HAL variable HAL_IncTick(); // Prepare next interrupt __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); PreviousVal = val; } } } }
// Used to increment the slave counter static void tim_update_irq_handler(void) { TimMasterHandle.Instance = TIM_MST; // Clear Update interrupt flag if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) { __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE); SlaveCounter++; } }
void TIM6_DAC_IRQHandler(void) { if (__HAL_TIM_GET_FLAG(&handleTIM6forTimer, TIM_FLAG_UPDATE) == SET && __HAL_TIM_GET_ITSTATUS(&handleTIM6forTimer, TIM_IT_UPDATE)) { Timer_Update1ms(); __HAL_TIM_CLEAR_FLAG(&handleTIM6forTimer, TIM_FLAG_UPDATE); __HAL_TIM_CLEAR_IT(&handleTIM6forTimer, TIM_IT_UPDATE); } }
//void WWDG_IRQHandler( void ) //void PVD_IRQHandler( void ) //void TAMP_STAMP_IRQHandler( void ) //void RTC_WKUP_IRQHandler( void ) //void FLASH_IRQHandler( void ) //void RCC_IRQHandler( void ) //void EXTI0_IRQHandler( void ) //void EXTI1_IRQHandler( void ) //void EXTI2_IRQHandler( void ) //void EXTI3_IRQHandler( void ) //void EXTI4_IRQHandler( void ) //void DMA1_Stream0_IRQHandler( void ) //void DMA1_Stream1_IRQHandler( void ) //void DMA1_Stream2_IRQHandler( void ) //void DMA1_Stream3_IRQHandler( void ) //void DMA1_Stream4_IRQHandler( void ) //void DMA1_Stream5_IRQHandler( void ) //void DMA1_Stream6_IRQHandler( void ) //void ADC_IRQHandler( void ) //void EXTI9_5_IRQHandler( void ) //void TIM1_BRK_TIM9_IRQHandler( void ) //void TIM1_UP_TIM10_IRQHandler( void ) //void TIM1_TRG_COM_TIM11_IRQHandler( void ) //void TIM1_CC_IRQHandler( void ) //void TIM2_IRQHandler( void ) void TIM3_IRQHandler( void ) { if (__HAL_TIM_GET_FLAG(hTimAhrs.handle, TIM_FLAG_UPDATE) != RESET) { if (__HAL_TIM_GET_IT_SOURCE(hTimAhrs.handle, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_IT(hTimAhrs.handle, TIM_IT_UPDATE); hTimAhrs.EventCallback(); } } }
void TIM6_IRQHandler(void) { if(__HAL_TIM_GET_FLAG(&TIM_ADC_HandleStruct, TIM_FLAG_UPDATE) != RESET){ if(__HAL_TIM_GET_IT_SOURCE(&TIM_ADC_HandleStruct, TIM_IT_UPDATE) !=RESET){ __HAL_TIM_CLEAR_IT(&TIM_ADC_HandleStruct, TIM_IT_UPDATE); ///时基中断 drv_StartMeasureTemperatureRaw(); } } }
void TIM3_IRQHandler(void) { if (__HAL_TIM_GET_FLAG(&TIM_Handle, TIM_FLAG_UPDATE) != RESET) //In case other interrupts are also running { if (__HAL_TIM_GET_ITSTATUS(&TIM_Handle, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_FLAG(&TIM_Handle, TIM_FLAG_UPDATE); LED_update(); } } }
// Timer 1 Update interrupt void TIM1_UP_TIM16_IRQHandler(void) { if(__HAL_TIM_GET_FLAG(&TimHandle[1], TIM_FLAG_UPDATE) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[1], TIM_IT_UPDATE) !=RESET) { __HAL_TIM_CLEAR_IT(&TimHandle[1], TIM_IT_UPDATE); encoderSpeed[1] = PERIOD; // If timer expires we have got no pulse during measurment period, set max time } } }
/// \method read_timed(buf, freq) /// Read analog values into the given buffer at the given frequency. Buffer /// can be bytearray or array.array for example. If a buffer with 8-bit elements /// is used, sample resolution will be reduced to 8 bits. /// /// Example: /// /// adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19 /// buf = bytearray(100) # create a buffer of 100 bytes /// adc.read_timed(buf, 10) # read analog values into buf at 10Hz /// # this will take 10 seconds to finish /// for val in buf: # loop over all values /// print(val) # print the value out /// /// This function does not allocate any memory. STATIC mp_obj_t adc_read_timed(mp_obj_t self_in, mp_obj_t buf_in, mp_obj_t freq_in) { pyb_obj_adc_t *self = self_in; mp_buffer_info_t bufinfo; mp_get_buffer_raise(buf_in, &bufinfo, MP_BUFFER_WRITE); int typesize = mp_binary_get_size('@', bufinfo.typecode, NULL); // Init TIM6 at the required frequency (in Hz) timer_tim6_init(mp_obj_get_int(freq_in)); // Start timer HAL_TIM_Base_Start(&TIM6_Handle); // configure the ADC channel adc_config_channel(self); // This uses the timer in polling mode to do the sampling // TODO use DMA uint nelems = bufinfo.len / typesize; for (uint index = 0; index < nelems; index++) { // Wait for the timer to trigger so we sample at the correct frequency while (__HAL_TIM_GET_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE) == RESET) { } __HAL_TIM_CLEAR_FLAG(&TIM6_Handle, TIM_FLAG_UPDATE); if (index == 0) { // for the first sample we need to turn the ADC on HAL_ADC_Start(&self->handle); } else { // for subsequent samples we can just set the "start sample" bit ADCx->CR2 |= (uint32_t)ADC_CR2_SWSTART; } // wait for sample to complete uint32_t tickstart = HAL_GetTick(); while ((ADCx->SR & ADC_FLAG_EOC) != ADC_FLAG_EOC) { #define READ_TIMED_TIMEOUT (10) // in ms if (((HAL_GetTick() - tickstart ) > READ_TIMED_TIMEOUT)) { break; // timeout } } // read value uint value = ADCx->DR; // store value in buffer if (typesize == 1) { value >>= 4; } mp_binary_set_val_array_from_int(bufinfo.typecode, bufinfo.buf, index, value); }
void TIM4_IRQHandler(void) { if (__HAL_TIM_GET_FLAG(&TIM_Handle2, TIM_FLAG_UPDATE) != RESET) //In case other interrupts are also running { if (__HAL_TIM_GET_ITSTATUS(&TIM_Handle2, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_FLAG(&TIM_Handle2, TIM_FLAG_UPDATE); osSignalSet(temperature_thread_id, TEMP_DATA_READY_SIGNAL); } } }
void TIM4_IRQHandler(void) { if (__HAL_TIM_GET_FLAG(&handleTimer4, TIM_FLAG_UPDATE) != RESET) //In case other interrupts are also running { if (__HAL_TIM_GET_ITSTATUS(&handleTimer4, TIM_IT_UPDATE) != RESET) { __HAL_TIM_CLEAR_FLAG(&handleTimer4, TIM_FLAG_UPDATE); static int tmp = 0; tmp++; } } }
// Used for mbed timeout (channel 1) and HAL tick (channel 2) void timer_oc_irq_handler(void) { uint16_t cval = TIM_MST->CNT; TimMasterHandle.Instance = TIM_MST; // Channel 1 for mbed timeout if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); if (oc_rem_part > 0) { set_compare(oc_rem_part); // Finish the remaining time left oc_rem_part = 0; } else { if (oc_int_part > 0) { set_compare(0xFFFF); oc_rem_part = cval; // To finish the counter loop the next time oc_int_part--; } else { us_ticker_irq_handler(); } } } // Channel 2 for HAL tick if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC2) == SET) { __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC2); uint32_t val = __HAL_TIM_GetCounter(&TimMasterHandle); if ((val - PreviousVal) >= HAL_TICK_DELAY) { // Increment HAL variable HAL_IncTick(); // Prepare next interrupt __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, val + HAL_TICK_DELAY); PreviousVal = val; #if 0 // For DEBUG only HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_6); #endif } } }
// Timer 1 capture compare interrupt void TIM1_CC_IRQHandler(void) { if(__HAL_TIM_GET_FLAG(&TimHandle[1], TIM_FLAG_CC2) != RESET) { if(__HAL_TIM_GET_ITSTATUS(&TimHandle[1], TIM_IT_CC2) !=RESET) { { __HAL_TIM_CLEAR_IT(&TimHandle[1], TIM_IT_CC2); encoderSpeed[1] = HAL_TIM_ReadCapturedValue(&TimHandle[1], TIM_CHANNEL_2); TIM1->CNT = 0; } } } }
static void tim_irq_handler(void) { TIM_HandleTypeDef *htim; for (hacs_timer_t index = 0; index < HACS_NUM_TIMER_PERIPH; index++) { htim = &tim_handles[index]; // Only care about update event. Code copied from stm32f4xx_tim.c if(__HAL_TIM_GET_FLAG(htim, TIM_FLAG_UPDATE) != RESET) { if(__HAL_TIM_GET_ITSTATUS(htim, TIM_IT_UPDATE) !=RESET) { __HAL_TIM_CLEAR_IT(htim, TIM_IT_UPDATE); if (tim_overflow_cb[index] != NULL) tim_overflow_cb[index](); } } } }
void TIM_IRQH(void){ #if MEAS_ENABLED static volatile uint32_t PerformaceTimer = MEAS_PERIOD; #endif //If update interrupt is pending and update interrupt is enabled, clear update int flag, increment tick variable and if needed calculate cpu usage if( ( __HAL_TIM_GET_FLAG(&Tmr, TIM_FLAG_UPDATE) != RESET ) && ( __HAL_TIM_GET_ITSTATUS(&Tmr, TIM_IT_UPDATE) !=RESET ) ){ __HAL_TIM_CLEAR_IT(&Tmr, TIM_IT_UPDATE); Tick++; #if MEAS_ENABLED if( PerformaceTimer-- <= 0 ){ PerformaceTimer = MEAS_PERIOD; Perf.CpuLoadVal = PerformanceCounter; Perf.CPULoad = 1 - (float)PerformanceCounter/(float)NOLOAD_PERF; PerformanceCounter = 0; } #endif } }
// Used by interrupt system static void tim_oc_irq_handler(void) { uint16_t cval = TIM_MST->CNT; TimMasterHandle.Instance = TIM_MST; // Clear CC1 interrupt flag if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) { __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1); } if (oc_rem_part > 0) { set_compare(oc_rem_part); // Finish the remaining time left oc_rem_part = 0; } else { if (oc_int_part > 0) { set_compare(0xFFFF); oc_rem_part = cval; // To finish the counter loop the next time oc_int_part--; } else { us_ticker_irq_handler(); } } }