MEAM.Design - M4 - USB Communications


Overview

For either debugging or data communications, it is possible to send and receive data between the m4 and a computer via the same USB port that you use for programming.




printf will print to stdout which is set to line buffered to improve throughput compared to no buffering. So the stream is only sent when a newline character is encountered or you can manually call fflush(stdout) to force sending the data. The buffer is of 255 bytes so you need to atleast have a "\n" every 255 bytes or call fflush(stdout) before that or you will lose some data.

To have floating point support for printf include this statements in your main.c file (just after all #include)

__asm__(".global _printf_float");

__asm__(".global _scanf_float");

scanf not supported at this time




This doesn't apply to white M4's.

On green M4's this procedure is required to use the virtual serial port over the USB.

After loading the M4 you need to disconnect the M4 from the computer and connect back again within 1 or 2 seconds. If it directly executes the loaded code then the COM port should get detected on the computer. To reset you need to follow the same procedure of disconnecting and connecting again to get the virtual COM port working. Just pushing the reset button will reset the M4 but will not be able to connect to computer.




Computer-side connection options

In Windows, open a serial terminal and connect to the proper COM port. Options include:

Terminal - this may even be able to run on the GM lab computers as it's an executable.
When you press rescan you should see a COM port in the COM dialog box, which should correspond to the location of your device. Press connect and you should be receiving transmitted data immediately. On some machines it might help to run Terminal as an Administrator.
Note - if you are working outside of the GM lab and have a Windows computer, you may need to install this driver to help the OS recognize the M4 (needs to be run as administrator).

In Mac OS X, you can use terminal to send/receive packets. First, find the serial object ("ls /dev/tty.*"), then start the session ("screen /dev/tty.usbmodem###"). To end the session, press Ctrl-A then Ctrl-\.

In Linux, you can use terminal to send/receive packets. First, find the serial object ("ls /dev/ttyACM*"), then start the session ("screen /dev/ttyACM*"). To end the session, press 'Ctrl-A' then '\'. You may need sudo.

You can stream data directly in/out of Matlab, like this:

handle = serial(port,'Baudrate', 115200); where port is either 'COM#' in Windows or '/dev/tty.usbmodem#' in OS X.
fopen(handle);
fprintf(handle, message); where message is a string
fwrite(handle, variable); where variable is a value
fclose(handle);