/** * @brief Handle TSC interrupt request. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains * the configuration information for the specified TSC. * @retval None */ void HAL_TSC_IRQHandler(TSC_HandleTypeDef* htsc) { /* Check the parameters */ assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); /* Check if the end of acquisition occurred */ if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET) { /* Clear EOA flag */ __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA); } /* Check if max count error occurred */ if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET) { /* Clear MCE flag */ __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE); /* Change TSC state */ htsc->State = HAL_TSC_STATE_ERROR; /* Conversion completed callback */ HAL_TSC_ErrorCallback(htsc); } else { /* Change TSC state */ htsc->State = HAL_TSC_STATE_READY; /* Conversion completed callback */ HAL_TSC_ConvCpltCallback(htsc); } }
/** * @brief Stop the acquisition previously launched in interrupt mode. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains * the configuration information for the specified TSC. * @retval HAL status */ HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef* htsc) { /* Check the parameters */ assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); /* Process locked */ __HAL_LOCK(htsc); /* Stop the acquisition */ __HAL_TSC_STOP_ACQ(htsc); /* Set touch sensing IOs in low power mode (output push-pull) */ __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); /* Disable interrupts */ __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE)); /* Clear flags */ __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); /* Change TSC state */ htsc->State = HAL_TSC_STATE_READY; /* Process unlocked */ __HAL_UNLOCK(htsc); /* Return function status */ return HAL_OK; }
/** * @brief Starts the acquisition. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains * the configuration information for the specified TSC. * @retval HAL status */ HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef* htsc) { /* Check the parameters */ assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); /* Process locked */ __HAL_LOCK(htsc); /* Change TSC state */ htsc->State = HAL_TSC_STATE_BUSY; /* Clear interrupts */ __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE)); /* Clear flags */ __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); /* Stop discharging the IOs */ __HAL_TSC_SET_IODEF_INFLOAT(htsc); /* Launch the acquisition */ __HAL_TSC_START_ACQ(htsc); /* Process unlocked */ __HAL_UNLOCK(htsc); /* Return function status */ return HAL_OK; }
/** * @brief Start the acquisition in interrupt mode. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains * the configuration information for the specified TSC. * @retval HAL status. */ HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef* htsc) { /* Check the parameters */ assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt)); /* Process locked */ __HAL_LOCK(htsc); /* Change TSC state */ htsc->State = HAL_TSC_STATE_BUSY; /* Enable end of acquisition interrupt */ __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA); /* Enable max count error interrupt (optional) */ if (htsc->Init.MaxCountInterrupt == ENABLE) { __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE); } else { __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE); } /* Clear flags */ __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); /* Set touch sensing IOs not acquired to the specified IODefaultMode */ if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW) { __HAL_TSC_SET_IODEF_OUTPPLOW(htsc); } else { __HAL_TSC_SET_IODEF_INFLOAT(htsc); } /* Launch the acquisition */ __HAL_TSC_START_ACQ(htsc); /* Process unlocked */ __HAL_UNLOCK(htsc); /* Return function status */ return HAL_OK; }
/** * @brief Stops the acquisition previously launched in polling mode * @param htsc: pointer to a TSC_HandleTypeDef structure that contains * the configuration information for the specified TSC. * @retval HAL status */ HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef* htsc) { /* Check the parameters */ assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance)); /* Process locked */ __HAL_LOCK(htsc); /* Stop the acquisition */ __HAL_TSC_STOP_ACQ(htsc); /* Clear flags */ __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE)); /* Change TSC state */ htsc->State = HAL_TSC_STATE_READY; /* Process unlocked */ __HAL_UNLOCK(htsc); /* Return function status */ return HAL_OK; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LEDs */ BSP_LED_Init(LED1); /* Touch activity */ BSP_LED_Init(LED2); /* Touch activity */ BSP_LED_Init(LED3); /* Touch activity */ BSP_LED_Init(LED4); /* Error */ /*##-1- Configure the TSC peripheral #######################################*/ TscHandle.Instance = TSCx; TscHandle.Init.AcquisitionMode = TSC_ACQ_MODE_NORMAL; TscHandle.Init.CTPulseHighLength = TSC_CTPH_1CYCLE; TscHandle.Init.CTPulseLowLength = TSC_CTPL_1CYCLE; TscHandle.Init.IODefaultMode = TSC_IODEF_OUT_PP_LOW; TscHandle.Init.MaxCountInterrupt = DISABLE; TscHandle.Init.MaxCountValue = TSC_MCV_16383; TscHandle.Init.PulseGeneratorPrescaler = TSC_PG_PRESC_DIV64; TscHandle.Init.SpreadSpectrum = DISABLE; TscHandle.Init.SpreadSpectrumDeviation = 127; TscHandle.Init.SpreadSpectrumPrescaler = TSC_SS_PRESC_DIV1; TscHandle.Init.SynchroPinPolarity = TSC_SYNC_POLARITY_FALLING; TscHandle.Init.ChannelIOs = TSC_GROUP8_IO2; /* TS1 touchkey */ TscHandle.Init.SamplingIOs = (TSC_GROUP8_IO1 | TSC_GROUP6_IO1); TscHandle.Init.ShieldIOs = TSC_GROUP6_IO2; if (HAL_TSC_Init(&TscHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Infinite loop */ while (1) { /*##-2- Discharge the touch-sensing IOs ##################################*/ /* Must be done before each acquisition */ HAL_TSC_IODischarge(&TscHandle, ENABLE); HAL_Delay(1); /* 1 ms is more than enough to discharge all capacitors */ /*##-3- Start the acquisition process ####################################*/ if (HAL_TSC_Start(&TscHandle) != HAL_OK) { /* Acquisition Error */ Error_Handler(); } /*##-4- Wait for the end of acquisition ##################################*/ /* Before starting a new acquisition, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current acquisition before starting a new one. */ while (HAL_TSC_GetState(&TscHandle) == HAL_TSC_STATE_BUSY) { /* For simplicity reasons, this example is just waiting till the end of the acquisition, but application may perform other tasks while acquisition operation is ongoing. */ } /*##-5- Clear flags ######################################################*/ __HAL_TSC_CLEAR_FLAG(&TscHandle, (TSC_FLAG_EOA | TSC_FLAG_MCE)); /*##-6- Check if the acquisition is correct (no max count) ###############*/ if (HAL_TSC_GroupGetStatus(&TscHandle, TSC_GROUP8_IDX) == TSC_GROUP_COMPLETED) { /*##-7- Store the acquisition value ####################################*/ uhTSCAcquisitionValue = HAL_TSC_GroupGetValue(&TscHandle, TSC_GROUP8_IDX); /* Show touch activity on LEDs */ /* Threshold values are indicative and may need to be adjusted */ if ((uhTSCAcquisitionValue > TSCx_MIN_THRESHOLD) && (uhTSCAcquisitionValue < TSCx_LOW_MAXTHRESHOLD)) { BSP_LED_On(LED1); if (uhTSCAcquisitionValue < TSCx_MEDIUM_MAXTHRESHOLD) { BSP_LED_On(LED2); if (uhTSCAcquisitionValue < TSCx_HIGH_MAXTHRESHOLD) { BSP_LED_On(LED3); } else { BSP_LED_Off(LED3); } } else { BSP_LED_Off(LED2); BSP_LED_Off(LED3); } } else { BSP_LED_Off(LED1); BSP_LED_Off(LED2); BSP_LED_Off(LED3); } } } }