0

I am using STM32MP157-DK2 board and I am creating a program to communicate with processor. To do this, I have created a VIRTUAL UART (ttyRPMSG0 channel) and I am able to send messages from A7 to M4 and take actions like switching leds.

A7 side:

static void LED_ON (GtkWidget *widget, gpointer data)
{
    fd = open("/dev/ttyRPMSG0", O_RDWR |  O_NOCTTY | O_NONBLOCK);
    if (fd < 0) {
        printf("CA7 : Error opening "/dev/ttyRPMSG0"\n");
    }

    write(fd, "start", 5);
    close(fd);
}

M4 side:

  while (1)
  {
    OPENAMP_check_for_message();

    if (VirtUart0RxMsg) {
      VirtUart0RxMsg = RESET;

      if (!strncmp((char *)VirtUart0ChannelBuffRx,"start",5))
      {
          HAL_GPIO_WritePin(GPIOA, GPIO_PIN_14, GPIO_PIN_RESET);
          sprintf ((char*)SendMsgVirt0, "LED green ON\n");
      }
      else if (!strncmp((char *)VirtUart0ChannelBuffRx,"stop",4))
      {
          HAL_GPIO_WritePin(GPIOA, GPIO_PIN_14, GPIO_PIN_SET);
          sprintf ((char*)SendMsgVirt0, "LED green OFF\n");
      }
      else if (!strncmp((char *)VirtUart0ChannelBuffRx,"xValue",6))
      {
          sprintf ((char*)SendMsgVirt0, "X value is: %d\n", x);
      }

      VIRT_UART_Transmit(&huart0, SendMsgVirt0, SendMsgVirt0Size);
      memset(SendMsgVirt0, '\0', sizeof(SendMsgVirt0));
    }
  }

But when I send a message from M4 to A7, I can't read it in the linux side.

A7 side:

static gboolean update_M4_variable (gpointer user_data)
{
    char data [32];
    char msg[128];

    fd = open("/dev/ttyRPMSG0", O_RDWR | O_NOCTTY | O_NONBLOCK | O_NDELAY);
    if (fd < 0) {
       g_print("CA7 : Error opening "/dev/ttyRPMSG0"\n");
    }

    write(fd, "xValue", 6);
    int size = read(fd, &data, 32);

    if(size < 0)
    {
        sprintf (msg, "Cannot read the message: %s\n", data);
    }
    else
    {
        sprintf (msg, "The message has been received: %s\n", data);
    }

    gtk_label_set_text(GTK_LABEL(text_status), msg);
    close (fd);
    return TRUE;
}

With this code, I can see the message that has been sent from M4 in the terminal but what I always get is:

Size = -1 data = empty

Can someone help me?

Thanks!

Telmo

Telmo
  • 13
  • 2
  • `read` failed since it returned `-1`. What is `errno` (or what does give `perror()`?) – Mathieu Nov 21 '22 at 11:05
  • And what does `write` returned? – Mathieu Nov 21 '22 at 11:08
  • read(fd, &data, 32) looks wrong. It should be 'data', not '&data'. – pmacfarlane Nov 21 '22 at 12:15
  • Hi @Mathieu! The write function returns a number that is equal to the length of the bits you send: write(fd, "start", 5) return 5 and write(fd, "stop",4) return 4 – Telmo Nov 21 '22 at 12:41
  • Hi @pmacfarlane I have changed what you mention but the result is still the same. – Telmo Nov 21 '22 at 12:44
  • @Telmo. I was speaking of the `write()` function just above the `read()` returning `-1` in A7 code. Again, what is the `errno` value after `read()` call? – Mathieu Nov 21 '22 at 13:06
  • 2
    @pmacfarlane `&data` and `data` is the same value in that context. see https://stackoverflow.com/q/30194630/1212012 – Mathieu Nov 21 '22 at 13:07
  • @Mathieu, the errno values is 11 and the error description is: Resource temporarily unavailable. Thanks! – Telmo Nov 21 '22 at 13:39

1 Answers1

0

So your error is 11: EAGAIN

From man read

  EAGAIN The file descriptor fd refers to a file other than a
         socket and has been marked nonblocking (O_NONBLOCK), and
         the read would block.  See open(2) for further details on
         the O_NONBLOCK flag.

I believe you get an error because no data is ready to read.

You can try to remove O_NONBLOCK | O_NDELAY from the open call, or wait for a data to be ready.

Mathieu
  • 8,840
  • 7
  • 32
  • 45