Part 3 : Omega2 UART

How to send text data over the UART TX Data line, and other things

When developing an embedded microcontroller (MCU) system it is essential to be able to monitor the status and progress of your code for debugging purposes. In addition, you may want a permanent 2-line x 16-character Liquid Crystal Display (LCD) module on your system for data entry and display. Let us look at various ways in which we can monitor the progress of our code.

Simple LED flash
In Part 2 of this series on the Omega2 MCU, we saw how we can use a simple Light Emitting Diode (LED) to monitor program activity. With some creative flash schemes, we can use a single LED to indicate various events. The Omega2 chip itself uses such an LED to indicate when the system is starting up (LED flashes) and when the system is ready (LED stays ON).

Oscilloscope
An oscilloscope is also an indispensable tool during the development, debugging and trouble shooting stage. The 'scope allows you to see and measure events that are too rapid for the eye to capture. Of course, the 'scope must be dispensed with in the final product.

To make sure that we understand how text messages can be generated, let us examine the following techniques.

The important thing to remember here is the fact that we are now communicating with the Omega2 MCU wirelessly over WiFi. The USB cable between your PC and the Expansion Dock provides 5V and 3.3V power to the Omega2. There is no need to be plugged into the PC. The Expansion Dock can be powered by any other 5V source.


Linux echo command

echo Hello World

From the Linux command line, the command above will display Hello World on the command console.

Python print command

From the Linux command line, enter python

You are now in the python command line as indicated by the >>> prompt
From the Python command line, enter

print "Hello World"

To terminate a Python session, type Ctrl-D.

Printing from a python script

Invoke the editor from the Onion Console and enter this one-line program and save the file as test.py
Code:
print "Hello World"
From the Linux terminal console, enter
python test.py


Going Further

Let us put this into an endless loop. You will notice that I have changed the program slightly from the Part 1 example just to be different to give you a different flavor of the syntax.
Code:
n = 0
while 1:
  print "Hello World ", n
  count = 0
  n += 1
  while count < 200000:
    count += 1
All output is emitted from Python to the currently active console terminal using WiFi.

Omega2 UART (Universal Asynchronous Receiver/Transmitter)

The Omega2 has various channels for communicating to the outside world as follows (besides other means via GPIO, I2C, I2S, and SPI):
  1. UART0 = RX0/TX0
  2. UART1 = RX1/TX1
  3. Omega2 USB
On the Expansion Dock, UART0 is connected to the micro-USB jack via a Silicon Labs CP2102 USB-to-UART bridge.
UART1 RX1/TX1 pins are available on the break-out pins on the Expansion Dock.
Omega2 USB is wired to the USB-B jack (large square USB jack).

UART1 Example

Firstly, we need to install the serial package. From the Linux command line, enter:
opkg update
opkg install python-pyserial


Create this simple program and run it.
Code:
import serial
com1 = serial.Serial("/dev/ttyS1")
com1.write("Hello World\r\n")
com1.close()
Program Notes
com1
is our object name which we will use to refer to our UART device.
On the Omega2 itself, the name of UART0 is /dev/ttyS0, which is a module called ttyS0 in the dev directory.
The name of UART1 is /dev/ttyS1.
The default baud setting is 9600. We can explicitly configure the baud to 9600 by using this statement:
com1 = serial.Serial("/dev/ttyS1", 9600)

The program will output "Hello World" to the TX1 pin. There will be nothing to see if you have nothing connected to the TX1 pin on the Expansion Dock. Hence what good is this?

Arduino LCD interface
2x16 LCD text character modules typically come with a parallel bus interface based on the Hitachi HD44780 LCD controller. What we really need now is an LCD module that accepts TTL 0-5V serial UART data, just like a terminal emulator. I call this a "dumb terminal" module. Here is a perfect exercise for an Arduino project with an LCD shield.


CP2102 USB-to-UART Bridge
An alternative is to use a CP2102 USB-to-UART bridge adapter or something similar.
1574643152369.png

These little adapters are very popular and inexpensive. These are little handy-dandy things to have for a situation as this. Plug this into a USB port on your PC. Go to the Device Manager (right-click on the Windows icon at the left bottom on your screen). Click on Ports (COM & LPT) and make a note of the CP210x USB to UART Bridge COMx assignment. Invoke the PuTTy terminal emulator program and configure the serial COMx port for 9600 baud.

Connect a jumper wire from the GND on the Expansion Dock to the GND on the CP2102 adapter.
Connect a second jumper wire from TX1 on the Expansion Dock to RXD on the CP2102 adapter.

With either of these options you will now be able to display the text characters being transmitted from the Omega2.


UART0 Example

As another example, modify the program to send data on TX0.
Code:
import serial
com0 = serial.Serial("/dev/ttyS0")
com0.write("Hello World\r\n")
com0.close()
This will transmit text characters to the micro-USB port on the Expansion Dock. Again, check the Device Manager to see which COMx port has been assigned to this USB device. Of course, if you have both this cable and the CP2102 adapter plugged into the same PC you should see two different COMx assignments on the Device Manager. You will have to determine which is which by plugging in the USB cable and adapter one at a time.

You can have both channels in operation at the same time as we shall see in the final example.


Putting It All Together

Now we put all the pieces together in one massive Python script incorporating:
  1. GPIO LED flash
  2. UART1 TX1 TTL output to LCD module
  3. UART0 TX0 output via USB cable to PuTTy Terminal
  4. Omega2 WiFi back to Python console
  5. Text and numeric display
  6. One-second delay loop

Code:
import onionGpio
import serial
gpio0 = onionGpio.OnionGpio(0)
gpio0.setOutputDirection(0)
com0 = serial.Serial("/dev/ttyS0")
com1 = serial.Serial("/dev/ttyS1")
num = 0
value = 1

# endless loop
while 1:

  gpio0.setValue(value)   # set GPIO0 pin to value
  value = 1 - value       # toggle value between 0 and 1

  # show on PC PuTTy terminal
  com0.write(str(num))
  com0.write(" com0\r\n")

  # show on LCD module
  com1.write("\001")      # my LCD command to display on line 1
  com1.write(str(num))    # show num on LCD

  # show on Python console via WiFi
  print num
  num += 1        # add 1 to num

  # delay for 1 second
  count = 0
  while count < 200000:
     count += 1   # add 1 to count

#end of main program
There you have it.

In Part 4 I will demonstrate interfacing to an I2C LCD module.

Blog entry information

Author
MrChips
Views
2,749
Last update

More entries in General

More entries from MrChips

Share this entry

Top