summaryrefslogtreecommitdiffstats
path: root/uc_str912/prj_template91x/str91x_lib/src/91x_mc.c
blob: d3ca96a67106215c0e42dd0ec581d04481ab8ee6 (plain)
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
/******************** (C) COPYRIGHT 2006 STMicroelectronics ********************
* File Name          : 91x_mc.c
* Author             : MCD Application Team
* Date First Issued  : 05/18/2006 : Version 1.0
* Description        : This file provides all the MC software functions.
********************************************************************************
* History:
* 05/24/2006 : Version 1.1
* 05/18/2006 : Version 1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "91x_mc.h"
#include "91x_scu.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

#define MC_ODCS_Set    0x0001
#define MC_ODCS_Reset  0x00FE

#define MC_CMS_Set    0x0004
#define MC_CMS_Reset  0x00FB

#define MC_CPC_Set  0x0008
#define MC_CTC_Set  0x0010

#define MC_PCE_Set   0x0020
#define MC_PCE_Reset 0xFFDF

#define MC_TCE_Set   0x0040
#define MC_TCE_Reset 0x00BF

#define MC_DTE_Set   0x0080
#define MC_DTE_Reset 0x007F

#define MC_TCB_Set    0x0004
#define MC_TCB_Reset  0x00FB

#define MC_STC_Set  0x0008

#define MC_TES_Set    0x0010
#define MC_TES_Reset  0x00EF

#define MC_CCPT_Set    0x0020
#define MC_CCPT_Reset  0x005F

#define MC_DISEST_Set    0x0040
#define MC_DISEST_Reset  0x003F

#define MC_DTS_Set    0x0001
#define MC_DTS_Reset  0x00FE

#define MC_SDT_Set    0x0002

#define MC_C0SE_Set    0x0004
#define MC_C0SE_Reset  0x00FB

#define MC_CUSE_Set    0x0008
#define MC_CUSE_Reset  0x00F7 

#define MC_CVSE_Set    0x0010
#define MC_CVSE_Reset  0x00EF

#define MC_CWSE_Set   0x0020
#define MC_CWSE_Reset 0x00D0

#define MC_RSE_Set    0x0040
#define MC_RSE_Reset  0x00BF

#define MC_GPI_Set    0x0080
#define MC_GPI_Reset  0x007F

#define MC_PUH_Set    0x0020
#define MC_PUH_Reset  0x005F
#define MC_PUL_Set    0x0010
#define MC_PUL_Reset  0x006F

#define MC_PVH_Set    0x0008
#define MC_PVH_Reset  0x0077
#define MC_PVL_Set    0x0004
#define MC_PVL_Reset  0x007B

#define MC_PWH_Set    0x0002
#define MC_PWH_Reset  0x007D
#define MC_PWL_Set    0x0001
#define MC_PWL_Reset  0x007E

#define MC_ODS_Set    0x0040
#define MC_ODS_Reset  0xFF3F

#define MC_ESC_Clear  0x4321

#define MC_PCR1_TIN_MASK 0xFFFC
#define MC_OPR_Mask      0x0040 
#define MC_UDCS_Mask     0x0002
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/******************************************************************************
* Function Name  : MC_DeInit
* Description    : Deinitializes MC peripheral registers to their default reset
*                  values.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MC_DeInit(void)
{      
  /* MC peripheral is under Reset */
  SCU_APBPeriphReset(__MC, ENABLE);

  /* MC peripheral Reset off */
  SCU_APBPeriphReset(__MC, DISABLE);           
}

