You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

526 lines
14 KiB

#include "pwm_ss.h"
bool PWM_SS0_OK = false;
bool PWM_SS1_OK = false;
bool PWM_SS2_OK = false;
void *CM_MMAP = NULL;
void *PWM_SS0_MMAP = NULL;
void *PWM_SS1_MMAP = NULL;
void *PWM_SS2_MMAP = NULL;
#define PWM_SS0_CTRL (*((volatile sysPwmss*) PWM_SS0_MMAP ))
#define PWM_SS1_CTRL (*((volatile sysPwmss*) PWM_SS1_MMAP ))
#define PWM_SS2_CTRL (*((volatile sysPwmss*) PWM_SS2_MMAP ))
#define CM_CTRL (*((volatile sysCM_REGS*) CM_MMAP ))
bool init_PWM_SS0( void ){
int MEM_FD;
//pwm_clk_enable( 0 );
if( PWM_SS0_MMAP!= NULL ) {
munmap( PWM_SS0_MMAP, PWMSS_BLOCK_SIZE );
};
MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
if( MEM_FD == -1 ) {
printf( "Memory File Open Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
PWM_SS0_MMAP = mmap( NULL,
PWMSS_BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED,
MEM_FD,
PWM_SS0 );
close( MEM_FD );
if( PWM_SS0_MMAP == MAP_FAILED ) {
printf( "Memory Region Map Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
return (PWM_SS0_OK=true);
};
bool init_PWM_SS1( void ){
int MEM_FD;
//pwm_clk_enable( 1 );
if( PWM_SS1_MMAP!= NULL ) {
munmap( PWM_SS1_MMAP, PWMSS_BLOCK_SIZE );
};
MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
if( MEM_FD == -1 ) {
printf( "Memory File Open Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
PWM_SS1_MMAP = mmap( NULL,
PWMSS_BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED,
MEM_FD,
PWM_SS1 );
close( MEM_FD );
if( PWM_SS1_MMAP == MAP_FAILED ) {
printf( "Memory Region Map Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
return (PWM_SS1_OK=true);
};
bool init_PWM_SS2( void ){
int MEM_FD;
//pwm_clk_enable( 2 );
if( PWM_SS2_MMAP!= NULL ) {
munmap( PWM_SS2_MMAP, PWMSS_BLOCK_SIZE );
};
MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
if( MEM_FD == -1 ) {
printf( "Memory File Open Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
PWM_SS2_MMAP = mmap( NULL,
PWMSS_BLOCK_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED,
MEM_FD,
PWM_SS2 );
close( MEM_FD );
if( PWM_SS2_MMAP == MAP_FAILED ) {
printf( "Memory Region Map Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
return (PWM_SS2_OK=true);
};
bool pwm_disable( uint8_t system ){
volatile sysPwmss *c_pPWM = NULL;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
if( pwm_clk_enable( system, 0, 100000 ) ) {
//c_pPWM->EPWM_TBCTL_bit. = 0;
usleep( DEFAULT_SIGNAL_REACTION );
//if( c_pPWM->CTRL_bit.RUNSTATE == 0 ) return true;
};
return false;
};
bool pwm_enable( uint8_t system ){
volatile sysPwmss *c_pPWM = NULL;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
if( pwm_clk_enable( system, 0, 100000 ) ) {
c_pPWM->ECAP_ECCTL1_bit.PRESCALE = 0;
usleep( DEFAULT_SIGNAL_REACTION );
// if( c_pPWM->CTRL_bit.RUNSTATE != 0 ) return true;
};
return false;
};
bool pwm_clk_disable( uint8_t system ){
volatile sysPwmss *c_pPWM = NULL;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
c_pPWM->CLKCONFIG_bit.ECAPCLKSTOP_REQ = 1;
c_pPWM->CLKCONFIG_bit.EPWMCLKSTOP_REQ = 1;
c_pPWM->CLKCONFIG_bit.EQEPCLKSTOP_REQ = 1;
if( CM_MMAP == NULL ){
int MEM_FD;
MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
if( MEM_FD == -1 ) {
printf( "Memory File Open Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
CM_MMAP = mmap( NULL,
CM_SPACE_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED,
MEM_FD,
CM_SPACE );
close( MEM_FD );
if( CM_MMAP == MAP_FAILED ) {
printf( "Memory Region Map Failed\n");
printf( "Error: %s\n", strerror(errno));
CM_MMAP = NULL;
return false;
};
CM_CTRL.CTRL_PWMSS &= ~( 1 << system );
munmap( CM_MMAP, CM_SPACE_SIZE );
CM_MMAP = NULL;
};
return true;
};
bool pwm_clk_enable( uint8_t system, uint8_t preScaler, uint32_t pwmPeriod ){
volatile sysPwmss *c_pPWM = NULL;
printf("Try Init CLK [%d] Prescaler: %d period: %d\n", system, preScaler, pwmPeriod );
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
if( CM_MMAP == NULL ){
int MEM_FD;
MEM_FD = open( "/dev/mem", O_RDWR | O_SYNC );
if( MEM_FD == -1 ) {
printf( "Memory File Open Failed\n");
printf( "Error: %s\n", strerror(errno));
return false;
};
CM_MMAP = mmap( NULL,
CM_SPACE_SIZE,
PROT_READ | PROT_WRITE,
MAP_SHARED | MAP_LOCKED,
MEM_FD,
CM_SPACE );
close( MEM_FD );
if( CM_MMAP == MAP_FAILED ) {
printf( "Memory Region Map Failed\n");
printf( "Error: %s\n", strerror(errno));
CM_MMAP = NULL;
return false;
};
printf( "CM Signature: %p\n", CM_CTRL.CTRL_REVISION );
printf( "CM PWMSS CLC: %d\n", CM_CTRL.CTRL_PWMSS );
CM_CTRL.CTRL_PWMSS |= 1 << system;
printf( "CM PWMSS CLC: %d\n\n", CM_CTRL.CTRL_PWMSS );
munmap( CM_MMAP, CM_SPACE_SIZE );
CM_MMAP = NULL;
} else printf( "CM MMAP RORY!\n");
c_pPWM->SYSCONFIG_bit.IDLEMODE = 2;
c_pPWM->SYSCONFIG_bit.FREEEMU = 1;
c_pPWM->CLKCONFIG_bit.ECAPCLKSTOP_REQ = 0;
c_pPWM->CLKCONFIG_bit.EQEPCLKSTOP_REQ = 0;
c_pPWM->CLKCONFIG_bit.EPWMCLK_EN = 1;
while( c_pPWM->CLKCONFIG_bit.EPWMCLK_EN != 1 );
c_pPWM->EPWM_TBCTL_bit.CLKDIV = 0x07 & preScaler;
c_pPWM->EPWM_TBCTL_bit.PRDLD = 1;
c_pPWM->EPWM_TBCTL_bit.CTRMODE = 2; // Up-Down
//c_pPWM->EPWM_TBCTL_bit.FREE_SOFT = 2; // Free Run
c_pPWM->EPWM_TBPRD = pwmPeriod;
// Resetting
//c_pPWM->SYSCONFIG_bit.SOFTRESET = 1;
//while( c_pPWM->SYSCONFIG_bit.SOFTRESET );
return true;
};
void PWMconfigAQActionOnA( volatile sysPwmss *c_pPWM,
unsigned int zero,
unsigned int period,
unsigned int CAUp,
unsigned int CADown,
unsigned int CBUp,
unsigned int CBDown,
unsigned int SWForced) {
c_pPWM->EPWM_AQCTLA_bit.CBD = CBDown;
c_pPWM->EPWM_AQCTLA_bit.CBU = CBUp;
c_pPWM->EPWM_AQCTLA_bit.CAD = CADown;
c_pPWM->EPWM_AQCTLA_bit.CAU = CAUp;
c_pPWM->EPWM_AQCTLA_bit.PRD = period;
c_pPWM->EPWM_AQCTLA_bit.ZRO = zero;
c_pPWM->EPWM_AQSFRC_bit.ACTSFA = SWForced;
}
void PWMconfigAQActionOnB( volatile sysPwmss *c_pPWM,
unsigned int zero,
unsigned int period,
unsigned int CAUp,
unsigned int CADown,
unsigned int CBUp,
unsigned int CBDown,
unsigned int SWForced) {
c_pPWM->EPWM_AQCTLB_bit.CBD = CBDown;
c_pPWM->EPWM_AQCTLB_bit.CBU = CBUp;
c_pPWM->EPWM_AQCTLB_bit.CAD = CADown;
c_pPWM->EPWM_AQCTLB_bit.CAU = CAUp;
c_pPWM->EPWM_AQCTLB_bit.PRD = period;
c_pPWM->EPWM_AQCTLB_bit.ZRO = zero;
c_pPWM->EPWM_AQSFRC_bit.ACTSFB = SWForced;
}
bool pwm_run( uint8_t system, uint32_t dutyCycle ){
volatile sysPwmss *c_pPWM = NULL;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
if( c_pPWM != NULL ) {
pwm_clk_enable( system, 7, TICKS_PER_MS );
c_pPWM->ECAP_ECCTL2_bit.CAP_APWM = 1;
return true;
};
return false;
};
bool pwm_stop( uint8_t system ){
volatile sysPwmss *c_pPWM = NULL;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
if( c_pPWM != NULL ) {
c_pPWM->ECAP_ECCTL2_bit.CAP_APWM = 0;
pwm_clk_disable( system );
return true;
}
return false;
};
bool pwm_set( uint8_t system, uint32_t dutyCycle ){
volatile sysPwmss *c_pPWM = NULL;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
} else return false;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
} else return false;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
} else return false;
break;
default:
return false;
};
//c_pPWM->CTRL_bit.SOFT_RST_N = 0;
//while( c_pPWM->CTRL_bit.SOFT_RST_N == 0 );
return true;
};
void close_PWM_SS0( void ){
if( PWM_SS0_OK ) {
munmap( PWM_SS0_MMAP, PWMSS_BLOCK_SIZE );
PWM_SS0_OK = false;
};
};
void close_PWM_SS1( void ){
if( PWM_SS1_OK ) {
munmap( PWM_SS1_MMAP, PWMSS_BLOCK_SIZE );
PWM_SS1_OK = false;
};
};
void close_PWM_SS2( void ){
if( PWM_SS2_OK ) {
munmap( PWM_SS2_MMAP, PWMSS_BLOCK_SIZE );
PWM_SS2_OK = false;
};
};
#ifdef CLI_NCURSES
void print_PWM_info( uint8_t system ){
volatile sysPwmss *c_pPWM = NULL;
uint32_t index = 0;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
index = PWM_SS0;
} else return;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
index = PWM_SS1;
} else return;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
index = PWM_SS2;
} else return;
break;
default:
return;
};
if( c_pPWM != NULL ) {
printf( "\n PWMSS%d Config at: \t%p\n", system, index );
printf( " PWMSS%d val: 0x%08x Scheme: %d Func: %d RTL: %d Revision ID: %d.%d Custom:%d\n", system,
c_pPWM->IDVER,
c_pPWM->IDVER_bit.SCHEME,
c_pPWM->IDVER_bit.FUNC,
c_pPWM->IDVER_bit.R_RTL,
c_pPWM->IDVER_bit.X_MAJOR,
c_pPWM->IDVER_bit.Y_MINOR,
c_pPWM->IDVER_bit.CUSTOM );
printf( " eCAP_APWM ID: \t0x%08x\n", c_pPWM->ECAP_REVID );
printf( " eCAP_APWM Polarity: %s\n",((c_pPWM->ECAP_ECCTL2_bit.APWMPOL==0)?"High":"Low") );
printf( " eCAP_APWM Time Stamp (TSCTR): %s\n",((c_pPWM->ECAP_ECCTL2_bit.TSCTRSTOP==0)?"Stoped":"Free-Run") );
printf( " eCAP_APWM Mode: %s\n", ((c_pPWM->ECAP_ECCTL2_bit.CAP_APWM==0)?"eCAP":"APWM") );
printf( " eCAP_APWM Period: \t\t%d (%d nSec)\n", c_pPWM->ECAP_CAP1, c_pPWM->ECAP_CAP1*10 );
printf( " eCAP_APWM PRESCALE: \t\t%d\n", c_pPWM->ECAP_ECCTL1_bit.PRESCALE );
printf( " eCAP_APWM Duty Cycle: \t\t%d (%d nSec)\n", c_pPWM->ECAP_CAP2, c_pPWM->ECAP_CAP2*10 );
printf( " eCAP_APWM Shadow Period: \t%d (%d nSec)\n", c_pPWM->ECAP_CAP3, c_pPWM->ECAP_CAP3*10 );
printf( " eCAP_APWM Shadow Duty Cycle: \t%d (%d nSec)\n\n", c_pPWM->ECAP_CAP4, c_pPWM->ECAP_CAP4*10 );
};
};
void wprint_PWM_info( WINDOW * wnd, int shX, int shY, uint8_t system ){
volatile sysPwmss *c_pPWM = NULL;
uint32_t index = 0;
switch(system) {
case 0:
if( PWM_SS0_OK ) {
c_pPWM = &PWM_SS0_CTRL;
index = PWM_SS0;
} else return;
break;
case 1:
if( PWM_SS1_OK ) {
c_pPWM = &PWM_SS1_CTRL;
index = PWM_SS1;
} else return;
break;
case 2:
if( PWM_SS2_OK ) {
c_pPWM = &PWM_SS2_CTRL;
index = PWM_SS2;
} else return;
break;
default:
return;
};
if( c_pPWM != NULL ) {
//mvwprintw( wnd, shY+2,2+shX,"PWMSS%d Config at: \t%p", system, index );
//mvwprintw( wnd, shY+3,2+shX,"PWMSS%d val: 0x%08x", system, c_pPWM->IDVER );
//mvwprintw( wnd, shY+4,2+shX," Scheme: %d Func: %d",
// c_pPWM->IDVER_bit.SCHEME,
// c_pPWM->IDVER_bit.FUNC );
//mvwprintw( wnd, shY+5,2+shX,"RTL: %d Revision ID: %d.%d Custom:%d",
// c_pPWM->IDVER_bit.R_RTL,
// c_pPWM->IDVER_bit.X_MAJOR,
// c_pPWM->IDVER_bit.Y_MINOR,
// c_pPWM->IDVER_bit.CUSTOM );
//mvwprintw( wnd, shY+7,2+shX,"ID: \t0x%08x", c_pPWM->ECAP_REVID );
//mvwprintw( wnd, shY+8,2+shX,"Polarity: %s",((c_pPWM->ECAP_ECCTL2_bit.APWMPOL==0)?"High":"Low") );
//mvwprintw( wnd, shY+9,2+shX,"Time Stamp (TSCTR): %s",((c_pPWM->ECAP_ECCTL2_bit.TSCTRSTOP==0)?"Stoped":"Free-Run") );
//mvwprintw( wnd, shY+10,2+shX,"Mode: %s", ((c_pPWM->ECAP_ECCTL2_bit.CAP_APWM==0)?"eCAP":"APWM") );
//mvwprintw( wnd, shY+11,2+shX,"Period: \t%d (%d nSec)", c_pPWM->ECAP_CAP1, c_pPWM->ECAP_CAP1*10 );
//mvwprintw( wnd, shY+12,2+shX,"PRESCALE: \t%d", c_pPWM->ECAP_ECCTL1_bit.PRESCALE );
//mvwprintw( wnd, shY+13,2+shX,"Duty Cycle: \t%d (%d nSec)", c_pPWM->ECAP_CAP2, c_pPWM->ECAP_CAP2*10 );
//mvwprintw( wnd, shY+14,2+shX,"S_Period: \t%d (%d nSec)", c_pPWM->ECAP_CAP3, c_pPWM->ECAP_CAP3*10 );
//mvwprintw( wnd, shY+15,2+shX,"S_Duty Cycle: %d (%d nSec)", c_pPWM->ECAP_CAP4, c_pPWM->ECAP_CAP4*10 );
};
};
#endif