This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320C6748: epwm计数器没启动

Part Number: TMS320C6748
Other Parts Discussed in Thread: SYSBIOS

按以下代码配置,epwm的计数器没反应,最后是强制输出。

请看以下代码对TBCNT寄存器的配置是否有问题,如果不是,请说明原因并给出解决方案

#include "drv_dsp_gpio.h"
#include "drv_dsp_epwm.h"
#include "c674x_dsp_psc.h"
#include "c674x_ehrpwm.h"
#include "c674x_dsp_common.h"
#include "string.h"
#include "print.h"
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/family/c64p/Hwi.h>
#include "c674x_dsp_global_source.h"
#include "proto_bios_common.h"

#define HWREG(x) \
(*((volatile unsigned int *)(x)))
#define HWREGH(x) \
(*((volatile unsigned short *)(x)))
#define HWREGB(x) \
(*((volatile unsigned char *)(x)))



#define EPWM_SYSCLK c674x_DSP_REF_CPU_FREQ//主时钟

/** @brief Base address of DEV memory mapped registers */
#define SOC_SYSCFG_0_REGS (0x01C14000)

#define SYSCFG0_CFGCHIP1 (0x180)

#define SYSCFG_CFGCHIP1_TBCLKSYNC (0x00001000u)


void (*epwm0_int_callback_func)(int) = NULL;//


//epwm0中断
void drv_dsp_epwm0_isr();


//epwm0中断
void drv_dsp_epwm0_isr()
{
while(HWREGH(EHRPWM_BASEADDR + EHRPWM_ETFLG))
{
HWREGH(EHRPWM_BASEADDR + EHRPWM_ETCLR)=1;//clear

if(epwm0_int_callback_func!=NULL)
epwm0_int_callback_func(0);
}
}

//配置epwm的输出clk的freq 0:3MHz, 1:4MHz, 2:4.8MHz, 3:6MHz
void drv_dsp_epwm0_freq_sel(unsigned int freq)
{
if(freq==3)
EHRPWMConfigureChopperFreq(EHRPWM_BASEADDR, EHRPWM_PCCTL_CHPFREQ_DIVBY4);
else if(freq==2)
EHRPWMConfigureChopperFreq(EHRPWM_BASEADDR, EHRPWM_PCCTL_CHPFREQ_DIVBY5);
else if(freq==1)
EHRPWMConfigureChopperFreq(EHRPWM_BASEADDR, EHRPWM_PCCTL_CHPFREQ_DIVBY6);
else
EHRPWMConfigureChopperFreq(EHRPWM_BASEADDR, EHRPWM_PCCTL_CHPFREQ_DIVBY8);
}

//配置chopperwave
void drv_dsp_chopperwave(unsigned int freq)
{
/* configure to 50% duty cycle */
EHRPWMConfigureChopperDuty(EHRPWM_BASEADDR, EHRPWM_CHP_DUTY_50_PER);

/* OSPW- div by 0xF */
EHRPWMConfigureChopperOSPW(EHRPWM_BASEADDR, 0xF);//288M/16=18MHz

/* chopper freq DIV by 4 */
//EHRPWMConfigureChopperFreq(EHRPWM_BASEADDR, EHRPWM_PCCTL_CHPFREQ_DIVBY6);
drv_dsp_epwm0_freq_sel(freq);// 18/6=3MHz

/* Enable Chopper */
EHRPWMChopperEnable(EHRPWM_BASEADDR);
}

//epwm初始化
void drv_dsp_epwm0_init(void (*epwm0_int_handle)(int))
{
//SIM卡芯片使能管脚,不使能
drv_dsp_sim_poweroff();

//SIM卡复位控制为复位
drv_dsp_sim_rst_ctl(1);

//SIM_IO配置输入模式
drv_dsp_sim_io_ctl(2);

//中断
if(epwm0_int_handle!=NULL)
{
proto_interrupt_linkbios(C67X_INTSEL12_LINE, EHRPWM0, drv_dsp_epwm0_isr, ti_sysbios_interfaces_IHwi_MaskingOption_LOWER);
Hwi_enableInterrupt(C67X_INTSEL12_LINE);

epwm0_int_callback_func=epwm0_int_handle;
}
}

