Skip to content

Instantly share code, notes, and snippets.

@guglielmino
Last active October 21, 2017 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guglielmino/663e7146ed0096a41aa1aecb884f796c to your computer and use it in GitHub Desktop.
Save guglielmino/663e7146ed0096a41aa1aecb884f796c to your computer and use it in GitHub Desktop.
#include "common/mbuf.h"
#include "common/platform.h"
#include "mgos.h"
#include "mgos_uart.h"
#include "mgos_timers.h"
/*
* GPIO pin numbers, default values depend on UART number.
*
* UART 0: Rx: 3, Tx: 1, CTS: 19, RTS: 22
* UART 1: Rx: 25, Tx: 26, CTS: 27, RTS: 13
* UART 2: Rx: 16, Tx: 17, CTS: 14, RTS: 15
*/
#define UART_NUM 1
static void uart_dispatcher(int uart_no, void *arg) {
assert(uart_no == UART_NUM);
size_t rx_av = mgos_uart_read_avail(uart_no);
if (rx_av > 0) {
struct mbuf rxb;
mbuf_init(&rxb, 0);
mgos_uart_read_mbuf(uart_no, &rxb, rx_av);
if (rxb.len > 0) {
LOG(LL_DEBUG, ("%.*s", (int) rxb.len, rxb.buf));
}
mbuf_free(&rxb);
}
(void) arg;
}
enum mgos_app_init_result mgos_app_init(void) {
cs_log_set_level(LL_DEBUG);
struct mgos_uart_config cfg = {
.baud_rate = 115200,
.num_data_bits = 8,
.parity = MGOS_UART_PARITY_NONE,
.stop_bits = MGOS_UART_STOP_BITS_1,
/*.dev = {
.tx_gpio = 4,
.rx_gpio = 36
}*/
};
mgos_uart_config_set_defaults(UART_NUM, &cfg);
if (!mgos_uart_configure(UART_NUM, &cfg)) {
return MGOS_APP_INIT_ERROR;
}
mgos_uart_set_dispatcher(UART_NUM, uart_dispatcher, NULL /* arg */);
mgos_uart_set_rx_enabled(UART_NUM, true);
return MGOS_APP_INIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment