tao_z
2021-04-11 6d4ddf7d2a2cd72c58876257700b34c63e5063d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
  ******************************************************************************
  * @file    stm8s_tim6.c
  * @author  MCD Application Team
  * @version V2.2.0
  * @date    30-September-2014
  * @brief   This file contains all the functions for the TIM6 peripheral.
   ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
  */
 
 
/* Includes ------------------------------------------------------------------*/
#include "stm8s_tim6.h"
 
/** @addtogroup STM8S_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
 
 
/** @addtogroup STM8S_StdPeriph_Driver
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/**
  * @addtogroup TIM6_Public_Functions
  * @{
  */
 
/**
  * @brief  Deinitializes the TIM6 peripheral registers to their default reset values.
  * @param  None
  * @retval None
  */
void TIM6_DeInit(void)
{
  TIM6->CR1     = TIM6_CR1_RESET_VALUE;
  TIM6->CR2     = TIM6_CR2_RESET_VALUE;
  TIM6->SMCR     = TIM6_SMCR_RESET_VALUE;
  TIM6->IER     = TIM6_IER_RESET_VALUE;
  TIM6->CNTR     = TIM6_CNTR_RESET_VALUE;
  TIM6->PSCR    = TIM6_PSCR_RESET_VALUE;
  TIM6->ARR     = TIM6_ARR_RESET_VALUE;
  TIM6->SR1     = TIM6_SR1_RESET_VALUE;
}
 
/**
  * @brief  Initializes the TIM6 Time Base Unit according to the specified
  * parameters.
  * @param   TIM6_Prescaler : This parameter can be any of the @Ref TIM5_Prescaler_TypeDef enumeration.
  * @param   TIM6_Period : This parameter must be a value between 0x00 and 0xFF.
  * @retval None
  */
void TIM6_TimeBaseInit(TIM6_Prescaler_TypeDef TIM6_Prescaler,
                       uint8_t TIM6_Period)
{
  /* Check TIM6 prescaler value */
  assert_param(IS_TIM6_PRESCALER_OK(TIM6_Prescaler));
  /* Set the Autoreload value */
  TIM6->ARR = (uint8_t)(TIM6_Period);
  /* Set the Prescaler value */
  TIM6->PSCR = (uint8_t)(TIM6_Prescaler);
}
 
/**
  * @brief  Enables or disables the TIM6 peripheral.
  * @param   NewState : The new state of the TIM6 peripheral.
  * This parameter can be any of the @ref FunctionalState enumeration.
  * @retval None
  */
void TIM6_Cmd(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  /* set or Reset the CEN Bit */
  if (NewState == ENABLE)
  {
    TIM6->CR1 |= TIM6_CR1_CEN ;
  }
  else
  {
    TIM6->CR1 &= (uint8_t)(~TIM6_CR1_CEN) ;
  }
}
 
/**
  * @brief  Enables or Disables the TIM6 Update event.
  * @param   NewState : The new state of the TIM6 peripheral Preload register.
  * This parameter can be any of the @ref FunctionalState enumeration.
  * @retval None
  */
void TIM6_UpdateDisableConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  /* Set or Reset the UDIS Bit */
  if (NewState == ENABLE)
  {
    TIM6->CR1 |= TIM6_CR1_UDIS ;
  }
  else
  {
    TIM6->CR1 &= (uint8_t)(~TIM6_CR1_UDIS) ;
  }
}
 
/**
  * @brief  Selects the TIM6 Update Request Interrupt source.
  * @param   TIM6_UpdateSource : Specifies the Update source.
  * This parameter can be one of the @ref TIM6_UpdateSource_TypeDef enumeration.
  * @retval None
  */
void TIM6_UpdateRequestConfig(TIM6_UpdateSource_TypeDef TIM6_UpdateSource)
{
  /* Check the parameters */
  assert_param(IS_TIM6_UPDATE_SOURCE_OK(TIM6_UpdateSource));
  
  /* Set or Reset the URS Bit */
  if (TIM6_UpdateSource == TIM6_UPDATESOURCE_REGULAR)
  {
    TIM6->CR1 |= TIM6_CR1_URS ;
  }
  else
  {
    TIM6->CR1 &= (uint8_t)(~TIM6_CR1_URS) ;
  }
}
 
/**
  * @brief  Selects the TIM6’s One Pulse Mode.
  * @param   TIM6_OPMode : Specifies the OPM Mode to be used.
  * This parameter can be one of the @ref TIM6_OPMode_TypeDef enumeration.
  * @retval None
  */
void TIM6_SelectOnePulseMode(TIM6_OPMode_TypeDef TIM6_OPMode)
{
  /* Check the parameters */
  assert_param(IS_TIM6_OPM_MODE_OK(TIM6_OPMode));
  
  /* Set or Reset the OPM Bit */
  if (TIM6_OPMode == TIM6_OPMODE_SINGLE)
  {
    TIM6->CR1 |= TIM6_CR1_OPM ;
  }
  else
  {
    TIM6->CR1 &= (uint8_t)(~TIM6_CR1_OPM) ;
  }
}
 
/**
  * @brief  Configures the TIM6 Prescaler.
  * @param   Prescaler : Specifies the Prescaler Register value
  * This parameter can be one of the @ref TIM6_Prescaler_TypeDef enumeration.
  * @param   TIM6_PSCReloadMode : Specifies the TIM6 Prescaler Reload mode.
  * This parameter can be one of the @ref TIM6_PSCReloadMode_TypeDef enumeration.
  * @retval None
  */
void TIM6_PrescalerConfig(TIM6_Prescaler_TypeDef Prescaler,
                          TIM6_PSCReloadMode_TypeDef TIM6_PSCReloadMode)
{
  /* Check the parameters */
  assert_param(IS_TIM6_PRESCALER_RELOAD_OK(TIM6_PSCReloadMode));
  assert_param(IS_TIM6_PRESCALER_OK(Prescaler));
  
  /* Set the Prescaler value */
  TIM6->PSCR = (uint8_t)Prescaler;
  
  /* Set or reset the UG Bit */
  if (TIM6_PSCReloadMode == TIM6_PSCRELOADMODE_IMMEDIATE)
  {
    TIM6->EGR |= TIM6_EGR_UG ;
  }
  else
  {
    TIM6->EGR &= (uint8_t)(~TIM6_EGR_UG) ;
  }
}
 
/**
  * @brief  Enables or disables TIM6 peripheral Preload register on ARR.
  * @param   NewState : The new state of the TIM6 peripheral Preload register.
  * This parameter can be any of the @ref FunctionalState enumeration.
  * @retval None
  */
void TIM6_ARRPreloadConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  /* Set or Reset the ARPE Bit */
  if (NewState == ENABLE)
  {
    TIM6->CR1 |= TIM6_CR1_ARPE ;
  }
  else
  {
    TIM6->CR1 &= (uint8_t)(~TIM6_CR1_ARPE) ;
  }
}
 
/**
  * @brief  Sets the TIM6 Counter Register value.
  * @param   Counter : Specifies the Counter register new value.
  * This parameter is between 0x00 and 0xFF.
  * @retval None
  */
void TIM6_SetCounter(uint8_t Counter)
{
  /* Set the Counter Register value */
  TIM6->CNTR = (uint8_t)(Counter);
}
 
/**
  * @brief  Sets the TIM6 Autoreload Register value.
  * @param   Autoreload : Specifies the Autoreload register new value.
  * This parameter is between 0x00 and 0xFF.
  * @retval None
  */
void TIM6_SetAutoreload(uint8_t Autoreload)
{
  /* Set the Autoreload Register value */
  TIM6->ARR = (uint8_t)(Autoreload);
}
 
/**
  * @brief  Gets the TIM6 Counter value.
  * @param  None
  * @retval uint8_t: Counter Register value.
  */
uint8_t TIM6_GetCounter(void)
{
  uint8_t tmpcntr=0;
  tmpcntr = TIM6->CNTR;
  /* Get the Counter Register value */
  return ((uint8_t)tmpcntr);
}
 
/**
  * @brief  Gets the TIM6 Prescaler value.
  * @param  None
  * @retval TIM6_Prescaler_TypeDef : Prescaler Register configuration value.
  */
TIM6_Prescaler_TypeDef TIM6_GetPrescaler(void)
{
  /* Get the Prescaler Register value */
  return ((TIM6_Prescaler_TypeDef)TIM6->PSCR);
}
 
