Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
9 years ago

NIOS UART and esp 8266

Hi all,

I'm trying to make an interface between an FPGA and an esp8266 with the NIOS II UART Core, here you found my transmit and receive functions


void uart_send_data(unsigned char char1)
{
	alt_u32 j,len;
	 j=strlen(char1);
	 alt_printf("Beginning transfer \n ");
	for (len =0 ; len <j; len++){
	     IOWR_ALTERA_AVALON_UART_TXDATA(UART_BASE,char1);
	    alt_printf("Sending : %c ",IORD_ALTERA_AVALON_UART_TXDATA(UART_BASE));//Character sent
	}
}

void uart_receive_data(void){alt_u16 j=0,len=0,i=0 ;
volatile alt_8 temp;
status = IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
while ( ( status & 0x0080)){
	temp= IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);
	Mem=temp;
	j++;
} // waiting for data
len=strlen(Mem);
for (i=0;i<len;len++){
alt_printf("REP%c",Mem);
}
}

this functions aren't working well I don't know if it is because the ESP8266 or I made smth wrong.

Please guys can you tell me if my program is okay.

TKHS

4 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The standard Altera UART core has no TX FIFO; then you need to check the tx buffer ready flag before sending each char.

    Moreover you have a bug in the receive function, too: you need to update the status var at each cycle of the while loop, otherwise your code will stay indefinitely in the loop until j gets out of control and cause a memory corruption.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    The standard Altera UART core has no TX FIFO.

    --- Quote End ---

    Humm I see, so for the TX/RX FIFO what do I have to do ? keep the standard one, or should I have an UART with FIFO ?

    I do have another question about altera commands: what is the return type of an IORD_ALTERA_AVALON_.... command?

    Thank so mush for your help you're saving me :)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Working without the FIFO is not an issue. But this depends on your code flow and the performance you require.

    IORD returns usually a 32bit data. In the case of uart or other device registers possibly only the lower 8/16 bits are significative
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi again,

    I changed my C code because it wasn't working good, so far my transmit/receive work's fine, the next step is to get the received message and compare it with a list of responses. but I couldn't do it. any ideas please??

    # define RRDY 0x80
    typedef volatile struct {
        char send;
        char rece;
        int lenS;
        int lenR;
    }message;
    void uart_receive_data(void)
    {
        int count=0;
        message mess;
        alt_u8 status;
        status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);
        while((status & RRDY)==RRDY    )    // check if reception ready
        {
            mess.rece=IORD_ALTERA_AVALON_UART_RXDATA(UART_BASE);//receive character
            printf("%c",mess.rece);   //print the character received
            status= IORD_ALTERA_AVALON_UART_STATUS(UART_BASE);    //update status register
            count++;
        }
    }