PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
bt.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
52 /******************************************************************************/
53 /* Include files */
54 /******************************************************************************/
55 #include "bt.h"
56 
57 #if (defined(PDL_PERIPHERAL_BT_ACTIVE))
58 
64 
65 /******************************************************************************/
66 /* Local pre-processor symbols/macros ('#define') */
67 /******************************************************************************/
68 #define BT_INSTANCE_COUNT (uint32_t)(sizeof(m_astcBtInstanceDataLut) / sizeof(m_astcBtInstanceDataLut[0]))
69 
70 /******************************************************************************/
71 /* Global variable definitions (declared in header file with 'extern') */
72 /******************************************************************************/
75 {
76 #if (PDL_PERIPHERAL_ENABLE_BT0 == PDL_ON)
77  {
78  &BT0, // pstcInstance
79  NULL // stcInternData (not initialized yet)
80  },
81 #endif
82 #if (PDL_PERIPHERAL_ENABLE_BT1 == PDL_ON)
83  {
84  &BT1, // pstcInstance
85  NULL // stcInternData (not initialized yet)
86  },
87 #endif
88 #if (PDL_PERIPHERAL_ENABLE_BT2 == PDL_ON)
89  {
90  &BT2, // pstcInstance
91  NULL // stcInternData (not initialized yet)
92  },
93 #endif
94 #if (PDL_PERIPHERAL_ENABLE_BT3 == PDL_ON)
95  {
96  &BT3, // pstcInstance
97  NULL // stcInternData (not initialized yet)
98  },
99 #endif
100 #if (PDL_PERIPHERAL_ENABLE_BT4 == PDL_ON)
101  {
102  &BT4, // pstcInstance
103  NULL // stcInternData (not initialized yet)
104  },
105 #endif
106 #if (PDL_PERIPHERAL_ENABLE_BT5 == PDL_ON)
107  {
108  &BT5, // pstcInstance
109  NULL // stcInternData (not initialized yet)
110  },
111 #endif
112 #if (PDL_PERIPHERAL_ENABLE_BT6 == PDL_ON)
113  {
114  &BT6, // pstcInstance
115  NULL // stcInternData (not initialized yet)
116  },
117 #endif
118 #if (PDL_PERIPHERAL_ENABLE_BT7 == PDL_ON)
119  {
120  &BT7, // pstcInstance
121  NULL // stcInternData (not initialized yet)
122  },
123 #endif
124 #if (PDL_PERIPHERAL_ENABLE_BT8 == PDL_ON)
125  {
126  &BT8, // pstcInstance
127  NULL // stcInternData (not initialized yet)
128  },
129 #endif
130 #if (PDL_PERIPHERAL_ENABLE_BT9 == PDL_ON)
131  {
132  &BT9, // pstcInstance
133  NULL // stcInternData (not initialized yet)
134  },
135 #endif
136 #if (PDL_PERIPHERAL_ENABLE_BT10 == PDL_ON)
137  {
138  &BT10, // pstcInstance
139  NULL // stcInternData (not initialized yet)
140  },
141 #endif
142 #if (PDL_PERIPHERAL_ENABLE_BT11 == PDL_ON)
143  {
144  &BT11, // pstcInstance
145  NULL // stcInternData (not initialized yet)
146  },
147 #endif
148 #if (PDL_PERIPHERAL_ENABLE_BT12 == PDL_ON)
149  {
150  &BT12, // pstcInstance
151  NULL // stcInternData (not initialized yet)
152  },
153 #endif
154 #if (PDL_PERIPHERAL_ENABLE_BT13 == PDL_ON)
155  {
156  &BT13, // pstcInstance
157  NULL // stcInternData (not initialized yet)
158  },
159 #endif
160 #if (PDL_PERIPHERAL_ENABLE_BT14 == PDL_ON)
161  {
162  &BT14, // pstcInstance
163  NULL // stcInternData (not initialized yet)
164  },
165 #endif
166 #if (PDL_PERIPHERAL_ENABLE_BT15 == PDL_ON)
167  {
168  &BT15, // pstcInstance
169  NULL // stcInternData (not initialized yet)
170  },
171 #endif
172 };
173 
184 {
185  uint32_t u32Instance;
186 
187  for (u32Instance = 0; u32Instance < BT_INSTANCE_COUNT; u32Instance++)
188  {
189  if (pstcBt == m_astcBtInstanceDataLut[u32Instance].pstcInstance)
190  {
191  return &m_astcBtInstanceDataLut[u32Instance].stcInternData;
192  }
193  }
194 
195  return NULL;
196 }
197 
198 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
199  (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
200  (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
201  (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON) || \
202  (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON) || \
203  (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON) || \
204  (PDL_INTERRUPT_ENABLE_BT12 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT13 == PDL_ON) || \
205  (PDL_INTERRUPT_ENABLE_BT14 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT15 == PDL_ON)
206 
214 void Bt_IrqHandler( volatile stc_btn_t* pstcBt,
215  stc_bt_intern_data_t* pstcBtInternData)
216 {
217  uint8_t u8Irq;
218 
219  // Get Interrupt Request
220  u8Irq = pstcBt->STC;
221  /*
222  * Trigger Interrupt (PWM,PPG,Reload timer)
223  * Measurement completion Interrupt (PWC)
224  */
225  if ((u8Irq & (1ul<<2)) == (1ul<<2))
226  {
227  // Clear Interrupt
228  pstcBt->STC &= ~(1ul<<2);
229 
230  if (pstcBtInternData->pfnBit2IntCallback != NULL)
231  {
232  // Call CallBackIrq
233  pstcBtInternData->pfnBit2IntCallback();
234  }
235  }
236 
237  // Duty Match Interrupt (PWM)
238  if ((u8Irq & (1ul<<1)) == (1ul<<1))
239  {
240  // Clear Interrupt
241  pstcBt->STC &= ~(1ul<<1);
242 
243  if (pstcBtInternData->pfnBit1IntCallback != NULL)
244  {
245  // Call CallBackIrq
246  pstcBtInternData->pfnBit1IntCallback();
247  }
248  }
249 
250  /*
251  * Underflow Interrupt (PWM,PPG,Reload timer)
252  * Overflow Interrupt (PWC)
253  */
254  if ((u8Irq & 1ul) == 1ul)
255  {
256  // Clear Interrupt
257  pstcBt->STC &= ~1ul;
258 
259  if (pstcBtInternData->pfnBit0IntCallback != NULL)
260  {
261  // Call CallBackIrq
262  pstcBtInternData->pfnBit0IntCallback();
263  }
264  }
265 
266 }
267 
275 static void Bt_InitNvic(volatile stc_btn_t* pstcBt)
276 {
277 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON)
278  if (pstcBt == (stc_btn_t*)(&BT0))
279  {
280  NVIC_ClearPendingIRQ(BT0_IRQn);
281  NVIC_EnableIRQ(BT0_IRQn);
282  NVIC_SetPriority(BT0_IRQn, PDL_IRQ_LEVEL_BT0);
283  }
284 #endif
285 #if (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON)
286  if (pstcBt == (stc_btn_t*)(&BT1))
287  {
288  NVIC_ClearPendingIRQ(BT1_IRQn);
289  NVIC_EnableIRQ(BT1_IRQn);
290  NVIC_SetPriority(BT1_IRQn, PDL_IRQ_LEVEL_BT1);
291  }
292 #endif
293 #if (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON)
294  if (pstcBt == (stc_btn_t*)(&BT2))
295  {
296  NVIC_ClearPendingIRQ(BT2_IRQn);
297  NVIC_EnableIRQ(BT2_IRQn);
298  NVIC_SetPriority(BT2_IRQn, PDL_IRQ_LEVEL_BT2);
299  }
300 #endif
301 #if (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON)
302  if (pstcBt == (stc_btn_t*)(&BT3))
303  {
304  NVIC_ClearPendingIRQ(BT3_IRQn);
305  NVIC_EnableIRQ(BT3_IRQn);
306  NVIC_SetPriority(BT3_IRQn, PDL_IRQ_LEVEL_BT3);
307  }
308 #endif
309 #if (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON)
310  if (pstcBt == (stc_btn_t*)(&BT4))
311  {
312  NVIC_ClearPendingIRQ(BT4_IRQn);
313  NVIC_EnableIRQ(BT4_IRQn);
314  NVIC_SetPriority(BT4_IRQn, PDL_IRQ_LEVEL_BT4);
315  }
316 #endif
317 #if (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON)
318  if (pstcBt == (stc_btn_t*)(&BT5))
319  {
320  NVIC_ClearPendingIRQ(BT5_IRQn);
321  NVIC_EnableIRQ(BT5_IRQn);
322  NVIC_SetPriority(BT5_IRQn, PDL_IRQ_LEVEL_BT5);
323  }
324 #endif
325 #if (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON)
326  if (pstcBt == (stc_btn_t*)(&BT6))
327  {
328  NVIC_ClearPendingIRQ(BT6_IRQn);
329  NVIC_EnableIRQ(BT6_IRQn);
330  NVIC_SetPriority(BT6_IRQn, PDL_IRQ_LEVEL_BT6);
331  }
332 #endif
333 #if (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
334  if (pstcBt == (stc_btn_t*)(&BT7))
335  {
336  NVIC_ClearPendingIRQ(BT7_IRQn);
337  NVIC_EnableIRQ(BT7_IRQn);
338  NVIC_SetPriority(BT7_IRQn, PDL_IRQ_LEVEL_BT7);
339  }
340 #endif
341 #if (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON)
342  if (pstcBt == (stc_btn_t*)(&BT8))
343  {
344  NVIC_ClearPendingIRQ(BT8_IRQn);
345  NVIC_EnableIRQ(BT8_IRQn);
346  NVIC_SetPriority(BT8_IRQn, PDL_IRQ_LEVEL_BT8);
347  }
348 #endif
349 #if (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON)
350  if (pstcBt == (stc_btn_t*)(&BT9))
351  {
352  NVIC_ClearPendingIRQ(BT9_IRQn);
353  NVIC_EnableIRQ(BT9_IRQn);
354  NVIC_SetPriority(BT9_IRQn, PDL_IRQ_LEVEL_BT9);
355  }
356 #endif
357 #if (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON)
358  if (pstcBt == (stc_btn_t*)(&BT10))
359  {
360  NVIC_ClearPendingIRQ(BT10_IRQn);
361  NVIC_EnableIRQ(BT10_IRQn);
362  NVIC_SetPriority(BT10_IRQn, PDL_IRQ_LEVEL_BT10);
363  }
364 #endif
365 #if (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON)
366  if (pstcBt == (stc_btn_t*)(&BT11))
367  {
368  NVIC_ClearPendingIRQ(BT11_IRQn);
369  NVIC_EnableIRQ(BT11_IRQn);
370  NVIC_SetPriority(BT11_IRQn, PDL_IRQ_LEVEL_BT11);
371  }
372 #endif
373 #if (PDL_INTERRUPT_ENABLE_BT12_13_14_15 == PDL_ON)
374  if ((pstcBt == (stc_btn_t*)(&BT12)) || (pstcBt == (stc_btn_t*)(&BT13)) ||
375  (pstcBt == (stc_btn_t*)(&BT14)) || (pstcBt == (stc_btn_t*)(&BT15)))
376  {
377  NVIC_ClearPendingIRQ(BT12_13_14_15_IRQn);
378  NVIC_EnableIRQ(BT12_13_14_15_IRQn);
379  NVIC_SetPriority(BT12_13_14_15_IRQn, PDL_IRQ_LEVEL_BT12_13_14_15);
380  }
381 #endif
382 }
383 
391 static void Bt_DeInitNvic(volatile stc_btn_t* pstcBt)
392 {
393 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON)
394  if (pstcBt == (stc_btn_t*)(&BT0))
395  {
396  NVIC_ClearPendingIRQ(BT0_IRQn);
397  NVIC_DisableIRQ(BT0_IRQn);
398  NVIC_SetPriority(BT0_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
399  }
400 #endif
401 #if (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON)
402  if (pstcBt == (stc_btn_t*)(&BT1))
403  {
404  NVIC_ClearPendingIRQ(BT1_IRQn);
405  NVIC_DisableIRQ(BT1_IRQn);
406  NVIC_SetPriority(BT1_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
407  }
408 #endif
409 #if (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON)
410  if (pstcBt == (stc_btn_t*)(&BT2))
411  {
412  NVIC_ClearPendingIRQ(BT2_IRQn);
413  NVIC_DisableIRQ(BT2_IRQn);
414  NVIC_SetPriority(BT2_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
415  }
416 #endif
417 #if (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON)
418  if (pstcBt == (stc_btn_t*)(&BT3))
419  {
420  NVIC_ClearPendingIRQ(BT3_IRQn);
421  NVIC_DisableIRQ(BT3_IRQn);
422  NVIC_SetPriority(BT3_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
423  }
424 #endif
425 #if (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON)
426  if (pstcBt == (stc_btn_t*)(&BT4))
427  {
428  NVIC_ClearPendingIRQ(BT4_IRQn);
429  NVIC_DisableIRQ(BT4_IRQn);
430  NVIC_SetPriority(BT4_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
431  }
432 #endif
433 #if (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON)
434  if (pstcBt == (stc_btn_t*)(&BT5))
435  {
436  NVIC_ClearPendingIRQ(BT5_IRQn);
437  NVIC_DisableIRQ(BT5_IRQn);
438  NVIC_SetPriority(BT5_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
439  }
440 #endif
441 #if (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON)
442  if (pstcBt == (stc_btn_t*)(&BT6))
443  {
444  NVIC_ClearPendingIRQ(BT6_IRQn);
445  NVIC_DisableIRQ(BT6_IRQn);
446  NVIC_SetPriority(BT6_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
447  }
448 #endif
449 #if (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
450  if (pstcBt == (stc_btn_t*)(&BT7))
451  {
452  NVIC_ClearPendingIRQ(BT7_IRQn);
453  NVIC_DisableIRQ(BT7_IRQn);
454  NVIC_SetPriority(BT7_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
455  }
456 #endif
457 #if (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON)
458  if (pstcBt == (stc_btn_t*)(&BT8))
459  {
460  NVIC_ClearPendingIRQ(BT8_IRQn);
461  NVIC_DisableIRQ(BT8_IRQn);
462  NVIC_SetPriority(BT8_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
463  }
464 #endif
465 #if (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON)
466  if (pstcBt == (stc_btn_t*)(&BT9))
467  {
468  NVIC_ClearPendingIRQ(BT9_IRQn);
469  NVIC_DisableIRQ(BT9_IRQn);
470  NVIC_SetPriority(BT9_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
471  }
472 #endif
473 #if (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON)
474  if (pstcBt == (stc_btn_t*)(&BT10))
475  {
476  NVIC_ClearPendingIRQ(BT10_IRQn);
477  NVIC_DisableIRQ(BT10_IRQn);
478  NVIC_SetPriority(BT10_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
479  }
480 #endif
481 #if (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON)
482  if (pstcBt == (stc_btn_t*)(&BT11))
483  {
484  NVIC_ClearPendingIRQ(BT11_IRQn);
485  NVIC_DisableIRQ(BT11_IRQn);
486  NVIC_SetPriority(BT11_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
487  }
488 #endif
489 #if (PDL_INTERRUPT_ENABLE_BT12_13_14_15 == PDL_ON)
490  if ((pstcBt == (stc_btn_t*)(&BT12)) || (pstcBt == (stc_btn_t*)(&BT13)) ||
491  (pstcBt == (stc_btn_t*)(&BT14)) || (pstcBt == (stc_btn_t*)(&BT15)))
492  {
493  /* only when interrupts of ch12,13,14,15 are all disabled, disable NVIC */
494  if(((FM4_BT12_PWM->STC & 0x70u) == 0x00u) &&
495  ((FM4_BT13_PWM->STC & 0x70u) == 0x00u) &&
496  ((FM4_BT14_PWM->STC & 0x70u) == 0x00u) &&
497  ((FM4_BT15_PWM->STC & 0x70u) == 0x00u) )
498  {
499  NVIC_ClearPendingIRQ(BT12_13_14_15_IRQn);
500  NVIC_DisableIRQ(BT12_13_14_15_IRQn);
501  NVIC_SetPriority(BT12_13_14_15_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
502  }
503 
504  }
505 #endif
506 
507 }
508 
509 #endif
510 
523 {
524  // Pointer to internal data
525  stc_bt_intern_data_t* pstcBtInternData ;
526  // Check for NULL pointer
527  if ((pstcBt == NULL) || (enIoMode > BtIoMode8))
528  {
529  return ErrorInvalidParameter ;
530  }
531 
532  // Get pointer to internal data structure ...
533  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
534 
535  if(pstcBtInternData == NULL)
536  {
537  return ErrorInvalidParameter ;
538  }
539 
540  if((pstcBt == &BT0) || (pstcBt == &BT1))
541  {
542  FM4_BTIOSEL03->BTSEL0123 &= 0xF0;
543  FM4_BTIOSEL03->BTSEL0123 |= enIoMode;
544  }
545  else if((pstcBt == &BT2) || (pstcBt == &BT3))
546  {
547  FM4_BTIOSEL03->BTSEL0123 &= 0x0F;
548  FM4_BTIOSEL03->BTSEL0123 |= (enIoMode<<4);
549  }
550  else if((pstcBt == &BT4) || (pstcBt == &BT5))
551  {
552  FM4_BTIOSEL47->BTSEL4567 &= 0xF0;
553  FM4_BTIOSEL47->BTSEL4567 |= enIoMode;
554  }
555  else if((pstcBt == &BT6) || (pstcBt == &BT7))
556  {
557  FM4_BTIOSEL47->BTSEL4567 &= 0x0F;
558  FM4_BTIOSEL47->BTSEL4567 |= (enIoMode<<4);
559  }
560  else if((pstcBt == &BT8) || (pstcBt == &BT9))
561  {
562  FM4_BTIOSEL8B->BTSEL89AB &= 0xF0;
563  FM4_BTIOSEL8B->BTSEL89AB |= enIoMode;
564  }
565  else if((pstcBt == &BT10) || (pstcBt == &BT11))
566  {
567  FM4_BTIOSEL8B->BTSEL89AB &= 0x0F;
568  FM4_BTIOSEL8B->BTSEL89AB |= (enIoMode<<4);
569  }
570  else if((pstcBt == &BT12) || (pstcBt == &BT13))
571  {
572  FM4_BTIOSELCF->BTSELCDEF &= 0xF0;
573  FM4_BTIOSELCF->BTSELCDEF |= enIoMode;
574  }
575  else if((pstcBt == &BT14) || (pstcBt == &BT15))
576  {
577  FM4_BTIOSELCF->BTSELCDEF &= 0x0F;
578  FM4_BTIOSELCF->BTSELCDEF |= (enIoMode<<4);
579  }
580 
581  return Ok;
582 }
583 
596 {
597  // Pointer to internal data
598  stc_bt_intern_data_t* pstcBtInternData ;
599  // Check for NULL pointer
600  if ((pstcBt == NULL) || (enTimerMode > BtPwcMode))
601  {
602  return ErrorInvalidParameter ;
603  }
604 
605  // Get pointer to internal data structure ...
606  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
607 
608  if(pstcBtInternData == NULL)
609  {
610  return ErrorInvalidParameter ;
611  }
612 
613  pstcBt->TMCR &= ~(7ul<<4);
614  pstcBt->TMCR |= (enTimerMode<<4);
615 
616  return Ok;
617 }
618 
630 en_result_t Bt_Pwm_Init(volatile stc_btn_t* pstcBt, stc_bt_pwm_config_t* pstcPwmConfig)
631 {
632  volatile FM4_BT_PWM_TypeDef* pRegBt = (FM4_BT_PWM_TypeDef*)pstcBt;
633  // Pointer to internal data
634  stc_bt_intern_data_t* pstcBtInternData ;
635  // Check for NULL pointer
636  if (pstcBt == NULL)
637  {
638  return ErrorInvalidParameter ;
639  }
640  // Check configuration parameter
641  if((pstcPwmConfig->enPres > PwmPres1Div2048) ||
642  (pstcPwmConfig->enRestartEn > PwmRestartEnable) ||
643  (pstcPwmConfig->enOutputMask > PwmOutputMask) ||
644  (pstcPwmConfig->enExtTrig > PwmExtTrigBoth) ||
645  (pstcPwmConfig->enOutputPolarity > PwmPolarityHigh) ||
646  (pstcPwmConfig->enMode > PwmOneshot) )
647  {
648  return ErrorInvalidParameter ;
649  }
650 
651  // Get pointer to internal data structure ...
652  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
653 
654  if(pstcBtInternData == NULL)
655  {
656  return ErrorInvalidParameter ;
657  }
658 
659  // Clock prescaler setting
660  if(pstcPwmConfig->enPres < PwmPres1Div512)
661  {
662  pRegBt->TMCR2 = 0x00;
663  pRegBt->TMCR &= ~(7ul<<12);
664  pRegBt->TMCR |= (pstcPwmConfig->enPres<<12);
665  }
666  else
667  {
668  pRegBt->TMCR2 = 0x01;
669  pRegBt->TMCR &= ~(7ul<<12);
670  pRegBt->TMCR |= ((pstcPwmConfig->enPres & 0x7)<<12);
671  }
672  // Restart enable setting
673  pRegBt->TMCR_f.RTGEN = pstcPwmConfig->enRestartEn;
674  // Output Mask setting
675  pRegBt->TMCR_f.PMSK = pstcPwmConfig->enOutputMask;
676  // Output polarity setting
677  pRegBt->TMCR_f.OSEL = pstcPwmConfig->enOutputPolarity;
678  // External trigger setting
679  pRegBt->TMCR &= ~(3ul<<8);
680  pRegBt->TMCR |= (pstcPwmConfig->enExtTrig<<8);
681  // Mode setting
682  pRegBt->TMCR_f.MDSE = pstcPwmConfig->enMode;
683  return Ok;
684 }
685 
696 {
697  // Check for NULL pointer
698  if (pstcBt == NULL)
699  {
700  return ErrorInvalidParameter ;
701  }
702 
703  ((FM4_BT_PWM_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
704  return Ok;
705 }
706 
717 {
718  // Check for NULL pointer
719  if (pstcBt == NULL)
720  {
721  return ErrorInvalidParameter ;
722  }
723 
724  ((FM4_BT_PWM_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
725  return Ok;
726 }
727 
738 {
739  // Check for NULL pointer
740  if (pstcBt == NULL)
741  {
742  return ErrorInvalidParameter ;
743  }
744 
745  ((FM4_BT_PWM_TypeDef*)pstcBt)->TMCR_f.STRG = 1;
746  return Ok;
747 }
748 
749 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
750  (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
751  (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
752  (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON) || \
753  (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON) || \
754  (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON) || \
755  (PDL_INTERRUPT_ENABLE_BT12 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT13 == PDL_ON) || \
756  (PDL_INTERRUPT_ENABLE_BT14 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT15 == PDL_ON)
757 
771  stc_pwm_int_cb_t* pstcIntCallback)
772 {
773  // Pointer to internal data
774  stc_bt_intern_data_t* pstcBtInternData ;
775  // Check parameter
776  if ((pstcBt == NULL) ||
777  (pstcIntSel->bPwmDutyMatchInt > 1) ||
778  (pstcIntSel->bPwmTrigInt > 1) ||
779  (pstcIntSel->bPwmUnderflowInt > 1) ||
780  (pstcIntCallback == NULL))
781  {
782  return ErrorInvalidParameter ;
783  }
784 
785  // Get pointer to internal data structure ...
786  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
787 
788  if(pstcBtInternData == NULL)
789  {
790  return ErrorInvalidParameter ;
791  }
792 
793  if(pstcIntSel->bPwmTrigInt == 1)
794  {
795  pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnPwmTrigIntCallback;
796  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIE = 1;
797  }
798  if(pstcIntSel->bPwmDutyMatchInt == 1)
799  {
800  pstcBtInternData->pfnBit1IntCallback = pstcIntCallback->pfnPwmDutyMatchIntCallback;
801  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIE = 1;
802  }
803  if(pstcIntSel->bPwmUnderflowInt == 1)
804  {
805  pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnPwmUnderflowIntCallback;
806  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIE = 1;
807  }
808 
809  Bt_InitNvic(pstcBt);
810 
811  return Ok;
812 }
813 
826 {
827  // Pointer to internal data
828  stc_bt_intern_data_t* pstcBtInternData ;
829  // Check parameter
830  if ((pstcBt == NULL) ||
831  (pstcIntSel->bPwmDutyMatchInt > 1) ||
832  (pstcIntSel->bPwmTrigInt > 1) ||
833  (pstcIntSel->bPwmUnderflowInt > 1))
834  {
835  return ErrorInvalidParameter ;
836  }
837 
838  // Get pointer to internal data structure ...
839  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
840 
841  if(pstcBtInternData == NULL)
842  {
843  return ErrorInvalidParameter ;
844  }
845 
846  if(pstcIntSel->bPwmTrigInt == 1)
847  {
848  pstcBtInternData->pfnBit2IntCallback = NULL;
849  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIE = 0;
850  }
851  if(pstcIntSel->bPwmDutyMatchInt == 1)
852  {
853  pstcBtInternData->pfnBit1IntCallback = NULL;
854  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIE = 0;
855  }
856  if(pstcIntSel->bPwmUnderflowInt == 1)
857  {
858  pstcBtInternData->pfnBit0IntCallback = NULL;
859  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIE = 0;
860  }
861  if( (((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIE == 0) &&
862  (((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIE == 0) &&
863  (((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIE == 0) )
864  {
865  Bt_DeInitNvic(pstcBt);
866  }
867 
868  return Ok;
869 }
870 
871 #endif
872 
884 {
885  en_int_flag_t enFlag;
886 
887  switch (enIntType)
888  {
889  case PwmTrigInt:
890  enFlag = ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIR ? PdlSet : PdlClr;
891  break;
892  case PwmDutyMatchInt:
893  enFlag = ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIR ? PdlSet : PdlClr;
894  break;
895  case PwmUnderflowInt:
896  enFlag = ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIR ? PdlSet : PdlClr;
897  break;
898  default:
899  break;
900  }
901 
902  return enFlag;
903 }
904 
917 {
918  if((pstcBt == NULL) || (enIntType > PwmUnderflowInt))
919  {
920  return ErrorInvalidParameter ;
921  }
922 
923  switch (enIntType)
924  {
925  case PwmTrigInt:
926  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIR = 0;
927  break;
928  case PwmDutyMatchInt:
929  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIR = 0;
930  break;
931  case PwmUnderflowInt:
932  ((FM4_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIR = 0;
933  break;
934  default:
935  break;
936  }
937 
938  return Ok;
939 }
940 
951 en_result_t Bt_Pwm_WriteCycleVal(volatile stc_btn_t* pstcBt, uint16_t u16Cycle)
952 {
953  // Check parameter
954  if ((pstcBt == NULL))
955  {
956  return ErrorInvalidParameter ;
957  }
958 
959  ((FM4_BT_PWM_TypeDef*)pstcBt)->PCSR = u16Cycle;
960  return Ok;
961 }
962 
973 en_result_t Bt_Pwm_WriteDutyVal(volatile stc_btn_t* pstcBt, uint16_t u16Duty)
974 {
975  // Check parameter
976  if ((pstcBt == NULL))
977  {
978  return ErrorInvalidParameter ;
979  }
980 
981  ((FM4_BT_PWM_TypeDef*)pstcBt)->PDUT = u16Duty;
982  return Ok;
983 }
984 
993 uint16_t Bt_Pwm_ReadCurCnt(volatile stc_btn_t* pstcBt)
994 {
995  return ((FM4_BT_PWM_TypeDef*)pstcBt)->TMR;
996 }
997 
1009 en_result_t Bt_Ppg_Init(volatile stc_btn_t* pstcBt, stc_bt_ppg_config_t* pstcPpgConfig)
1010 {
1011  volatile FM4_BT_PPG_TypeDef* pRegBt = (FM4_BT_PPG_TypeDef*) pstcBt;
1012  // Pointer to internal data
1013  stc_bt_intern_data_t* pstcBtInternData ;
1014  // Check for NULL pointer
1015  if (pstcBt == NULL)
1016  {
1017  return ErrorInvalidParameter ;
1018  }
1019  // Check configuration parameter
1020  if((pstcPpgConfig->enPres > PpgPres1Div2048) ||
1021  (pstcPpgConfig->enRestartEn > PpgRestartEnable) ||
1022  (pstcPpgConfig->enOutputMask > PpgOutputMask) ||
1023  (pstcPpgConfig->enExtTrig > PpgExtTrigBoth) ||
1024  (pstcPpgConfig->enOutputPolarity > PpgPolarityHigh) ||
1025  (pstcPpgConfig->enMode > PpgOneshot) )
1026  {
1027  return ErrorInvalidParameter ;
1028  }
1029 
1030  // Get pointer to internal data structure ...
1031  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1032 
1033  if(pstcBtInternData == NULL)
1034  {
1035  return ErrorInvalidParameter ;
1036  }
1037 
1038  // Clock prescaler setting
1039  if(pstcPpgConfig->enPres < PpgPres1Div512)
1040  {
1041  pRegBt->TMCR2 = 0x00;
1042  pRegBt->TMCR &= ~(7ul<<12);
1043  pRegBt->TMCR |= (pstcPpgConfig->enPres<<12);
1044  }
1045  else
1046  {
1047  pRegBt->TMCR2 = 0x01;
1048  pRegBt->TMCR &= ~(7ul<<12);
1049  pRegBt->TMCR |= ((pstcPpgConfig->enPres & 0x7)<<12);
1050  }
1051  // Restart enable setting
1052  pRegBt->TMCR_f.RTGEN = pstcPpgConfig->enRestartEn;
1053  // Output Mask setting
1054  pRegBt->TMCR_f.PMSK = pstcPpgConfig->enOutputMask;
1055  // Output polarity setting
1056  pRegBt->TMCR_f.OSEL = pstcPpgConfig->enOutputPolarity;
1057  // External trigger setting
1058  pRegBt->TMCR &= ~(3ul<<8);
1059  pRegBt->TMCR |= pstcPpgConfig->enExtTrig;
1060  // Mode setting
1061  pRegBt->TMCR_f.MDSE = pstcPpgConfig->enMode;
1062  return Ok;
1063 }
1064 
1075 {
1076  // Check for NULL pointer
1077  if (pstcBt == NULL)
1078  {
1079  return ErrorInvalidParameter ;
1080  }
1081 
1082  ((FM4_BT_PPG_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
1083  return Ok;
1084 }
1085 
1096 {
1097  // Check for NULL pointer
1098  if (pstcBt == NULL)
1099  {
1100  return ErrorInvalidParameter ;
1101  }
1102 
1103  ((FM4_BT_PPG_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
1104  return Ok;
1105 }
1106 
1117 {
1118  // Check for NULL pointer
1119  if (pstcBt == NULL)
1120  {
1121  return ErrorInvalidParameter ;
1122  }
1123 
1124  ((FM4_BT_PPG_TypeDef*)pstcBt)->TMCR_f.STRG = 1;
1125  return Ok;
1126 }
1127 
1128 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
1129  (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
1130  (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
1131  (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON) || \
1132  (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON) || \
1133  (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON) || \
1134  (PDL_INTERRUPT_ENABLE_BT12 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT13 == PDL_ON) || \
1135  (PDL_INTERRUPT_ENABLE_BT14 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT15 == PDL_ON)
1136 
1150  stc_ppg_int_cb_t* pstcIntCallback)
1151 {
1152  // Pointer to internal data
1153  stc_bt_intern_data_t* pstcBtInternData ;
1154  // Check parameter
1155  if ((pstcBt == NULL) ||
1156  (pstcIntSel->bPpgTrigInt > 1) ||
1157  (pstcIntSel->bPpgUnderflowInt > 1) ||
1158  (pstcIntCallback == NULL))
1159  {
1160  return ErrorInvalidParameter ;
1161  }
1162 
1163  // Get pointer to internal data structure ...
1164  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1165 
1166  if(pstcBtInternData == NULL)
1167  {
1168  return ErrorInvalidParameter ;
1169  }
1170 
1171  if(pstcIntSel->bPpgTrigInt == 1)
1172  {
1173  pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnPpgTrigIntCallback;
1174  ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIE = 1;
1175  }
1176  if(pstcIntSel->bPpgUnderflowInt == 1)
1177  {
1178  pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnPpgUnderflowIntCallback;
1179  ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIE = 1;
1180  }
1181 
1182  Bt_InitNvic(pstcBt);
1183 
1184  return Ok;
1185 }
1186 
1199 {
1200  // Pointer to internal data
1201  stc_bt_intern_data_t* pstcBtInternData ;
1202  // Check parameter
1203  if ((pstcBt == NULL) ||
1204  (pstcIntSel->bPpgTrigInt > 1) ||
1205  (pstcIntSel->bPpgUnderflowInt > 1) )
1206  {
1207  return ErrorInvalidParameter ;
1208  }
1209 
1210  // Get pointer to internal data structure ...
1211  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1212 
1213  if(pstcBtInternData == NULL)
1214  {
1215  return ErrorInvalidParameter ;
1216  }
1217 
1218  if(pstcIntSel->bPpgTrigInt == 1)
1219  {
1220  pstcBtInternData->pfnBit2IntCallback = NULL;
1221  ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIE = 0;
1222  }
1223  if(pstcIntSel->bPpgUnderflowInt == 1)
1224  {
1225  pstcBtInternData->pfnBit0IntCallback = NULL;
1226  ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIE = 0;
1227  }
1228 
1229  if( (((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIE == 0) &&
1230  (((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIE == 0))
1231  {
1232  Bt_DeInitNvic(pstcBt);
1233  }
1234  return Ok;
1235 }
1236 
1237 #endif
1238 
1250 {
1251  en_int_flag_t enFlag;
1252 
1253  switch (enIntType)
1254  {
1255  case PpgTrigInt:
1256  enFlag = ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIR ? PdlSet : PdlClr;
1257  break;
1258  case PpgUnderflowInt:
1259  enFlag = ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIR ? PdlSet : PdlClr;
1260  break;
1261  default:
1262  break;
1263  }
1264 
1265  return enFlag;
1266 }
1267 
1280 {
1281  if((pstcBt == NULL) || (enIntType > PpgUnderflowInt))
1282  {
1283  return ErrorInvalidParameter;
1284  }
1285 
1286  switch (enIntType)
1287  {
1288  case PpgTrigInt:
1289  ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIR = 0;
1290  break;
1291  case PpgUnderflowInt:
1292  ((FM4_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIR = 0;
1293  break;
1294  default:
1295  break;
1296  }
1297 
1298  return Ok;
1299 }
1300 
1311 en_result_t Bt_Ppg_WriteLowWidthVal(volatile stc_btn_t* pstcBt, uint16_t u16Val)
1312 {
1313  // Check parameter
1314  if ((pstcBt == NULL))
1315  {
1316  return ErrorInvalidParameter ;
1317  }
1318 
1319  ((FM4_BT_PPG_TypeDef*)pstcBt)->PRLL = u16Val;
1320  return Ok;
1321 }
1322 
1333 en_result_t Bt_Ppg_WriteHighWidthVal(volatile stc_btn_t* pstcBt, uint16_t u16Val)
1334 {
1335  // Check parameter
1336  if ((pstcBt == NULL))
1337  {
1338  return ErrorInvalidParameter ;
1339  }
1340 
1341  ((FM4_BT_PPG_TypeDef*)pstcBt)->PRLH = u16Val;
1342  return Ok;
1343 }
1344 
1353 uint16_t Bt_Ppg_ReadCurCnt(volatile stc_btn_t* pstcBt)
1354 {
1355  return ((FM4_BT_PPG_TypeDef*)pstcBt)->TMR;
1356 }
1357 
1369 en_result_t Bt_Rt_Init(volatile stc_btn_t* pstcBt, stc_bt_rt_config_t* pstcRtConfig)
1370 {
1371  volatile FM4_BT_RT_TypeDef* pRegBt = (FM4_BT_RT_TypeDef*) pstcBt;
1372  // Pointer to internal data
1373  stc_bt_intern_data_t* pstcBtInternData ;
1374  // Check for NULL pointer
1375  if (pstcBt == NULL)
1376  {
1377  return ErrorInvalidParameter ;
1378  }
1379  // Check configuration parameter
1380  if((pstcRtConfig->enPres > RtPres1Div2048) ||
1381  (pstcRtConfig->enSize > RtSize32Bit) ||
1382  (pstcRtConfig->enExtTrig > RtExtTiggerHighLevel) ||
1383  (pstcRtConfig->enOutputPolarity > RtPolarityHigh) ||
1384  (pstcRtConfig->enMode > RtOneshot) )
1385  {
1386  return ErrorInvalidParameter ;
1387  }
1388 
1389  // Get pointer to internal data structure ...
1390  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1391 
1392  if(pstcBtInternData == NULL)
1393  {
1394  return ErrorInvalidParameter ;
1395  }
1396 
1397  // Clock prescaler setting
1398  if(pstcRtConfig->enPres < RtPres1Div512)
1399  {
1400  pRegBt->TMCR2 = 0x00;
1401  pRegBt->TMCR &= ~(7ul<<12);
1402  pRegBt->TMCR |= (pstcRtConfig->enPres<<12);
1403  }
1404  else
1405  {
1406  pRegBt->TMCR2 = 0x01;
1407  pRegBt->TMCR &= ~(7ul<<12);
1408  pRegBt->TMCR |= ((pstcRtConfig->enPres & 0x7)<<12);
1409 
1410  }
1411  // Timer size setting
1412  pRegBt->TMCR_f.T32 = pstcRtConfig->enSize;
1413  // Output polarity setting
1414  pRegBt->TMCR_f.OSEL = pstcRtConfig->enOutputPolarity;
1415  // External trigger setting
1416  if(pstcRtConfig->enExtTrig <= RtExtTiggerBothEdge)
1417  {
1418  pRegBt->TMCR2_f.GATE = 0;
1419  pRegBt->TMCR &= ~(3ul<<8);
1420  pRegBt->TMCR |= (pstcRtConfig->enExtTrig)<<8;
1421  }
1422  else if (pstcRtConfig->enExtTrig == RtExtTiggerLowLevel)
1423  {
1424  pRegBt->TMCR2_f.GATE = 1;
1425  pRegBt->TMCR &= ~(3ul<<8);
1426  }
1427  else if (pstcRtConfig->enExtTrig == RtExtTiggerHighLevel)
1428  {
1429  pRegBt->TMCR2_f.GATE = 1;
1430  pRegBt->TMCR &= ~(3ul<<8);
1431  pRegBt->TMCR |= (1ul<<8);
1432  }
1433  // Mode setting
1434  pRegBt->TMCR_f.MDSE = pstcRtConfig->enMode;
1435  return Ok;
1436 }
1437 
1448 {
1449  // Check for NULL pointer
1450  if (pstcBt == NULL)
1451  {
1452  return ErrorInvalidParameter ;
1453  }
1454 
1455  ((FM4_BT_RT_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
1456  return Ok;
1457 }
1458 
1469 {
1470  // Check for NULL pointer
1471  if (pstcBt == NULL)
1472  {
1473  return ErrorInvalidParameter ;
1474  }
1475 
1476  ((FM4_BT_RT_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
1477  return Ok;
1478 }
1479 
1490 {
1491  // Check for NULL pointer
1492  if (pstcBt == NULL)
1493  {
1494  return ErrorInvalidParameter ;
1495  }
1496 
1497  ((FM4_BT_RT_TypeDef*)pstcBt)->TMCR_f.STRG = 1;
1498  return Ok;
1499 }
1500 
1501 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
1502  (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
1503  (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
1504  (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON) || \
1505  (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON) || \
1506  (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON) || \
1507  (PDL_INTERRUPT_ENABLE_BT12 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT13 == PDL_ON) || \
1508  (PDL_INTERRUPT_ENABLE_BT14 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT15 == PDL_ON)
1509 
1523  stc_rt_int_cb_t* pstcIntCallback)
1524 {
1525  // Pointer to internal data
1526  stc_bt_intern_data_t* pstcBtInternData ;
1527  // Check parameter
1528  if ((pstcBt == NULL) ||
1529  (pstcIntSel->bRtTrigInt > 1) ||
1530  (pstcIntSel->bRtUnderflowInt > 1) ||
1531  (pstcIntCallback == NULL))
1532  {
1533  return ErrorInvalidParameter ;
1534  }
1535 
1536  // Get pointer to internal data structure ...
1537  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1538 
1539  if(pstcBtInternData == NULL)
1540  {
1541  return ErrorInvalidParameter ;
1542  }
1543 
1544  if(pstcIntSel->bRtTrigInt == 1)
1545  {
1546  pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnRtTrigIntCallback;
1547  ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.TGIE = 1;
1548  }
1549  if(pstcIntSel->bRtUnderflowInt == 1)
1550  {
1551  pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnRtUnderflowIntCallback;
1552  ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.UDIE = 1;
1553  }
1554 
1555  Bt_InitNvic(pstcBt);
1556 
1557  return Ok;
1558 }
1559 
1572 {
1573  // Pointer to internal data
1574  stc_bt_intern_data_t* pstcBtInternData ;
1575  // Check parameter
1576  if ((pstcBt == NULL) ||
1577  (pstcIntSel->bRtTrigInt > 1) ||
1578  (pstcIntSel->bRtUnderflowInt > 1) )
1579  {
1580  return ErrorInvalidParameter ;
1581  }
1582 
1583  // Get pointer to internal data structure ...
1584  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1585 
1586  if(pstcBtInternData == NULL)
1587  {
1588  return ErrorInvalidParameter ;
1589  }
1590 
1591  if(pstcIntSel->bRtTrigInt == 1)
1592  {
1593  pstcBtInternData->pfnBit2IntCallback = NULL;
1594  ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.TGIE = 0;
1595  }
1596  if(pstcIntSel->bRtUnderflowInt == 1)
1597  {
1598  pstcBtInternData->pfnBit0IntCallback = NULL;
1599  ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.UDIE = 0;
1600  }
1601 
1602  Bt_DeInitNvic(pstcBt);
1603 
1604  return Ok;
1605 }
1606 
1607 #endif
1608 
1620 {
1621  en_int_flag_t enFlag;
1622 
1623  switch (enIntType)
1624  {
1625  case RtTrigInt:
1626  enFlag = ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.TGIR ? PdlSet : PdlClr;
1627  break;
1628  case RtUnderflowInt:
1629  enFlag = ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.UDIR ? PdlSet : PdlClr;
1630  break;
1631  default:
1632  break;
1633  }
1634 
1635  return enFlag;
1636 }
1637 
1650 {
1651  if((pstcBt == NULL) || (enIntType > RtUnderflowInt))
1652  {
1653  return ErrorInvalidParameter;
1654  }
1655 
1656  switch (enIntType)
1657  {
1658  case RtTrigInt:
1659  ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.TGIR = 0;
1660  break;
1661  case RtUnderflowInt:
1662  ((FM4_BT_RT_TypeDef*)pstcBt)->STC_f.UDIR = 0;
1663  break;
1664  default:
1665  break;
1666  }
1667 
1668  return Ok;
1669 }
1670 
1681 en_result_t Bt_Rt_WriteCycleVal(volatile stc_btn_t* pstcBt, uint16_t u16Val)
1682 {
1683  // Check parameter
1684  if ((pstcBt == NULL))
1685  {
1686  return ErrorInvalidParameter ;
1687  }
1688 
1689  ((FM4_BT_RT_TypeDef*)pstcBt)->PCSR = u16Val;
1690  return Ok;
1691 }
1692 
1701 uint16_t Bt_Rt_ReadCurCnt(volatile stc_btn_t* pstcBt)
1702 {
1703  return ((FM4_BT_RT_TypeDef*)pstcBt)->TMR;
1704 }
1705 
1717 en_result_t Bt_Pwc_Init(volatile stc_btn_t* pstcBt, stc_bt_pwc_config_t* pstcPwcConfig)
1718 {
1719  volatile FM4_BT_PWC_TypeDef* pRegBt = (FM4_BT_PWC_TypeDef*) pstcBt;
1720  // Pointer to internal data
1721  stc_bt_intern_data_t* pstcBtInternData ;
1722  // Check for NULL pointer
1723  if (pstcBt == NULL)
1724  {
1725  return ErrorInvalidParameter ;
1726  }
1727  // Check configuration parameter
1728  if((pstcPwcConfig->enPres > PwcPres1Div2048) ||
1729  (pstcPwcConfig->enSize > PwcSize32Bit) ||
1730  (pstcPwcConfig->enMeasureEdge > PwcMeasureFallingToRising) ||
1731  (pstcPwcConfig->enMode > PwcOneshot) )
1732  {
1733  return ErrorInvalidParameter ;
1734  }
1735 
1736  // Get pointer to internal data structure ...
1737  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1738 
1739  if(pstcBtInternData == NULL)
1740  {
1741  return ErrorInvalidParameter ;
1742  }
1743 
1744  // Clock prescaler setting
1745  if(pstcPwcConfig->enPres < PwcPres1Div512)
1746  {
1747  pRegBt->TMCR2 = 0x00;
1748  pRegBt->TMCR &= ~(7ul<<12);
1749  pRegBt->TMCR |= (pstcPwcConfig->enPres<<12);
1750  }
1751  else
1752  {
1753  pRegBt->TMCR2 = 0x01;
1754  pRegBt->TMCR &= ~(7ul<<12);
1755  pRegBt->TMCR |= ((pstcPwcConfig->enPres & 0x7)<<12);
1756  }
1757  // Timer size setting
1758  pRegBt->TMCR_f.T32 = pstcPwcConfig->enSize;
1759  // Measurement mode setting
1760  pRegBt->TMCR &= ~(7ul<<8);
1761  pRegBt->TMCR |= (pstcPwcConfig->enMeasureEdge<<8);
1762  // Mode setting
1763  pRegBt->TMCR_f.MDSE = pstcPwcConfig->enMode;
1764  return Ok;
1765 }
1766 
1777 {
1778  // Check for NULL pointer
1779  if (pstcBt == NULL)
1780  {
1781  return ErrorInvalidParameter ;
1782  }
1783 
1784  ((FM4_BT_PWC_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
1785  return Ok;
1786 }
1787 
1798 {
1799  // Check for NULL pointer
1800  if (pstcBt == NULL)
1801  {
1802  return ErrorInvalidParameter ;
1803  }
1804 
1805  ((FM4_BT_PWC_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
1806  return Ok;
1807 }
1808 
1809 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
1810  (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
1811  (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
1812  (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON) || \
1813  (PDL_INTERRUPT_ENABLE_BT8 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT9 == PDL_ON) || \
1814  (PDL_INTERRUPT_ENABLE_BT10 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT11 == PDL_ON) || \
1815  (PDL_INTERRUPT_ENABLE_BT12 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT13 == PDL_ON) || \
1816  (PDL_INTERRUPT_ENABLE_BT14 == PDL_ON) || (PDL_INTERRUPT_ENABLE_BT15 == PDL_ON)
1817 
1831  stc_pwc_int_cb_t* pstcIntCallback)
1832 {
1833  // Pointer to internal data
1834  stc_bt_intern_data_t* pstcBtInternData ;
1835  // Check parameter
1836  if ((pstcBt == NULL) ||
1837  (pstcIntSel->bPwcMeasCmpInt > 1) ||
1838  (pstcIntSel->bPwcMeasOverflowInt > 1) ||
1839  (pstcIntCallback == NULL))
1840  {
1841  return ErrorInvalidParameter ;
1842  }
1843 
1844  // Get pointer to internal data structure ...
1845  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1846 
1847  if(pstcBtInternData == NULL)
1848  {
1849  return ErrorInvalidParameter ;
1850  }
1851 
1852  if(pstcIntSel->bPwcMeasCmpInt == 1)
1853  {
1854  pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnPwcMeasCmpCallback;
1855  ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIE = 1;
1856  }
1857  if(pstcIntSel->bPwcMeasOverflowInt == 1)
1858  {
1859  pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnPwcMeasOverflowCallback;
1860  ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIE = 1;
1861  }
1862 
1863  Bt_InitNvic(pstcBt);
1864 
1865  return Ok;
1866 }
1867 
1880 {
1881  // Pointer to internal data
1882  stc_bt_intern_data_t* pstcBtInternData ;
1883  // Check parameter
1884  if ((pstcBt == NULL) ||
1885  (pstcIntSel->bPwcMeasCmpInt > 1) ||
1886  (pstcIntSel->bPwcMeasOverflowInt > 1))
1887  {
1888  return ErrorInvalidParameter ;
1889  }
1890 
1891  // Get pointer to internal data structure ...
1892  pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
1893 
1894  if(pstcBtInternData == NULL)
1895  {
1896  return ErrorInvalidParameter ;
1897  }
1898 
1899  if(pstcIntSel->bPwcMeasCmpInt == 1)
1900  {
1901  pstcBtInternData->pfnBit2IntCallback = NULL;
1902  ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIE = 0;
1903  }
1904  if(pstcIntSel->bPwcMeasOverflowInt == 1)
1905  {
1906  pstcBtInternData->pfnBit0IntCallback = NULL;
1907  ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIE = 0;
1908  }
1909 
1910  if( (((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIE == 0) &&
1911  (((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIE == 0))
1912  {
1913  Bt_DeInitNvic(pstcBt);
1914  }
1915 
1916  return Ok;
1917 }
1918 #endif
1919 
1931 {
1932  en_int_flag_t enFlag;
1933 
1934  switch (enIntType)
1935  {
1936  case PwcMeasureCompleteInt:
1937  enFlag = ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIR ? PdlSet : PdlClr;
1938  break;
1939  case PwcMeasureOverflowInt:
1940  enFlag = ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIR ? PdlSet : PdlClr;
1941  break;
1942  default:
1943  break;
1944  }
1945 
1946  return enFlag;
1947 }
1948 
1961 {
1962  if((pstcBt == NULL) || (enIntType > PwcMeasureOverflowInt))
1963  {
1964  return ErrorInvalidParameter;
1965  }
1966 
1967  switch (enIntType)
1968  {
1969  case PwcMeasureCompleteInt:
1970  break;
1971  case PwcMeasureOverflowInt:
1972  ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIR = 0;
1973  break;
1974  default:
1975  break;
1976  }
1977 
1978  return Ok;
1979 }
1980 
1994 {
1995  en_int_flag_t enFlag;
1996 
1997  enFlag = ((FM4_BT_PWC_TypeDef*)pstcBt)->STC_f.ERR ? PdlSet : PdlClr;
1998 
1999  return enFlag;
2000 }
2001 
2012 uint16_t Bt_Pwc_Get16BitMeasureData(volatile stc_btn_t* pstcBt)
2013 {
2014  uint16_t u16RegDtbf;
2015  u16RegDtbf = ((FM4_BT_PWC_TypeDef*)pstcBt)->DTBF;
2016  return u16RegDtbf;
2017 }
2018 
2030 uint32_t Bt_Pwc_Get32BitMeasureData(volatile stc_btn_t* pstcBt)
2031 {
2032  uint16_t u16RegLow, u16RegHigh;
2033 
2034  if((pstcBt == &BT1) || (pstcBt == &BT3) || (pstcBt == &BT5) || (pstcBt == &BT7) ||
2035  (pstcBt == &BT9) || (pstcBt == &BT11) || (pstcBt == &BT13) || (pstcBt == &BT15))
2036  return 0;
2037  u16RegLow = ((FM4_BT_PWC_TypeDef*)pstcBt)->DTBF;
2038  u16RegHigh = ((FM4_BT_PWC_TypeDef*)((uint8_t*)pstcBt + 0x40))->DTBF;
2039  return ((u16RegHigh<<16) | u16RegLow);
2040 }
2041 
2056 void Bt_SetSimultaneousStart(uint16_t u16Value)
2057 {
2058  FM4_SBSSR->BTSSSR = u16Value;
2059 }
2060 
2062 
2063 #endif
2064 
2065 /******************************************************************************/
2066 /* EOF (not truncated) */
2067 /******************************************************************************/