esp8266 serial (UART0) tx/rx

The esp8266 SDK manual and examples are pretty poor. There an example uart.c knocking around, but the receive code is incomplete. I built this out a bit so I could read serial data from the circular buffer. You can download my update files here: uart.c,
uart.h
, uart_register.h. Only uart.c has been changed significantly. (update, I’ve put a tarball which a complete echo example here. UART data can then be received as follows:

  int c = uart0_rx_one_char();

  if(c != -1) {
    char out[30];
    os_sprintf(out,"received %c",c);
    debug(out);
  }

Or if you just want to echo received characters:

  int c = uart0_rx_one_char();

  if(c != -1) {
   uart_tx_one_char(c);
  }

I’ve only built out reading a single character, but it should be easy to build more general functions on top of that. You can still use the old function to transmit:

uart0_tx_buffer(data,strlen(data));

As always you need to init the UART in user_init with something like:

uart_init(BIT_RATE_9600,BIT_RATE_9600);

Or the UART will be configured with 74KBPS (which is a pretty odd value).

Update: You may also want to take a look at my notes on messaging system in the esp8266 SDK, and how it can potentially be used for this application.

This is a continuation of my notes on the esp8266 microcontroller, you can find a complete list of my esp8266 posts here