/*******************************************************************************
* Function Name  : MC_Init
* Description    : Initializes the MC peripheral according to the specified
*                  parameters in the MC_InitStruct .
* Input          : IMC_InitStruct: pointer to a MC_InitTypeDef structure that
*                  contains the configuration information for the MC peripheral.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_Init(MC_InitTypeDef* MC_InitStruct)
{
  /* Select the operating Mode */
  if(MC_InitStruct->MC_OperatingMode == MC_SoftwareOperating_Mode)
   {
     /* Select the Data transfer Mode */
     MC->PCR2 |= MC_DTS_Set;

     /* Enable the registers Software Data Transfer */
     MC->PCR2 |= MC_C0SE_Set | MC_CUSE_Set | MC_CVSE_Set | MC_CWSE_Set 
                 |MC_RSE_Set;
     
   }
   else
   {
     /* Select the Data transfer Mode */
     MC->PCR2 &= MC_DTS_Reset;   

     /* Disable the registers Software Data Transfer */
     MC->PCR2 &= MC_C0SE_Reset | MC_CUSE_Reset | MC_CVSE_Reset | MC_CWSE_Reset 
                 |MC_RSE_Reset;
    }

   /* Select the MC PWM counter Mode */
   if(MC_InitStruct->MC_PWMMode == MC_PWMZeroCentered_Mode)
   {
     MC->PCR0 |= MC_CMS_Set;
   }
   else
   {
     MC->PCR0 &= MC_CMS_Reset;
   }

   /* Set the MC PWM counter Prescaler */
   MC->CPRS = MC_InitStruct->MC_Prescaler;

   /* Set the MC PWM Period */
   MC->CMP0 = MC_InitStruct->MC_Period;

   /* Set the MC PWM Repetition counter */
   MC->REP = MC_InitStruct->MC_RepetitionCounter;

   /* Set the Tacho Compare value */
   MC->TCMP = MC_InitStruct->MC_TachoPeriod;

   /* Set the Tacho Prescaler value */
   MC->TPRS = MC_InitStruct->MC_TachoPrescaler;

   /* Set the MC Tacho Input Polarity */
   MC->PCR1 = (MC->PCR1 & MC_PCR1_TIN_MASK) | MC_InitStruct->MC_TachoPolarity;

   /* Set the MC PWM Forced State */
   MC->OPR |= MC_ODS_Set;
   MC->OPR = (MC->OPR & MC_OPR_Mask) | MC_InitStruct->MC_ForcedPWMState; 
   
   /* Select the Tacho Mode */
   if(MC_InitStruct->MC_TachoMode == MC_TachoOneShot_Mode)
   {
     MC->PCR1 |= MC_TCB_Set;
   }
   else
   {
     MC->PCR1 &= MC_TCB_Reset;
   }

   /* Select the Tacho Event Mode */
   if(MC_InitStruct->MC_TachoEvent_Mode == MC_TachoEvent_Software_Mode)
   {
     MC->PCR1 |= MC_TES_Set;
   }
   else
   {
     MC->PCR1 &= MC_TES_Reset;
   }

   /* Enable or disable the emergency input */
   if(MC_InitStruct->MC_Emergency == MC_Emergency_Enable)
   {
     MC->PCR1 &= MC_DISEST_Reset;
   }
   else
   {
     MC->PCR1 |= MC_DISEST_Set;
   }

