| PDL for FM0+ Version1.0 Peripheral Driverl Library for FM0+

- Main Page
- Modules
- Data Structures
- Files

- File List
- Globals

C:/pdl_v10/library/driver/bt/bt.c
Go to the documentation of this file.

    00001 /*******************************************************************************
    00002 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
    00003 *
    00004 * This software is owned and published by:
    00005 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA  94088-3453 ("Spansion").
    00006 *
    00007 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
    00008 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
    00009 *
    00010 * This software contains source code for use with Spansion
    00011 * components. This software is licensed by Spansion to be adapted only
    00012 * for use in systems utilizing Spansion components. Spansion shall not be
    00013 * responsible for misuse or illegal use of this software for devices not
    00014 * supported herein.  Spansion is providing this software "AS IS" and will
    00015 * not be responsible for issues arising from incorrect user implementation
    00016 * of the software.
    00017 *
    00018 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
    00019 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
    00020 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
    00021 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
    00022 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
    00023 * WARRANTY OF NONINFRINGEMENT.
    00024 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
    00025 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
    00026 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
    00027 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
    00028 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
    00029 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
    00030 * SAVINGS OR PROFITS,
    00031 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
    00032 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
    00033 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
    00034 * FROM, THE SOFTWARE.
    00035 *
    00036 * This software may be replicated in part or whole for the licensed use,
    00037 * with the restriction that this Disclaimer and Copyright notice must be
    00038 * included with each copy of this software, whether used in part or whole,
    00039 * at all times.
    00040 */
    00041 /******************************************************************************/
    00052 /******************************************************************************/
    00053 /* Include files                                                              */
    00054 /******************************************************************************/
    00055 #include "bt.h"
    00056
    00057 #if (defined(PDL_PERIPHERAL_BT_ACTIVE))
    00058
    00064
    00065 /******************************************************************************/
    00066 /* Global variable definitions (declared in header file with 'extern')        */
    00067 /******************************************************************************/
    00069 stc_bt_instance_data_t m_astcBtInstanceDataLut[BT_INSTANCE_COUNT] =
    00070 {
    00071 #if (PDL_PERIPHERAL_ENABLE_BT0 == PDL_ON)
    00072     {
    00073         &BT0,                 // pstcInstance
    00074         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00075     },
    00076 #endif
    00077 #if (PDL_PERIPHERAL_ENABLE_BT1 == PDL_ON)
    00078     {
    00079         &BT1,                 // pstcInstance
    00080         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00081     },
    00082 #endif
    00083 #if (PDL_PERIPHERAL_ENABLE_BT2 == PDL_ON)
    00084     {
    00085         &BT2,                 // pstcInstance
    00086         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00087     },
    00088 #endif
    00089 #if (PDL_PERIPHERAL_ENABLE_BT3 == PDL_ON)
    00090     {
    00091         &BT3,                 // pstcInstance
    00092         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00093     },
    00094 #endif
    00095 #if (PDL_PERIPHERAL_ENABLE_BT4 == PDL_ON)
    00096     {
    00097         &BT4,                 // pstcInstance
    00098         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00099     },
    00100 #endif
    00101 #if (PDL_PERIPHERAL_ENABLE_BT5 == PDL_ON)
    00102     {
    00103         &BT5,                 // pstcInstance
    00104         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00105     },
    00106 #endif
    00107 #if (PDL_PERIPHERAL_ENABLE_BT6 == PDL_ON)
    00108     {
    00109         &BT6,                 // pstcInstance
    00110         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00111     },
    00112 #endif
    00113 #if (PDL_PERIPHERAL_ENABLE_BT7 == PDL_ON)
    00114     {
    00115         &BT7,                 // pstcInstance
    00116         {NULL, NULL, NULL}    // stcInternData (not initialized yet)
    00117     },
    00118 #endif
    00119 };
    00120
    00130 static stc_bt_intern_data_t* BtGetInternDataPtr(volatile stc_btn_t* pstcBt)
    00131 {
    00132     uint32_t u32Instance;
    00133
    00134     for (u32Instance = 0; u32Instance < BT_INSTANCE_COUNT; u32Instance++)
    00135     {
    00136         if (pstcBt == m_astcBtInstanceDataLut[u32Instance].pstcInstance)
    00137         {
    00138             return &m_astcBtInstanceDataLut[u32Instance].stcInternData;
    00139         }
    00140     }
    00141
    00142     return NULL;
    00143 }
    00144
    00145 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    00146     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    00147     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    00148     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    00149
    00157 void Bt_IrqHandler( volatile stc_btn_t* pstcBt,
    00158                     stc_bt_intern_data_t* pstcBtInternData)
    00159 {
    00160     uint8_t u8Irq;
    00161
    00162     // Get Interrupt Request
    00163     u8Irq = pstcBt->STC;
    00164      /*
    00165       * Trigger Interrupt (PWM,PPG,Reload timer)
    00166       * Measurement completion Interrupt (PWC)
    00167       */
    00168     if ((u8Irq & (1ul<<2)) == (1ul<<2))
    00169     {
    00170         //  Clear Interrupt
    00171         pstcBt->STC &= ~(1ul<<2);
    00172
    00173         if (pstcBtInternData->pfnBit2IntCallback != NULL)
    00174         {
    00175             // Call CallBackIrq
    00176             pstcBtInternData->pfnBit2IntCallback();
    00177         }
    00178     }
    00179
    00180     // Duty Match Interrupt (PWM)
    00181     if ((u8Irq & (1ul<<1)) == (1ul<<1))
    00182     {
    00183         // Clear Interrupt
    00184         pstcBt->STC &= ~(1ul<<1);
    00185
    00186         if (pstcBtInternData->pfnBit1IntCallback != NULL)
    00187         {
    00188             // Call CallBackIrq
    00189             pstcBtInternData->pfnBit1IntCallback();
    00190         }
    00191     }
    00192
    00193     /*
    00194      * Underflow Interrupt (PWM,PPG,Reload timer)
    00195      * Overflow Interrupt (PWC)
    00196      */
    00197     if ((u8Irq & 1ul) == 1ul)
    00198     {
    00199         // Clear Interrupt
    00200         pstcBt->STC &= ~1ul;
    00201
    00202         if (pstcBtInternData->pfnBit0IntCallback != NULL)
    00203         {
    00204             // Call CallBackIrq
    00205             pstcBtInternData->pfnBit0IntCallback();
    00206         }
    00207     }
    00208
    00209 }
    00210
    00218 static void Bt_InitNvic(volatile stc_btn_t* pstcBt)
    00219 {
    00220     if ((pstcBt == (volatile stc_btn_t*)(&BT0)) || (pstcBt == (volatile stc_btn_t*)(&BT1)) ||
    00221         (pstcBt == (volatile stc_btn_t*)(&BT2)) || (pstcBt == (volatile stc_btn_t*)(&BT3)) ||
    00222         (pstcBt == (volatile stc_btn_t*)(&BT4)) || (pstcBt == (volatile stc_btn_t*)(&BT5)) ||
    00223         (pstcBt == (volatile stc_btn_t*)(&BT6)) || (pstcBt == (volatile stc_btn_t*)(&BT7)))
    00224     {
    00225         NVIC_ClearPendingIRQ(BTIM0_7_FLASH_IRQn);
    00226         NVIC_EnableIRQ(BTIM0_7_FLASH_IRQn);
    00227         NVIC_SetPriority(BTIM0_7_FLASH_IRQn, PDL_IRQ_LEVEL_BT0_7_FLASH);
    00228     }
    00229 }
    00230
    00238 static void Bt_DeInitNvic(volatile stc_btn_t* pstcBt)
    00239 {
    00240     if ((pstcBt == (volatile stc_btn_t*)(&BT0)) || (pstcBt == (volatile stc_btn_t*)(&BT1)) ||
    00241         (pstcBt == (volatile stc_btn_t*)(&BT2)) || (pstcBt == (volatile stc_btn_t*)(&BT3)) ||
    00242         (pstcBt == (volatile stc_btn_t*)(&BT4)) || (pstcBt == (volatile stc_btn_t*)(&BT5)) ||
    00243         (pstcBt == (volatile stc_btn_t*)(&BT6)) || (pstcBt == (volatile stc_btn_t*)(&BT7)))
    00244     {
    00245         /* only when interrupts of ch0~ch7 and Flash are all disabled, disable NVIC */
    00246         if((FM0P_BT0_PWM->STC & 0x70u) != 0x00u)
    00247         {
    00248             return;
    00249         }
    00250         if((FM0P_BT1_PWM->STC & 0x70u) != 0x00u)
    00251         {
    00252             return;
    00253         }
    00254         if((FM0P_BT2_PWM->STC & 0x70u) != 0x00u)
    00255         {
    00256             return;
    00257         }
    00258         if((FM0P_BT3_PWM->STC & 0x70u) != 0x00u)
    00259         {
    00260             return;
    00261         }
    00262         if((FM0P_BT4_PWM->STC & 0x70u) != 0x00u)
    00263         {
    00264             return;
    00265         }
    00266         if((FM0P_BT5_PWM->STC & 0x70u) != 0x00u)
    00267         {
    00268             return;
    00269         }
    00270         if((FM0P_BT6_PWM->STC & 0x70u) != 0x00u)
    00271         {
    00272             return;
    00273         }
    00274         if((FM0P_BT7_PWM->STC & 0x70u) != 0x00u)
    00275         {
    00276             return;
    00277         }
    00278         if((FM0P_FLASH_IF->FICR & 0x03u) != 0x00u)
    00279         {
    00280             return;
    00281         }
    00282         NVIC_ClearPendingIRQ(BTIM0_7_FLASH_IRQn);
    00283         NVIC_DisableIRQ(BTIM0_7_FLASH_IRQn);
    00284         NVIC_SetPriority(BTIM0_7_FLASH_IRQn, PDL_DEFAULT_INTERRUPT_LEVEL);
    00285     }
    00286
    00287     return;
    00288 }
    00289
    00290 #endif
    00291
    00312 en_result_t Bt_ConfigIOMode(volatile stc_btn_t* pstcBt, en_bt_io_mode_t enIoMode)
    00313 {
    00314     // Pointer to internal data
    00315     stc_bt_intern_data_t* pstcBtInternData ;
    00316     // Check for NULL pointer
    00317     if ((pstcBt == NULL) || (enIoMode > BtIoMode8))
    00318     {
    00319         return ErrorInvalidParameter ;
    00320     }
    00321
    00322     // Get pointer to internal data structure ...
    00323     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00324
    00325     if(pstcBtInternData == NULL)
    00326     {
    00327         return ErrorInvalidParameter ;
    00328     }
    00329
    00330     if((pstcBt == &BT0) || (pstcBt == &BT1))
    00331     {
    00332         FM0P_BTIOSEL03->BTSEL0123_f.SEL01 = (uint8_t)enIoMode;
    00333     }
    00334     else if((pstcBt == &BT2) || (pstcBt == &BT3))
    00335     {
    00336         FM0P_BTIOSEL03->BTSEL0123_f.SEL23 = (uint8_t)enIoMode;
    00337     }
    00338     else if((pstcBt == &BT4) || (pstcBt == &BT5))
    00339     {
    00340         FM0P_BTIOSEL47->BTSEL4567_f.SEL45 = (uint8_t)enIoMode;
    00341     }
    00342     else if((pstcBt == &BT6) || (pstcBt == &BT7))
    00343     {
    00344         FM0P_BTIOSEL47->BTSEL4567_f.SEL67 = (uint8_t)enIoMode;
    00345     }
    00346
    00347     return Ok;
    00348 }
    00349
    00366 en_result_t Bt_SelTimerMode(volatile stc_btn_t* pstcBt, en_bt_timer_mode_t enTimerMode)
    00367 {
    00368     // Pointer to internal data
    00369     stc_bt_intern_data_t* pstcBtInternData ;
    00370     // Check for NULL pointer
    00371     if (pstcBt == NULL)
    00372     {
    00373         return ErrorInvalidParameter ;
    00374     }
    00375
    00376     // Get pointer to internal data structure ...
    00377     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00378
    00379     if(pstcBtInternData == NULL)
    00380     {
    00381         return ErrorInvalidParameter ;
    00382     }
    00383
    00384     pstcBt->TMCR &= ~(7ul<<4);
    00385
    00386     // Reset BT at first
    00387     pstcBt->TMCR_f.FMD = 0u;
    00388
    00389     // Set BT mode
    00390     switch (enTimerMode)
    00391     {
    00392         case BtPwmMode:
    00393             pstcBt->TMCR_f.FMD = 1u;
    00394             break;
    00395         case BtPpgMode:
    00396             pstcBt->TMCR_f.FMD = 2u;
    00397             break;
    00398         case BtRtMode:
    00399             pstcBt->TMCR_f.FMD = 3u;
    00400             break;
    00401         case BtPwcMode:
    00402             pstcBt->TMCR_f.FMD = 4u;
    00403             break;
    00404         case BtResetMode:
    00405             break;
    00406         default:
    00407             return ErrorInvalidParameter ;
    00408     }
    00409
    00410     return Ok;
    00411 }
    00412
    00424 en_result_t Bt_Pwm_Init(volatile stc_btn_t* pstcBt, stc_bt_pwm_config_t* pstcPwmConfig)
    00425 {
    00426     volatile FM0P_BT_PWM_TypeDef* pRegBt = (volatile FM0P_BT_PWM_TypeDef*)pstcBt;
    00427     // Pointer to internal data
    00428     stc_bt_intern_data_t* pstcBtInternData ;
    00429     uint16_t u16Pres;
    00430     // Check for NULL pointer
    00431     if (pstcBt == NULL)
    00432     {
    00433         return ErrorInvalidParameter ;
    00434     }
    00435     // Check configuration parameter
    00436     if((pstcPwmConfig->enPres > PwmPres1Div2048)       ||
    00437        (pstcPwmConfig->enRestartEn > PwmRestartEnable) ||
    00438        (pstcPwmConfig->enOutputMask > PwmOutputMask)   ||
    00439        (pstcPwmConfig->enExtTrig > PwmExtTrigBoth)     ||
    00440        (pstcPwmConfig->enOutputPolarity > PwmPolarityHigh) ||
    00441        (pstcPwmConfig->enMode > PwmOneshot) )
    00442     {
    00443         return ErrorInvalidParameter ;
    00444     }
    00445
    00446     // Get pointer to internal data structure ...
    00447     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00448
    00449     if(pstcBtInternData == NULL)
    00450     {
    00451         return ErrorInvalidParameter ;
    00452     }
    00453
    00454     u16Pres = pstcPwmConfig->enPres;  // Workaround for MISRA-C 2004 rule 12.7
    00455
    00456     // Clock prescaler setting
    00457     if(pstcPwmConfig->enPres < PwmPres1Div512)
    00458     {
    00459         pRegBt->TMCR2_f.CKS3 = 0u;
    00460         pRegBt->TMCR_f.CKS = u16Pres;
    00461     }
    00462     else
    00463     {
    00464         pRegBt->TMCR2_f.CKS3 = 1u;
    00465         pRegBt->TMCR_f.CKS = (u16Pres & 0x07u);
    00466     }
    00467     // Restart enable setting
    00468     pRegBt->TMCR_f.RTGEN = (uint8_t)pstcPwmConfig->enRestartEn;
    00469     // Output Mask setting
    00470     pRegBt->TMCR_f.PMSK = (uint8_t)pstcPwmConfig->enOutputMask;
    00471     // Output polarity setting
    00472     pRegBt->TMCR_f.OSEL = (uint8_t)pstcPwmConfig->enOutputPolarity;
    00473     // External trigger setting
    00474     pRegBt->TMCR_f.EGS = (uint8_t)pstcPwmConfig->enExtTrig;
    00475     // Mode setting
    00476     pRegBt->TMCR_f.MDSE = (uint8_t)pstcPwmConfig->enMode;
    00477
    00478     return Ok;
    00479 }
    00480
    00491 en_result_t Bt_Pwm_DeInit(volatile stc_btn_t* pstcBt)
    00492 {
    00493     volatile FM0P_BT_PWM_TypeDef* pRegBt = (volatile FM0P_BT_PWM_TypeDef*)pstcBt;
    00494     // Pointer to internal data
    00495     stc_bt_intern_data_t* pstcBtInternData ;
    00496     // Check for NULL pointer
    00497     if (pstcBt == NULL)
    00498     {
    00499         return ErrorInvalidParameter ;
    00500     }
    00501
    00502     // Get pointer to internal data structure ...
    00503     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00504
    00505     if(pstcBtInternData == NULL)
    00506     {
    00507         return ErrorInvalidParameter ;
    00508     }
    00509
    00510     pRegBt->TMCR = 0x0000u;
    00511     pRegBt->TMCR2 = 0x0000u;
    00512     pRegBt->PCSR = 0x0000u;
    00513     pRegBt->STC = 0x0000u;
    00514     pRegBt->PDUT = 0x0000u;
    00515 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    00516     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    00517     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    00518     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    00519     Bt_DeInitNvic(pstcBt);
    00520 #endif
    00521
    00522     return Ok;
    00523 }
    00524
    00534 en_result_t Bt_Pwm_EnableCount(volatile stc_btn_t* pstcBt)
    00535 {
    00536     // Check for NULL pointer
    00537     if (pstcBt == NULL)
    00538     {
    00539         return ErrorInvalidParameter ;
    00540     }
    00541
    00542     ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
    00543     return Ok;
    00544 }
    00545
    00555 en_result_t Bt_Pwm_DisableCount(volatile stc_btn_t* pstcBt)
    00556 {
    00557     // Check for NULL pointer
    00558     if (pstcBt == NULL)
    00559     {
    00560         return ErrorInvalidParameter ;
    00561     }
    00562
    00563     ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
    00564     return Ok;
    00565 }
    00566
    00576 en_result_t Bt_Pwm_EnableSwTrig(volatile stc_btn_t* pstcBt)
    00577 {
    00578     // Check for NULL pointer
    00579     if (pstcBt == NULL)
    00580     {
    00581         return ErrorInvalidParameter ;
    00582     }
    00583
    00584     ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->TMCR_f.STRG = 1;
    00585     return Ok;
    00586 }
    00587
    00588 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    00589     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    00590     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    00591     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    00592
    00606 en_result_t Bt_Pwm_EnableInt(volatile stc_btn_t* pstcBt, stc_pwm_int_sel_t* pstcIntSel,
    00607                              stc_pwm_int_cb_t*  pstcIntCallback)
    00608 {
    00609     // Pointer to internal data
    00610     stc_bt_intern_data_t* pstcBtInternData ;
    00611     // Check parameter
    00612     if ((pstcBt == NULL) ||
    00613         (pstcIntSel->bPwmDutyMatchInt > 1) ||
    00614         (pstcIntSel->bPwmTrigInt > 1) ||
    00615         (pstcIntSel->bPwmUnderflowInt > 1) ||
    00616         (pstcIntCallback == NULL))
    00617     {
    00618         return ErrorInvalidParameter ;
    00619     }
    00620
    00621     // Get pointer to internal data structure ...
    00622     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00623
    00624     if(pstcBtInternData == NULL)
    00625     {
    00626         return ErrorInvalidParameter ;
    00627     }
    00628
    00629     if(pstcIntSel->bPwmTrigInt == 1)
    00630     {
    00631         pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnPwmTrigIntCallback;
    00632         ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIE = 1;
    00633     }
    00634     if(pstcIntSel->bPwmDutyMatchInt == 1)
    00635     {
    00636         pstcBtInternData->pfnBit1IntCallback = pstcIntCallback->pfnPwmDutyMatchIntCallback;
    00637         ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIE = 1;
    00638     }
    00639     if(pstcIntSel->bPwmUnderflowInt == 1)
    00640     {
    00641         pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnPwmUnderflowIntCallback;
    00642         ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIE = 1;
    00643     }
    00644
    00645     Bt_InitNvic(pstcBt);
    00646
    00647     return Ok;
    00648 }
    00649
    00661 en_result_t Bt_Pwm_DisableInt(volatile stc_btn_t* pstcBt, stc_pwm_int_sel_t* pstcIntSel)
    00662 {
    00663     // Pointer to internal data
    00664     stc_bt_intern_data_t* pstcBtInternData ;
    00665     // Check parameter
    00666     if ((pstcBt == NULL) ||
    00667         (pstcIntSel->bPwmDutyMatchInt > 1) ||
    00668         (pstcIntSel->bPwmTrigInt > 1) ||
    00669         (pstcIntSel->bPwmUnderflowInt > 1))
    00670     {
    00671         return ErrorInvalidParameter ;
    00672     }
    00673
    00674     // Get pointer to internal data structure ...
    00675     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00676
    00677     if(pstcBtInternData == NULL)
    00678     {
    00679         return ErrorInvalidParameter ;
    00680     }
    00681
    00682     if(pstcIntSel->bPwmTrigInt == 1)
    00683     {
    00684         pstcBtInternData->pfnBit2IntCallback = NULL;
    00685         ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIE = 0;
    00686     }
    00687     if(pstcIntSel->bPwmDutyMatchInt == 1)
    00688     {
    00689         pstcBtInternData->pfnBit1IntCallback = NULL;
    00690         ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIE = 0;
    00691     }
    00692     if(pstcIntSel->bPwmUnderflowInt == 1)
    00693     {
    00694         pstcBtInternData->pfnBit0IntCallback = NULL;
    00695         ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIE = 0;
    00696     }
    00697
    00698     if(((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIE == 1)
    00699     {
    00700         return Ok;
    00701     }
    00702
    00703     if(((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIE == 1)
    00704     {
    00705         return Ok;
    00706     }
    00707
    00708     if(((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIE == 1)
    00709     {
    00710         return Ok;
    00711     }
    00712
    00713     Bt_DeInitNvic(pstcBt);
    00714
    00715     return Ok;
    00716 }
    00717
    00718 #endif
    00719
    00733 en_int_flag_t Bt_Pwm_GetIntFlag(volatile stc_btn_t* pstcBt, en_bt_pwm_int_t enIntType)
    00734 {
    00735     en_int_flag_t enFlag;
    00736
    00737     switch (enIntType)
    00738     {
    00739         case PwmTrigInt:
    00740             enFlag = ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIR ? PdlSet : PdlClr;
    00741             break;
    00742         case PwmDutyMatchInt:
    00743             enFlag = ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIR ? PdlSet : PdlClr;
    00744             break;
    00745         case PwmUnderflowInt:
    00746             enFlag = ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIR ? PdlSet : PdlClr;
    00747             break;
    00748         default:
    00749             break;
    00750     }
    00751
    00752     return enFlag;
    00753 }
    00754
    00769 en_result_t Bt_Pwm_ClrIntFlag(volatile stc_btn_t* pstcBt, en_bt_pwm_int_t enIntType)
    00770 {
    00771     if((pstcBt == NULL) || (enIntType > PwmUnderflowInt))
    00772     {
    00773         return ErrorInvalidParameter ;
    00774     }
    00775
    00776     switch (enIntType)
    00777     {
    00778         case PwmTrigInt:
    00779             ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.TGIR = 0;
    00780             break;
    00781         case PwmDutyMatchInt:
    00782             ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.DTIR = 0;
    00783             break;
    00784         case PwmUnderflowInt:
    00785             ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->STC_f.UDIR = 0;
    00786             break;
    00787         default:
    00788             break;
    00789     }
    00790
    00791     return Ok;
    00792 }
    00793
    00804 en_result_t Bt_Pwm_WriteCycleVal(volatile stc_btn_t* pstcBt, uint16_t u16Cycle)
    00805 {
    00806     // Check parameter
    00807     if ((pstcBt == NULL))
    00808     {
    00809         return ErrorInvalidParameter ;
    00810     }
    00811
    00812     ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->PCSR = u16Cycle;
    00813     return Ok;
    00814 }
    00815
    00826 en_result_t Bt_Pwm_WriteDutyVal(volatile stc_btn_t* pstcBt, uint16_t u16Duty)
    00827 {
    00828     // Check parameter
    00829     if ((pstcBt == NULL))
    00830     {
    00831         return ErrorInvalidParameter ;
    00832     }
    00833
    00834     ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->PDUT = u16Duty;
    00835     return Ok;
    00836 }
    00837
    00846 uint16_t Bt_Pwm_ReadCurCnt(volatile stc_btn_t* pstcBt)
    00847 {
    00848     return ((volatile FM0P_BT_PWM_TypeDef*)pstcBt)->TMR;
    00849 }
    00850
    00863 en_result_t Bt_Ppg_Init(volatile stc_btn_t* pstcBt, stc_bt_ppg_config_t* pstcPpgConfig)
    00864 {
    00865     volatile FM0P_BT_PPG_TypeDef* pRegBt = (volatile FM0P_BT_PPG_TypeDef*)pstcBt;
    00866     // Pointer to internal data
    00867     stc_bt_intern_data_t* pstcBtInternData ;
    00868     uint16_t u16Pres;
    00869     // Check for NULL pointer
    00870     if (pstcBt == NULL)
    00871     {
    00872         return ErrorInvalidParameter ;
    00873     }
    00874     // Check configuration parameter
    00875     if((pstcPpgConfig->enPres > PpgPres1Div2048)       ||
    00876        (pstcPpgConfig->enRestartEn > PpgRestartEnable) ||
    00877        (pstcPpgConfig->enOutputMask > PpgOutputMask)   ||
    00878        (pstcPpgConfig->enExtTrig > PpgExtTrigBoth)     ||
    00879        (pstcPpgConfig->enOutputPolarity > PpgPolarityHigh) ||
    00880        (pstcPpgConfig->enMode > PpgOneshot) )
    00881     {
    00882         return ErrorInvalidParameter ;
    00883     }
    00884
    00885     // Get pointer to internal data structure ...
    00886     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00887
    00888     if(pstcBtInternData == NULL)
    00889     {
    00890         return ErrorInvalidParameter ;
    00891     }
    00892
    00893     u16Pres = pstcPpgConfig->enPres; // Workaround for MISRA-C 2004 rule 12.7
    00894
    00895     // Clock prescaler setting
    00896     if(pstcPpgConfig->enPres < PpgPres1Div512)
    00897     {
    00898         pRegBt->TMCR2_f.CKS3 = 0u;
    00899         pRegBt->TMCR_f.CKS = u16Pres;
    00900     }
    00901     else
    00902     {
    00903         pRegBt->TMCR2_f.CKS3 = 1u;
    00904         pRegBt->TMCR_f.CKS = (u16Pres & 0x7u);
    00905     }
    00906     // Restart enable setting
    00907     pRegBt->TMCR_f.RTGEN = (uint8_t)pstcPpgConfig->enRestartEn;
    00908     // Output Mask setting
    00909     pRegBt->TMCR_f.PMSK = (uint8_t)pstcPpgConfig->enOutputMask;
    00910     // Output polarity setting
    00911     pRegBt->TMCR_f.OSEL = (uint8_t)pstcPpgConfig->enOutputPolarity;
    00912     // External trigger setting
    00913     pRegBt->TMCR_f.EGS = (uint8_t)pstcPpgConfig->enExtTrig;
    00914     // Mode setting
    00915     pRegBt->TMCR_f.MDSE = (uint8_t)pstcPpgConfig->enMode;
    00916
    00917     return Ok;
    00918 }
    00919
    00931 en_result_t Bt_Ppg_DeInit(volatile stc_btn_t* pstcBt)
    00932 {
    00933     volatile FM0P_BT_PPG_TypeDef* pRegBt = (volatile FM0P_BT_PPG_TypeDef*)pstcBt;
    00934     // Pointer to internal data
    00935     stc_bt_intern_data_t* pstcBtInternData ;
    00936     // Check for NULL pointer
    00937     if (pstcBt == NULL)
    00938     {
    00939         return ErrorInvalidParameter ;
    00940     }
    00941
    00942     // Get pointer to internal data structure ...
    00943     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    00944
    00945     if(pstcBtInternData == NULL)
    00946     {
    00947         return ErrorInvalidParameter ;
    00948     }
    00949
    00950     pRegBt->TMCR = 0x0000u;
    00951     pRegBt->TMCR2 = 0x0000u;
    00952     pRegBt->STC = 0x0000u;
    00953     pRegBt->PRLL = 0x0000u;
    00954     pRegBt->PRLH = 0x0000u;
    00955
    00956 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    00957     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    00958     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    00959     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    00960     Bt_DeInitNvic(pstcBt);
    00961 #endif
    00962     return Ok;
    00963 }
    00964
    00974 en_result_t Bt_Ppg_EnableCount(volatile stc_btn_t* pstcBt)
    00975 {
    00976     // Check for NULL pointer
    00977     if (pstcBt == NULL)
    00978     {
    00979         return ErrorInvalidParameter ;
    00980     }
    00981
    00982     ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
    00983     return Ok;
    00984 }
    00985
    00995 en_result_t Bt_Ppg_DisableCount(volatile stc_btn_t* pstcBt)
    00996 {
    00997     // Check for NULL pointer
    00998     if (pstcBt == NULL)
    00999     {
    01000         return ErrorInvalidParameter ;
    01001     }
    01002
    01003     ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
    01004     return Ok;
    01005 }
    01006
    01016 en_result_t Bt_Ppg_EnableSwTrig(volatile stc_btn_t* pstcBt)
    01017 {
    01018     // Check for NULL pointer
    01019     if (pstcBt == NULL)
    01020     {
    01021         return ErrorInvalidParameter ;
    01022     }
    01023
    01024     ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->TMCR_f.STRG = 1;
    01025     return Ok;
    01026 }
    01027
    01028 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    01029     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    01030     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    01031     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    01032
    01046 en_result_t Bt_Ppg_EnableInt(volatile stc_btn_t* pstcBt, stc_ppg_int_sel_t* pstcIntSel,
    01047                              stc_ppg_int_cb_t*  pstcIntCallback)
    01048 {
    01049     // Pointer to internal data
    01050     stc_bt_intern_data_t* pstcBtInternData ;
    01051     // Check parameter
    01052     if ((pstcBt == NULL) ||
    01053         (pstcIntSel->bPpgTrigInt > 1) ||
    01054         (pstcIntSel->bPpgUnderflowInt > 1) ||
    01055         (pstcIntCallback == NULL))
    01056     {
    01057         return ErrorInvalidParameter ;
    01058     }
    01059
    01060     // Get pointer to internal data structure ...
    01061     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01062
    01063     if(pstcBtInternData == NULL)
    01064     {
    01065         return ErrorInvalidParameter ;
    01066     }
    01067
    01068     if(pstcIntSel->bPpgTrigInt == 1)
    01069     {
    01070         pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnPpgTrigIntCallback;
    01071         ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIE = 1;
    01072     }
    01073     if(pstcIntSel->bPpgUnderflowInt == 1)
    01074     {
    01075         pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnPpgUnderflowIntCallback;
    01076         ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIE = 1;
    01077     }
    01078
    01079     Bt_InitNvic(pstcBt);
    01080
    01081     return Ok;
    01082 }
    01083
    01096 en_result_t Bt_Ppg_DisableInt(volatile stc_btn_t* pstcBt, stc_ppg_int_sel_t* pstcIntSel)
    01097 {
    01098     // Pointer to internal data
    01099     stc_bt_intern_data_t* pstcBtInternData ;
    01100     // Check parameter
    01101     if ((pstcBt == NULL) ||
    01102         (pstcIntSel->bPpgTrigInt > 1) ||
    01103         (pstcIntSel->bPpgUnderflowInt > 1)  )
    01104     {
    01105         return ErrorInvalidParameter ;
    01106     }
    01107
    01108     // Get pointer to internal data structure ...
    01109     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01110
    01111     if(pstcBtInternData == NULL)
    01112     {
    01113         return ErrorInvalidParameter ;
    01114     }
    01115
    01116     if(pstcIntSel->bPpgTrigInt == 1)
    01117     {
    01118         pstcBtInternData->pfnBit2IntCallback = NULL;
    01119         ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIE = 0;
    01120     }
    01121     if(pstcIntSel->bPpgUnderflowInt == 1)
    01122     {
    01123         pstcBtInternData->pfnBit0IntCallback = NULL;
    01124         ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIE = 0;
    01125     }
    01126
    01127     if(((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIE == 1)
    01128     {
    01129         return Ok;
    01130     }
    01131
    01132     if(((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIE == 1)
    01133     {
    01134         return Ok;
    01135     }
    01136
    01137     Bt_DeInitNvic(pstcBt);
    01138
    01139     return Ok;
    01140 }
    01141
    01142 #endif
    01143
    01156 en_int_flag_t Bt_Ppg_GetIntFlag(volatile stc_btn_t* pstcBt, en_bt_ppg_int_t enIntType)
    01157 {
    01158     en_int_flag_t enFlag;
    01159
    01160     switch (enIntType)
    01161     {
    01162         case PpgTrigInt:
    01163             enFlag = ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIR ? PdlSet : PdlClr;
    01164             break;
    01165         case PpgUnderflowInt:
    01166             enFlag = ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIR ? PdlSet : PdlClr;
    01167             break;
    01168         default:
    01169             break;
    01170     }
    01171
    01172     return enFlag;
    01173 }
    01174
    01189 en_result_t Bt_Ppg_ClrIntFlag(volatile stc_btn_t* pstcBt, en_bt_ppg_int_t enIntType)
    01190 {
    01191     if((pstcBt == NULL) || (enIntType > PpgUnderflowInt))
    01192     {
    01193         return ErrorInvalidParameter;
    01194     }
    01195
    01196     switch (enIntType)
    01197     {
    01198         case PpgTrigInt:
    01199             ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.TGIR = 0;
    01200             break;
    01201         case PpgUnderflowInt:
    01202             ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->STC_f.UDIR = 0;
    01203             break;
    01204         default:
    01205             break;
    01206     }
    01207
    01208     return Ok;
    01209 }
    01210
    01221 en_result_t Bt_Ppg_WriteLowWidthVal(volatile stc_btn_t* pstcBt, uint16_t u16Val)
    01222 {
    01223     // Check parameter
    01224     if ((pstcBt == NULL))
    01225     {
    01226         return ErrorInvalidParameter ;
    01227     }
    01228
    01229     ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->PRLL = u16Val;
    01230     return Ok;
    01231 }
    01232
    01243 en_result_t Bt_Ppg_WriteHighWidthVal(volatile stc_btn_t* pstcBt, uint16_t u16Val)
    01244 {
    01245     // Check parameter
    01246     if ((pstcBt == NULL))
    01247     {
    01248         return ErrorInvalidParameter ;
    01249     }
    01250
    01251     ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->PRLH = u16Val;
    01252     return Ok;
    01253 }
    01254
    01263 uint16_t Bt_Ppg_ReadCurCnt(volatile stc_btn_t* pstcBt)
    01264 {
    01265     return ((volatile FM0P_BT_PPG_TypeDef*)pstcBt)->TMR;
    01266 }
    01267
    01280 en_result_t Bt_Rt_Init(volatile stc_btn_t* pstcBt, stc_bt_rt_config_t* pstcRtConfig)
    01281 {
    01282     volatile FM0P_BT_RT_TypeDef* pRegBt = (volatile FM0P_BT_RT_TypeDef*) pstcBt;
    01283     // Pointer to internal data
    01284     stc_bt_intern_data_t* pstcBtInternData ;
    01285     uint16_t u16Pres, u16ExtTrig;
    01286     // Check for NULL pointer
    01287     if (pstcBt == NULL)
    01288     {
    01289         return ErrorInvalidParameter ;
    01290     }
    01291     // Check configuration parameter
    01292     if((pstcRtConfig->enPres > RtPres1Div2048)           ||
    01293        (pstcRtConfig->enSize > RtSize32Bit)              ||
    01294        (pstcRtConfig->enExtTrig > RtExtTiggerHighLevel)  ||
    01295        (pstcRtConfig->enOutputPolarity > RtPolarityHigh) ||
    01296        (pstcRtConfig->enMode > RtOneshot) )
    01297     {
    01298         return ErrorInvalidParameter ;
    01299     }
    01300
    01301     // Get pointer to internal data structure ...
    01302     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01303
    01304     if(pstcBtInternData == NULL)
    01305     {
    01306         return ErrorInvalidParameter ;
    01307     }
    01308
    01309     // Workaround for MISRA-C 2004 rule 12.7
    01310     u16Pres = pstcRtConfig->enPres;
    01311
    01312     // Clock prescaler setting
    01313     if(pstcRtConfig->enPres < RtPres1Div512)
    01314     {
    01315         pRegBt->TMCR2_f.CKS3 = 0;
    01316         pRegBt->TMCR_f.CKS = u16Pres;
    01317     }
    01318     else
    01319     {
    01320         pRegBt->TMCR2_f.CKS3 = 1;
    01321         pRegBt->TMCR_f.CKS = (u16Pres & 0x7u);
    01322
    01323     }
    01324     // Timer size setting
    01325     pRegBt->TMCR_f.T32 = (uint8_t)pstcRtConfig->enSize;
    01326     // Output polarity setting
    01327     pRegBt->TMCR_f.OSEL = (uint8_t)pstcRtConfig->enOutputPolarity;
    01328
    01329     // Workaround for MISRA-C 2004 rule 12.7
    01330     u16ExtTrig = pstcRtConfig->enExtTrig;
    01331
    01332     // External trigger setting
    01333     if(pstcRtConfig->enExtTrig <= RtExtTiggerBothEdge)
    01334     {
    01335         pRegBt->TMCR2_f.GATE = 0u;
    01336         pRegBt->TMCR_f.EGS = u16ExtTrig;
    01337     }
    01338     else if (pstcRtConfig->enExtTrig == RtExtTiggerLowLevel)
    01339     {
    01340         pRegBt->TMCR2_f.GATE = 1u;
    01341         pRegBt->TMCR_f.EGS = 0u;
    01342     }
    01343     else if (pstcRtConfig->enExtTrig == RtExtTiggerHighLevel)
    01344     {
    01345         pRegBt->TMCR2_f.GATE = 1u;
    01346         pRegBt->TMCR_f.EGS = 1u;
    01347     }
    01348     // Mode setting
    01349     pRegBt->TMCR_f.MDSE = (uint8_t)pstcRtConfig->enMode;
    01350
    01351     return Ok;
    01352 }
    01353
    01354
    01366 en_result_t Bt_Rt_DeInit(volatile stc_btn_t* pstcBt)
    01367 {
    01368     volatile FM0P_BT_RT_TypeDef* pRegBt = (volatile FM0P_BT_RT_TypeDef*)pstcBt;
    01369     // Pointer to internal data
    01370     stc_bt_intern_data_t* pstcBtInternData ;
    01371     // Check for NULL pointer
    01372     if (pstcBt == NULL)
    01373     {
    01374         return ErrorInvalidParameter ;
    01375     }
    01376
    01377     // Get pointer to internal data structure ...
    01378     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01379
    01380     if(pstcBtInternData == NULL)
    01381     {
    01382         return ErrorInvalidParameter ;
    01383     }
    01384
    01385     pRegBt->TMCR = 0x0000u;
    01386     pRegBt->TMCR2 = 0x0000u;
    01387     pRegBt->STC = 0x0000u;
    01388     pRegBt->PCSR = 0x0000u;
    01389
    01390 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    01391     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    01392     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    01393     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    01394     Bt_DeInitNvic(pstcBt);
    01395 #endif
    01396     return Ok;
    01397 }
    01398
    01399
    01409 en_result_t Bt_Rt_EnableCount(volatile stc_btn_t* pstcBt)
    01410 {
    01411     // Check for NULL pointer
    01412     if (pstcBt == NULL)
    01413     {
    01414         return ErrorInvalidParameter ;
    01415     }
    01416
    01417     ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
    01418     return Ok;
    01419 }
    01420
    01430 en_result_t Bt_Rt_DisableCount(volatile stc_btn_t* pstcBt)
    01431 {
    01432     // Check for NULL pointer
    01433     if (pstcBt == NULL)
    01434     {
    01435         return ErrorInvalidParameter ;
    01436     }
    01437
    01438     ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
    01439     return Ok;
    01440 }
    01441
    01451 en_result_t Bt_Rt_EnableSwTrig(volatile stc_btn_t* pstcBt)
    01452 {
    01453     // Check for NULL pointer
    01454     if (pstcBt == NULL)
    01455     {
    01456         return ErrorInvalidParameter ;
    01457     }
    01458
    01459     ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->TMCR_f.STRG = 1;
    01460     return Ok;
    01461 }
    01462
    01463 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    01464     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    01465     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    01466     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    01467
    01481 en_result_t Bt_Rt_EnableInt(volatile stc_btn_t* pstcBt, stc_rt_int_sel_t* pstcIntSel,
    01482                             stc_rt_int_cb_t*  pstcIntCallback)
    01483 {
    01484     // Pointer to internal data
    01485     stc_bt_intern_data_t* pstcBtInternData ;
    01486     // Check parameter
    01487     if ((pstcBt == NULL) ||
    01488         (pstcIntSel->bRtTrigInt > 1) ||
    01489         (pstcIntSel->bRtUnderflowInt > 1) ||
    01490         (pstcIntCallback == NULL))
    01491     {
    01492         return ErrorInvalidParameter ;
    01493     }
    01494
    01495     // Get pointer to internal data structure ...
    01496     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01497
    01498     if(pstcBtInternData == NULL)
    01499     {
    01500         return ErrorInvalidParameter ;
    01501     }
    01502
    01503     if(pstcIntSel->bRtTrigInt == 1)
    01504     {
    01505         pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnRtTrigIntCallback;
    01506         ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.TGIE = 1;
    01507     }
    01508     if(pstcIntSel->bRtUnderflowInt == 1)
    01509     {
    01510         pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnRtUnderflowIntCallback;
    01511         ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.UDIE = 1;
    01512     }
    01513
    01514     Bt_InitNvic(pstcBt);
    01515
    01516     return Ok;
    01517 }
    01518
    01531 en_result_t Bt_Rt_DisableInt(volatile stc_btn_t* pstcBt, stc_rt_int_sel_t* pstcIntSel)
    01532 {
    01533     // Pointer to internal data
    01534     stc_bt_intern_data_t* pstcBtInternData ;
    01535     // Check parameter
    01536     if ((pstcBt == NULL) ||
    01537         (pstcIntSel->bRtTrigInt > 1) ||
    01538         (pstcIntSel->bRtUnderflowInt > 1)  )
    01539     {
    01540         return ErrorInvalidParameter ;
    01541     }
    01542
    01543     // Get pointer to internal data structure ...
    01544     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01545
    01546     if(pstcBtInternData == NULL)
    01547     {
    01548         return ErrorInvalidParameter ;
    01549     }
    01550
    01551     if(pstcIntSel->bRtTrigInt == 1)
    01552     {
    01553         pstcBtInternData->pfnBit2IntCallback = NULL;
    01554         ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.TGIE = 0;
    01555     }
    01556     if(pstcIntSel->bRtUnderflowInt == 1)
    01557     {
    01558         pstcBtInternData->pfnBit0IntCallback = NULL;
    01559         ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.UDIE = 0;
    01560     }
    01561
    01562     Bt_DeInitNvic(pstcBt);
    01563
    01564     return Ok;
    01565 }
    01566
    01567 #endif
    01568
    01581 en_int_flag_t Bt_Rt_GetIntFlag(volatile stc_btn_t* pstcBt, en_bt_rt_int_t enIntType)
    01582 {
    01583     en_int_flag_t enFlag;
    01584
    01585     switch (enIntType)
    01586     {
    01587         case RtTrigInt:
    01588             enFlag = ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.TGIR ? PdlSet : PdlClr;
    01589             break;
    01590         case RtUnderflowInt:
    01591             enFlag = ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.UDIR ? PdlSet : PdlClr;
    01592             break;
    01593         default:
    01594             break;
    01595     }
    01596
    01597     return enFlag;
    01598 }
    01599
    01614 en_result_t Bt_Rt_ClrIntFlag(volatile stc_btn_t* pstcBt, en_bt_rt_int_t enIntType)
    01615 {
    01616     if((pstcBt == NULL) || (enIntType > RtUnderflowInt))
    01617     {
    01618         return ErrorInvalidParameter;
    01619     }
    01620
    01621     switch (enIntType)
    01622     {
    01623         case RtTrigInt:
    01624             ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.TGIR = 0;
    01625             break;
    01626         case RtUnderflowInt:
    01627             ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->STC_f.UDIR = 0;
    01628             break;
    01629         default:
    01630             break;
    01631     }
    01632
    01633     return Ok;
    01634 }
    01635
    01646 en_result_t Bt_Rt_WriteCycleVal(volatile stc_btn_t* pstcBt, uint16_t u16Val)
    01647 {
    01648     // Check parameter
    01649     if ((pstcBt == NULL))
    01650     {
    01651         return ErrorInvalidParameter ;
    01652     }
    01653
    01654     ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->PCSR = u16Val;
    01655     return Ok;
    01656 }
    01657
    01666 uint16_t Bt_Rt_ReadCurCnt(volatile stc_btn_t* pstcBt)
    01667 {
    01668     return ((volatile FM0P_BT_RT_TypeDef*)pstcBt)->TMR;
    01669 }
    01670
    01683 en_result_t Bt_Pwc_Init(volatile stc_btn_t* pstcBt, stc_bt_pwc_config_t* pstcPwcConfig)
    01684 {
    01685     volatile FM0P_BT_PWC_TypeDef* pRegBt = (volatile FM0P_BT_PWC_TypeDef*) pstcBt;
    01686     // Pointer to internal data
    01687     stc_bt_intern_data_t* pstcBtInternData ;
    01688     uint16_t u16Pres;
    01689     // Check for NULL pointer
    01690     if (pstcBt == NULL)
    01691     {
    01692         return ErrorInvalidParameter ;
    01693     }
    01694     // Check configuration parameter
    01695     if((pstcPwcConfig->enPres > PwcPres1Div2048)           ||
    01696        (pstcPwcConfig->enSize > PwcSize32Bit)              ||
    01697        (pstcPwcConfig->enMeasureEdge > PwcMeasureFallingToRising)  ||
    01698        (pstcPwcConfig->enMode > PwcOneshot) )
    01699     {
    01700         return ErrorInvalidParameter ;
    01701     }
    01702
    01703     // Get pointer to internal data structure ...
    01704     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01705
    01706     if(pstcBtInternData == NULL)
    01707     {
    01708         return ErrorInvalidParameter ;
    01709     }
    01710
    01711     // workaround for MISRA-C 2004 rule 12.7
    01712     u16Pres = pstcPwcConfig->enPres;
    01713
    01714     // Clock prescaler setting
    01715     if(pstcPwcConfig->enPres < PwcPres1Div512)
    01716     {
    01717         pRegBt->TMCR2_f.CKS3 = 0u;
    01718         pRegBt->TMCR_f.CKS = u16Pres;
    01719     }
    01720     else
    01721     {
    01722         pRegBt->TMCR2_f.CKS3 = 1u;
    01723         pRegBt->TMCR_f.CKS = (u16Pres & 0x07u);
    01724     }
    01725     // Timer size setting
    01726     pRegBt->TMCR_f.T32 = (uint8_t)pstcPwcConfig->enSize;
    01727     // Measurement mode setting
    01728     pRegBt->TMCR_f.EGS = (uint8_t)pstcPwcConfig->enMeasureEdge;
    01729     // Mode setting
    01730     pRegBt->TMCR_f.MDSE = (uint8_t)pstcPwcConfig->enMode;
    01731
    01732     return Ok;
    01733 }
    01734
    01746 en_result_t Bt_Pwc_DeInit(volatile stc_btn_t* pstcBt)
    01747 {
    01748     volatile FM0P_BT_PWC_TypeDef* pRegBt = (volatile FM0P_BT_PWC_TypeDef*)pstcBt;
    01749     // Pointer to internal data
    01750     stc_bt_intern_data_t* pstcBtInternData ;
    01751     // Check for NULL pointer
    01752     if (pstcBt == NULL)
    01753     {
    01754         return ErrorInvalidParameter ;
    01755     }
    01756
    01757     // Get pointer to internal data structure ...
    01758     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01759
    01760     if(pstcBtInternData == NULL)
    01761     {
    01762         return ErrorInvalidParameter ;
    01763     }
    01764
    01765     pRegBt->TMCR = 0x0000u;
    01766     pRegBt->TMCR2 = 0x0000u;
    01767     pRegBt->STC = 0x0000u;
    01768
    01769 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    01770     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    01771     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    01772     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    01773     Bt_DeInitNvic(pstcBt);
    01774 #endif
    01775     return Ok;
    01776 }
    01777
    01787 en_result_t Bt_Pwc_EnableCount(volatile stc_btn_t* pstcBt)
    01788 {
    01789     // Check for NULL pointer
    01790     if (pstcBt == NULL)
    01791     {
    01792         return ErrorInvalidParameter ;
    01793     }
    01794
    01795     ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->TMCR_f.CTEN = 1;
    01796     return Ok;
    01797 }
    01798
    01808 en_result_t Bt_Pwc_DisableCount(volatile stc_btn_t* pstcBt)
    01809 {
    01810     // Check for NULL pointer
    01811     if (pstcBt == NULL)
    01812     {
    01813         return ErrorInvalidParameter ;
    01814     }
    01815
    01816     ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->TMCR_f.CTEN = 0;
    01817     return Ok;
    01818 }
    01819
    01820 #if (PDL_INTERRUPT_ENABLE_BT0 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT1 == PDL_ON) || \
    01821     (PDL_INTERRUPT_ENABLE_BT2 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT3 == PDL_ON) || \
    01822     (PDL_INTERRUPT_ENABLE_BT4 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT5 == PDL_ON) || \
    01823     (PDL_INTERRUPT_ENABLE_BT6 == PDL_ON) ||  (PDL_INTERRUPT_ENABLE_BT7 == PDL_ON)
    01824
    01838 en_result_t Bt_Pwc_EnableInt(volatile stc_btn_t* pstcBt, stc_pwc_int_sel_t* pstcIntSel,
    01839                              stc_pwc_int_cb_t*  pstcIntCallback)
    01840 {
    01841     // Pointer to internal data
    01842     stc_bt_intern_data_t* pstcBtInternData ;
    01843     // Check parameter
    01844     if ((pstcBt == NULL) ||
    01845         (pstcIntSel->bPwcMeasCmpInt > 1) ||
    01846         (pstcIntSel->bPwcMeasOverflowInt > 1) ||
    01847         (pstcIntCallback == NULL))
    01848     {
    01849         return ErrorInvalidParameter ;
    01850     }
    01851
    01852     // Get pointer to internal data structure ...
    01853     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01854
    01855     if(pstcBtInternData == NULL)
    01856     {
    01857         return ErrorInvalidParameter ;
    01858     }
    01859
    01860     if(pstcIntSel->bPwcMeasCmpInt == 1)
    01861     {
    01862         pstcBtInternData->pfnBit2IntCallback = pstcIntCallback->pfnPwcMeasCmpCallback;
    01863         ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIE = 1;
    01864     }
    01865     if(pstcIntSel->bPwcMeasOverflowInt == 1)
    01866     {
    01867         pstcBtInternData->pfnBit0IntCallback = pstcIntCallback->pfnPwcMeasOverflowCallback;
    01868         ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIE = 1;
    01869     }
    01870
    01871     Bt_InitNvic(pstcBt);
    01872
    01873     return Ok;
    01874 }
    01875
    01888 en_result_t Bt_Pwc_DisableInt(volatile stc_btn_t* pstcBt, stc_pwc_int_sel_t* pstcIntSel)
    01889 {
    01890     // Pointer to internal data
    01891     stc_bt_intern_data_t* pstcBtInternData ;
    01892     // Check parameter
    01893     if ((pstcBt == NULL) ||
    01894         (pstcIntSel->bPwcMeasCmpInt > 1) ||
    01895         (pstcIntSel->bPwcMeasOverflowInt > 1))
    01896     {
    01897         return ErrorInvalidParameter ;
    01898     }
    01899
    01900     // Get pointer to internal data structure ...
    01901     pstcBtInternData = BtGetInternDataPtr( pstcBt ) ;
    01902
    01903     if(pstcBtInternData == NULL)
    01904     {
    01905         return ErrorInvalidParameter ;
    01906     }
    01907
    01908     if(pstcIntSel->bPwcMeasCmpInt == 1)
    01909     {
    01910         pstcBtInternData->pfnBit2IntCallback = NULL;
    01911         ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIE = 0;
    01912     }
    01913     if(pstcIntSel->bPwcMeasOverflowInt == 1)
    01914     {
    01915         pstcBtInternData->pfnBit0IntCallback = NULL;
    01916         ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIE = 0;
    01917     }
    01918
    01919     if(((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIE == 1)
    01920     {
    01921         return Ok;
    01922     }
    01923
    01924     if(((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIE == 1)
    01925     {
    01926         return Ok;
    01927     }
    01928
    01929     Bt_DeInitNvic(pstcBt);
    01930
    01931     return Ok;
    01932 }
    01933 #endif
    01934
    01947 en_int_flag_t Bt_Pwc_GetIntFlag(volatile stc_btn_t* pstcBt, en_bt_pwc_int_t enIntType)
    01948 {
    01949     en_int_flag_t enFlag;
    01950
    01951     switch (enIntType)
    01952     {
    01953         case PwcMeasureCompleteInt:
    01954             enFlag = ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.EDIR ? PdlSet : PdlClr;
    01955             break;
    01956         case PwcMeasureOverflowInt:
    01957             enFlag = ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIR ? PdlSet : PdlClr;
    01958             break;
    01959         default:
    01960             break;
    01961     }
    01962
    01963     return enFlag;
    01964 }
    01965
    01980 en_result_t Bt_Pwc_ClrIntFlag(volatile stc_btn_t* pstcBt, en_bt_pwc_int_t enIntType)
    01981 {
    01982     if((pstcBt == NULL) || (enIntType > PwcMeasureOverflowInt))
    01983     {
    01984         return ErrorInvalidParameter;
    01985     }
    01986
    01987     switch (enIntType)
    01988     {
    01989         case PwcMeasureCompleteInt:
    01990             break;
    01991         case PwcMeasureOverflowInt:
    01992             ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.OVIR = 0;
    01993             break;
    01994         default:
    01995             break;
    01996     }
    01997
    01998     return Ok;
    01999 }
    02000
    02013 en_stat_flag_t Bt_Pwc_GetErrorFlag(volatile stc_btn_t* pstcBt)
    02014 {
    02015     en_int_flag_t enFlag;
    02016
    02017     enFlag = ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->STC_f.ERR ? PdlSet : PdlClr;
    02018
    02019     return enFlag;
    02020 }
    02021
    02032 uint16_t Bt_Pwc_Get16BitMeasureData(volatile stc_btn_t* pstcBt)
    02033 {
    02034     uint16_t u16RegDtbf;
    02035     u16RegDtbf = ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->DTBF;
    02036     return u16RegDtbf;
    02037 }
    02038
    02050 uint32_t Bt_Pwc_Get32BitMeasureData(volatile stc_btn_t* pstcBt)
    02051 {
    02052     uint16_t u16RegLow, u16RegHigh;
    02053
    02054     if((pstcBt == &BT1) || (pstcBt == &BT3) || (pstcBt == &BT5) || (pstcBt == &BT7))
    02055     {
    02056         return 0;
    02057     }
    02058     u16RegLow = ((volatile FM0P_BT_PWC_TypeDef*)pstcBt)->DTBF;
    02059     u16RegHigh = ((volatile FM0P_BT_PWC_TypeDef*)((volatile uint8_t*)pstcBt + 0x40))->DTBF;
    02060     return (((uint32_t)u16RegHigh<<16) | (uint32_t)u16RegLow);
    02061 }
    02062
    02078 void Bt_SetSimultaneousStart(uint16_t u16Value)
    02079 {
    02080     FM0P_SBSSR->BTSSSR = u16Value;
    02081 }
    02082
    02084
    02085 #endif