4 Replies Latest reply on Nov 17, 2016 2:37 PM by Intel Corporation

    Problems with uart_read UART1 in Edison MCU.

    shmkv

      Dear Intel and Edison Community,

      I tried to use the Edison MCU in my project to perform low level tasks such as receiving data from the host and sending via UART1 (and in the opposite direction) with manual flow control (for RS485), but I've encountered some problems.

      #include "mcu_api.h"
      #include "mcu_errno.h"
      
      #define MAX_BUF 255
      unsigned char buf[MAX_BUF];
      unsigned char buf_r;
      
      void mcu_main()
      {
        gpio_setup(165, 1);
        gpio_write(165, 0);
        uart_setup(1, 9600);
        while (1)
        {
             if (uart_read(1, &buf_r, 1)>=0)
                  host_send(&buf_r, 1);
      
             unsigned int len;
             len = host_receive(buf, MAX_BUF);
             if (len>0)
             {
                  gpio_write(165, 1);
                  mcu_delay(5);
                  uart_write(1, buf, len);
                  mcu_delay(5);
                  gpio_write(165, 0);
             }
        }
      }
      

      Using uart_read function (which is a non-blocking only when reading one byte), I can not know the number of the received bytes. Therefore, even when really nothing to read - we get the garbage bytes via host_send. Is there any way to fix it?

       

      With the MPAA we can flexibly manage the UART mraa: /iotdk/jenkins/workspace/mraa-doc-stable/api/mraa/uart.h File Reference , but with MCU API we have only 3 functions.

        • 1. Re: Problems with uart_read UART1 in Edison MCU.
          Intel Corporation
          This message was posted on behalf of Intel Corporation

          Hi Shmkv,

           

          Thanks for reaching out.

           

          Well, I'm not sure if it is possible to know the number of received bytes, but did you try with MAX_BUF instead of 1 on the functions uart_read and host send?

           

          The function uart_read returns 0 if it is successful, I think that you get garbage because you are using the if with >=0.

           

          With all that said, I recommend you to use the following lines:

           

          if (uart_read(1, &buf_r, MAX_BUF)==0)  
                      host_send(&buf_r, MAX_BUF);
          

           

          Let me know if it works.

           

          Regards, 
          -Leonardo

          • 2. Re: Problems with uart_read UART1 in Edison MCU.
            shmkv

            Thanks for the answer.

             

            I try it and in this case uart_read is blocking (wait until it is read MAX_BUF bytes). Changing >=0 to ==0 also did not help: uart_read always return 0, even if in reality the data has not been sent to the UART (Therefore, I can not understand what has been read - data or garbage).

            • 3. Re: Problems with uart_read UART1 in Edison MCU.
              Intel Corporation
              This message was posted on behalf of Intel Corporation

              Hi Shmkv,

              I think this is not possible with the APIs to access UART, I recommend you to use another GPIO as a flag, that indicates when the data is in the port.

              You can also check the example to read from UART in your folder: MCU_SDK_HOME/docs/api_doc/html/sample__uart__receive_8c_source.html, it is the only information available to read data from UART.

              I hope you find this information useful.

              Regards,  
              -Leonardo

              • 4. Re: Problems with uart_read UART1 in Edison MCU.
                Intel Corporation
                This message was posted on behalf of Intel Corporation

                Hi Shmkv,

                I was wondering if you have updates on this. Was my recommendation useful?

                Remember that we are here to help you.

                Best Regards,
                -Leonardo