/* Select the complementary Mode */
   if(MC_InitStruct->MC_Complementary == MC_Complementary_Enable)
   {
     MC->DTG = MC_InitStruct->MC_DeadTime;
     MC->PCR0 |= MC_ODCS_Set;
   }
   else
   {
     MC->PCR0 &= MC_ODCS_Reset;
   }

   /* Tacho Mode selection */
   if(MC_InitStruct->MC_TachoMode == MC_TachoOneShot_Mode)
   {
     MC->PCR1 |= MC_TCB_Set;
   }
   else
   {
     MC->PCR1 &= MC_TCB_Reset;
   }

   switch(MC_InitStruct->MC_Channel)
   {
     /* Channel U configuration */
     case MC_Channel_U:
     {   
       MC->CMPU = MC_InitStruct->MC_PulseU;

       if(MC_InitStruct->MC_PolarityUL == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PUL_Set;
       }
       else
       {
         MC->PSR &= MC_PUL_Reset;
       }
       if(MC_InitStruct->MC_PolarityUH == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PUH_Set;
       }
       else
       {
         MC->PSR &= MC_PUH_Reset;
       }
       break;
     }

     /* Channel V configuration */
     case MC_Channel_V:
     {
       MC->CMPV = MC_InitStruct->MC_PulseV;

       if(MC_InitStruct->MC_PolarityVL == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PVL_Set;
       }
       else
       {
         MC->PSR &= MC_PVL_Reset;
       }
       if(MC_InitStruct->MC_PolarityVH == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PVH_Set;
       }
       else
       {
         MC->PSR &= MC_PVH_Reset;
       }
       break;
     }

     /* Channel W configuration */
     case MC_Channel_W:
     {
       MC->CMPW = MC_InitStruct->MC_PulseW;
       
       if(MC_InitStruct->MC_PolarityWL == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PWL_Set;
       }
       else
       {
         MC->PSR &= MC_PWL_Reset;
       }
       if(MC_InitStruct->MC_PolarityWH == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PWH_Set;
       }
       else
       {
         MC->PSR &= MC_PWH_Reset;
       }
       break;
     }
     /* All Channel Configuration */
     case MC_Channel_ALL:
     {
       MC->CMPU = MC_InitStruct->MC_PulseU;
       MC->CMPV = MC_InitStruct->MC_PulseV;
       MC->CMPW = MC_InitStruct->MC_PulseW;
       
       if(MC_InitStruct->MC_PolarityUL == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PUL_Set;
       }
       else
       {
         MC->PSR &= MC_PUL_Reset;
       }
       if(MC_InitStruct->MC_PolarityUH == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PUH_Set;
       }
       else
       {
         MC->PSR &= MC_PUH_Reset;
       }

       if(MC_InitStruct->MC_PolarityVL == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PVL_Set;
       }
       else
       {
         MC->PSR &= MC_PVL_Reset;
       }
       if(MC_InitStruct->MC_PolarityVH == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PVH_Set;
       }
       else
       {
         MC->PSR &= MC_PVH_Reset;
       }

       if(MC_InitStruct->MC_PolarityWL == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PWL_Set;
       }
       else
       {
         MC->PSR &= MC_PWL_Reset;
       }
       if(MC_InitStruct->MC_PolarityWH == MC_Polarity_Inverted)
       {
       	 MC->PSR |= MC_PWH_Set;
       }
       else
       {
         MC->PSR &= MC_PWH_Reset;
       }
     }
     default:
     break;
   }
}

/*******************************************************************************
* Function Name  : MC_StructInit
* Description    : Fills each MC_InitStruct member with its default value.
* Input          : MC_InitStruct : pointer to a MC_InitTypeDef structure which
*                  will be initialized.
* Output         : None                        
* Return         : None.
*******************************************************************************/
void MC_StructInit(MC_InitTypeDef* MC_InitStruct)
{
  MC_InitStruct->MC_OperatingMode = MC_HardwareOperating_Mode;
  MC_InitStruct->MC_TachoMode = MC_TachoContinuous_Mode;
  MC_InitStruct->MC_TachoEvent_Mode = MC_TachoEvent_Hardware_Mode;
  MC_InitStruct->MC_Prescaler = 0x00;
  MC_InitStruct->MC_TachoPrescaler = 0x0000;
  MC_InitStruct->MC_PWMMode = MC_PWMClassical_Mode;
  MC_InitStruct->MC_Complementary = MC_Complementary_Enable;
  MC_InitStruct->MC_Emergency = MC_Emergency_Disable;
  MC_InitStruct->MC_ForcedPWMState = 0x003F;
  MC_InitStruct->MC_Period = 0x0000;
  MC_InitStruct->MC_TachoPeriod = 0x00FF;
  MC_InitStruct->MC_Channel = MC_Channel_ALL;
  MC_InitStruct->MC_PulseU = 0x0000;
  MC_InitStruct->MC_PulseV = 0x0000;
  MC_InitStruct->MC_PulseW = 0x0000;
  MC_InitStruct->MC_PolarityUL = MC_Polarity_NonInverted;
  MC_InitStruct->MC_PolarityUH = MC_Polarity_NonInverted;
  MC_InitStruct->MC_PolarityVL = MC_Polarity_NonInverted;
  MC_InitStruct->MC_PolarityVH = MC_Polarity_NonInverted;
  MC_InitStruct->MC_PolarityWL = MC_Polarity_NonInverted;
  MC_InitStruct->MC_PolarityWH = MC_Polarity_NonInverted;
  MC_InitStruct->MC_TachoPolarity = MC_TachoEventEdge_RisingFalling;
  MC_InitStruct->MC_DeadTime = 0x003F;
  MC_InitStruct->MC_RepetitionCounter = 0x0000;
}

