TI-AM335X A8的EHRPWM双路输出代码分享
分享一个做A8控制两路电机的写的一个驱动
- /*****************************************Copyright(C)******************************************
- *******************************************XXXX*********************************************
- **-----------------------------------------文件信息---------------------------------------------
- **文 件 名: bsp_pwm.c
- **硬 件: am335x
- **创 建 人: WB
- **创 建 日 期: 2015-01-21
- **最 新 版 本: V0.1
- **描 述: EHRPWM配置文件
- **---------------------------------------历史版本信息-------------------------------------------
- **修 改 人:
- **日 期:
- **版 本:
- **描 述:
- **----------------------------------------------------------------------------------------------
- ***********************************************************************************************/
- /* Includes-----------------------------------------------------------------------------------*/
- #include "include.h"
- #include "bsp_pwm.h"
- #include "soc_AM335x.h"
- #include "evmAM335x.h"
- #include "ehrpwm.h"
- #include "hw_control_AM335x.h"
- #include "hw_cm_per.h"
- #include "hw_types.h"
- /* Private define-----------------------------------------------------------------------------*/
- #define CLOCK_DIV_VAL (10) //分频,也就是计数时钟(SOC_EHRPWM_2_MODULE_FREQ/CLOCK_DIV_VAL)
- #define SOC_EHRPWM_2_MODULE_FREQ (100000000) //模块时钟
- /* Private typedef----------------------------------------------------------------------------*/
- /* Private macro------------------------------------------------------------------------------*/
- //PWMSS基地址
- const INT32U PWMSS_REGS_Table[3] = {
- SOC_PWMSS0_REGS,
- SOC_PWMSS1_REGS,
- SOC_PWMSS2_REGS,
- };
- //EHRPWM基地址
- const INT32U EPWM_REGS_Table[3] = {
- SOC_EPWM_0_REGS,
- SOC_EPWM_1_REGS,
- SOC_EPWM_2_REGS,
- };
- /* Private variables--------------------------------------------------------------------------*/
- /* Private function prototypes----------------------------------------------------------------*/
- /* Private functions--------------------------------------------------------------------------*/
- /***********************************************************************************************
- * Function Name : EHRPWMConfigure
- * Description :PWM配置主要是配置占空比
- * Input :
- * Return :
- * Note(s) :
- * Contributor : 150204 WB
- ***********************************************************************************************/
- void EHRPWMConfigure(_BSPEHRPWM_CONFIG *epwmconfig)
- {
-
-
- /* TimeBase configuration */
- /* Configure the clock frequency */
- EHRPWMTimebaseClkConfig(EPWM_REGS_Table[epwmconfig->EPWMOutNum],
- SOC_EHRPWM_2_MODULE_FREQ/CLOCK_DIV_VAL,
- SOC_EHRPWM_2_MODULE_FREQ);
- /* Configure the period of the output waveform */
- EHRPWMPWMOpFreqSet(EPWM_REGS_Table[epwmconfig->EPWMOutNum],
- SOC_EHRPWM_2_MODULE_FREQ/CLOCK_DIV_VAL,//计数时钟
- (unsigned int)(SOC_EHRPWM_2_MODULE_FREQ/CLOCK_DIV_VAL)/epwmconfig->EPWMLoad,//255计数重载
- (unsigned int)EHRPWM_COUNT_UP,//向上计数
- (bool)EHRPWM_SHADOW_WRITE_DISABLE);
- /* Disable synchronization*/
- EHRPWMTimebaseSyncDisable(EPWM_REGS_Table[epwmconfig->EPWMOutNum]);
- /* Disable syncout*/
- EHRPWMSyncOutModeSet(EPWM_REGS_Table[epwmconfig->EPWMOutNum], EHRPWM_SYNCOUT_DISABLE);
- /* Configure the emulation behaviour*/
- EHRPWMTBEmulationModeSet(EPWM_REGS_Table[epwmconfig->EPWMOutNum], EHRPWM_STOP_AFTER_NEXT_TB_INCREMENT);
- /* Configure Counter compare cub-module */
- /* Load Compare A value *///配置A的比较计数值
- EHRPWMLoadCMPA(EPWM_REGS_Table[epwmconfig->EPWMOutNum],
- epwmconfig->LoadCMPA,
- (bool)EHRPWM_SHADOW_WRITE_DISABLE,
- (unsigned int)EHRPWM_COMPA_NO_LOAD,
- (bool)EHRPWM_CMPCTL_OVERWR_SH_FL);
- /* Load Compare B value *///配置B的比较计数值
- EHRPWMLoadCMPB(EPWM_REGS_Table[epwmconfig->EPWMOutNum],
- epwmconfig->LoadCMPB,
- (bool)EHRPWM_SHADOW_WRITE_DISABLE,
- (unsigned int) EHRPWM_COMPB_NO_LOAD,
- (bool)EHRPWM_CMPCTL_OVERWR_SH_FL);
- /* Configure Action qualifier */
- /* Toggle when CTR = CMPA *///设置A输出的一些配置,这里配置为ZRO即初始低,PRD即EHRPriod到低,CAU即A计数值到为高电平,其余节点不做
- EHRPWMConfigureAQActionOnA(EPWM_REGS_Table[epwmconfig->EPWMOutNum],
- EHRPWM_AQCTLA_ZRO_EPWMXALOW,
- EHRPWM_AQCTLA_PRD_EPWMXALOW,
- EHRPWM_AQCTLA_CAU_EPWMXAHIGH,
- EHRPWM_AQCTLA_CAD_DONOTHING,
- EHRPWM_AQCTLA_CBU_DONOTHING,
- EHRPWM_AQCTLA_CBD_DONOTHING,
- EHRPWM_AQSFRC_ACTSFA_DONOTHING);
- /* Configure Action qualifier */
- /* Toggle when CTR = CMPB *///设置B输出的一些配置,这里配置为ZRO初始低,PRD即EHRPriod到低,,CBU即B计数值到为高电平,其余节点不做
- EHRPWMConfigureAQActionOnB(EPWM_REGS_Table[epwmconfig->EPWMOutNum],
- EHRPWM_AQCTLB_ZRO_EPWMXBLOW,
- EHRPWM_AQCTLB_PRD_EPWMXBLOW,
- EHRPWM_AQCTLB_CAU_DONOTHING,
- EHRPWM_AQCTLB_CAD_DONOTHING,
- EHRPWM_AQCTLB_CBU_EPWMXBHIGH,
- EHRPWM_AQCTLB_CBD_DONOTHING,
- EHRPWM_AQSFRC_ACTSFB_DONOTHING);
-
- /* Bypass dead band sub-module */
- EHRPWMDBOutput(EPWM_REGS_Table[epwmconfig->EPWMOutNum], EHRPWM_DBCTL_OUT_MODE_BYPASS);
- /* Disable Chopper sub-module */
- EHRPWMChopperDisable(EPWM_REGS_Table[epwmconfig->EPWMOutNum]);
- /* Disable trip events */
- EHRPWMTZTripEventDisable(EPWM_REGS_Table[epwmconfig->EPWMOutNum],(bool)EHRPWM_TZ_ONESHOT);
- EHRPWMTZTripEventDisable(EPWM_REGS_Table[epwmconfig->EPWMOutNum],(bool)EHRPWM_TZ_CYCLEBYCYCLE);
- /* Event trigger */
- /* Generate interrupt every 3rd occurance of the event */
- //EHRPWMETIntPrescale(EPWM_REGS_Table[epwmconfig->EPWMOutNum], EHRPWM_ETPS_INTPRD_THIRDEVENT);
- /* Generate event when CTR = CMPB */
- // EHRPWMETIntSourceSelect(EPWM_REGS_Table[epwmconfig->EPWMOutNum], EHRPWM_ETSEL_INTSEL_TBCTREQUCMPBINC);
- /* Disable High resolution capability 禁止高分辨率*/
- EHRPWMHRDisable(EPWM_REGS_Table[epwmconfig->EPWMOutNum]);
- }
- /***********************************************************************************************
- * Function Name : EPWMPinMuxSetup
- * Description :
- * Input :
- * Return :
- * Note(s) :
- * Contributor : 150204 WB
- ***********************************************************************************************/
- void EPWMPinMuxSetup(INT8U pwmnum)
- {
- switch (pwmnum)
- {
- case 0:
- HWREG(SOC_CONTROL_REGS + CONTROL_CONF_MCASP0_ACLKX) = CONTROL_CONF_MUXMODE(1);
- HWREG(SOC_CONTROL_REGS + CONTROL_CONF_MCASP0_FSX) = CONTROL_CONF_MUXMODE(1);
- break;
- case 1:
- HWREG(SOC_CONTROL_REGS + CONTROL_CONF_LCD_DATA(10)) = CONTROL_CONF_MUXMODE(2);
- HWREG(SOC_CONTROL_REGS + CONTROL_CONF_LCD_DATA(11)) = CONTROL_CONF_MUXMODE(2);
- break;
- case 2:
- HWREG(SOC_CONTROL_REGS + CONTROL_CONF_GPMC_AD(9)) = CONTROL_CONF_MUXMODE(4);
- HWREG(SOC_CONTROL_REGS + CONTROL_CONF_GPMC_AD(8)) = CONTROL_CONF_MUXMODE(4);
- break;
- default:
- break;
- }
- }
- /***********************************************************************************************
- * Function Name : Bsp_PwmInit
- * Description :PWM初始化配置
- * Input :
- * Return :
- * Note(s) :
- * Contributor : 150204 WB
- ***********************************************************************************************/
- void Bsp_EhrPwmConfig(_BSPEHRPWM_CONFIG *epwmconfig)
- {
- PWMSSModuleClkConfig(epwmconfig->EPWMOutNum);
- EPWMPinMuxSetup(epwmconfig->EPWMOutNum);
-
- /* Enable Clock for EHRPWM in PWM sub system */
- EHRPWMClockEnable(PWMSS_REGS_Table[epwmconfig->EPWMOutNum]);
- /* Enable Timer Base Module Clock in control module */
- PWMSSTBClkEnable(epwmconfig->EPWMOutNum);
- /* EHRPWM is configured to generate PWM waveform on EPWMBxB Pin*/
- EHRPWMConfigure(epwmconfig);
- }
- /************************(C)COPYRIGHT 2015 XXXX*****END OF FILE****************************/
[color=rgb(51, 102, 153) !important]复制代码
- /*****************************************Copyright(C)******************************************
- *******************************************XXXX*********************************************
- **-----------------------------------------文件信息---------------------------------------------
- **文 件 名: bsp_pwm.h
- **硬 件: am3352
- **创 建 人: WB
- **创 建 日 期: 2015-01-21
- **最 新 版 本: V0.1
- **描 述: PWM
- **---------------------------------------历史版本信息-------------------------------------------
- **修 改 人:
- **日 期:
- **版 本:
- **描 述:
- **----------------------------------------------------------------------------------------------
- ***********************************************************************************************/
- #ifndef __BSP_PWM_H_
- #define __BSP_PWM_H_
- /* Includes-----------------------------------------------------------------------------------*/
- #include "bsp_conf.h"
- /* Private define-----------------------------------------------------------------------------*/
- /* Private typedef----------------------------------------------------------------------------*/
- /*EPWMLoad为一个周期,EPWMLoad-LoadCMP为一个周期内的高电平时间,其余周期内的时间
- 为低电平,这个就可以作为一个占空比的调节。PWM计数时钟默认为10M,可更改,若EPWMLoad=255,则
- PWM频率为10M/255=39.215K*/
- typedef struct
- {
- INT8U EPWMOutNum; //0~2输出号
- INT8U EPWMLoad; //EPWM重载计数值,即分辨率
- INT8U LoadCMPA; //A通道计数值
- INT8U LoadCMPB; //B通道计数值
-
- }_BSPEHRPWM_CONFIG;
- /* Private macro------------------------------------------------------------------------------*/
- /* Private variables--------------------------------------------------------------------------*/
- /* Private function prototypes----------------------------------------------------------------*/
- /* Private functions--------------------------------------------------------------------------*/
- /***********************************************************************************************
- * Function Name : Bsp_PwmInit
- * Description :PWM初始化
- * Input :
- * Return :
- * Note(s) :
- * Contributor : 150120 WB
- ***********************************************************************************************/
- void Bsp_EhrPwmConfig(_BSPEHRPWM_CONFIG *epwmconfig);
- #endif //__BSP_PWM_H_
- /************************(C)COPYRIGHT 2015 XXXX*****END OF FILE****************************/
[color=rgb(51, 102, 153) !important]复制代码
|
|