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();
}
Solved! Go to Solution.
Your Timer does not run in deep sleep mode, so no interrupt fired. Better use another source for interrupt.
Bob
I cannot see an interrupt handler for your timer. Can you please post the complete project?
Bob
What sets timer flag ? ISR ?
Where is the code hung ? Which line ?
Regards, Dana.
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;
}
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
Is Timer_Flag declared as volatile?
Bob
This is my declaration for Time_Flag
volatile uint8 Timer_Flag = 0;
Your Timer does not run in deep sleep mode, so no interrupt fired. Better use another source for interrupt.
Bob
I managed to get it working. The timer is started after waking from deep sleep and then stopped.
Thanks for all the help