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