Tool/software: TI-RTOS
Hi
My goal is to use PWM to preform a frequncy sweep, without using task nor a scond timer .
For example, when pwm timeout is over, I want to call to a callback function, which changes period and duty.
I wrote a small code:
GPTimerCC26XX_Handle hTimer;
Task_Struct timerTask;
char timerTaskStack[512];
/*
counts = (fo/f)
fo - cpu frequency 48[MHz], f - pwm frequency [Hz]
if we set count = 1
1 = 48e6/f
f = 48e6[Hz] =48[MHz]
T(1count) = 1/f = 1/48MHz = (1/48)[usec] = (1000/48)[nsec] = (125/6)[nsec]
count(1nsec) = (6/125)count
generic formula:
count = 6t/125 , t[nsec]
*/
#define PWM_GET_COUNT(t) ( ( 6*(t)/125 ) -1 )
PIN_State gpTimerPinState;
PIN_Handle gpTimerPinHandle;
#define PWM_0 IOID_4
// 1msec = 1000000nsec in pwm count term
#define PWM_PERIOD_1 PWM_GET_COUNT(1000000)
// 2msec = 2000000nsec in pwm count term
#deine PWM_PERIOD_2 PWM_GET_COUNT(2000000)
#define PWM_DUTY_FACTOR (2)
PIN_Config pwmPin[] = {
PWM_0 | PIN_INPUT_DIS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH|
PIN_INV_INOUT | PIN_PUSHPULL | PIN_DRVSTR_MAX,
PIN_TERMINATE };
//////// Prototypes//////////
void timerCallback(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask);
void taskFxn(UArg a0, UArg a1);
/////////// implementation///////////////
void main(void)
{
Board_initGeneral();
Task_Params taskParams;
/* Configure the OS task */
Task_Params_init(&taskParams);
taskParams.stack = timerTaskStack;
taskParams.stackSize = sizeof(timerTaskStack);
taskParams.priority = 1;
Task_construct(&timerTask, taskFxn, &taskParams, NULL);
BIOS_start();
}
void taskFxn(UArg a0, UArg a1)
{
GPTimerCC26XX_Params params;
GPTimerCC26XX_Params_init(¶ms);
params.width = GPT_CONFIG_16BIT;
params.mode = GPT_MODE_PERIODIC_UP;
//params.mode = GPT_MODE_PWM;
params.debugStallMode = GPTimerCC26XX_DEBUG_STALL_ON;
//params.debugStallMode = GPTimerCC26XX_DEBUG_STALL_OFF;
hTimer = GPTimerCC26XX_open(0, ¶ms);
gpTimerPinHandle = PIN_open(&gpTimerPinState,pwmPin);
Types_FreqHz freq;
BIOS_getCpuFreq(&freq);
GPTimerCC26XX_Value loadVal = PWM_PERIOD_1
GPTimerCC26XX_setLoadValue(hTimer, loadVal);
GPTimerCC26XX_setMatchValue(hTimer,loadVal/PWM_DUTY_FACTOR);
GPTimerCC26XX_registerInterrupt(hTimer, timerCallback, GPT_INT_TIMEOUT);
PINCC26XX_setMux(gpTimerPinHandle, PWM_0, GPT_PIN_0A);
GPTimerCC26XX_start(hTimer);
while(1)
{
Task_sleep(BIOS_WAIT_FOREVER);
}
}
#pragma optimize=none
void timerCallback(GPTimerCC26XX_Handle handle, GPTimerCC26XX_IntMask interruptMask)
{
static int s_indicator = 0;
UInt32 period = (0 == s_indicator) ? : PWM_PERIOD_1;
if(0 == s_indicator)
{
period = PWM_PERIOD_2;
s_indicator = 1;
}
else
{
period = PWM_PERIOD_1;
s_indicator = 0;
}
GPTimerCC26XX_setLoadValue(hTimer, period);
GPTimerCC26XX_setMatchValue(hTimer,period/PWM_DUTY_FACTOR);
}
My problem is when I set pwm mode to GPT_MODE_PERIODIC_UP, I do reach to callback, yet I don't see the pwm when I connect scope to PWM_0.
However when I set pwm mode to GPT_MODE_PWM , I can see the pwm when I connect scope to PWM_0, yet I don't reach to callback.