/*******************************************************************************
* Function Name  : MC_Cmd
* Description    : Enables or disables the MC peripheral.
* Input          : Newstate: new state of the MC peripheral.
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_Cmd(FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
  /* Enable the PWM counter */
    MC->PCR0 |= MC_PCE_Set;
  
  /* Enable the Tacho counter */
    MC->PCR0 |= MC_TCE_Set;
  
  /* Enable the Dead Time counter */
    MC->PCR0 |= MC_DTE_Set;
  }
  else
  {
  /* Disable the PWM counter */
    MC->PCR0 &= MC_PCE_Reset;
    
  /* Disable the Tacho counter */
    MC->PCR0 &= MC_TCE_Reset;
    
  /* Disable the Dead counter */
    MC->PCR0 &= MC_DTE_Reset;
  }
}

/*******************************************************************************
* Function Name  : MC_ClearPWMCounter
* Description    : Clears the MC PWM counter.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ClearPWMCounter(void)
{
/* Clear the PWM counter */
  MC->PCR0 |= MC_CPC_Set;
}

/*******************************************************************************
* Function Name  : MC_ClearTachoCounter
* Description    : Clears the MC Tacho counter.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ClearTachoCounter(void)
{
/* Clear the Tacho counter */
  MC->PCR0 |= MC_CTC_Set;
}

/*******************************************************************************
* Function Name  : MC_CtrlPWMOutputs
* Description    : Enables or disables MC peripheral Main Outputs.
* Input          : Newstate: new state of the MC peripheral Main Outputs.
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_CtrlPWMOutputs(FunctionalState Newstate)
{
  if(Newstate == ENABLE)
  {
  /* Enable the dead time generator data */
    MC->OPR &= MC_ODS_Reset;
  }
  else
  {
  /* Enable the default state data */
    MC->OPR |= MC_ODS_Set;
  }
}

/*******************************************************************************
* Function Name  : MC_ITConfig
* Description    : Enables or disables the MC interrupts.
* Input          : - MC_IT: specifies the MC interrupts sources to be enabled
*                    or disabled.
*                    This parameter can be any combination of the following values:
*                         - MC_IT_CMPW: Compare W Interrupt.
*                         - MC_IT_CMPV: Compare V Interrupt.
*                         - MC_IT_CMPU: Compare U Interrupt.
*                         - MC_IT_ZPC: Zero of PWM counter Interrupt.
*                         - MC_IT_ADT: Automatic Data Transfer Interrupt.
*                         - MC_IT_OTC: Overflow of Tacho counter Interrupt.
*                         - MC_IT_CPT: Capture of Tacho counter Interrupt.
*                         - MC_IT_CM0: Compare 0 Interrupt.
*                  - Newstate: new state of IMC interrupts.
*                    This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ITConfig(u16 MC_IT, FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    /* Enable the specific interrupt source */
    MC->IMR |= MC_IT;

    /* Enable the global peripheral interrupt sources */
    MC->PCR2 |= MC_GPI_Set;
  }
  else
  {   
    /* Disable the specific interrupt source */
    MC->IMR &= ~MC_IT;

    /* Disable the global peripheral interrupt sources */
    MC->PCR2 &= MC_GPI_Reset;
  }
}

