This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

UART trash value on Thingy91

Hi Devzone,

I use Thingy91. On the nrf9160 side, I run mqtt_simple example; on the nrf52840, I run connectivity_bridge with BLE feature.

When I send "900" via nus service, what I receive on nrf9160 side are:

The reason is because of noise or software config?

Thanks in advance.

Development setup: Ubuntu, ncs v1.5.1, Thingy91.

And this is my additional file to the original example. I also change AT Host to uart1 (to unused it)

#include "my_uart_handler.h"
#include <drivers/uart.h>
#include <zephyr.h>
#include <stdio.h>
#include <string.h>
#include <logging/log.h>
#include "my_led.h"

LOG_MODULE_REGISTER(my_uart, 3);

#define CONFIG_UART_0_NAME      "UART_0"
static led_color color = {.c[0] = 250, .c[1] = 10, .c[2] = 5};
static const struct device *uart_dev;
char my_rx_buffer[100];
static uint8_t i;
bool rcv_flg = false;
bool ctrl_flag = false;
static bool receive_handler(char input)
{

  switch (input)
  {
    case 'I':
      rcv_flg = true;
      return;
      break;
    case 'Q':
      rcv_flg = false;
      break;
    default:
      break;
  }

  if (rcv_flg)
  {
    if ((input >= 48)&&(input <= 57))
    strcat(my_rx_buffer,&input);
    ctrl_flag = true;
    //if (i++ >= 5) rcv_flg = false;
  }
  else
  {
    //LOG_INF("rcv12: %s",log_strdup(my_rx_buffer));
    if (ctrl_flag)
    {
      color.c[0] = my_rx_buffer[0];
      color.c[1] = my_rx_buffer[1];
      color.c[2] = my_rx_buffer[2];
      pwm_out(&color);
      ctrl_flag = false;
    }
    memset(my_rx_buffer,0,100);
  } 

}

static void isr(const struct device *dev, void *user_data)
{
	ARG_UNUSED(user_data); 
        char character;
	uart_irq_update(dev);

	if (!uart_irq_rx_ready(dev)) {
		return;
	}
        LOG_INF("rcv");
        while (uart_fifo_read(dev, &character, 1))
        {
          receive_handler(character);
        }


}


static int my_uart_init(const struct device *arg)
{
  ARG_UNUSED(arg);

  int err;
  uint8_t dummy;

  uart_dev = device_get_binding(CONFIG_UART_0_NAME);
  if (uart_dev == NULL) {
          LOG_ERR("Cannot bind %s\n", CONFIG_UART_0_NAME);
          return -EINVAL;
  }

  uint32_t start_time = k_uptime_get_32();

  /* Wait for the UART line to become valid */
  do {
          err = uart_err_check(uart_dev);
          if (err) {
                  if (k_uptime_get_32() - start_time >
                      500) {
                          LOG_ERR("UART check failed: %d. "
                                  "UART initialization timed out.", err);
                          return -EIO;
                  }

                  LOG_INF("UART check failed: %d. "
                          "Dropping buffer and retrying.", err);

                  while (uart_fifo_read(uart_dev, &dummy, 1)) {
                          /* Do nothing with the data */
                  }
                  k_sleep(K_MSEC(10));
          }
  } while (err);

  uart_irq_callback_set(uart_dev, isr);
  uart_irq_rx_enable(uart_dev);
  return err;

}
SYS_INIT(my_uart_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

Parents Reply Children
No Data
Related