/**
  * @brief  Enables or disables the specified TIM6 interrupts.
  * @param   TIM6_IT : Specifies the TIM6 interrupts sources to be enabled or disabled.
  * This parameter can be any combination of the @ref TIM6_IT_TypeDef enumeration.
  * @param   NewState : The new state of the TIM6 peripheral.
  * This parameter can be any of the @ref FunctionalState enumeration.
  * @retval None
  * @par Required preconditions:
  * If QST option bit is enabled, the TIM6 Interrupt vector will be mapped on IRQ number 2 (irq0).
  * Otherwise, it will be mapped on IRQ number 27 (irq25).
  */
void TIM6_ITConfig(TIM6_IT_TypeDef TIM6_IT, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_TIM6_IT_OK(TIM6_IT));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  if (NewState == ENABLE)
  {
    /* Enable the Interrupt sources */
    TIM6->IER |= (uint8_t)TIM6_IT;
  }
  else
  {
    /* Disable the Interrupt sources */
    TIM6->IER &= (uint8_t)(~(uint8_t)TIM6_IT);
  }
}
 
/**
  * @brief  Clears the TIM’s pending flags.
  * @param   TIM6_FLAG : Specifies the flag to clear.
  * This parameter can be one of the @ref TIM6_FLAG_TypeDef enumeration.
  * @retval None
  */
void TIM6_ClearFlag(TIM6_FLAG_TypeDef TIM6_FLAG)
{
  /* Check the parameters */
  assert_param(IS_TIM6_CLEAR_FLAG_OK((uint8_t)TIM6_FLAG));
  /* Clear the flags (rc_w0) clear this bit by writing 0. Writing ‘1’ has no effect*/
  TIM6->SR1 &= (uint8_t)(~((uint8_t)TIM6_FLAG));
}
 
/**
  * @brief  Checks whether the TIM6 interrupt has occurred or not.
  * @param   TIM6_IT : Specifies the TIM6 interrupt source to check.
  * This parameter can be one of the @ref TIM6_IT_TypeDef enumeration.
  * @retval ITStatus : The new state of the TIM6_IT.
  * This parameter can be any of the @ref ITStatus enumeration.
  */
ITStatus TIM6_GetITStatus(TIM6_IT_TypeDef TIM6_IT)
{
  ITStatus bitstatus = RESET;
  uint8_t itStatus = 0, itEnable = 0;
  
  /* Check the parameters */
  assert_param(IS_TIM6_GET_IT_OK(TIM6_IT));
  
  itStatus = (uint8_t)(TIM6->SR1 & (uint8_t)TIM6_IT);
  
  itEnable = (uint8_t)(TIM6->IER & (uint8_t)TIM6_IT);
  
  if ((itStatus != (uint8_t)RESET ) && (itEnable != (uint8_t)RESET ))
  {
    bitstatus = (ITStatus)SET;
  }
  else
  {
    bitstatus = (ITStatus)RESET;
  }
  return ((ITStatus)bitstatus);
}
 
/**
  * @brief  Configures the TIM6 event to be generated by software.
  * @param   TIM6_EventSource : Specifies the event source.
  * This parameter can be one of the @ref TIM6_EventSource_TypeDef enumeration.
  * @retval None
  */
void TIM6_GenerateEvent(TIM6_EventSource_TypeDef TIM6_EventSource)
{
  /* Check the parameters */
  assert_param(IS_TIM6_EVENT_SOURCE_OK((uint8_t)TIM6_EventSource));
  
  /* Set the event sources */
  TIM6->EGR |= (uint8_t)TIM6_EventSource;
}
 
/**
  * @brief  Checks whether the specified TIM6 flag is set or not.
  * @param   TIM6_FLAG : Specifies the flag to check.
  * This parameter can be one of the @ref TIM6_FLAG_TypeDef enumeration.
  * @retval FlagStatus : The new state of TIM6_FLAG.
  * This parameter can be any of the @ref FlagStatus enumeration.
  */
FlagStatus TIM6_GetFlagStatus(TIM6_FLAG_TypeDef TIM6_FLAG)
{
  volatile FlagStatus bitstatus = RESET;
  
  /* Check the parameters */
  assert_param(IS_TIM6_GET_FLAG_OK(TIM6_FLAG));
  
  if ((TIM6->SR1 & (uint8_t)TIM6_FLAG)  != 0)
  {
    bitstatus = SET;
  }
  else
  {
    bitstatus = RESET;
  }
  return ((FlagStatus)bitstatus);
}
 
