This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Timer implementation, to avoid infinite receive loop.

Hello together, I got stuck a bit. Maybe someone could push me into right direction. I am sending and receiving data with my F2806. Depending on type of function I receive in my requst, I know the expected amount of bytes, which the request should contain. What I am worrying about is, if some byte would get lost, then I would get stuck in my "getchar" function. Therefore I have tried to implement a timer, which should help me to leave the "getchar" loop in case one or more bits are missing, got lost. But it doesnt work properly. Here some code,

 

Here I am starting my timer, and calling my function to read data from RXBUF (n could be any number ( 6 - 10), depending on the received function in request)

   StartCpuTimer1();
   for (i = 1; i <n; i++)
   {
          
           rx_buf[i] = serial_port_getchar(&SciRegstr);
           test_variable=rx_buf[i];
           ReloadCpuTimer1();
          
   }
   StopCpuTimer1();

 

My function to read data from RXBUF, here I took 0xFFF0BDBF as threshold (I just wanted to wait some fixed time (0,01 sec), here 100MHz quartz ->   number_of_cycles= 0,01(s)/(10ns) = 1.000.000cycles ->  1.000.000cycles=0xF4240 -> timer initialized with "CpuTimer1Regs.PRD.all  = 0xFFFFFFFF;" no prescaler ->

0xFFFFFFFF-0xF4240=0xFFF0BDBF, that means timer has to count down from 0xFFFFFFFF to 0xFFF0BDBF and the while condition will be fullfilled


unsigned char serial_port_getchar(struct REG_VARS *p2_register)
{
  
   while((p2_register->reg_type->SCIFFRX.bit.RXFFST==0) && (CpuTimer1Regs.TIM.all > 0xFFF0BDBF));// (timer0_period)));
  
   return (p2_register->reg_type->SCIRXBUF.all);
}

 

My problem is I am not able to receive whole data correctly, doestn matter wich timer I am using for "CpuTimer1Regs.TIM.all" condition.

Do I miss something? I have spent the whole weekend to solve this, but I am running against a wall!

Does anyone have any suggestion?

Thanks

Bye

 

  • Ok, another approach. I wanted to test my timer1, because not sure what the problem is.

    Following few lines,

     

    int timer1_period = 1000;

    if(ReadCpuTimer1Counter()< timer1_period)
    {
         tx_buf[0]=1;   // DEBUG

         ReloadCpuTimer1();
    }

     

    Timer1 initialization,

     

    // Initialize timer period to maximum:
        CpuTimer1Regs.PRD.all  = 0xFFFFFFFF;
        // Initialize pre-scale counter to divide by 1 (SYSCLKOUT):
        CpuTimer1Regs.TPR.all  = 0;//0x00FF;
        CpuTimer1Regs.TPRH.all = 0;//0x00FF;
        // Make sure timer is stopped:
        CpuTimer1Regs.TCR.bit.TSS = 1;
        // Reload all counter register with period value:
        CpuTimer1Regs.TCR.bit.TRB = 1;

     

    with SYSCLOCK 100000000 (100MHz) timer1 would need (0xFFFFFFFF/100000000)=~42seconds to count down to 0, theoretically my DEBUG line should be called averey 42 seconds, but in my case, its like it is called every cycle. My timer1 "StartCpuTimer1();" is called right before I jump into my for(:;) infinite loop. What am I doing wrong?