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:
1 2 3 4 5 6 7 | 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:
1 2 3 4 5 | 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:
1 | uart0_tx_buffer(data, strlen (data)); |
As always you need to init the UART in user_init with something like:
1 | 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.
Hi there! Any idea how may I disable the default WIFI debug output while still being able to write through serial?
Thank you!
Another way to do this is to use the task messaging function. i.e. In the uart0_rx_intr_handler(), call:
system_os_post(0, 1, RcvChar);
Where we send the Signal=1, and the received character is optionally embedded inside the message. This way, you’ll get an immediate callback on pre-register system_os_task() task, without having to resort to polling.
Hi Wing Poon
can you post a more detailed example?
Your link http://41j.com/blog/wp-content/uploads/2015/01/uart_test.tar.gz seems to be broken. If I click it there is an redirection to your blog main site….