/*******************************************************************************
* Function Name  : MC_SetPrescaler
* Description    : Sets the MC prescaler value.
* Input          : MC_Prescaler: MC prescaler new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetPrescaler(u8 MC_Prescaler)
{
/* Set the Prescaler Register value */
  MC->CPRS = MC_Prescaler;
}

/*******************************************************************************
* Function Name  : MC_SetPeriod
* Description    : Sets the MC period value.
* Input          : MC_Period: MC period new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetPeriod(u16 MC_Period)
{
/* Set the Period Register value */
  MC->CMP0 = MC_Period;
}

/*******************************************************************************
* Function Name  : MC_SetPulseU
* Description    : Sets the MC pulse U value.
* Input          : MC_PulseU: MC pulse U new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetPulseU(u16 MC_PulseU)
{
/* Set the Pulse U Register value */
  MC->CMPU = MC_PulseU;
}

/*******************************************************************************
* Function Name  : MC_SetPulseV
* Description    : Sets the MC pulse V value.
* Input          : MC_PulseV: MC pulse V new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetPulseV(u16 MC_PulseV)
{
/* Set the Pulse V Register value */
   MC->CMPV = MC_PulseV;
}

/*******************************************************************************
* Function Name  : MC_SetPulseW
* Description    : Sets the MC pulse W value.
* Input          : MC_PulseW: MC pulse W new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetPulseW(u16 MC_PulseW)
{
/* Set the Pulse W Register value */
   MC->CMPW = MC_PulseW;
}

/*******************************************************************************
* Function Name  : MC_PWMModeConfig
* Description    : Selects the MC PWM counter Mode.
* Input          : MC_PWMMode: MC PWM counter Mode.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_PWMModeConfig(u16 MC_PWMMode)
{
   /* Select the MC PWM counter Mode */
   if(MC_PWMMode == MC_PWMZeroCentered_Mode)
   {
     MC->PCR0 |= MC_CMS_Set;
   }
   else
   {
     MC->PCR0 &= MC_CMS_Reset;
   }
}

/*******************************************************************************
* Function Name  : MC_SetDeadTime
* Description    : Sets the MC dead time value.
* Input          : MC_DeadTime: MC dead time new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetDeadTime(u16 MC_DeadTime)
{
/* Set the dead time Register value */
  MC->DTG = MC_DeadTime;
}

/*******************************************************************************
* Function Name  : MC_SetTachoCompare
* Description    : Sets the MC Tacho Compare Register value.
* Input          : MC_Compare: MC Tacho compare new value.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SetTachoCompare(u8 MC_Compare)
{
 /* Sets the Tacho Compare Register value */
  MC->TCMP = MC_Compare;
}
/*******************************************************************************
* Function Name  : MC_EmergencyCmd
* Description    : Enables or disables the MC emergency feauture.
* Input          : Newstate: new state of the MC peripheral Emergency.
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_EmergencyCmd(FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
   /* Reset the DISEST Bit in the PCR1 Register to enable the emergency stop input */
    MC->PCR1 &= MC_DISEST_Reset;
  }
  else
  {
  /* Set the DISEST Bit in the PCR1 Register to disable the emergency stop input */
    MC->PCR1 |= MC_DISEST_Reset;
  }
}

/*******************************************************************************
* Function Name  : MC_EmergencyClear
* Description    : Clears the MC Emergency Register.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MC_EmergencyClear(void)
{
/* Force PWM outputs in safe state (All OFF) */
   MC->OPR |= MC_ODS_Set;

/* Clear EST bit */
   MC->ESC = MC_ESC_Clear;
   
/* Re-enable protection */
   MC->ESC = 0;
   
/* Enables the Outputs */
   MC->OPR &= MC_ODS_Reset;
}

/*******************************************************************************
* Function Name  : MC_GetPeriod
* Description    : Gets the MC period value.
* Input          : None
* Output         : None
* Return         : MC period value.
*******************************************************************************/
u16 MC_GetPeriod(void)
{
/* Return the PWM signal period value */
  return MC->CMP0;
}