//freq: 配置epwm的输出clk的freq 0:3MHz, 1:4MHz, 2:4.8MHz, 3:6MHz
//int_enable:中断使能,1为使能
void drv_dsp_epwm0_conf(unsigned int freq,unsigned int int_enable)
{
//psc配置
PSCModuleControl(PSC_1_REGS_BASEADDR, HW_PSC_EHRPWM, PSC_POWERDOMAIN_ALWAYS_ON, PSC_MDCTL_NEXT_ENABLE);//enable ehrpwm
PSCModuleControl(PSC_1_REGS_BASEADDR, HW_PSC_SCRF8_SS, PSC_POWERDOMAIN_ALWAYS_ON, PSC_MDCTL_NEXT_ENABLE);//enable scrf8

/* Enable PWM Clock in chip config reg 1 */
HWREG(SOC_SYSCFG_0_REGS + SYSCFG0_CFGCHIP1) |= SYSCFG_CFGCHIP1_TBCLKSYNC;

//配置
HWREGH(EHRPWM_BASEADDR + EHRPWM_TBCTL)= EHRPWM_TBCTL_FREE_SOFT|
EHRPWM_TBCTL_CLKDIV_DIVBY8|
EHRPWM_TBCTL_HSPCLKDIV_DIVBY4|
(EHRPWM_TBCTL_SYNCOSEL_DISABLE<<4)|
EHRPWM_TBCTL_PRDLD| //Load the TBPRD register immediately without using a shadow register.
EHRPWM_TBCTL_CTRMODE_UP; //Down-count mode


//周期设置
HWREGH(EHRPWM_BASEADDR + EHRPWM_TBPRD)= (EPWM_SYSCLK/8)/100000;

//cmpb设置
HWREGH(EHRPWM_BASEADDR + EHRPWM_CMPCTL)= EHRPWM_CMPCTL_SHDWBMODE; //Counter-compare B (CMPB) Register Operating Mode:Immediate mode

HWREGH(EHRPWM_BASEADDR + EHRPWM_CMPB)= HWREGH(EHRPWM_BASEADDR + EHRPWM_TBPRD)>>1;

//AQCTL配置
HWREGH(EHRPWM_BASEADDR + EHRPWM_AQCTLA)=0;
HWREGH(EHRPWM_BASEADDR + EHRPWM_AQCTLB)=(2<<3)| // when the counter equals the period,force EPWMxB output high.
(1<<10); // when the counter equals the active CMPB register and the counter is decrementing,force EPWMxB output low.

/* Disable synchronization*/
EHRPWMTimebaseSyncDisable(EHRPWM_BASEADDR);

/* Disable syncout*/
EHRPWMSyncOutModeSet(EHRPWM_BASEADDR, EHRPWM_SYNCOUT_DISABLE);

/* Configure the emulation behaviour*/
EHRPWMTBEmulationModeSet(EHRPWM_BASEADDR, EHRPWM_STOP_AFTER_NEXT_TB_INCREMENT);

/* Bypass dead band sub-module */
EHRPWMDBOutput(EHRPWM_BASEADDR, EHRPWM_DBCTL_OUT_MODE_BYPASS);

/* Disable Chopper sub-module */
EHRPWMChopperDisable(EHRPWM_BASEADDR);

/* Disable trip events */
EHRPWMTZTripEventDisable(EHRPWM_BASEADDR, EHRPWM_TZ_ONESHOT);
EHRPWMTZTripEventDisable(EHRPWM_BASEADDR, EHRPWM_TZ_CYCLEBYCYCLE);

/* Disable High resolution capability */
EHRPWMHRDisable(EHRPWM_BASEADDR);

if(int_enable)
{
HWREGH(EHRPWM_BASEADDR + EHRPWM_ETCLR)=1;//clear
HWREGH(EHRPWM_BASEADDR + EHRPWM_ETSEL)= (1<<3)| //Enable EPWMx_INT generation
7;// //Enable event: time-base counter equal to CMPB when the timer is decrementing.
}
else
{
;
}

drv_dsp_chopperwave(freq);
}

//启动epwm0输出pwm
void drv_dsp_epwm0_start(void)
{
HWREGH(EHRPWM_BASEADDR + EHRPWM_AQCSFRC)=8;//强制输出有效
}

//停止epwm0输出pwm
void drv_dsp_epwm0_stop(void)
{
HWREGH(EHRPWM_BASEADDR + EHRPWM_AQCSFRC)=4;//强制输出无效
}

//sim卡供电
void drv_dsp_sim_poweron()
{
//SIM卡芯片使能管脚使能
SIM_ENABLE_INIT;
SIM_ENABLE_ENABLE;
}

//sim卡不供电
void drv_dsp_sim_poweroff()
{
//SIM卡芯片使能管脚使能
SIM_ENABLE_INIT;
SIM_ENABLE_DISABLE;
}

//sim卡复位控制,1为复位,0为不复位
void drv_dsp_sim_rst_ctl(int type)
{
SIM_RST_CTL_INIT;
if(type)
SIM_RST_CTL_RES;
else
SIM_RST_CTL_DIS;
}

//sim卡的io控制,type 0为输出0,1为输出1,2为输入
//返回,sim卡的io电平,1为高,0为低
void drv_dsp_sim_io_ctl(int type)
{
if(type==0)
{
SIM_IO_CTL_OUT;
SIM_IO_OUT_LOW;
return 0;
}
else if(type==1)
{
SIM_IO_CTL_OUT;
SIM_IO_OUT_HIGH;
return 1;
}
else if(type==2)
{
SIM_IO_CTL_IN;

return (SIM_IO_DATA_IN)?1:0;
}
return 0;
}

//sim卡冷复位
void drv_dsp_sim_cold_reset()
{
drv_dsp_sim_io_ctl(2);//sim io输入状态
drv_dsp_sim_rst_ctl(1);//复位状态
drv_dsp_sim_poweroff();//sim卡不上电

drv_dsp_cache_delay_us(200);

drv_dsp_sim_poweron();//sim卡上电
drv_dsp_cache_delay_us(200);
drv_dsp_epwm0_start();//clk输出

drv_dsp_cache_delay_us(2000);

drv_dsp_sim_rst_ctl(0);//不复位状态
}

//sim卡热复位
void drv_dsp_sim_hot_reset()
{
drv_dsp_sim_io_ctl(2);//sim io输入状态
drv_dsp_sim_poweron();//sim卡上电
drv_dsp_epwm0_start();//clk输出
drv_dsp_sim_rst_ctl(0);//不复位状态

drv_dsp_cache_delay_us(200);
drv_dsp_sim_rst_ctl(1);//复位状态

drv_dsp_cache_delay_us(2000);

drv_dsp_sim_rst_ctl(0);//不复位状态
}


//for test reg
unsigned int reg_data=0;
void test_reg_write(unsigned int reg_addr)
{
HWREGH(reg_addr)=reg_data;
mysyslog("写入addr 0x%x: 0x%x\n",reg_addr,HWREGH(reg_addr));
}

void test_reg_read(unsigned int reg_addr)
{
mysyslog("读取addr 0x%x: 0x%x\n",reg_addr,HWREGH(reg_addr));
}