/**
  * @brief  Clears the TIM6's interrupt pending bits.
  * @param   TIM6_IT : Specifies the pending bit to clear.
  * This parameter can be one of the @ref TIM6_IT_TypeDef enumeration.
  * @retval None
  */
void TIM6_ClearITPendingBit(TIM6_IT_TypeDef TIM6_IT)
{
  /* Check the parameters */
  assert_param(IS_TIM6_IT_OK(TIM6_IT));
  
  /* Clear the IT pending Bit */
  TIM6->SR1 &= (uint8_t)(~(uint8_t)TIM6_IT);
}
 
/**
  * @brief  Selects the TIM6 Trigger Output Mode.
  * @param   TIM6_TRGOSource : Specifies the Trigger Output source.
  * This parameter can be one of the @ref TIM6_TRGOSource_TypeDef enumeration.
  * @retval None
  */
void TIM6_SelectOutputTrigger(TIM6_TRGOSource_TypeDef TIM6_TRGOSource)
{
  uint8_t tmpcr2 = 0;
  
  /* Check the parameters */
  assert_param(IS_TIM6_TRGO_SOURCE_OK(TIM6_TRGOSource));
  
  tmpcr2 = TIM6->CR2;
  
  /* Reset the MMS Bits */
  tmpcr2 &= (uint8_t)(~TIM6_CR2_MMS);
  
  /* Select the TRGO source */
  tmpcr2 |=  (uint8_t)TIM6_TRGOSource;
  
  TIM6->CR2 = tmpcr2;
}
 
/**
  * @brief  Sets or Resets the TIM6 Master/Slave Mode.
  * @param   NewState : The new state of the synchronization between TIM6 and its slaves (through TRGO).
  * This parameter can be any of the @ref FunctionalState enumeration.
  * @retval None
  */
 
void TIM6_SelectMasterSlaveMode(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  
  /* Set or Reset the MSM Bit */
  if (NewState == ENABLE)
  {
    TIM6->SMCR |= TIM6_SMCR_MSM;
  }
  else
  {
    TIM6->SMCR &= (uint8_t)(~TIM6_SMCR_MSM);
  }
}
 
/**
  * @brief  Selects the TIM6 Input Trigger source.
  * @param   TIM6_InputTriggerSource : Specifies Input Trigger source.
  * This parameter can be one of the @ref TIM6_TS_TypeDef enumeration.
  * @retval None
  */
void TIM6_SelectInputTrigger(TIM6_TS_TypeDef TIM6_InputTriggerSource)
{
  uint8_t tmpsmcr = 0;
  
  /* Check the parameters */
  assert_param(IS_TIM6_TRIGGER_SELECTION_OK(TIM6_InputTriggerSource));
  
  tmpsmcr = TIM6->SMCR;
  
  /* Select the Trigger Source */
  tmpsmcr &= (uint8_t)(~TIM6_SMCR_TS);
  tmpsmcr |= (uint8_t)TIM6_InputTriggerSource;
  
  TIM6->SMCR = (uint8_t)tmpsmcr;
}
 
/**
  * @brief  Enables the TIM6 internal Clock.
  * @param  None
  * @retval None
  */
void TIM6_InternalClockConfig(void)
{
  /* Disable slave mode to clock the prescaler directly with the internal clock */
  TIM6->SMCR &=  (uint8_t)(~TIM6_SMCR_SMS);
}
 
/**
  * @brief  Selects the TIM6 Slave Mode.
  * @param   TIM6_SlaveMode : Specifies the TIM6 Slave Mode.
  * This parameter can be one of the @ref TIM6_SlaveMode_TypeDef enumeration.
  * @retval None
  */
void TIM6_SelectSlaveMode(TIM6_SlaveMode_TypeDef TIM6_SlaveMode)
{
  uint8_t tmpsmcr = 0;
  
  /* Check the parameters */
  assert_param(IS_TIM6_SLAVE_MODE_OK(TIM6_SlaveMode));
  
  tmpsmcr = TIM6->SMCR;
  
  /* Reset the SMS Bits */
  tmpsmcr &= (uint8_t)(~TIM6_SMCR_SMS);
  
  /* Select the Slave Mode */
  tmpsmcr |= (uint8_t)TIM6_SlaveMode;
  
  TIM6->SMCR = tmpsmcr;
}
 
/**
  * @}
  */
  
/**
  * @}
  */
  
 
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/