/*******************************************************************************
* Function Name  : MC_GetPulseU
* Description    : Gets the MC pulse U value.
* Input          : None
* Output         : None
* Return         : MC pulse U value.
*******************************************************************************/
u16 MC_GetPulseU(void)
{
/* Return the PWM pulse U Register value */
  return MC->CMPU;
}

/*******************************************************************************
* Function Name  : MC_GetPulseV
* Description    : Gets the MC pulse V value.
* Input          : None
* Output         : None
* Return         : MC pulse V value.
*******************************************************************************/
u16 MC_GetPulseV(void)
{
/* Return the PWM pulse V Register value */
  return MC->CMPV;
}

/*******************************************************************************
* Function Name  : MC_GetPulseW
* Description    : Gets the MC pulse W value.
* Input          : None
* Output         : None
* Return         : MC pulse W value.
*******************************************************************************/
u16 MC_GetPulseW(void)
{
/* Return the PWM pulse W Register value */
  return MC->CMPW;
}

/*******************************************************************************
* Function Name  : MC_GetTachoCapture
* Description    : Gets the MC Tacho period value.
* Input          : None
* Output         : None
* Return         : MC Tacho capture value.
*******************************************************************************/
u16 MC_GetTachoCapture(void)
{
/* Return the Tacho Capture Register value */
  return MC->TCPT;
}

/*******************************************************************************
* Function Name  : MC_ClearOnTachoCapture
* Description    : Enables or disables the the Clear on capture of tacho counter.
* Input          : Newstate: new state of the CCPT bit.
*                  This parameter can be: ENABLE or DISABLE.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ClearOnTachoCapture(FunctionalState NewState)
{
  if(NewState == ENABLE)
  {
    /* Enable the Clear on capture of tacho counter */
    MC->PCR1 |= MC_CCPT_Set;
  }
  else
  {   
    /* Disable the Clear on capture of tacho counter */
    MC->PCR1 &= MC_CCPT_Reset;
  }
}
/*******************************************************************************
* Function Name  : MC_ForceDataTransfer
* Description    : Sets the MC Outputs default states.
* Input          : MC_ForcedData: MC outputs new states.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ForceDataTransfer(u8 MC_ForcedData)
{
   /* Set the MC PWM Forced State */
   MC->OPR |= MC_ODS_Set;
   MC->OPR = (MC->OPR & MC_OPR_Mask) | MC_ForcedData;
}

/*******************************************************************************
* Function Name  : MC_PreloadConfig
* Description    : Enables the Software Data Transfer.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SoftwarePreloadConfig(void)
{
  /* Set the SDT: Software Data Transfer bit */
  MC->PCR2 |= MC_SDT_Set;
}

/*******************************************************************************
* Function Name  : MC_SoftwareTachoCapture
* Description    : Enables the Software Tacho Capture.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void MC_SoftwareTachoCapture(void)
{
  /* Set the STC: Software Tacho Capture bit */
  MC->PCR1 |= MC_STC_Set;
}

/*******************************************************************************
* Function Name  : MC_GetCountingStatus
* Description    : Checks whether the PWM Counter is counting Up or Down.
* Input          : None
* Output         : None
* Return         : The new state of the PWM Counter(DOWN or UP).
*******************************************************************************/
CountingStatus MC_GetCountingStatus(void)
{
  if((MC->PCR0 & MC_UDCS_Mask) != DOWN)
  {
    return UP;
  }
  else
  {
    return DOWN;
  }
}

