CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Strange behavior of function return value
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








PostPosted: Thu Oct 16, 2008 8:05 am     Reply with quote

Ah ok. Sorry I forgot to post the rest...

Code:

void main(void)
{
   struct timref_t mainTimRef;

   setTimer(10000, &mainTimRef);
   // some code

   while(1)
   {
      if(getTimeout(&mainTimRef))
      {
         setTimer(10000, &mainTimRef);
         // some code
      }
      // some code
   }
}


The interrupt routine which causes the problem is used for receiving data on the UART and contains the following code:
Code:

#INT_RDA
void rda_handler(void)
{
   unsetTimer(&rs232TimerRef);

   inDataBuffer[BufPtr] = RCREG;               // Get received character from receive buffer
   BufPtr++;

   rs232receiving = 1;                        // driver is receiving data
   setTimer(RS232_TIMEOUT, &rs232TimerRef);
}

The Timer in the UART receiving Interrupt is used to detect gaps in the received data stream. As one can see the Interrupt uses an other timer reference (rs232TimerRef) which is declared globally (in order to make it accessible to the interrupt handler and to other functions in the UART module).
Guest








PostPosted: Thu Oct 16, 2008 8:08 am     Reply with quote

Oh sorry I didn't read your second post Wayne_. I do not deallocate the memory because I don't use dynamic memory. Therefore the mainTimRef should always be valid.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 8:11 am     Reply with quote

And the definition for rs232TimerRef ?
Guest








PostPosted: Thu Oct 16, 2008 8:17 am     Reply with quote

This one is declared globally in the file for the RS232 driver (in the same file the RDA interrupt is implemented).

Code:

struct timref_t rs232TimerRef;


The pointers in the timers[] array are only used in the interrupt in order to know which timref_t's I have to increment and check for a Timeout. However the timref_t's itself (which actually allocate the memory) remain valid throughout the runtime of the program because they are either declared globally or static (if only used in a function) or in main.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 8:30 am     Reply with quote

How big is your inDataBuffer[BufPtr] buffer ?
Is it possible that you recieve too many characters before resetiing it therfore overwriting something else ?
Guest








PostPosted: Thu Oct 16, 2008 8:36 am     Reply with quote

Huh ok this one I forgot to check Confused But it is not the solution to my problem. I quickly fixed it (BufPtr will be reset if bigger than the buffer size) and checked it with my hardware. Still the same behavior...
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 8:40 am     Reply with quote

This may just be a case of you trying to debug it !

Is your breakpoint in the if statement ?
Guest








PostPosted: Thu Oct 16, 2008 8:47 am     Reply with quote

No I set the breakpoint after the decision on the first statement in the if path. I tried to run the progam without debugging (program the chip and let it run) -> same behavior (checked it with some stupid LED's).

But to come back to the memory locations which are commonly used by the compiler. If the function getTimeout works correct (what is the case in 99% of the calls) the two memory location 8c7 and 8c8 hold the address to mainTimRef. When it fails locations 8c7 and 8c8 hold either the address of rs232TimerRef or a mix between the address of mainTimRef and rs232TimerRef...
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 8:55 am     Reply with quote

There is a potential problem with setTimer.

It is possible that during setTimer in main the rs232 interrupt occurs and calls setTimer.
Depending on where in the setTimer routine the main call is the rs232 call could use the same timer index as the mainTimRef causing corruption.

I would sort this and see if it fixes your problem. I can see where you may see what you are with this problem.

The only way to stop this I can see is by disabling interrupts during the allocation of the timerindex in the setTimer routine.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Thu Oct 16, 2008 9:01 am     Reply with quote

Just a question to anyone who can answer.

If this is true shouldn't the compiler issues a "Disabled interrupts due to re-entrency" warning ?
Guest








PostPosted: Thu Oct 16, 2008 9:03 am     Reply with quote

Well I think this is also not the problem because the compiler states that it disables interrupts during call of setTimer and unsetTimer to prevent reentrancy (sorry I forgot to post that one Embarassed ). Do you know if there is a directive for the compiler to disable interrupts during the execution of a particular function (i.e. the same thing it does to setTimer and unsetTimer)?
Guest








PostPosted: Thu Oct 16, 2008 12:05 pm     Reply with quote

Again, Wayne_ thanks for your support. I now passed the problem also to CCS. I will post the answer I get here too. Meanwhile any other suggestions are greatly appreciated.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Oct 17, 2008 1:50 am     Reply with quote

Yes, please let us know the outcome.
Ttelmah
Guest







PostPosted: Fri Oct 17, 2008 2:15 am     Reply with quote

I'd say the most likely problem source here, is accessing multi byte values both inside and outside the interrupt.
The compiler automatically disables interrupts around _functions_, that might result in re-entrancy (function calling itself), but does not disable interrupts around multi-byte I/O operations to access values both from inside and outside the interrupt. So (for instance), when I want to access a counter, that is being updated inside an interrupt, I'd use:
Code:

int32 mirror_counter;
disable_interrupts(global);
mirror_conter=counter;
enable_interrupts(global);

and then use this 'mirror' value for my operations.

Best Wishes
Guest








PostPosted: Fri Oct 17, 2008 3:10 am     Reply with quote

Hi Ttelmah

Thanks for your input. However I think my problem is slightly another one because the passing parameter of the function gets corrupted during the function call and not the variable itself. I will try to use a local variable in my getTimeout to save the passing parameter there but I think that this will not prevent the data corruption...
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group