/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { TIM_IC_InitTypeDef TIMInput_Config; /* Configure the TIM peripheral *********************************************/ /* Set TIMx instance */ Input_Handle.Instance = TIM5; /* TIM5 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM5 TIM_CHANNEL_4. The Rising edge is used as active edge. The TIM5 CCR TIM_CHANNEL_4 is used to compute the frequency value. ------------------------------------------------------------ */ Input_Handle.Init.Prescaler = 0; Input_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Input_Handle.Init.Period = 0xFFFF; Input_Handle.Init.ClockDivision = 0; if(HAL_TIM_IC_Init(&Input_Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Connect internally the TIM5 TIM_CHANNEL_4 Input Capture to the LSI clock output */ __HAL_RCC_AFIO_CLK_ENABLE(); __HAL_AFIO_REMAP_TIM5CH4_ENABLE(); /* Configure the Input Capture of TIM_CHANNEL_4 */ TIMInput_Config.ICPolarity = TIM_ICPOLARITY_RISING; TIMInput_Config.ICSelection = TIM_ICSELECTION_DIRECTTI; TIMInput_Config.ICPrescaler = TIM_ICPSC_DIV8; TIMInput_Config.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&Input_Handle, &TIMInput_Config, TIM_CHANNEL_4) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Start the TIM Input Capture measurement in interrupt mode */ if(HAL_TIM_IC_Start_IT(&Input_Handle, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } /* Wait until the TIM5 get 2 LSI edges */ while(uwCaptureNumber != 2) { } /* Disable TIM5 CC1 Interrupt Request */ HAL_TIM_IC_Stop_IT(&Input_Handle, TIM_CHANNEL_4); /* Deinitialize the TIM5 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&Input_Handle); return uwLsiFreq; }
void DHT22_InterruptHandler(DHT22_HandleTypeDef* handle) { uint16_t val = HAL_TIM_ReadCapturedValue(&handle->timHandle, handle->timChannel); uint32_t freq = HAL_RCC_GetPCLK2Freq(); uint16_t val2; if (val > handle->lastVal) val2 = val - handle->lastVal; else val2 = 65535 + val - handle->lastVal; handle->lastVal = val; float time = 1000000.0 * val2 / freq; if (handle->bitPos < 0) { if (time > 155.0 && time < 165.0) { handle->bitPos = 0; } } else if (handle->bitPos >= 0 && handle->bitPos < 40) { if (time > 78.0 && time < 97.0) { handle->bitsRX[handle->bitPos / 8] &= ~(1 << (7 - handle->bitPos % 8)); handle->bitPos++; } else if (time > 120.0 && time < 145.0) { handle->bitsRX[handle->bitPos / 8] |= 1 << (7 - handle->bitPos % 8); handle->bitPos++; } else { handle->bitPos = -1; HAL_TIM_IC_Stop_IT(&handle->timHandle, handle->timChannel); handle->state = DHT22_READY; } } if(handle->bitPos==40){ handle->bitPos = -1; //TODO This was changed (commented out) recently i'm not sure why HAL_TIM_IC_Stop_IT(&handle->timHandle, handle->timChannel); uint8_t sum = 0; for (int i = 0; i < 4; i++) { sum += handle->bitsRX[i]; } if (sum == handle->bitsRX[4]) { handle->crcErrorFlag = 0; int16_t temp10 = 0; if ((handle->bitsRX[2] & 0x80) == 0x80) { temp10 |= (handle->bitsRX[2] & 0x7F) << 8; temp10 |= handle->bitsRX[3]; temp10 *= -1; } else { temp10 |= handle->bitsRX[2] << 8; temp10 |= handle->bitsRX[3]; } handle->temp = 0.1 * temp10; int16_t hum10 = 0; if ((handle->bitsRX[0] & 0x80) == 0x80) { hum10 |= (handle->bitsRX[0] & 0x7F) << 8; hum10 |= handle->bitsRX[1]; hum10 *= -1; } else { hum10 |= handle->bitsRX[0] << 8; hum10 |= handle->bitsRX[1]; } handle->hum = 0.1 * hum10; } else { handle->crcErrorFlag = 1; } handle->state = DHT22_RECEIVED; } }
/** * @brief Configures TIM14 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { TIM_IC_InitTypeDef TIMInput_Config; /* Configure the TIM peripheral *********************************************/ /* Set TIMx instance */ Input_Handle.Instance = TIM14; /* TIM14 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM14 CH1. The Rising edge is used as active edge. The TIM14 CCR1 is used to compute the frequency value. ------------------------------------------------------------ */ Input_Handle.Init.Prescaler = 0; Input_Handle.Init.CounterMode = TIM_COUNTERMODE_UP; Input_Handle.Init.Period = 0xFFFF; Input_Handle.Init.ClockDivision = 0; Input_Handle.Init.RepetitionCounter = 0; if(HAL_TIM_IC_Init(&Input_Handle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Connect internally the TIM14_CH1 Input Capture to the LSI clock output */ Input_Handle.Instance->OR |= 0x3; // TIM14_OR linked to MCO HAL_RCC_MCOConfig(RCC_MCO, RCC_MCO1SOURCE_LSI, RCC_MCODIV_1); /* Configure the Input Capture of channel 1 */ TIMInput_Config.ICPolarity = TIM_ICPOLARITY_RISING; TIMInput_Config.ICSelection = TIM_ICSELECTION_DIRECTTI; TIMInput_Config.ICPrescaler = TIM_ICPSC_DIV8; TIMInput_Config.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&Input_Handle, &TIMInput_Config, TIM_CHANNEL_1) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Reset the flags */ Input_Handle.Instance->SR = 0; /* Start the TIM Input Capture measurement in interrupt mode */ if(HAL_TIM_IC_Start_IT(&Input_Handle, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } /* Wait until the TIM14 get 2 LSI edges (refer to TIM14_IRQHandler() in stm32f3xx_it.c file) ******************************************************/ while(uwCaptureNumber != 2) { } /* Disable TIM14 CC1 Interrupt Request */ HAL_TIM_IC_Stop_IT(&Input_Handle, TIM_CHANNEL_1); /* Deinitialize the TIM14 peripheral registers to their default reset values */ HAL_TIM_IC_DeInit(&Input_Handle); return uwLsiFreq/*0*/; }