STM8S/A Standard Peripherals Drivers
|
stm8s_tim6.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8s_tim6.c 00004 * @author MCD Application Team 00005 * @version V2.3.0 00006 * @date 16-June-2017 00007 * @brief This file contains all the functions for the TIM6 peripheral. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 00029 /* Includes ------------------------------------------------------------------*/ 00030 #include "stm8s_tim6.h" 00031 00032 /** @addtogroup STM8S_StdPeriph_Driver 00033 * @{ 00034 */ 00035 /* Private typedef -----------------------------------------------------------*/ 00036 /* Private define ------------------------------------------------------------*/ 00037 /* Private typedef -----------------------------------------------------------*/ 00038 /* Private define ------------------------------------------------------------*/ 00039 /* Private macro -------------------------------------------------------------*/ 00040 /* Private variables ---------------------------------------------------------*/ 00041 /* Private function prototypes -----------------------------------------------*/ 00042 /** 00043 * @addtogroup TIM6_Public_Functions 00044 * @{ 00045 */ 00046 00047 /** 00048 * @brief Deinitializes the TIM6 peripheral registers to their default reset values. 00049 * @param None 00050 * @retval None 00051 */ 00052 void TIM6_DeInit(void) 00053 { 00054 TIM6->CR1 = TIM6_CR1_RESET_VALUE; 00055 TIM6->CR2 = TIM6_CR2_RESET_VALUE; 00056 TIM6->SMCR = TIM6_SMCR_RESET_VALUE; 00057 TIM6->IER = TIM6_IER_RESET_VALUE; 00058 TIM6->CNTR = TIM6_CNTR_RESET_VALUE; 00059 TIM6->PSCR = TIM6_PSCR_RESET_VALUE; 00060 TIM6->ARR = TIM6_ARR_RESET_VALUE; 00061 TIM6->SR1 = TIM6_SR1_RESET_VALUE; 00062 } 00063 00064 /** 00065 * @brief Initializes the TIM6 Time Base Unit according to the specified 00066 * parameters. 00067 * @param TIM6_Prescaler : This parameter can be any of the @ref TIM6_Prescaler_TypeDef enumeration. 00068 * @param TIM6_Period : This parameter must be a value between 0x00 and 0xFF. 00069 * @retval None 00070 */ 00071 void TIM6_TimeBaseInit(TIM6_Prescaler_TypeDef TIM6_Prescaler, 00072 uint8_t TIM6_Period) 00073 { 00074 /* Check TIM6 prescaler value */ 00075 assert_param(IS_TIM6_PRESCALER_OK(TIM6_Prescaler)); 00076 /* Set the Autoreload value */ 00077 TIM6->ARR = (uint8_t)(TIM6_Period); 00078 /* Set the Prescaler value */ 00079 TIM6->PSCR = (uint8_t)(TIM6_Prescaler); 00080 } 00081 00082 /** 00083 * @brief Enables or disables the TIM6 peripheral. 00084 * @param NewState : The new state of the TIM6 peripheral. 00085 * This parameter can be any of the @ref FunctionalState enumeration. 00086 * @retval None 00087 */ 00088 void TIM6_Cmd(FunctionalState NewState) 00089 { 00090 /* Check the parameters */ 00091 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00092 00093 /* set or Reset the CEN Bit */ 00094 if (NewState == ENABLE) 00095 { 00096 TIM6->CR1 |= TIM6_CR1_CEN ; 00097 } 00098 else 00099 { 00100 TIM6->CR1 &= (uint8_t)(~TIM6_CR1_CEN) ; 00101 } 00102 } 00103 00104 /** 00105 * @brief Enables or Disables the TIM6 Update event. 00106 * @param NewState : The new state of the TIM6 peripheral Preload register. 00107 * This parameter can be any of the @ref FunctionalState enumeration. 00108 * @retval None 00109 */ 00110 void TIM6_UpdateDisableConfig(FunctionalState NewState) 00111 { 00112 /* Check the parameters */ 00113 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00114 00115 /* Set or Reset the UDIS Bit */ 00116 if (NewState == ENABLE) 00117 { 00118 TIM6->CR1 |= TIM6_CR1_UDIS ; 00119 } 00120 else 00121 { 00122 TIM6->CR1 &= (uint8_t)(~TIM6_CR1_UDIS) ; 00123 } 00124 } 00125 00126 /** 00127 * @brief Selects the TIM6 Update Request Interrupt source. 00128 * @param TIM6_UpdateSource : Specifies the Update source. 00129 * This parameter can be one of the @ref TIM6_UpdateSource_TypeDef enumeration. 00130 * @retval None 00131 */ 00132 void TIM6_UpdateRequestConfig(TIM6_UpdateSource_TypeDef TIM6_UpdateSource) 00133 { 00134 /* Check the parameters */ 00135 assert_param(IS_TIM6_UPDATE_SOURCE_OK(TIM6_UpdateSource)); 00136 00137 /* Set or Reset the URS Bit */ 00138 if (TIM6_UpdateSource == TIM6_UPDATESOURCE_REGULAR) 00139 { 00140 TIM6->CR1 |= TIM6_CR1_URS ; 00141 } 00142 else 00143 { 00144 TIM6->CR1 &= (uint8_t)(~TIM6_CR1_URS) ; 00145 } 00146 } 00147 00148 /** 00149 * @brief Selects the TIM6�s One Pulse Mode. 00150 * @param TIM6_OPMode : Specifies the OPM Mode to be used. 00151 * This parameter can be one of the @ref TIM6_OPMode_TypeDef enumeration. 00152 * @retval None 00153 */ 00154 void TIM6_SelectOnePulseMode(TIM6_OPMode_TypeDef TIM6_OPMode) 00155 { 00156 /* Check the parameters */ 00157 assert_param(IS_TIM6_OPM_MODE_OK(TIM6_OPMode)); 00158 00159 /* Set or Reset the OPM Bit */ 00160 if (TIM6_OPMode == TIM6_OPMODE_SINGLE) 00161 { 00162 TIM6->CR1 |= TIM6_CR1_OPM ; 00163 } 00164 else 00165 { 00166 TIM6->CR1 &= (uint8_t)(~TIM6_CR1_OPM) ; 00167 } 00168 } 00169 00170 /** 00171 * @brief Configures the TIM6 Prescaler. 00172 * @param Prescaler : Specifies the Prescaler Register value 00173 * This parameter can be one of the @ref TIM6_Prescaler_TypeDef enumeration. 00174 * @param TIM6_PSCReloadMode : Specifies the TIM6 Prescaler Reload mode. 00175 * This parameter can be one of the @ref TIM6_PSCReloadMode_TypeDef enumeration. 00176 * @retval None 00177 */ 00178 void TIM6_PrescalerConfig(TIM6_Prescaler_TypeDef Prescaler, 00179 TIM6_PSCReloadMode_TypeDef TIM6_PSCReloadMode) 00180 { 00181 /* Check the parameters */ 00182 assert_param(IS_TIM6_PRESCALER_RELOAD_OK(TIM6_PSCReloadMode)); 00183 assert_param(IS_TIM6_PRESCALER_OK(Prescaler)); 00184 00185 /* Set the Prescaler value */ 00186 TIM6->PSCR = (uint8_t)Prescaler; 00187 00188 /* Set or reset the UG Bit */ 00189 if (TIM6_PSCReloadMode == TIM6_PSCRELOADMODE_IMMEDIATE) 00190 { 00191 TIM6->EGR |= TIM6_EGR_UG ; 00192 } 00193 else 00194 { 00195 TIM6->EGR &= (uint8_t)(~TIM6_EGR_UG) ; 00196 } 00197 } 00198 00199 /** 00200 * @brief Enables or disables TIM6 peripheral Preload register on ARR. 00201 * @param NewState : The new state of the TIM6 peripheral Preload register. 00202 * This parameter can be any of the @ref FunctionalState enumeration. 00203 * @retval None 00204 */ 00205 void TIM6_ARRPreloadConfig(FunctionalState NewState) 00206 { 00207 /* Check the parameters */ 00208 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00209 00210 /* Set or Reset the ARPE Bit */ 00211 if (NewState == ENABLE) 00212 { 00213 TIM6->CR1 |= TIM6_CR1_ARPE ; 00214 } 00215 else 00216 { 00217 TIM6->CR1 &= (uint8_t)(~TIM6_CR1_ARPE) ; 00218 } 00219 } 00220 00221 /** 00222 * @brief Sets the TIM6 Counter Register value. 00223 * @param Counter : Specifies the Counter register new value. 00224 * This parameter is between 0x00 and 0xFF. 00225 * @retval None 00226 */ 00227 void TIM6_SetCounter(uint8_t Counter) 00228 { 00229 /* Set the Counter Register value */ 00230 TIM6->CNTR = (uint8_t)(Counter); 00231 } 00232 00233 /** 00234 * @brief Sets the TIM6 Autoreload Register value. 00235 * @param Autoreload : Specifies the Autoreload register new value. 00236 * This parameter is between 0x00 and 0xFF. 00237 * @retval None 00238 */ 00239 void TIM6_SetAutoreload(uint8_t Autoreload) 00240 { 00241 /* Set the Autoreload Register value */ 00242 TIM6->ARR = (uint8_t)(Autoreload); 00243 } 00244 00245 /** 00246 * @brief Gets the TIM6 Counter value. 00247 * @param None 00248 * @retval uint8_t: Counter Register value. 00249 */ 00250 uint8_t TIM6_GetCounter(void) 00251 { 00252 uint8_t tmpcntr=0; 00253 tmpcntr = TIM6->CNTR; 00254 /* Get the Counter Register value */ 00255 return ((uint8_t)tmpcntr); 00256 } 00257 00258 /** 00259 * @brief Gets the TIM6 Prescaler value. 00260 * @param None 00261 * @retval TIM6_Prescaler_TypeDef : Prescaler Register configuration value. 00262 */ 00263 TIM6_Prescaler_TypeDef TIM6_GetPrescaler(void) 00264 { 00265 /* Get the Prescaler Register value */ 00266 return ((TIM6_Prescaler_TypeDef)TIM6->PSCR); 00267 } 00268 00269 /** 00270 * @brief Enables or disables the specified TIM6 interrupts. 00271 * @param TIM6_IT : Specifies the TIM6 interrupts sources to be enabled or disabled. 00272 * This parameter can be any combination of the @ref TIM6_IT_TypeDef enumeration. 00273 * @param NewState : The new state of the TIM6 peripheral. 00274 * This parameter can be any of the @ref FunctionalState enumeration. 00275 * @retval None 00276 * @par Required preconditions: 00277 * If QST option bit is enabled, the TIM6 Interrupt vector will be mapped on IRQ number 2 (irq0). 00278 * Otherwise, it will be mapped on IRQ number 27 (irq25). 00279 */ 00280 void TIM6_ITConfig(TIM6_IT_TypeDef TIM6_IT, FunctionalState NewState) 00281 { 00282 /* Check the parameters */ 00283 assert_param(IS_TIM6_IT_OK(TIM6_IT)); 00284 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00285 00286 if (NewState == ENABLE) 00287 { 00288 /* Enable the Interrupt sources */ 00289 TIM6->IER |= (uint8_t)TIM6_IT; 00290 } 00291 else 00292 { 00293 /* Disable the Interrupt sources */ 00294 TIM6->IER &= (uint8_t)(~(uint8_t)TIM6_IT); 00295 } 00296 } 00297 00298 /** 00299 * @brief Clears the TIM�s pending flags. 00300 * @param TIM6_FLAG : Specifies the flag to clear. 00301 * This parameter can be one of the @ref TIM6_FLAG_TypeDef enumeration. 00302 * @retval None 00303 */ 00304 void TIM6_ClearFlag(TIM6_FLAG_TypeDef TIM6_FLAG) 00305 { 00306 /* Check the parameters */ 00307 assert_param(IS_TIM6_CLEAR_FLAG_OK((uint8_t)TIM6_FLAG)); 00308 /* Clear the flags (rc_w0) clear this bit by writing 0. Writing �1� has no effect*/ 00309 TIM6->SR1 &= (uint8_t)(~((uint8_t)TIM6_FLAG)); 00310 } 00311 00312 /** 00313 * @brief Checks whether the TIM6 interrupt has occurred or not. 00314 * @param TIM6_IT : Specifies the TIM6 interrupt source to check. 00315 * This parameter can be one of the @ref TIM6_IT_TypeDef enumeration. 00316 * @retval ITStatus : The new state of the TIM6_IT. 00317 * This parameter can be any of the @ref ITStatus enumeration. 00318 */ 00319 ITStatus TIM6_GetITStatus(TIM6_IT_TypeDef TIM6_IT) 00320 { 00321 ITStatus bitstatus = RESET; 00322 uint8_t itStatus = 0, itEnable = 0; 00323 00324 /* Check the parameters */ 00325 assert_param(IS_TIM6_GET_IT_OK(TIM6_IT)); 00326 00327 itStatus = (uint8_t)(TIM6->SR1 & (uint8_t)TIM6_IT); 00328 00329 itEnable = (uint8_t)(TIM6->IER & (uint8_t)TIM6_IT); 00330 00331 if ((itStatus != (uint8_t)RESET ) && (itEnable != (uint8_t)RESET )) 00332 { 00333 bitstatus = (ITStatus)SET; 00334 } 00335 else 00336 { 00337 bitstatus = (ITStatus)RESET; 00338 } 00339 return ((ITStatus)bitstatus); 00340 } 00341 00342 /** 00343 * @brief Configures the TIM6 event to be generated by software. 00344 * @param TIM6_EventSource : Specifies the event source. 00345 * This parameter can be one of the @ref TIM6_EventSource_TypeDef enumeration. 00346 * @retval None 00347 */ 00348 void TIM6_GenerateEvent(TIM6_EventSource_TypeDef TIM6_EventSource) 00349 { 00350 /* Check the parameters */ 00351 assert_param(IS_TIM6_EVENT_SOURCE_OK((uint8_t)TIM6_EventSource)); 00352 00353 /* Set the event sources */ 00354 TIM6->EGR |= (uint8_t)TIM6_EventSource; 00355 } 00356 00357 /** 00358 * @brief Checks whether the specified TIM6 flag is set or not. 00359 * @param TIM6_FLAG : Specifies the flag to check. 00360 * This parameter can be one of the @ref TIM6_FLAG_TypeDef enumeration. 00361 * @retval FlagStatus : The new state of TIM6_FLAG. 00362 * This parameter can be any of the @ref FlagStatus enumeration. 00363 */ 00364 FlagStatus TIM6_GetFlagStatus(TIM6_FLAG_TypeDef TIM6_FLAG) 00365 { 00366 volatile FlagStatus bitstatus = RESET; 00367 00368 /* Check the parameters */ 00369 assert_param(IS_TIM6_GET_FLAG_OK(TIM6_FLAG)); 00370 00371 if ((TIM6->SR1 & (uint8_t)TIM6_FLAG) != 0) 00372 { 00373 bitstatus = SET; 00374 } 00375 else 00376 { 00377 bitstatus = RESET; 00378 } 00379 return ((FlagStatus)bitstatus); 00380 } 00381 00382 /** 00383 * @brief Clears the TIM6's interrupt pending bits. 00384 * @param TIM6_IT : Specifies the pending bit to clear. 00385 * This parameter can be one of the @ref TIM6_IT_TypeDef enumeration. 00386 * @retval None 00387 */ 00388 void TIM6_ClearITPendingBit(TIM6_IT_TypeDef TIM6_IT) 00389 { 00390 /* Check the parameters */ 00391 assert_param(IS_TIM6_IT_OK(TIM6_IT)); 00392 00393 /* Clear the IT pending Bit */ 00394 TIM6->SR1 &= (uint8_t)(~(uint8_t)TIM6_IT); 00395 } 00396 00397 /** 00398 * @brief Selects the TIM6 Trigger Output Mode. 00399 * @param TIM6_TRGOSource : Specifies the Trigger Output source. 00400 * This parameter can be one of the @ref TIM6_TRGOSource_TypeDef enumeration. 00401 * @retval None 00402 */ 00403 void TIM6_SelectOutputTrigger(TIM6_TRGOSource_TypeDef TIM6_TRGOSource) 00404 { 00405 uint8_t tmpcr2 = 0; 00406 00407 /* Check the parameters */ 00408 assert_param(IS_TIM6_TRGO_SOURCE_OK(TIM6_TRGOSource)); 00409 00410 tmpcr2 = TIM6->CR2; 00411 00412 /* Reset the MMS Bits */ 00413 tmpcr2 &= (uint8_t)(~TIM6_CR2_MMS); 00414 00415 /* Select the TRGO source */ 00416 tmpcr2 |= (uint8_t)TIM6_TRGOSource; 00417 00418 TIM6->CR2 = tmpcr2; 00419 } 00420 00421 /** 00422 * @brief Sets or Resets the TIM6 Master/Slave Mode. 00423 * @param NewState : The new state of the synchronization between TIM6 and its slaves (through TRGO). 00424 * This parameter can be any of the @ref FunctionalState enumeration. 00425 * @retval None 00426 */ 00427 00428 void TIM6_SelectMasterSlaveMode(FunctionalState NewState) 00429 { 00430 /* Check the parameters */ 00431 assert_param(IS_FUNCTIONALSTATE_OK(NewState)); 00432 00433 /* Set or Reset the MSM Bit */ 00434 if (NewState == ENABLE) 00435 { 00436 TIM6->SMCR |= TIM6_SMCR_MSM; 00437 } 00438 else 00439 { 00440 TIM6->SMCR &= (uint8_t)(~TIM6_SMCR_MSM); 00441 } 00442 } 00443 00444 /** 00445 * @brief Selects the TIM6 Input Trigger source. 00446 * @param TIM6_InputTriggerSource : Specifies Input Trigger source. 00447 * This parameter can be one of the @ref TIM6_TS_TypeDef enumeration. 00448 * @retval None 00449 */ 00450 void TIM6_SelectInputTrigger(TIM6_TS_TypeDef TIM6_InputTriggerSource) 00451 { 00452 uint8_t tmpsmcr = 0; 00453 00454 /* Check the parameters */ 00455 assert_param(IS_TIM6_TRIGGER_SELECTION_OK(TIM6_InputTriggerSource)); 00456 00457 tmpsmcr = TIM6->SMCR; 00458 00459 /* Select the Trigger Source */ 00460 tmpsmcr &= (uint8_t)(~TIM6_SMCR_TS); 00461 tmpsmcr |= (uint8_t)TIM6_InputTriggerSource; 00462 00463 TIM6->SMCR = (uint8_t)tmpsmcr; 00464 } 00465 00466 /** 00467 * @brief Enables the TIM6 internal Clock. 00468 * @param None 00469 * @retval None 00470 */ 00471 void TIM6_InternalClockConfig(void) 00472 { 00473 /* Disable slave mode to clock the prescaler directly with the internal clock */ 00474 TIM6->SMCR &= (uint8_t)(~TIM6_SMCR_SMS); 00475 } 00476 00477 /** 00478 * @brief Selects the TIM6 Slave Mode. 00479 * @param TIM6_SlaveMode : Specifies the TIM6 Slave Mode. 00480 * This parameter can be one of the @ref TIM6_SlaveMode_TypeDef enumeration. 00481 * @retval None 00482 */ 00483 void TIM6_SelectSlaveMode(TIM6_SlaveMode_TypeDef TIM6_SlaveMode) 00484 { 00485 uint8_t tmpsmcr = 0; 00486 00487 /* Check the parameters */ 00488 assert_param(IS_TIM6_SLAVE_MODE_OK(TIM6_SlaveMode)); 00489 00490 tmpsmcr = TIM6->SMCR; 00491 00492 /* Reset the SMS Bits */ 00493 tmpsmcr &= (uint8_t)(~TIM6_SMCR_SMS); 00494 00495 /* Select the Slave Mode */ 00496 tmpsmcr |= (uint8_t)TIM6_SlaveMode; 00497 00498 TIM6->SMCR = tmpsmcr; 00499 } 00500 00501 /** 00502 * @} 00503 */ 00504 00505 /** 00506 * @} 00507 */ 00508 00509 00510 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/