Copyright © https://mongoose-os.com

Mongoose OS Forum

frame

Issue In reading from UART without setting dispatcher.

edited November 23 in Mongoose OS
#define UART_NO 0

static void timer_cb(void *arg) {
  /*
   * Note: do not use mgos_uart_write to output to console UART (0 in our case).
   * It will work, but output may be scrambled by console debug output.
   */
//  printf("Hello, UART0!\r\n");

  mgos_uart_printf(UART_NO, "Hello, UART1!\r\n");

  (void) arg;
}

int esp32_uart_rx_fifo_len(int uart_no);

static void uart_dispatcher(int uart_no, void *arg) {
  assert(uart_no == UART_NO);

  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) {
      printf("%.*s", (int) rxb.len, rxb.buf);
    }
    mbuf_free(&rxb);
  }
  (void) arg;

}

void uart_thread( void* param )
{

    char ch[20];
  size_t rx_av = 0;
  struct mbuf rxb;
  printf("---------In UART READ THREAD----------\r\n");
        while((rx_av = mgos_uart_read_avail(UART_NO)) > 0) {
            printf("data available\r\n");
//        if (rx_av > 0) {
//            struct mbuf rxb;
            mbuf_init(&rxb, 0);
            mgos_uart_read_mbuf(UART_NO, &rxb, rx_av);
            if (rxb.len > 0) {
                printf("%.*s", (int) rxb.len, rxb.buf);
            }
            mbuf_free(&rxb);
        }

   while(1);
}
enum mgos_app_init_result mgos_app_init(void) {
  struct mgos_uart_config ucfg;
  mgos_uart_config_set_defaults(UART_NO, &ucfg);
  /*
   * At this point it is possible to adjust baud rate, pins and other settings.
   * 115200 8-N-1 is the default mode, but we set it anyway
   */
  ucfg.baud_rate = 115200;
  ucfg.num_data_bits = 8;
  ucfg.parity = MGOS_UART_PARITY_NONE;
  ucfg.stop_bits = MGOS_UART_STOP_BITS_1;
  if (!mgos_uart_configure(UART_NO, &ucfg)) {
    return MGOS_APP_INIT_ERROR;
  }

//  mgos_set_timer(1000 /* ms */, true /* repeat */, timer_cb, NULL /* arg */);

//  mgos_uart_set_dispatcher(UART_NO, uart_dispatcher, NULL /* arg */);
  mgos_uart_set_rx_enabled(UART_NO, true);
  if(pdPASS != xTaskCreate((TaskFunction_t)uart_thread,"uart_thread",2000/sizeof( portSTACK_TYPE ),NULL,4,NULL))
  {
      printf("task creation failed\r\n");
  }
  return MGOS_APP_INIT_SUCCESS;
}

Comments

Sign In or Register to comment.