How many characters Uart Read return ?

 Hello,

I am using SK-S7G2 to make serial communication (RS485 Modbus RTU) with the Hal Driver, her is my code :

#define NAB 5
#define DEB_MAPPING 0
#define FIN_MAPPING 80

void hal_entry(void)
{int i, j;
  static ssp_err_t err;              // Create a variable for the error
  uint16_t crc;
  timer_info_t info_timer;
    
 err = g_timer0.p_api->open(g_timer0.p_ctrl, g_timer0.p_cfg);
 err = g_timer0.p_api->periodSet(g_timer0.p_ctrl, 5000, TIMER_UNIT_PERIOD_USEC);
 g_timer0.p_api->stop(g_timer0.p_ctrl);
 g_timer0.p_api->infoGet(g_timer0.p_ctrl, &info_timer);
 
 g_uart0.p_api->open(g_uart0.p_ctrl, g_uart0.p_cfg);
 
 while (1)
    {do
        err = g_uart0.p_api->read(g_uart0.p_ctrl, BufRead, 8);
     while(FlagRequest == 0);
     
         FlagRequest = 0;
         if((BufRead[0] == NAB) && ((BufRead[1] == 3)||(BufRead[1] == 4)))  // Nab == 2 et Fct == 3 ou 4 ?
            {i = (BufRead[2] << 8) + BufRead[3];
             j = (BufRead[4] << 8) + BufRead[5];
             if((i >= DEB_MAPPING) && (i <= FIN_MAPPING) && ((i + j) <= FIN_MAPPING))
                {crc = CalcCRC16 (BufRead, 6);
                 if((BufRead[7] == (crc >> 8) && (BufRead[6] == (crc & 0xFF))))
                 {Buff[0] = BufRead[0];
                  Buff[1] = BufRead[1];
                  Buff[2] = 2*BufRead[5];
                  for(i=0;i<Buff[2];i++)
                    Buff[3+i] = 0;
                  crc = CalcCRC16 (Buff, 3+Buff[2]);
                  Buff[3+Buff[2]] = crc & 0xFF;
                  Buff[4+Buff[2]] = (crc >> 8) & 0xFF;
                  err = R_IOPORT_PinWrite(IOPORT_PORT_04_PIN_13, IOPORT_LEVEL_HIGH);              // Set pin on port 6 bit 0 low
                  g_uart0.p_api->write(g_uart0.p_ctrl, Buff, 5+Buff[2]);
                 }
                }
            }        
         index = 0;
    }
}


void user_uart_callback(uart_callback_args_t * p_args)
{ssp_err_t err;                         // Create a variable for the error

 if (0U == p_args->channel)             // UART0
    {switch (p_args->event)
        {case UART_EVENT_RX_COMPLETE : g_timer0.p_api->start(g_timer0.p_ctrl);  // Use if Transfert Driver on r_dtc Event enabled
                                                                  g_timer0.p_api->reset(g_timer0.p_ctrl);
                                                                  break;
         case UART_EVENT_TX_COMPLETE : err = R_IOPORT_PinWrite(IOPORT_PORT_04_PIN_13, IOPORT_LEVEL_LOW);  // Set PdL low
         default                     :                       break;
        }
    }
}

The code work correctly, but i want to be sure that i receive 8 characters when i go to the callback fonction with

the UART_EVENT_RX_COMPLETE event.

Because i can have the UART_EVENT_RX_COMPLETE event with less than 8 characters

" DR flag (Receive Data Ready Flag
  When FRDRHL contains less data than the specified receive triggering number, and no next data was received after
  15 ETU
*1 from the last stop bit, and the SSR_FIFO.FER and SSR_FIFO.PER flags are 0 "

The question is : how know how much characters i have received

    Thank you for your help

  • Hi Eliec,

    If you make a call to UART read then you will only observe UART_EVENT_RX_COMPLETE once desired number of characters has been received. If you don't use DTC for reception, you can use UART_EVENT_RX_CHAR which is invoked per each byte received on UART (when read is not called).

    Regards
  • In reply to Renesas Karol:

    Hi Karol,
    Thank you for your quick response, in the beginning i use the UART_EVENT_RX_CHAR without DTC and i active an AGT timer on each character reception and i use the Timer callback to detect the end of the trame, it works correctly and now i want to do the same thing with the use of UART_EVENT_RX_COMPLETE and DTC

    I believe that there is a time out (15 bit) in the Read fonction.
    " DR flag (Receive Data Ready Flag
    When FRDRHL contains less data than the specified receive triggering number, and no next data was received after
    15 ETU*1 from the last stop bit, and the SSR_FIFO.FER and SSR_FIFO.PER flags are 0 "

    So you mean i can't detect the end of a trame by an inactivity on the line with the Read function ?

    Is there an other possibility to introduce a time out of 3 or 4 characters in the Read to be compatible with
    Modbus RTU ?

    Thank you for your help
  • In reply to Eliec:

    Hello Eliec,

    By default, DTC-less operation on FIFO-capable channels sets FIFO trigger level to 15 bytes. Receive interrupt will only be executed when:
    * You received 15 characters.
    * You received less than 15 characters and sufficient delay has been observed since the final byte.

    Such delay may prove troublesome in certain applications (i.e. MODBUS). If you still require UART_EVENT_RX_CHAR, I recommend setting UART FIFO trigger leve to 1 in UART properties. If you plan to use read with UART_EVENT_RX_COMPLETE only, then you can simply add DTC for reception (RXI signal) underneath the UART driver.

    Regards
  • In reply to Renesas Karol:

    Hi Karol
    Thank you for tour response