4 Replies Latest reply on Sep 2, 2015 8:58 AM by PeteB

    Two-way communication with MCU

    Cooper132

      Hi all,

       

      I'm trying to get some two-way communication between the MCU and the Atom going, via the ttymcu0 interface.

      The idea is to have some control over what the MCU does from the host system, and to eventually send sensor data

      from the MCU to the host for further processing. I'm developing in C++ on the host.

       

      So far I've managed to reproduce the behaviour of the communication code example in [1], however any further fstream

      write actions on the TTY from the host application aren't recieved by the MCU. Echoing via the console still works however.

       

       

      My MCU code:

      #include "mcu_api.h"
      #include "mcu_errno.h"
      #include <string.h>
      
      void mcu_main()
      {
          char buf[64];
          int len;
          char active = 0;
      
          while (1)
          {
              if (!active) {
                  do {
                      len = host_receive((unsigned char *)buf, 64);
                      mcu_sleep(10); // 10 Hz
                  } while (len <= 0);
      
                  if (strncmp(buf, "mcu_start", 9) == 0) {
                      debug_print(DBG_INFO, "received start command!\n");
                      mcu_sleep(100);
                      host_send((unsigned char*)"[MCU] start\n", 12);
                      active = 1;
                      len = 0;
                  }
              } else {
                  len = host_receive((unsigned char *)buf, 64);
                  if (len > 0) {
                      debug_print(DBG_INFO, "received %s\n", buf);
                      if (strncmp(buf, "mcu_stop", 8) == 0) {
                          debug_print(DBG_INFO, "received stop command!\n");
                          host_send((unsigned char*)"[MCU] stop\n", 11);
                          active = 0;
                          len = 0;
                      }
                  }
              }
      
              mcu_sleep(10); // 10 Hz
          }
      }
      
              mcu_sleep(10); // 10 Hz
          }
      }
      
      
      

       

      Some of my host code:

      init

      std::fstream m_tty;
      m_tty.open("/dev/ttymcu0", std::fstream::in | std::fstream::out | std::fstream::app);
      if (!m_tty.is_open()) {
        printf("[MCU] Error opening TTY!\n");
      } else {
        m_tty.write("mcu_start", 9);
      
        std::string rec;
        std::getline(m_tty, rec);
      
        if (rec.find("[MCU] start") != std::string::npos) {
          printf("[MCU] Running.\n");
        } else {
          printf("[MCU] Error initializing MCU!\n");
        }
      }
      
      
      
      

      main loop

      m_tty.write("test", 4);
      m_tty.flush();
      usleep(100000);
      

      destructor

      m_tty.write("mcu_stop", 8);
      std::string rec;
      std::getline(m_tty, rec);
      if (rec.find("[MCU] stop") != std::string::npos) {
        printf("[MCU] Stopped.\n");
      } else {
        printf("[MCU] Error stopping MCU!\n");
      }
      
      m_tty.close();
      
      

       

       

      From the debug output of the MCU SDK I can see that the first "mcu_start" is recieved fine, but all further strings aren't.

      Neither the "test" from the main loop nor the "mcu_stop" in the end show up. I get the "[MCU] Error stopping MCU!" when I stop the program.

      Doing

      echo mcu_stop > /dev/ttymcu0
      

      works fine, the debug messages report the string received.

      I am not cat-ing the /dev/ttymcu0 in parallel.

       

      Any Idea what I'm doing wrong here? Any help would be appreciated.

       

      Thanks!

       

      [1] https://software.intel.com/en-us/node/557354#Communicating_between_the_host_and_the_MCU