⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drv_ftm_pwm.c

📁 最新版IAR FOR ARM(EWARM)5.11中的代码例子
💻 C
字号:
/************************************************************************/
/*                                                                      */
/*    Copyright (C) 2006 Oki Electric Industry Co., LTD.                */
/*                                                                      */
/*    System Name    :  ML675050 series                                 */
/*    Module Name    :  ML675050 ftm_pwm driver program                 */
/*    File   Name    :  drv_ftm_pwm.c                                   */
/*    Date           :  2006/01/16 initial version                      */
/*                                                                      */
/************************************************************************/

#include "common.h"
#if defined(__arm)
#include "ml675050.h"
#else
#include "ml675050sim.h"
#endif
#include "intrinsics.h"
#include "hal_api.h"
#include "drv_common.h"
#include "drv_ftm_pwm.h"


#define FTM_PORT1_SEL_MASK 0xff00ffff
#define FTM_PORT1_SEL_SET  0x00550000
#define FTM_PORT4_SEL_MASK 0x3fff3fff
#define FTM_PORT4_SEL_SET  0x40004000

/******************************/
/*     Private defines        */
/******************************/
/*--------- variable ---------*/
static volatile uint16_t pwm_counter = 0;

/*--------- function ---------*/
static void pwm1_callback(uint16_t state);


/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_OpenFtm_Pwm                                */
/*  Input           : struct ML675050_FtmPwm                            */
/*  Output          : DRV_OK(1)                                         */
/*                  : DRV_PARAM_ERROR(-2)                               */
/*                                                                      */
/*  Note : Open the driver of ftm_pwm.                                  */
/*                                                                      */
/************************************************************************/
int16_t smpDrv_OpenFtm_Pwm(void)
{
    int16_t rtnVal = DRV_OK;
    uPLAT_PioFunctionParam port_param;
    uPLAT_InterruptParam int_param;

    /* Get port function. */
    rtnVal = uplat7dHAL_PioGetFunction(&port_param);
    if (rtnVal != DRV_OK) {
        return rtnVal;
    }
    /* Set FTM port Secondary function. */
    port_param.port_sel1 = (port_param.port_sel1 & FTM_PORT1_SEL_MASK)
                           | FTM_PORT1_SEL_SET;
    port_param.port_sel4 = (port_param.port_sel4 & FTM_PORT4_SEL_MASK)
                           | FTM_PORT4_SEL_SET;
    rtnVal = uplat7dHAL_PioSetFunction(&port_param);
    if (rtnVal != DRV_OK) {
        return rtnVal;
    }

    int_param.primary = INT_FTM1_PWM;
    int_param.level = EXILCA_ILC16 & INT_LV1;
    int_param.callback = pwm1_callback;
    rtnVal = uplat7dHAL_InterruptSetPrimary(&int_param);
    if (rtnVal != DRV_OK) {
        return rtnVal;
    }

    /* Initailize FTM_PWM HAL. */
    rtnVal = ml675050HAL_FtmPwmInit();

    /* Enable irq interrupt. */
    __enable_interrupt(); //irq_en();

    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_CloseFtm_Pwm                               */
/*  Input           : void                                              */
/*  Output          : DRV_OK(1)                                         */
/*                                                                      */
/*  Note : Close the driver of ftm_pwm.                                 */
/*                                                                      */
/************************************************************************/
int16_t smpDrv_CloseFtm_Pwm(void)
{
    int16_t rtnVal = DRV_OK;

    __disable_interrupt(); //irq_dis();                              /* Disable Interrupt */

    return rtnVal;
}

/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_IoctlFtm_Pwm                               */
/*  Input           : cmd The number of PWM HAL-API.                    */
/*                    arg The argument of PWM HAL-API.                  */
/*  Output          : DRV_OK(1)                                         */
/*                  : DRV_PARAM_ERROR(-2)                               */
/*                                                                      */
/*  Note : Control the driver of ftm_pwm.                               */
/*                                                                      */
/************************************************************************/
int16_t smpDrv_IoctlFtm_Pwm(uint16_t cmd, uint32_t arg)
{
    int16_t rtnVal = DRV_OK;
    struct ML675050_FtmPwm *pwm;

    switch (cmd) {
    case ML675050_HAL_FTM_PWM_SET:
        pwm = (struct ML675050_FtmPwm *)arg;
        if (pwm->cycle != 65536 - (10 * FTM_APB_CLK * 1000) / 16){
            rtnVal = DRV_PARAM_ERROR;
        }
        if (rtnVal == DRV_OK) {
            rtnVal = ml675050HAL_FtmPwmSet (pwm);
        }
        break;
    case ML675050_HAL_FTM_PWM_START: 
        if ((arg != 1) && (arg != 0)) {
            rtnVal = DRV_PARAM_ERROR;
        }
        if (rtnVal == DRV_OK) {
            rtnVal = ml675050HAL_FtmPwmStart((uint16_t)arg);
        }        
        break;
    default :
        rtnVal = DRV_PARAM_ERROR;
        break;
    }

    return rtnVal;
}
/************************************************************************/
/*                                                                      */
/*  Function Name   : smpDrv_PollTimer                                  */
/*  Input           : cmd   None                                        */
/*                    count Count of waiting cycle.                     */
/*  Output          : void                                              */
/*                                                                      */
/*  Note : Delay to wait.                                               */
/*                                                                      */
/************************************************************************/
void smpDrv_PollFtm_Pwm(uint16_t cmd, uint16_t count)
{
    pwm_counter = 0;
    while (pwm_counter < count) {
        ;
    }
}
/************************************************************************/
/*                                                                      */
/*  Function Name   : pwm0_callback                                     */
/*  Input           : state                                             */
/*  Output          : void                                              */
/*                                                                      */
/*  Note : PWM  call back function.                                     */
/*                                                                      */
/************************************************************************/
static void pwm1_callback(uint16_t state)
{
    pwm_counter++;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -