UART0: Can't get FIFO length

Pato
Posts: 32
Joined: Sat Mar 24, 2018 12:52 am

UART0: Can't get FIFO length

Postby Pato » Tue Jul 24, 2018 1:04 am

Hi folks,

I'm trying to receive a string on UART0, and at the most basic step to get the number of bytes available in the RX FIFO buffer.
I try to use the magic formulae
fifo_len = (READ_PERI_REG(UART_STATUS(UART0)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT;

in a callback function, and from my PC I send char onto the RX pin of the ESP.

But the fifo_len is always lower than the actual number of char I really sent, and is almost alway 0, unless I call my callback function at very short intervalls likes 10ms. In this case, the count starts to be correct until some mysterious printf at 11500 bauds from the SDK, ending with a '\n', flushes out the UART buffer and reset the count (as it seems that any '\n' in printfed or sent on the UART flushes the buffer... -_- The UART still confuse me: https://bbs.espressif.com/viewtopic.php?f=7&t=9581).

Would you have any clue on how to get the number of bytes received in FIFO ?

.....
I'm working with NONOS-SDK 2.2.0 built with esp-open-sdk.
The mysterious printf in question is like "tail 4r\r\ chksum 0xr1c, \r\n csum 0x1c..."
My test code is:

Code: Select all

#include "ets_sys.h"
#include "osapi.h"
#include "os_type.h"   
#include "user_interface.h"                    // Provide init_done_cb()
#include "espconn.h"                              // Provides the UDP/TCP functions
#include "../esp-open-sdk/sdk/driver_lib/include/driver/uart.h"



static os_timer_t readUartTimer_st;

/**
 *   Callback called on each tick of the timer, for the moment just to print the number of bytes available...
 */
void ReadUartTimerCb(void)
{
    uint8 fifo_len;
    fifo_len = (READ_PERI_REG(UART_STATUS(UART0)) >> UART_RXFIFO_CNT_S) & UART_RXFIFO_CNT;   
    os_printf("UART0: len = %d \n", fifo_len); 
}



/**
 *   Callback called when the ESP is fully initialized.
 */
void PostInitCb(void)
{
    // Hello
    gpio_init();
    PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0);
    GPIO_OUTPUT_SET(GPIO_ID_PIN(0u), 0);
   
    // Setting up UART0
    uart_init(BIT_RATE_9600, BIT_RATE_9600);

    os_timer_disarm(&readUartTimer_st);
    os_timer_setfn(&readUartTimer_st, (os_timer_func_t *) ReadUartTimerCb, NULL);
    os_timer_arm(&readUartTimer_st, 100, true);
}


/**
 *   Main entry point of our programm
 */
void user_init()
{
    system_init_done_cb(PostInitCb);
}


Pato
Posts: 32
Joined: Sat Mar 24, 2018 12:52 am

Re: UART0: Can't get FIFO length

Postby Pato » Tue Jul 24, 2018 5:46 pm

Okay, my mistake.

It was because I did not understand that the code in uart.c was actually already doing some custom processing of the data that arrived on the UART, like reprinting it and clearing the buffer, especially on the UART_RXFIFO_TOUT_INT interrupt event. It was not a basic UART driver to read and write bytes as I thought before.

I simply cleared and remove most of the code in this file to remove all the "processing code" that was called via the sysetm tasks, and now I can really manage the UART with my own code.

Who is online

Users browsing this forum: No registered users and 127 guests