DSPRelated.com
Forums

problem when using C6711 timer and TOUT as a GPIO pin

Started by tien vuong June 11, 2006
Hello all,

I have problem with C6711 timer when using TOUT as a GPIO pin (with CCS-3.1, DSP/BIOS-5.2).

In my configuration, Timer0 is used by CLK manager module with "Microseconds/Int = 100". And "Use CLK Manager to driver PRD" in PRD manager module is set.

I want to measure execution time of some code stuff by both internal timer counter (to be sent to host) and GPIO (to be checked by OSC).

The following trial worked fine. Execution time inferred from internal timer counter and signal length from OSC are same. And if I replace TOUT0 by TOUT1 for output signal, the result is same.

void main()
{
TIMER_FSET( CTL0, FUNC, 0 ); //TOUT0 as GPIO
}

//hwi4 is the highest priority external IRQ, automatically generated
//by hardware every 200microseconds
void hwi4_isr(void)
{
volatile Uns loop;
LgUns start_count, end_count, period;

start_count = CLK_gethtime();
TIMER_FSET(CTL0, DATOUT, 1);

for (loop=0; loop<1000; loop++); //Code of interest

TIMER_FSET(CTL0, DATOUT, 0);
end_count = CLK_gethtime();

period = end_count -start_count;
}

But the real program, which is a little more complex, doesn't work. I am wondering about the execution sequence, or if I did some violation while programming the TOUT of timer. Here is the real code

void main()
{
TIMER_FSET( CTL0, FUNC, 0 ); //TOUT0 as GPIO
TIMER_FSET( CTL1, FUNC, 0 ); //TOUT1 as GPIO
}

//hwi4 is the highest priority external IRQ, automatically generated
//by hardware every 200microseconds
void hwi4_isr(void)
{
LgUns start_count, end_count, period;

start_count = CLK_gethtime();
TIMER_FSET(CTL1, DATOUT, 1);

//Some code stuff of interest here

TIMER_FSET(CTL1, DATOUT, 0);
end_count = CLK_gethtime();

period = end_count -start_count;
}

//period function, starts every 1 second
void prd_1sec(void)
{
TIMER_FSET(CTL0, DATOUT, 1);
//Some processing here
TIMER_FSET(CTL0, DATOUT, 0);
}

The above code produced quite smaller internal timer counter value than the execution time indicated by the GPIO signal length. (hwi4 period = 200microseconds, and the real execution time of the code is around 120microseconds which can't make the high resolution timer wrap back to 0 frequently during execution).

I've found in spru190d some text says "Writing to registers of an active timer: Writes from the peripheral bus override register updates to the CNT and new status updates to the timer control register (CTL)." and found this post in c6x message archives http://groups.yahoo.com/group/c6x/message/2629 in which Gary said "I was using Timer 0 as both a timer and GPIO. I had failed to interrupt
protect my read/writes, which seems to have interfered with things.".

I tried to insert IRQ_disable(IRQ_EVT_TINT0); IRQ_enable(IRQ_EVT_TINT0); and IRQ_disable(IRQ_EVT_TINT1); IRQ_enable(IRQ_EVT_TINT1); before and after every TIMER_FSET(CTL0, DATOUT, 0); and TIMER_FSET(CTL1, DATOUT, 0). Actually, I don't feel much sense to do this. I think timer interupt may cause context switch to the interest code stuff (how can the timer interrupt concern writing data to TOUT? what Gary really meaned when he said "to interrupt protect my read/writes"?), but hwi4 can't be interrupted because it's the highest priority one. But surprisingly, it works.

Now it works, but I just don't understand why and not sure about the correctness. Could you give some comments?

Thanks
Tien

__________________________________________________
Hello Dileepan,

Thanks for your message.

--- Dileepan C wrote:
> Hello Tien,
>
> I have not used these bios timers yet. STill some
> general questions which may help us solve the
> mystery:
> 1) Is it possible to use the timer0 as a GPIO when
> it is being used as timer for bios?

I have found no detail info on this. If I don't miss
some text, TI DSP/BIOS documents don't say that TOUT0
can't be used as GPIO while timer 0 is enabled, and
they don't say the opposite too. It would be nice to
me to firgure this out and waste no more time. In this
thread

http://groups.yahoo.com/group/c6x/message/2629

Tom Dillon suggested that TOUT0 shouldn't be used as
GPIO while timer0 is being used. But I need a stronger
confirmation. In our custom board, TOUT0 and TOUT1 are
the only 2 LED pins while DSP/BIOS uses either timer0
or timer1. So it ins't a good news that TOUT0 and
TOUT1 can't be GPIOs.

> What i understood from Gary's post is that,this
> doesnt work. But i am not sure how your test code
> worked with timer0 as GPIO.

Gary said this hadn't worked until he "interrupt
protected his read/writes". But I just don't
understand his solution. However, he wasn't using
DSP/BIOS. His own timer0 and TOUT0 conflicted to each
other. But I think that DSP/BIOS programs the timer0
the same way we program the timers for counting time.
So I think his case is same with what has happened to
my code, I may also need to "interrupt protected my
read/writes".
> 2) In your actual code, you are using timer1 as gpio
> and timer0 as bios clock. this seems to be ok. But
> in the prd_1sec() you are using timer0 as gpio.
> can you try commenting this TIMER_FSET() for timer0
> inside the prd_1sec and inside the main() function?
> Does the gpio result and the internal timer match
> then ?

I did your recommended test, the internal timer and
gpio doesn't match randomly (sometimes it matches). It
makes me very surprised that how TOUT1 can intefere
with timer0 (now TOUT0 setting stuff is disabled).

