Trouble with Timer code for PSOC 4200

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Hi,

   

I need to implement a synchronous receiver that receives 4 Data Bytes. The bytes are clocked by a hardware clock pin. I have written a routine that works well when data is coming in but if the transmitter stops at certain points part way through, my code hangs. My code should be receiving bits based on clock state but have an overall timeout so if data stops then the routine exits after timeout period but it is not working.   If I call the "sampleLF" routine when no data being received, it hangs in the loop and never sees Timer_Flag set.

   

Any help much appreciated, thanks.

   

Here is my code that uses a 32 bit timer component

   

#define LFSAMPLETIME  24000000/1000*80

   

void StartTimer(volatile uint32 tdelay){
    Timer_WritePeriod(tdelay);
    Timer_WriteCounter(tdelay);
    Timer_Enable(); 
    Timer_Flag = 0;    
}

   

void StopTimer(void){
   Timer_Stop();
   Timer_Flag = 0;   
}

   

void SampleLFData(uint8 * buf){
    volatile uint8  Sample;
    volatile uint8  nBytes;
    volatile uint8 SamplePtr;

   

    Sample = 0;
    nBytes = 0;
    SamplePtr = 0;
 
    StartTimer(LFSAMPLETIME);  //12ms Data 4 Bytes should take 8ms to receive
    while ((!Timer_Flag)&&(nBytes < 4)){   //get 4 bytes or timeout
         while(!CL_DAT_Read() & !Timer_Flag){} //wait for clock high
         Sample<<=1;
         if(DAT_Read()) Sample |= 1;   //read data bit
         SamplePtr++;
         if(SamplePtr == 8){            //save every 8 bits as byte
          if(nBytes < 4) buf[nBytes] = Sample;
          Sample= 0;
          nBytes++;
          SamplePtr = 0;
         }
         while(CL_DAT_Read() & !Timer_Flag){}  //wait for clock low
         CyDelayUs(50);  //clock settle time
    }
    StopTimer();
 }

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your Timer does not run in deep sleep mode, so no interrupt fired. Better use another source for interrupt.

   

 

   

Bob

View solution in original post

0 Likes
10 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I cannot see an interrupt handler for your timer. Can you please post the complete project?

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

What sets timer flag ? ISR ?

   

 

   

Where is the code hung ? Which line ?

   

 

   

Regards, Dana.

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Here is the interrupt code. The code hangs in this line when I test with debugger because no clock pulses are being received, but the timer should timeout so I can exit the routine ready for next lot of data.

   

It is expected from time to time the transmitter will stop sending part way through a packet so I just need a function timeout to prevent it from hanging if data stops.

   

while(!CL_DAT_Read() & !Timer_Flag){} //wait for clock high

   

CY_ISR(TimerINT){
    Timer_ReadStatusRegister();
    Timer_Flag = 1;
}

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

The last post may have been a bit confusing, when I said "This line" I meant the test for timeout or clock, not the ISR

   

while(!CL_DAT_Read() & !Timer_Flag){} //wait for clock high

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Is Timer_Flag declared as volatile?

   

 

   

Bob

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

This is my declaration for Time_Flag

   

volatile uint8  Timer_Flag = 0;

0 Likes
lock attach
Attachments are accessible only for community members.
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Attached is project work bundle

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Your Timer does not run in deep sleep mode, so no interrupt fired. Better use another source for interrupt.

   

 

   

Bob

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

I managed to get it working. The timer is started after waking from deep sleep and then stopped.

   

Thanks for all the help

0 Likes