/*******************************************************************************
* Function Name  : MC_GetFlagStatus
* Description    : Checks whether the specified MC flag is set or not.
* Input          : MC_FLAG: specifies the flag to check.
*                  This parameter can be one of the following values:
*                         - MC_FLAG_CMPW: Compare W Flag.
*                         - MC_FLAG_CMPV: Compare V Flag.
*                         - MC_FLAG_CMPU: Compare U Flag.
*                         - MC_FLAG_ZPC: Zero of PWM counter Flag.
*                         - MC_FLAG_ADT: Automatic Data Transfer Flag.
*                         - MC_FLAG_OTC: Overflow of Tacho counter Flag.
*                         - MC_FLAG_CPT: Capture of Tacho counter Flag.
*                         - MC_FLAG_CM0: Compare 0 Flag.
*                         - MC_FLAG_EST: Emergency Stop Flag.
* Output         : None
* Return         : The new state of the MC_FLAG(SET or RESET).
*******************************************************************************/
FlagStatus MC_GetFlagStatus(u16 MC_FLAG)
{
  if((MC->IPR & MC_FLAG) != RESET)
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : MC_ClearFlag
* Description    : Clears the MCs pending flags.
* Input          : MC_FLAG: specifies the flag to clear.
*                  This parameter can be any combination of the following values:
*                         - MC_FLAG_CMPW: Compare W Flag.
*                         - MC_FLAG_CMPV: Compare V Flag.
*                         - MC_FLAG_CMPU: Compare U Flag.
*                         - MC_FLAG_ZPC: Zero of PWM counter Flag.
*                         - MC_FLAG_ADT: Automatic Data Transfer Flag.
*                         - MC_FLAG_OTC: Overflow of Tacho counter Flag.
*                         - MC_FLAG_CPT: Capture of Tacho counter Flag.
*                         - MC_FLAG_CM0: Compare 0 Flag.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ClearFlag(u16 MC_FLAG)
{
/* Clear the corresponding Flag */
  MC->IPR &= ~MC_FLAG;
}

/*******************************************************************************
* Function Name  : MC_GetITStatus
* Description    : Checks whether the MC interrupt has occurred or not.
* Input          : MC_IT: specifies the MC interrupt source to check.
*                  This parameter can be one of the following values:
*                         - MC_IT_CMPW: Compare W Interrupt.
*                         - MC_IT_CMPV: Compare V Interrupt.
*                         - MC_IT_CMPU: Compare U Interrupt.
*                         - MC_IT_ZPC: Zero of PWM counter Interrupt.
*                         - MC_IT_ADT: Automatic Data Transfer Interrupt.
*                         - MC_IT_OTC: Overflow of Tacho counter Interrupt.
*                         - MC_IT_CPT: Capture of Tacho counter Interrupt.
*                         - MC_IT_CM0: Compare 0 Interrupt.
* Output         : None
* Return         : The new state of the MC_IT(SET or RESET).
*******************************************************************************/
ITStatus MC_GetITStatus(u16 MC_IT)
{
  if((MC->IPR & MC_IT) && (MC->IMR & MC_IT))
  {
    return SET;
  }
  else
  {
    return RESET;
  }
}

/*******************************************************************************
* Function Name  : MC_ClearITPendingBit
* Description    : Clears the IMC's interrupt pending bits.
* Input          : MC_IT: specifies the pending bit to clear.
*                  This parameter can be any combination of the following values:
*                         - MC_IT_CMPW: Compare W Interrupt.
*                         - MC_IT_CMPV: Compare V Interrupt.
*                         - MC_IT_CMPU: Compare U Interrupt.
*                         - MC_IT_ZPC: Zero of PWM counter Interrupt.
*                         - MC_IT_ADT: Automatic Data Transfer Interrupt.
*                         - MC_IT_OTC: Overflow of Tacho counter Interrupt.
*                         - MC_IT_CPT: Capture of Tacho counter Interrupt.
*                         - MC_IT_CM0: Compare 0 Interrupt.
* Output         : None
* Return         : None
*******************************************************************************/
void MC_ClearITPendingBit(u16 MC_IT)
{
/* Clear the corresponding interrupt pending bit */ 
  MC->IPR &= ~MC_IT;
}

/******************* (C) COPYRIGHT 2006 STMicroelectronics *****END OF FILE****/
OpenPOWER on IntegriCloud