You are on page 1of 2

Program the UART with State Machine

In this assignment, you have to program the functionality of the UART by software only (not using
the HW registers).
The goal of this assignment is:
 to better understand timing issues in receiving serial, asynchronous data,
 to learn how to debug this kind of issues,
 to design and implement a state machines in the communication protocol context,
 to understand the UART protocol in detail.

Hardware needed:
 Arduino, RedBoard or Nucleo
 (Logic Analyzer)
 Some wires, LED’s, etc

The main steps to do are:


 Study the timing of receiving serial communication, as used by a UART; especially:
o How the start of a transmission is detected
o What the optimal times are to take samples of the data (only one bit per sample is
required here)
o How parity is calculated
o How a transmission is ended
 Design a State Machine for receiving one byte with frame type 8E1.
 For every state, define clearly the entry and exit actions (if any).
For every transition, define clearly the trigger condition, and the Transition Actions (if any).
 Implement this State Machine according to the way described in the slides for this
assignment (using function fsm, and a switch statement to distinguish between the
states).
Usually, this will require some changes in the original design of the State Machine.
 Take care of transmission errors: when any error (like parity) is detected, raise the level of a
certain output pin.
You can use 1 output pin for all errors, or you can use several output pins, depending on the
type of error (like output 5 for wrong parity, output 6 for wrong stop bit, etc).
Instead of using output pins, you can also think of other ways to notify the user about the
error.
 Test your implementation carefully.

Experiment setup:
 Connect your Arduino, RedBoard or Nucleo to your laptop with the USB cable.
 Use putty, or any other terminal program of your liking to send data to your device.
Make sure the settings comply with the ones used in your implementation.
 Check out which pin is normally used by the UART to receive serial data from the USB
connection.
Configure this pin as an ordinary input (so this pin now has to be read by software, and not
by the UART).
 Implement your function for receiving one byte of serial data , as described above.
 For testing purposes, make a program that triggers certain actions when certain characters
have been received (for example: switch LED on after receiving character ‘A’ and off after
receiving ‘B’). In this way you can check whether your UART implementation works correctly.
To make sure that this test program works well, first use the standard UART function to read
the data (Serial.read). When it all works as expected, then replace Serial.read by
your own function.

Some debugging tips:


 Use your Logic Analyzer to visualize the signal that is received from the laptop on the input
pin.
 Use another output pin to show some debugging signal, and show that signal also in the
Logic Analyzer (for example: change the level on output pin 8 at the same moment that you
take a sample from the UART data, so you can see whether you are taking samples at the
right moment).

Possible extensions, when you are done, have some time left:
 Implement different frame formats (different number of data bits, odd/even/no parity,
multiple stop bits).
 Implement different baud rates.
 Implement multiple samples per bit.
 Implement sending of data also.
 Or maybe you have an idea of your own; feel free.

Deliverables required:
 Make a video that shows clearly that your solution works well.
 Make a document with the following information:
o Your complete state machine, including the diagram, state descriptions, transitions
with conditions, actions.
o Explanation about how and why you changed your initial design after the
implementation.
o Explanation of special parts of your code (things that took more time and effort than
expected, special ways in which you implemented things, how you debugged).
o Description of the tests you performed.
o Link to the video that you made.
o What extensions you made to the basic version (if applicable).

Planning:
 Week 4, Tuesday: show the first design of your State Machine
 Week 5, Tuesday: show progress; ask questions
 Week 6, Monday: hand in the code + documentation

You might also like