Worse, the test that I reported in the last mail that
I seemed to succeed using IRQ_disble/IRQ_enable to
make my stuff work is also randomly wrong now. So far,
all my test results in wrong. Some with higher wrong
times than the others but all is wrong. Except when I
tried to use timer1 in DSP/BIOS CLK manager. With
timer1, all goes well so far (I have tested many
times, with or without IRQ_disable/IRQ_enable). I
don't see any difference between timer0 and timer1 and
I will not be surprised if tomorrow timer1 goes wrong
too :(

> 3)Refer the following link which may give you some
> more ideas:
>
http://focus.ti.com/docs/apps/catalog/resources/appnoteabstract.jhtml?abstractName=spra829

I have read the appnote thoroughly. But it just
doesn't mention GPIO stuff.

Best regards,
Tien

__________________________________________________
Hello Dileepan,

Thanks for the clues. Now I do testing with a fresh
and simple project. All runs well. It's glad to be
sure that TOUT can be used as a GPIO safely. I did
mistake that I didn't create a fresh project for
testing. I had thought timer0 and TOUT0 stuff just
aren't affected by the remaining code. Now it seems
that I am wrong. I am debugging more my project now.

Best regards,
Tien

--- Dileepan C wrote:

> hi Tien,
>
> Following are some useful info from DSP knoledge
> base.
http://www-k.ext.ti.com/SRVS/CGI-BIN/WEBCGI.EXE/,/?StV,E00000000004793808,Ku67,Sxi=7,Case=obj(24939)
http://www-k.ext.ti.com/SRVS/CGI-BIN/WEBCGI.EXE/,/?St(,E00000000004793808,Ku67,Sxi=7,Case=obj(14736)
http://www-k.ext.ti.com/SRVS/CGI-BIN/WEBCGI.EXE/,/?St5,E00000000004793808,Ku67,Sxi=7,Case=obj(12122)
>
> This is what i can conclude:
> 1)TOUT0 can be used as a GPIO pin even when the
> timer0
> is in use.
> 2)When BIOS is using timer0,DATAOUT0 can be
> overwritten once the BIOS has started.
>
> regards,
> Dileepan.
>
>
> --- tien vuong wrote:
>
> > Hello Dileepan,
> >
> > Thanks for your message.
> >
> > --- Dileepan C wrote:
> > > Hello Tien,
> > >
> > > I have not used these bios timers yet. STill
> some
> > > general questions which may help us solve the
> > > mystery:
> > > 1) Is it possible to use the timer0 as a GPIO
> when
> > > it is being used as timer for bios?
> >
> > I have found no detail info on this. If I don't
> miss
> > some text, TI DSP/BIOS documents don't say that
> > TOUT0
> > can't be used as GPIO while timer 0 is enabled,
> and
> > they don't say the opposite too. It would be nice
> to
> > me to firgure this out and waste no more time. In
> > this
> > thread
> >
> > http://groups.yahoo.com/group/c6x/message/2629
> >
> > Tom Dillon suggested that TOUT0 shouldn't be used
> as
> > GPIO while timer0 is being used. But I need a
> > stronger
> > confirmation. In our custom board, TOUT0 and TOUT1
> > are
> > the only 2 LED pins while DSP/BIOS uses either
> > timer0
> > or timer1. So it ins't a good news that TOUT0 and
> > TOUT1 can't be GPIOs.
> >
> > > What i understood from Gary's post is that,this
> > > doesnt work. But i am not sure how your test
> code
> > > worked with timer0 as GPIO.
> >
> > Gary said this hadn't worked until he "interrupt
> > protected his read/writes". But I just don't
> > understand his solution. However, he wasn't using
> > DSP/BIOS. His own timer0 and TOUT0 conflicted to
> > each
> > other. But I think that DSP/BIOS programs the
> timer0
> > the same way we program the timers for counting
> > time.
> > So I think his case is same with what has happened
> > to
> > my code, I may also need to "interrupt protected
> my
> > read/writes".
> >
> >
> > > 2) In your actual code, you are using timer1 as
> > gpio
> > > and timer0 as bios clock. this seems to be ok.
> But
> >
> > > in the prd_1sec() you are using timer0 as gpio.
> > > can you try commenting this TIMER_FSET() for
> > timer0
> > > inside the prd_1sec and inside the main()
> > function?
> > > Does the gpio result and the internal timer
> match
> > > then ?
> >
> > I did your recommended test, the internal timer
> and
> > gpio doesn't match randomly (sometimes it
> matches).
> > It
> > makes me very surprised that how TOUT1 can
> intefere
> > with timer0 (now TOUT0 setting stuff is disabled).
> >
> > Worse, the test that I reported in the last mail
> > that
> > I seemed to succeed using IRQ_disble/IRQ_enable to
> > make my stuff work is also randomly wrong now. So
> > far,
> > all my test results in wrong. Some with higher
> wrong
> > times than the others but all is wrong. Except
> when
> > I
> > tried to use timer1 in DSP/BIOS CLK manager. With
> > timer1, all goes well so far (I have tested many
> > times, with or without IRQ_disable/IRQ_enable). I
> > don't see any difference between timer0 and timer1
> > and
> > I will not be surprised if tomorrow timer1 goes
> > wrong
> > too :(
> >
> > > 3)Refer the following link which may give you
> some
> > > more ideas:
> > >
> http://focus.ti.com/docs/apps/catalog/resources/appnoteabstract.jhtml?abstractName=spra829
> >
> > I have read the appnote thoroughly. But it just
> > doesn't mention GPIO stuff.
> >
> > Best regards,
> > Tien
> >