Rev 196 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
196 | killagreg | 1 | /******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** |
1 | ingob | 2 | * File Name : 91x_mc.c |
3 | * Author : MCD Application Team |
||
196 | killagreg | 4 | * Version : V2.1 |
5 | * Date : 12/22/2008 |
||
6 | * Description : This file provides all the MC firmware functions. |
||
1 | ingob | 7 | ******************************************************************************** |
196 | killagreg | 8 | * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS WITH |
9 | * CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. AS |
||
10 | * A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT |
||
11 | * OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT |
||
12 | * OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION |
||
13 | * CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. |
||
1 | ingob | 14 | *******************************************************************************/ |
15 | |||
196 | killagreg | 16 | |
17 | /* Standard include ----------------------------------------------------------*/ |
||
1 | ingob | 18 | #include "91x_mc.h" |
19 | #include "91x_scu.h" |
||
20 | /* Private typedef -----------------------------------------------------------*/ |
||
21 | /* Private define ------------------------------------------------------------*/ |
||
22 | /* Private macro -------------------------------------------------------------*/ |
||
23 | /* Private variables ---------------------------------------------------------*/ |
||
24 | |||
25 | #define MC_ODCS_Set 0x0001 |
||
26 | #define MC_ODCS_Reset 0x00FE |
||
27 | |||
28 | #define MC_CMS_Set 0x0004 |
||
29 | #define MC_CMS_Reset 0x00FB |
||
30 | |||
31 | #define MC_CPC_Set 0x0008 |
||
32 | #define MC_CTC_Set 0x0010 |
||
33 | |||
34 | #define MC_PCE_Set 0x0020 |
||
35 | #define MC_PCE_Reset 0xFFDF |
||
36 | |||
37 | #define MC_TCE_Set 0x0040 |
||
38 | #define MC_TCE_Reset 0x00BF |
||
39 | |||
40 | #define MC_DTE_Set 0x0080 |
||
41 | #define MC_DTE_Reset 0x007F |
||
42 | |||
43 | #define MC_TCB_Set 0x0004 |
||
44 | #define MC_TCB_Reset 0x00FB |
||
45 | |||
46 | #define MC_STC_Set 0x0008 |
||
47 | |||
48 | #define MC_TES_Set 0x0010 |
||
49 | #define MC_TES_Reset 0x00EF |
||
50 | |||
51 | #define MC_CCPT_Set 0x0020 |
||
52 | #define MC_CCPT_Reset 0x005F |
||
53 | |||
54 | #define MC_DISEST_Set 0x0040 |
||
55 | #define MC_DISEST_Reset 0x003F |
||
56 | |||
57 | #define MC_DTS_Set 0x0001 |
||
58 | #define MC_DTS_Reset 0x00FE |
||
59 | |||
60 | #define MC_SDT_Set 0x0002 |
||
61 | |||
62 | #define MC_C0SE_Set 0x0004 |
||
63 | #define MC_C0SE_Reset 0x00FB |
||
64 | |||
65 | #define MC_CUSE_Set 0x0008 |
||
66 | #define MC_CUSE_Reset 0x00F7 |
||
67 | |||
68 | #define MC_CVSE_Set 0x0010 |
||
69 | #define MC_CVSE_Reset 0x00EF |
||
70 | |||
71 | #define MC_CWSE_Set 0x0020 |
||
72 | #define MC_CWSE_Reset 0x00D0 |
||
73 | |||
74 | #define MC_RSE_Set 0x0040 |
||
75 | #define MC_RSE_Reset 0x00BF |
||
76 | |||
77 | #define MC_GPI_Set 0x0080 |
||
78 | #define MC_GPI_Reset 0x007F |
||
79 | |||
80 | #define MC_PUH_Set 0x0020 |
||
81 | #define MC_PUH_Reset 0x005F |
||
82 | #define MC_PUL_Set 0x0010 |
||
83 | #define MC_PUL_Reset 0x006F |
||
84 | |||
85 | #define MC_PVH_Set 0x0008 |
||
86 | #define MC_PVH_Reset 0x0077 |
||
87 | #define MC_PVL_Set 0x0004 |
||
88 | #define MC_PVL_Reset 0x007B |
||
89 | |||
90 | #define MC_PWH_Set 0x0002 |
||
91 | #define MC_PWH_Reset 0x007D |
||
92 | #define MC_PWL_Set 0x0001 |
||
93 | #define MC_PWL_Reset 0x007E |
||
94 | |||
95 | #define MC_ODS_Set 0x0040 |
||
96 | #define MC_ODS_Reset 0xFF3F |
||
97 | |||
98 | #define MC_ESC_Clear 0x4321 |
||
99 | |||
100 | #define MC_PCR1_TIN_MASK 0xFFFC |
||
101 | #define MC_OPR_Mask 0x0040 |
||
102 | #define MC_UDCS_Mask 0x0002 |
||
196 | killagreg | 103 | |
104 | |||
105 | |||
106 | |||
1 | ingob | 107 | /* Private function prototypes -----------------------------------------------*/ |
108 | /* Private functions ---------------------------------------------------------*/ |
||
196 | killagreg | 109 | |
1 | ingob | 110 | /****************************************************************************** |
111 | * Function Name : MC_DeInit |
||
112 | * Description : Deinitializes MC peripheral registers to their default reset |
||
113 | * values. |
||
114 | * Input : None |
||
115 | * Output : None |
||
116 | * Return : None |
||
117 | *******************************************************************************/ |
||
118 | void MC_DeInit(void) |
||
119 | { |
||
120 | /* MC peripheral is under Reset */ |
||
121 | SCU_APBPeriphReset(__MC, ENABLE); |
||
122 | |||
123 | /* MC peripheral Reset off */ |
||
124 | SCU_APBPeriphReset(__MC, DISABLE); |
||
125 | } |
||
126 | /******************************************************************************* |
||
127 | * Function Name : MC_Init |
||
128 | * Description : Initializes the MC peripheral according to the specified |
||
129 | * parameters in the MC_InitStruct . |
||
130 | * Input : IMC_InitStruct: pointer to a MC_InitTypeDef structure that |
||
131 | * contains the configuration information for the MC peripheral. |
||
132 | * Output : None |
||
133 | * Return : None |
||
134 | *******************************************************************************/ |
||
135 | void MC_Init(MC_InitTypeDef* MC_InitStruct) |
||
136 | { |
||
137 | /* Select the operating Mode */ |
||
138 | if(MC_InitStruct->MC_OperatingMode == MC_SoftwareOperating_Mode) |
||
139 | { |
||
140 | /* Select the Data transfer Mode */ |
||
141 | MC->PCR2 |= MC_DTS_Set; |
||
142 | |||
143 | /* Enable the registers Software Data Transfer */ |
||
144 | MC->PCR2 |= MC_C0SE_Set | MC_CUSE_Set | MC_CVSE_Set | MC_CWSE_Set |
||
145 | |MC_RSE_Set; |
||
146 | |||
147 | } |
||
148 | else |
||
149 | { |
||
150 | /* Select the Data transfer Mode */ |
||
151 | MC->PCR2 &= MC_DTS_Reset; |
||
152 | |||
153 | /* Disable the registers Software Data Transfer */ |
||
154 | MC->PCR2 &= MC_C0SE_Reset | MC_CUSE_Reset | MC_CVSE_Reset | MC_CWSE_Reset |
||
155 | |MC_RSE_Reset; |
||
156 | } |
||
157 | |||
158 | /* Select the MC PWM counter Mode */ |
||
159 | if(MC_InitStruct->MC_PWMMode == MC_PWMZeroCentered_Mode) |
||
160 | { |
||
161 | MC->PCR0 |= MC_CMS_Set; |
||
162 | } |
||
163 | else |
||
164 | { |
||
165 | MC->PCR0 &= MC_CMS_Reset; |
||
166 | } |
||
167 | |||
168 | /* Set the MC PWM counter Prescaler */ |
||
169 | MC->CPRS = MC_InitStruct->MC_Prescaler; |
||
170 | |||
171 | /* Set the MC PWM Period */ |
||
172 | MC->CMP0 = MC_InitStruct->MC_Period; |
||
173 | |||
174 | /* Set the MC PWM Repetition counter */ |
||
175 | MC->REP = MC_InitStruct->MC_RepetitionCounter; |
||
176 | |||
177 | /* Set the Tacho Compare value */ |
||
178 | MC->TCMP = MC_InitStruct->MC_TachoPeriod; |
||
179 | |||
180 | /* Set the Tacho Prescaler value */ |
||
181 | MC->TPRS = MC_InitStruct->MC_TachoPrescaler; |
||
182 | |||
183 | /* Set the MC Tacho Input Polarity */ |
||
184 | MC->PCR1 = (MC->PCR1 & MC_PCR1_TIN_MASK) | MC_InitStruct->MC_TachoPolarity; |
||
185 | |||
186 | /* Set the MC PWM Forced State */ |
||
187 | MC->OPR |= MC_ODS_Set; |
||
188 | MC->OPR = (MC->OPR & MC_OPR_Mask) | MC_InitStruct->MC_ForcedPWMState; |
||
189 | |||
190 | /* Select the Tacho Mode */ |
||
191 | if(MC_InitStruct->MC_TachoMode == MC_TachoOneShot_Mode) |
||
192 | { |
||
193 | MC->PCR1 |= MC_TCB_Set; |
||
194 | } |
||
195 | else |
||
196 | { |
||
197 | MC->PCR1 &= MC_TCB_Reset; |
||
198 | } |
||
199 | |||
200 | /* Select the Tacho Event Mode */ |
||
201 | if(MC_InitStruct->MC_TachoEvent_Mode == MC_TachoEvent_Software_Mode) |
||
202 | { |
||
203 | MC->PCR1 |= MC_TES_Set; |
||
204 | } |
||
205 | else |
||
206 | { |
||
207 | MC->PCR1 &= MC_TES_Reset; |
||
208 | } |
||
209 | |||
210 | /* Enable or disable the emergency input */ |
||
211 | if(MC_InitStruct->MC_Emergency == MC_Emergency_Enable) |
||
212 | { |
||
213 | MC->PCR1 &= MC_DISEST_Reset; |
||
214 | } |
||
215 | else |
||
216 | { |
||
217 | MC->PCR1 |= MC_DISEST_Set; |
||
218 | } |
||
219 | |||
220 | /* Select the complementary Mode */ |
||
221 | if(MC_InitStruct->MC_Complementary == MC_Complementary_Enable) |
||
222 | { |
||
223 | MC->DTG = MC_InitStruct->MC_DeadTime; |
||
224 | MC->PCR0 |= MC_ODCS_Set; |
||
225 | } |
||
226 | else |
||
227 | { |
||
228 | MC->PCR0 &= MC_ODCS_Reset; |
||
229 | } |
||
230 | |||
231 | /* Tacho Mode selection */ |
||
232 | if(MC_InitStruct->MC_TachoMode == MC_TachoOneShot_Mode) |
||
233 | { |
||
234 | MC->PCR1 |= MC_TCB_Set; |
||
235 | } |
||
236 | else |
||
237 | { |
||
238 | MC->PCR1 &= MC_TCB_Reset; |
||
239 | } |
||
240 | |||
241 | switch(MC_InitStruct->MC_Channel) |
||
242 | { |
||
243 | /* Channel U configuration */ |
||
244 | case MC_Channel_U: |
||
245 | { |
||
246 | MC->CMPU = MC_InitStruct->MC_PulseU; |
||
247 | |||
248 | if(MC_InitStruct->MC_PolarityUL == MC_Polarity_Inverted) |
||
249 | { |
||
250 | MC->PSR |= MC_PUL_Set; |
||
251 | } |
||
252 | else |
||
253 | { |
||
254 | MC->PSR &= MC_PUL_Reset; |
||
255 | } |
||
256 | if(MC_InitStruct->MC_PolarityUH == MC_Polarity_Inverted) |
||
257 | { |
||
258 | MC->PSR |= MC_PUH_Set; |
||
259 | } |
||
260 | else |
||
261 | { |
||
262 | MC->PSR &= MC_PUH_Reset; |
||
263 | } |
||
264 | break; |
||
265 | } |
||
266 | |||
267 | /* Channel V configuration */ |
||
268 | case MC_Channel_V: |
||
269 | { |
||
270 | MC->CMPV = MC_InitStruct->MC_PulseV; |
||
271 | |||
272 | if(MC_InitStruct->MC_PolarityVL == MC_Polarity_Inverted) |
||
273 | { |
||
274 | MC->PSR |= MC_PVL_Set; |
||
275 | } |
||
276 | else |
||
277 | { |
||
278 | MC->PSR &= MC_PVL_Reset; |
||
279 | } |
||
280 | if(MC_InitStruct->MC_PolarityVH == MC_Polarity_Inverted) |
||
281 | { |
||
282 | MC->PSR |= MC_PVH_Set; |
||
283 | } |
||
284 | else |
||
285 | { |
||
286 | MC->PSR &= MC_PVH_Reset; |
||
287 | } |
||
288 | break; |
||
289 | } |
||
290 | |||
291 | /* Channel W configuration */ |
||
292 | case MC_Channel_W: |
||
293 | { |
||
294 | MC->CMPW = MC_InitStruct->MC_PulseW; |
||
295 | |||
296 | if(MC_InitStruct->MC_PolarityWL == MC_Polarity_Inverted) |
||
297 | { |
||
298 | MC->PSR |= MC_PWL_Set; |
||
299 | } |
||
300 | else |
||
301 | { |
||
302 | MC->PSR &= MC_PWL_Reset; |
||
303 | } |
||
304 | if(MC_InitStruct->MC_PolarityWH == MC_Polarity_Inverted) |
||
305 | { |
||
306 | MC->PSR |= MC_PWH_Set; |
||
307 | } |
||
308 | else |
||
309 | { |
||
310 | MC->PSR &= MC_PWH_Reset; |
||
311 | } |
||
312 | break; |
||
313 | } |
||
314 | /* All Channel Configuration */ |
||
315 | case MC_Channel_ALL: |
||
316 | { |
||
317 | MC->CMPU = MC_InitStruct->MC_PulseU; |
||
318 | MC->CMPV = MC_InitStruct->MC_PulseV; |
||
319 | MC->CMPW = MC_InitStruct->MC_PulseW; |
||
320 | |||
321 | if(MC_InitStruct->MC_PolarityUL == MC_Polarity_Inverted) |
||
322 | { |
||
323 | MC->PSR |= MC_PUL_Set; |
||
324 | } |
||
325 | else |
||
326 | { |
||
327 | MC->PSR &= MC_PUL_Reset; |
||
328 | } |
||
329 | if(MC_InitStruct->MC_PolarityUH == MC_Polarity_Inverted) |
||
330 | { |
||
331 | MC->PSR |= MC_PUH_Set; |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | MC->PSR &= MC_PUH_Reset; |
||
336 | } |
||
337 | |||
338 | if(MC_InitStruct->MC_PolarityVL == MC_Polarity_Inverted) |
||
339 | { |
||
340 | MC->PSR |= MC_PVL_Set; |
||
341 | } |
||
342 | else |
||
343 | { |
||
344 | MC->PSR &= MC_PVL_Reset; |
||
345 | } |
||
346 | if(MC_InitStruct->MC_PolarityVH == MC_Polarity_Inverted) |
||
347 | { |
||
348 | MC->PSR |= MC_PVH_Set; |
||
349 | } |
||
350 | else |
||
351 | { |
||
352 | MC->PSR &= MC_PVH_Reset; |
||
353 | } |
||
354 | |||
355 | if(MC_InitStruct->MC_PolarityWL == MC_Polarity_Inverted) |
||
356 | { |
||
357 | MC->PSR |= MC_PWL_Set; |
||
358 | } |
||
359 | else |
||
360 | { |
||
361 | MC->PSR &= MC_PWL_Reset; |
||
362 | } |
||
363 | if(MC_InitStruct->MC_PolarityWH == MC_Polarity_Inverted) |
||
364 | { |
||
365 | MC->PSR |= MC_PWH_Set; |
||
366 | } |
||
367 | else |
||
368 | { |
||
369 | MC->PSR &= MC_PWH_Reset; |
||
370 | } |
||
371 | } |
||
372 | default: |
||
373 | break; |
||
374 | } |
||
375 | } |
||
376 | |||
377 | /******************************************************************************* |
||
378 | * Function Name : MC_StructInit |
||
379 | * Description : Fills each MC_InitStruct member with its default value. |
||
380 | * Input : MC_InitStruct : pointer to a MC_InitTypeDef structure which |
||
381 | * will be initialized. |
||
382 | * Output : None |
||
383 | * Return : None. |
||
384 | *******************************************************************************/ |
||
385 | void MC_StructInit(MC_InitTypeDef* MC_InitStruct) |
||
386 | { |
||
196 | killagreg | 387 | |
388 | u16 temp; |
||
389 | temp=MC->ECR; |
||
390 | temp &= MC_PWM_Counter ; |
||
391 | |||
1 | ingob | 392 | MC_InitStruct->MC_OperatingMode = MC_HardwareOperating_Mode; |
393 | MC_InitStruct->MC_TachoMode = MC_TachoContinuous_Mode; |
||
394 | MC_InitStruct->MC_TachoEvent_Mode = MC_TachoEvent_Hardware_Mode; |
||
395 | MC_InitStruct->MC_Prescaler = 0x00; |
||
396 | MC_InitStruct->MC_TachoPrescaler = 0x0000; |
||
397 | MC_InitStruct->MC_PWMMode = MC_PWMClassical_Mode; |
||
398 | MC_InitStruct->MC_Complementary = MC_Complementary_Enable; |
||
399 | MC_InitStruct->MC_Emergency = MC_Emergency_Disable; |
||
400 | MC_InitStruct->MC_ForcedPWMState = 0x003F; |
||
401 | MC_InitStruct->MC_Period = 0x0000; |
||
402 | MC_InitStruct->MC_TachoPeriod = 0x00FF; |
||
403 | MC_InitStruct->MC_Channel = MC_Channel_ALL; |
||
404 | MC_InitStruct->MC_PulseU = 0x0000; |
||
405 | MC_InitStruct->MC_PulseV = 0x0000; |
||
406 | MC_InitStruct->MC_PulseW = 0x0000; |
||
407 | MC_InitStruct->MC_PolarityUL = MC_Polarity_NonInverted; |
||
408 | MC_InitStruct->MC_PolarityUH = MC_Polarity_NonInverted; |
||
409 | MC_InitStruct->MC_PolarityVL = MC_Polarity_NonInverted; |
||
410 | MC_InitStruct->MC_PolarityVH = MC_Polarity_NonInverted; |
||
411 | MC_InitStruct->MC_PolarityWL = MC_Polarity_NonInverted; |
||
412 | MC_InitStruct->MC_PolarityWH = MC_Polarity_NonInverted; |
||
413 | MC_InitStruct->MC_TachoPolarity = MC_TachoEventEdge_RisingFalling; |
||
196 | killagreg | 414 | |
415 | if(temp==0) |
||
416 | { |
||
417 | MC_InitStruct->MC_DeadTime = 0x003F; |
||
418 | } |
||
419 | else |
||
420 | { |
||
421 | MC_InitStruct->MC_DeadTime = 0x03FF; |
||
422 | } |
||
423 | |||
424 | MC_InitStruct->MC_RepetitionCounter = 0x0000; |
||
1 | ingob | 425 | } |
426 | |||
427 | /******************************************************************************* |
||
428 | * Function Name : MC_Cmd |
||
429 | * Description : Enables or disables the MC peripheral. |
||
430 | * Input : Newstate: new state of the MC peripheral. |
||
431 | * This parameter can be: ENABLE or DISABLE. |
||
432 | * Output : None |
||
433 | * Return : None |
||
434 | *******************************************************************************/ |
||
435 | void MC_Cmd(FunctionalState NewState) |
||
436 | { |
||
437 | if(NewState == ENABLE) |
||
438 | { |
||
439 | /* Enable the PWM counter */ |
||
440 | MC->PCR0 |= MC_PCE_Set; |
||
441 | |||
442 | /* Enable the Tacho counter */ |
||
443 | MC->PCR0 |= MC_TCE_Set; |
||
444 | |||
445 | /* Enable the Dead Time counter */ |
||
446 | MC->PCR0 |= MC_DTE_Set; |
||
447 | } |
||
448 | else |
||
449 | { |
||
450 | /* Disable the PWM counter */ |
||
451 | MC->PCR0 &= MC_PCE_Reset; |
||
452 | |||
453 | /* Disable the Tacho counter */ |
||
454 | MC->PCR0 &= MC_TCE_Reset; |
||
455 | |||
456 | /* Disable the Dead counter */ |
||
457 | MC->PCR0 &= MC_DTE_Reset; |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /******************************************************************************* |
||
462 | * Function Name : MC_ClearPWMCounter |
||
463 | * Description : Clears the MC PWM counter. |
||
464 | * Input : None |
||
465 | * Output : None |
||
466 | * Return : None |
||
467 | *******************************************************************************/ |
||
468 | void MC_ClearPWMCounter(void) |
||
469 | { |
||
470 | /* Clear the PWM counter */ |
||
471 | MC->PCR0 |= MC_CPC_Set; |
||
472 | } |
||
473 | |||
474 | /******************************************************************************* |
||
475 | * Function Name : MC_ClearTachoCounter |
||
476 | * Description : Clears the MC Tacho counter. |
||
477 | * Input : None |
||
478 | * Output : None |
||
479 | * Return : None |
||
480 | *******************************************************************************/ |
||
481 | void MC_ClearTachoCounter(void) |
||
482 | { |
||
483 | /* Clear the Tacho counter */ |
||
484 | MC->PCR0 |= MC_CTC_Set; |
||
485 | } |
||
486 | |||
487 | /******************************************************************************* |
||
488 | * Function Name : MC_CtrlPWMOutputs |
||
489 | * Description : Enables or disables MC peripheral Main Outputs. |
||
490 | * Input : Newstate: new state of the MC peripheral Main Outputs. |
||
491 | * This parameter can be: ENABLE or DISABLE. |
||
492 | * Output : None |
||
493 | * Return : None |
||
494 | *******************************************************************************/ |
||
495 | void MC_CtrlPWMOutputs(FunctionalState Newstate) |
||
496 | { |
||
497 | if(Newstate == ENABLE) |
||
498 | { |
||
499 | /* Enable the dead time generator data */ |
||
500 | MC->OPR &= MC_ODS_Reset; |
||
501 | } |
||
502 | else |
||
503 | { |
||
504 | /* Enable the default state data */ |
||
505 | MC->OPR |= MC_ODS_Set; |
||
506 | } |
||
507 | } |
||
508 | |||
509 | /******************************************************************************* |
||
510 | * Function Name : MC_ITConfig |
||
511 | * Description : Enables or disables the MC interrupts. |
||
512 | * Input : - MC_IT: specifies the MC interrupts sources to be enabled |
||
513 | * or disabled. |
||
514 | * This parameter can be any combination of the following values: |
||
515 | * - MC_IT_CMPW: Compare W Interrupt. |
||
516 | * - MC_IT_CMPV: Compare V Interrupt. |
||
517 | * - MC_IT_CMPU: Compare U Interrupt. |
||
518 | * - MC_IT_ZPC: Zero of PWM counter Interrupt. |
||
519 | * - MC_IT_ADT: Automatic Data Transfer Interrupt. |
||
520 | * - MC_IT_OTC: Overflow of Tacho counter Interrupt. |
||
521 | * - MC_IT_CPT: Capture of Tacho counter Interrupt. |
||
522 | * - MC_IT_CM0: Compare 0 Interrupt. |
||
523 | * - Newstate: new state of IMC interrupts. |
||
524 | * This parameter can be: ENABLE or DISABLE. |
||
525 | * Output : None |
||
526 | * Return : None |
||
527 | *******************************************************************************/ |
||
528 | void MC_ITConfig(u16 MC_IT, FunctionalState NewState) |
||
529 | { |
||
530 | if(NewState == ENABLE) |
||
531 | { |
||
532 | /* Enable the specific interrupt source */ |
||
533 | MC->IMR |= MC_IT; |
||
534 | |||
535 | /* Enable the global peripheral interrupt sources */ |
||
536 | MC->PCR2 |= MC_GPI_Set; |
||
537 | } |
||
538 | else |
||
539 | { |
||
540 | /* Disable the specific interrupt source */ |
||
541 | MC->IMR &= ~MC_IT; |
||
542 | |||
543 | /* Disable the global peripheral interrupt sources */ |
||
544 | MC->PCR2 &= MC_GPI_Reset; |
||
545 | } |
||
546 | } |
||
547 | |||
548 | /******************************************************************************* |
||
549 | * Function Name : MC_SetPrescaler |
||
550 | * Description : Sets the MC prescaler value. |
||
551 | * Input : MC_Prescaler: MC prescaler new value. |
||
552 | * Output : None |
||
553 | * Return : None |
||
554 | *******************************************************************************/ |
||
555 | void MC_SetPrescaler(u8 MC_Prescaler) |
||
556 | { |
||
557 | /* Set the Prescaler Register value */ |
||
558 | MC->CPRS = MC_Prescaler; |
||
559 | } |
||
560 | |||
561 | /******************************************************************************* |
||
562 | * Function Name : MC_SetPeriod |
||
563 | * Description : Sets the MC period value. |
||
564 | * Input : MC_Period: MC period new value. |
||
565 | * Output : None |
||
566 | * Return : None |
||
567 | *******************************************************************************/ |
||
568 | void MC_SetPeriod(u16 MC_Period) |
||
569 | { |
||
570 | /* Set the Period Register value */ |
||
571 | MC->CMP0 = MC_Period; |
||
572 | } |
||
573 | |||
574 | /******************************************************************************* |
||
575 | * Function Name : MC_SetPulseU |
||
576 | * Description : Sets the MC pulse U value. |
||
577 | * Input : MC_PulseU: MC pulse U new value. |
||
578 | * Output : None |
||
579 | * Return : None |
||
580 | *******************************************************************************/ |
||
581 | void MC_SetPulseU(u16 MC_PulseU) |
||
582 | { |
||
583 | /* Set the Pulse U Register value */ |
||
584 | MC->CMPU = MC_PulseU; |
||
585 | } |
||
586 | |||
587 | /******************************************************************************* |
||
588 | * Function Name : MC_SetPulseV |
||
589 | * Description : Sets the MC pulse V value. |
||
590 | * Input : MC_PulseV: MC pulse V new value. |
||
591 | * Output : None |
||
592 | * Return : None |
||
593 | *******************************************************************************/ |
||
594 | void MC_SetPulseV(u16 MC_PulseV) |
||
595 | { |
||
596 | /* Set the Pulse V Register value */ |
||
597 | MC->CMPV = MC_PulseV; |
||
598 | } |
||
599 | |||
600 | /******************************************************************************* |
||
601 | * Function Name : MC_SetPulseW |
||
602 | * Description : Sets the MC pulse W value. |
||
603 | * Input : MC_PulseW: MC pulse W new value. |
||
604 | * Output : None |
||
605 | * Return : None |
||
606 | *******************************************************************************/ |
||
607 | void MC_SetPulseW(u16 MC_PulseW) |
||
608 | { |
||
609 | /* Set the Pulse W Register value */ |
||
610 | MC->CMPW = MC_PulseW; |
||
611 | } |
||
612 | |||
613 | /******************************************************************************* |
||
614 | * Function Name : MC_PWMModeConfig |
||
615 | * Description : Selects the MC PWM counter Mode. |
||
616 | * Input : MC_PWMMode: MC PWM counter Mode. |
||
617 | * Output : None |
||
618 | * Return : None |
||
619 | *******************************************************************************/ |
||
620 | void MC_PWMModeConfig(u16 MC_PWMMode) |
||
621 | { |
||
622 | /* Select the MC PWM counter Mode */ |
||
623 | if(MC_PWMMode == MC_PWMZeroCentered_Mode) |
||
624 | { |
||
625 | MC->PCR0 |= MC_CMS_Set; |
||
626 | } |
||
627 | else |
||
628 | { |
||
629 | MC->PCR0 &= MC_CMS_Reset; |
||
630 | } |
||
631 | } |
||
632 | |||
633 | /******************************************************************************* |
||
634 | * Function Name : MC_SetDeadTime |
||
635 | * Description : Sets the MC dead time value. |
||
636 | * Input : MC_DeadTime: MC dead time new value. |
||
637 | * Output : None |
||
638 | * Return : None |
||
639 | *******************************************************************************/ |
||
640 | void MC_SetDeadTime(u16 MC_DeadTime) |
||
641 | { |
||
642 | /* Set the dead time Register value */ |
||
643 | MC->DTG = MC_DeadTime; |
||
644 | } |
||
645 | |||
646 | /******************************************************************************* |
||
647 | * Function Name : MC_SetTachoCompare |
||
648 | * Description : Sets the MC Tacho Compare Register value. |
||
649 | * Input : MC_Compare: MC Tacho compare new value. |
||
650 | * Output : None |
||
651 | * Return : None |
||
652 | *******************************************************************************/ |
||
653 | void MC_SetTachoCompare(u8 MC_Compare) |
||
654 | { |
||
655 | /* Sets the Tacho Compare Register value */ |
||
656 | MC->TCMP = MC_Compare; |
||
657 | } |
||
658 | /******************************************************************************* |
||
659 | * Function Name : MC_EmergencyCmd |
||
660 | * Description : Enables or disables the MC emergency feauture. |
||
661 | * Input : Newstate: new state of the MC peripheral Emergency. |
||
662 | * This parameter can be: ENABLE or DISABLE. |
||
663 | * Output : None |
||
664 | * Return : None |
||
665 | *******************************************************************************/ |
||
666 | void MC_EmergencyCmd(FunctionalState NewState) |
||
667 | { |
||
668 | if(NewState == ENABLE) |
||
669 | { |
||
670 | /* Reset the DISEST Bit in the PCR1 Register to enable the emergency stop input */ |
||
671 | MC->PCR1 &= MC_DISEST_Reset; |
||
672 | } |
||
673 | else |
||
674 | { |
||
675 | /* Set the DISEST Bit in the PCR1 Register to disable the emergency stop input */ |
||
676 | MC->PCR1 |= MC_DISEST_Reset; |
||
677 | } |
||
678 | } |
||
679 | |||
680 | /******************************************************************************* |
||
681 | * Function Name : MC_EmergencyClear |
||
682 | * Description : Clears the MC Emergency Register. |
||
683 | * Input : None |
||
684 | * Output : None |
||
685 | * Return : None |
||
686 | *******************************************************************************/ |
||
687 | void MC_EmergencyClear(void) |
||
688 | { |
||
196 | killagreg | 689 | MC->ECR |= 0x0100; |
690 | |||
691 | /* Clear EST bit */ |
||
692 | MC->ESC = MC_ESC_Clear; |
||
1 | ingob | 693 | } |
694 | |||
695 | /******************************************************************************* |
||
696 | * Function Name : MC_GetPeriod |
||
697 | * Description : Gets the MC period value. |
||
698 | * Input : None |
||
699 | * Output : None |
||
700 | * Return : MC period value. |
||
701 | *******************************************************************************/ |
||
702 | u16 MC_GetPeriod(void) |
||
703 | { |
||
704 | /* Return the PWM signal period value */ |
||
705 | return MC->CMP0; |
||
706 | } |
||
707 | |||
708 | /******************************************************************************* |
||
709 | * Function Name : MC_GetPulseU |
||
710 | * Description : Gets the MC pulse U value. |
||
711 | * Input : None |
||
712 | * Output : None |
||
713 | * Return : MC pulse U value. |
||
714 | *******************************************************************************/ |
||
715 | u16 MC_GetPulseU(void) |
||
716 | { |
||
717 | /* Return the PWM pulse U Register value */ |
||
718 | return MC->CMPU; |
||
719 | } |
||
720 | |||
721 | /******************************************************************************* |
||
722 | * Function Name : MC_GetPulseV |
||
723 | * Description : Gets the MC pulse V value. |
||
724 | * Input : None |
||
725 | * Output : None |
||
726 | * Return : MC pulse V value. |
||
727 | *******************************************************************************/ |
||
728 | u16 MC_GetPulseV(void) |
||
729 | { |
||
730 | /* Return the PWM pulse V Register value */ |
||
731 | return MC->CMPV; |
||
732 | } |
||
733 | |||
734 | /******************************************************************************* |
||
735 | * Function Name : MC_GetPulseW |
||
736 | * Description : Gets the MC pulse W value. |
||
737 | * Input : None |
||
738 | * Output : None |
||
739 | * Return : MC pulse W value. |
||
740 | *******************************************************************************/ |
||
741 | u16 MC_GetPulseW(void) |
||
742 | { |
||
743 | /* Return the PWM pulse W Register value */ |
||
744 | return MC->CMPW; |
||
745 | } |
||
746 | |||
747 | /******************************************************************************* |
||
748 | * Function Name : MC_GetTachoCapture |
||
749 | * Description : Gets the MC Tacho period value. |
||
750 | * Input : None |
||
751 | * Output : None |
||
752 | * Return : MC Tacho capture value. |
||
753 | *******************************************************************************/ |
||
754 | u16 MC_GetTachoCapture(void) |
||
755 | { |
||
756 | /* Return the Tacho Capture Register value */ |
||
757 | return MC->TCPT; |
||
758 | } |
||
759 | |||
760 | /******************************************************************************* |
||
761 | * Function Name : MC_ClearOnTachoCapture |
||
762 | * Description : Enables or disables the the Clear on capture of tacho counter. |
||
763 | * Input : Newstate: new state of the CCPT bit. |
||
764 | * This parameter can be: ENABLE or DISABLE. |
||
765 | * Output : None |
||
766 | * Return : None |
||
767 | *******************************************************************************/ |
||
768 | void MC_ClearOnTachoCapture(FunctionalState NewState) |
||
769 | { |
||
770 | if(NewState == ENABLE) |
||
771 | { |
||
772 | /* Enable the Clear on capture of tacho counter */ |
||
773 | MC->PCR1 |= MC_CCPT_Set; |
||
774 | } |
||
775 | else |
||
776 | { |
||
777 | /* Disable the Clear on capture of tacho counter */ |
||
778 | MC->PCR1 &= MC_CCPT_Reset; |
||
779 | } |
||
780 | } |
||
781 | /******************************************************************************* |
||
782 | * Function Name : MC_ForceDataTransfer |
||
783 | * Description : Sets the MC Outputs default states. |
||
784 | * Input : MC_ForcedData: MC outputs new states. |
||
785 | * Output : None |
||
786 | * Return : None |
||
787 | *******************************************************************************/ |
||
788 | void MC_ForceDataTransfer(u8 MC_ForcedData) |
||
789 | { |
||
790 | /* Set the MC PWM Forced State */ |
||
791 | MC->OPR |= MC_ODS_Set; |
||
792 | MC->OPR = (MC->OPR & MC_OPR_Mask) | MC_ForcedData; |
||
793 | } |
||
794 | |||
795 | /******************************************************************************* |
||
796 | * Function Name : MC_PreloadConfig |
||
797 | * Description : Enables the Software Data Transfer. |
||
798 | * Input : None |
||
799 | * Output : None |
||
800 | * Return : None |
||
801 | *******************************************************************************/ |
||
802 | void MC_SoftwarePreloadConfig(void) |
||
803 | { |
||
804 | /* Set the SDT: Software Data Transfer bit */ |
||
805 | MC->PCR2 |= MC_SDT_Set; |
||
806 | } |
||
807 | |||
808 | /******************************************************************************* |
||
809 | * Function Name : MC_SoftwareTachoCapture |
||
810 | * Description : Enables the Software Tacho Capture. |
||
811 | * Input : None |
||
812 | * Output : None |
||
813 | * Return : None |
||
814 | *******************************************************************************/ |
||
815 | void MC_SoftwareTachoCapture(void) |
||
816 | { |
||
817 | /* Set the STC: Software Tacho Capture bit */ |
||
818 | MC->PCR1 |= MC_STC_Set; |
||
819 | } |
||
820 | |||
821 | /******************************************************************************* |
||
822 | * Function Name : MC_GetCountingStatus |
||
823 | * Description : Checks whether the PWM Counter is counting Up or Down. |
||
824 | * Input : None |
||
825 | * Output : None |
||
826 | * Return : The new state of the PWM Counter(DOWN or UP). |
||
827 | *******************************************************************************/ |
||
828 | CountingStatus MC_GetCountingStatus(void) |
||
829 | { |
||
830 | if((MC->PCR0 & MC_UDCS_Mask) != DOWN) |
||
831 | { |
||
832 | return UP; |
||
833 | } |
||
834 | else |
||
835 | { |
||
836 | return DOWN; |
||
837 | } |
||
838 | } |
||
839 | |||
840 | /******************************************************************************* |
||
841 | * Function Name : MC_GetFlagStatus |
||
842 | * Description : Checks whether the specified MC flag is set or not. |
||
843 | * Input : MC_FLAG: specifies the flag to check. |
||
844 | * This parameter can be one of the following values: |
||
845 | * - MC_FLAG_CMPW: Compare W Flag. |
||
846 | * - MC_FLAG_CMPV: Compare V Flag. |
||
847 | * - MC_FLAG_CMPU: Compare U Flag. |
||
848 | * - MC_FLAG_ZPC: Zero of PWM counter Flag. |
||
849 | * - MC_FLAG_ADT: Automatic Data Transfer Flag. |
||
850 | * - MC_FLAG_OTC: Overflow of Tacho counter Flag. |
||
851 | * - MC_FLAG_CPT: Capture of Tacho counter Flag. |
||
852 | * - MC_FLAG_CM0: Compare 0 Flag. |
||
853 | * - MC_FLAG_EST: Emergency Stop Flag. |
||
854 | * Output : None |
||
855 | * Return : The new state of the MC_FLAG(SET or RESET). |
||
856 | *******************************************************************************/ |
||
857 | FlagStatus MC_GetFlagStatus(u16 MC_FLAG) |
||
858 | { |
||
859 | if((MC->IPR & MC_FLAG) != RESET) |
||
860 | { |
||
861 | return SET; |
||
862 | } |
||
863 | else |
||
864 | { |
||
865 | return RESET; |
||
866 | } |
||
867 | } |
||
868 | |||
869 | /******************************************************************************* |
||
870 | * Function Name : MC_ClearFlag |
||
871 | * Description : Clears the MC’s pending flags. |
||
872 | * Input : MC_FLAG: specifies the flag to clear. |
||
873 | * This parameter can be any combination of the following values: |
||
874 | * - MC_FLAG_CMPW: Compare W Flag. |
||
875 | * - MC_FLAG_CMPV: Compare V Flag. |
||
876 | * - MC_FLAG_CMPU: Compare U Flag. |
||
877 | * - MC_FLAG_ZPC: Zero of PWM counter Flag. |
||
878 | * - MC_FLAG_ADT: Automatic Data Transfer Flag. |
||
879 | * - MC_FLAG_OTC: Overflow of Tacho counter Flag. |
||
880 | * - MC_FLAG_CPT: Capture of Tacho counter Flag. |
||
881 | * - MC_FLAG_CM0: Compare 0 Flag. |
||
882 | * Output : None |
||
883 | * Return : None |
||
884 | *******************************************************************************/ |
||
885 | void MC_ClearFlag(u16 MC_FLAG) |
||
886 | { |
||
887 | /* Clear the corresponding Flag */ |
||
888 | MC->IPR &= ~MC_FLAG; |
||
889 | } |
||
890 | |||
891 | /******************************************************************************* |
||
892 | * Function Name : MC_GetITStatus |
||
893 | * Description : Checks whether the MC interrupt has occurred or not. |
||
894 | * Input : MC_IT: specifies the MC interrupt source to check. |
||
895 | * This parameter can be one of the following values: |
||
896 | * - MC_IT_CMPW: Compare W Interrupt. |
||
897 | * - MC_IT_CMPV: Compare V Interrupt. |
||
898 | * - MC_IT_CMPU: Compare U Interrupt. |
||
899 | * - MC_IT_ZPC: Zero of PWM counter Interrupt. |
||
900 | * - MC_IT_ADT: Automatic Data Transfer Interrupt. |
||
901 | * - MC_IT_OTC: Overflow of Tacho counter Interrupt. |
||
902 | * - MC_IT_CPT: Capture of Tacho counter Interrupt. |
||
903 | * - MC_IT_CM0: Compare 0 Interrupt. |
||
904 | * Output : None |
||
905 | * Return : The new state of the MC_IT(SET or RESET). |
||
906 | *******************************************************************************/ |
||
907 | ITStatus MC_GetITStatus(u16 MC_IT) |
||
908 | { |
||
909 | if((MC->IPR & MC_IT) && (MC->IMR & MC_IT)) |
||
910 | { |
||
911 | return SET; |
||
912 | } |
||
913 | else |
||
914 | { |
||
915 | return RESET; |
||
916 | } |
||
917 | } |
||
918 | |||
919 | /******************************************************************************* |
||
920 | * Function Name : MC_ClearITPendingBit |
||
921 | * Description : Clears the IMC's interrupt pending bits. |
||
922 | * Input : MC_IT: specifies the pending bit to clear. |
||
923 | * This parameter can be any combination of the following values: |
||
924 | * - MC_IT_CMPW: Compare W Interrupt. |
||
925 | * - MC_IT_CMPV: Compare V Interrupt. |
||
926 | * - MC_IT_CMPU: Compare U Interrupt. |
||
927 | * - MC_IT_ZPC: Zero of PWM counter Interrupt. |
||
928 | * - MC_IT_ADT: Automatic Data Transfer Interrupt. |
||
929 | * - MC_IT_OTC: Overflow of Tacho counter Interrupt. |
||
930 | * - MC_IT_CPT: Capture of Tacho counter Interrupt. |
||
931 | * - MC_IT_CM0: Compare 0 Interrupt. |
||
932 | * Output : None |
||
933 | * Return : None |
||
934 | *******************************************************************************/ |
||
935 | void MC_ClearITPendingBit(u16 MC_IT) |
||
936 | { |
||
937 | /* Clear the corresponding interrupt pending bit */ |
||
938 | MC->IPR &= ~MC_IT; |
||
939 | } |
||
940 | |||
196 | killagreg | 941 | |
942 | /******************************************************************************* |
||
943 | * Function Name : MC_Lock |
||
944 | * Description : Enables the lock of certain control register bits |
||
945 | * Input : - MC_LockLevel: Specifies the level to be locked. |
||
946 | * This parameter can be any combination of the following values: |
||
947 | * - MC_LockLevel4: Lock Dead Time Generator register. |
||
948 | * - MC_LockLevel3: Lock Output Peripheral Register. |
||
949 | * - MC_LockLevel2: Lock phase polarity bits. |
||
950 | * - MC_LockLevel1: Lock Emergency Stop Disable bit. |
||
951 | * - MC_LockLevel0: Dead Time Enable and Output Dead Time |
||
952 | * counter Selection bits. |
||
953 | * Output : None |
||
954 | * Return : None |
||
955 | *******************************************************************************/ |
||
956 | void MC_Lock(u16 MC_LockLevel) |
||
957 | { |
||
958 | MC->LOK &= ~MC_LockLevel; |
||
959 | MC->LOK |= MC_LockLevel; |
||
960 | } |
||
961 | |||
962 | /****************************************************************************** |
||
963 | * Function Name : MC_CounterModeConfig |
||
964 | * Description : Enables the 10 bits mode for the dead time counter or enables |
||
965 | * the 16 bits mode for the PWM counter. |
||
966 | * Input : - MC_Counter : Specifies the counter |
||
967 | * This parameter can be any combination of the following values: |
||
968 | * - MC_DT_Counter : Dead Time Counter is in 10 bits mode. |
||
969 | * - MC_PWM_Counter : PWM_Counter is in 16 bits mode. |
||
970 | * Output : None |
||
971 | * Return : None |
||
972 | *******************************************************************************/ |
||
973 | void MC_CounterModeConfig(u16 MC_Counter) |
||
974 | { |
||
975 | MC->ECR &= ~MC_Counter; |
||
976 | MC->ECR |= MC_Counter; |
||
977 | } |
||
978 | |||
979 | /******************************************************************************* |
||
980 | * Function Name : MC_DoubleUpdateMode |
||
981 | * Description : Enables or disables the Double Update Mode for the MC |
||
982 | * Input : - Newstate: New state of the double update mode. |
||
983 | * This parameter can be: ENABLE or DISABLE. |
||
984 | * Output : None |
||
985 | * Return : None |
||
986 | *******************************************************************************/ |
||
987 | void MC_DoubleUpdateMode(FunctionalState NewState) |
||
988 | { |
||
989 | if( NewState==ENABLE) |
||
990 | { |
||
991 | MC->ECR |= MC_DUM; |
||
992 | } |
||
993 | else |
||
994 | { |
||
995 | MC->ECR &= ~MC_DUM; |
||
996 | } |
||
997 | } |
||
998 | |||
999 | /******************************************************************************* |
||
1000 | * Function Name : MC_ADCTrigger |
||
1001 | * Description : Enables or disables the Triggers to the ADC conversion |
||
1002 | * Input : IMC event : The IMC event to trigger the ADC conversion |
||
1003 | * This parameter can be one of the following values: |
||
1004 | * - MC_ZPC : When the PWM counter reaches zero. |
||
1005 | * - MC_CM0 : When the PWM counter reaches its maximum |
||
1006 | * count. |
||
1007 | * - MC_ADT : When the PWM counter equals zero and the |
||
1008 | * Repetition Down counter equals zero. |
||
1009 | * - Newstate: New state of the ADC trigger event. |
||
1010 | * This parameter can be: ENABLE or DISABLE. |
||
1011 | * Output : None |
||
1012 | * Return : None |
||
1013 | *******************************************************************************/ |
||
1014 | void MC_ADCTrigger(u16 IMC_Event, FunctionalState NewState) |
||
1015 | { |
||
1016 | MC->ECR &= 0x01F3; |
||
1017 | |||
1018 | if( NewState==ENABLE) |
||
1019 | { |
||
1020 | MC->ECR |= IMC_Event; |
||
1021 | } |
||
1022 | else |
||
1023 | { |
||
1024 | MC->ECR &= ~IMC_Event; |
||
1025 | } |
||
1026 | } |
||
1027 | |||
1028 | |||
1029 | /******************************************************************************* |
||
1030 | * Function Name : MC_EnhancedStop |
||
1031 | * Description : Enables or disables an Enhanced Motor Stop feature. |
||
1032 | * Input : NewState : This pararameter can be ENABLE or DISABLE. |
||
1033 | * Output : None |
||
1034 | * Return : None |
||
1035 | *******************************************************************************/ |
||
1036 | void MC_EnhancedStop(FunctionalState NewState) |
||
1037 | { |
||
1038 | if( NewState==ENABLE) |
||
1039 | { |
||
1040 | MC->ECR |= 0x0040; |
||
1041 | } |
||
1042 | else |
||
1043 | { |
||
1044 | MC->ECR &= ~ 0x0040; |
||
1045 | } |
||
1046 | } |
||
1047 | /******************************************************************************* |
||
1048 | * Function Name : MC_DebugOutputProtection |
||
1049 | * Description : Allows the output phases to follow the polarity set by PSR if |
||
1050 | * enabled or they remain in their last known state if disabled. |
||
1051 | * Input : NewState : This pararameter can be ENABLE or DISABLE. |
||
1052 | * Output : None |
||
1053 | * Return : None |
||
1054 | *******************************************************************************/ |
||
1055 | void MC_DebugOutputProtection(FunctionalState NewState) |
||
1056 | { |
||
1057 | if( NewState==ENABLE) |
||
1058 | { |
||
1059 | MC->ECR |= 0x0080; |
||
1060 | } |
||
1061 | else |
||
1062 | { |
||
1063 | MC->ECR &= ~0x0080; |
||
1064 | } |
||
1065 | } |
||
1066 | |||
1067 | /******************************************************************************* |
||
1068 | * Function Name : MC_EmergencyStopPolarity |
||
1069 | * Description : Enables or disables an Enhanced Stop Polarity feature. |
||
1070 | * Input : NewState : This pararameter can be ENABLE or DISABLE. |
||
1071 | * Output : None |
||
1072 | * Return : None |
||
1073 | *******************************************************************************/ |
||
1074 | void MC_EmergencyStopPolarity(FunctionalState NewState) |
||
1075 | { |
||
1076 | if( NewState==ENABLE) |
||
1077 | { |
||
1078 | MC->ECR |= 0x0002; |
||
1079 | } |
||
1080 | else |
||
1081 | { |
||
1082 | MC->ECR &= ~0x0002; |
||
1083 | } |
||
1084 | } |
||
1085 | /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ |