Establish UART Communication Between Raspberry Pi 4 and HiFive1 Boards
In this lab you’ll set up reliable UART links between a Raspberry Pi 4 and a HiFive1, program the HiFive1 to echo received data, and create a Python script on the Pi to forward steering angles to the board.
Part 0: Enabling UART on the Raspberry Pi 4
The Pi 4 ships with four UART controllers. For this exercise we’ll use uart2 and uart3. Add the following line to /boot/config.txt and reboot:
dtoverlay=uart2,115200 dtoverlay=uart3,115200
After rebooting, the device nodes /dev/ttyAMA1 (uart2) and /dev/ttyAMA2 (uart3) will be available.
Connect the boards as follows:
- HiFive1 UART1 RX (pin 7) ↔ Pi 4 UART2 TX (pin 27) – data path for steering commands.
- HiFive1 UART0 TX (pin 1) ↔ Pi 4 UART3 RX (pin 29) – debug channel for console output.
From the Pi you can access these channels via /dev/ttyAMA1 (send to HiFive1) and /dev/ttyAMA2 (receive HiFive1 console).
Part 1: Programming the HiFive1
On your PC (not the Pi), download the skeleton project:
$ cd ~/Documents/PlatformIO $ wget https://ittc.ku.edu/~heechul/courses/eecs388/l10-comm.tar.gz $ tar zxvf l10-comm.tar.gz
Add the l10-interrupt folder to your VSCode workspace. The goal is to read data from UART1, echo it on UART0, and print the result to the console.
Here’s a concise algorithm:
while (1) {
if (ser_isready(1)) {
char data = ser_read(1);
ser_write(0, data);
ser_printline(0, "Received: "); /* adjust formatting as needed */
}
}
The serial API is defined in eecs388_lib.h/c:
void ser_setup(int devid); int ser_isready(int devid); void ser_write(int devid, char c); void ser_printline(int devid, char *str); char ser_read(int devid); int ser_readline(int devid, int n, char *str);
Remember that devid = 0 selects UART0 and devid = 1 selects UART1. The ser_isready function checks the UART IP register for pending data:
int ser_isready(int devid) {
uint32_t regval = *(volatile uint32_t *)(UART_ADDR(devid) + UART_IP);
return regval;
}
After compiling and flashing, switch to the Pi and open two terminals:
- Data sender:
screen /dev/ttyAMA1 115200 - Debug viewer:
screen /dev/ttyAMA2 115200
Type any string in the sender terminal; the same string should appear in the debug terminal, confirming correct operation.
Part 2: Python Script on the Raspberry Pi 4
Instead of manual terminals, write a Python program that:
- Opens
/dev/ttyAMA1for writing steering angles. - Opens
/dev/ttyAMA2for debugging. - Captures camera frames, runs DNN inference, and sends the resulting angle to the HiFive1.
Example skeleton using pySerial:
import serial
import time
import cv2 # or your camera interface
# Open serial ports
ser1 = serial.Serial('/dev/ttyAMA1', 115200, timeout=1)
ser2 = serial.Serial('/dev/ttyAMA2', 115200, timeout=1)
while True:
# Capture frame
frame = cv2.imread('frame.jpg') # replace with actual camera read
# Run DNN inference
angle = dnn_inference(frame) # float32
# Convert float to 4-byte representation
angle_bytes = angle.to_bytes(4, byteorder='little', signed=True)
ser1.write(angle_bytes)
# Optional: read debug output
debug = ser2.readline().decode().strip()
if debug:
print('HiFive1:', debug)
# Timing control (e.g., 30 fps)
time.sleep(1/30)
# Clean up
ser1.close()
ser2.close()
Note: Convert the float to bytes before sending, and use the same byte order on the HiFive1 when interpreting the value.
Appendix: Raspberry Pi 4 GPIO and Peripheral Overview
The Pi 4 offers expanded I²C, SPI, and UART interfaces beyond the original Pi models. Use the pinout command to view all pins:
pinout
Key peripherals:
- I²C – up to 6 buses, discover devices with
sudo i2cdetect -y 1. - SPI – enable via
raspi-config, use standard SCLK/MOSI/MISO lines. - UART – asynchronous serial, ideal for headless communication or connecting to microcontrollers like the HiFive1.
For detailed configuration, consult the official Raspberry Pi documentation: Config‑txt Guide.
Manufacturing process
- Understanding Data Flow: From Simplex to Full‑Duplex Communication
- Mastering Wireless Communication Range: How Power, Data Rate, and Interference Shape Connectivity
- Mastering UART: The Universal Asynchronous Receiver/Transmitter Explained
- Seamless MQTT Integration: NodeMCU DHT22 Sensors with Raspberry Pi 3 B+ as Broker
- Facilitating Deaf-Blind Communication Using 1Sheeld and Arduino
- Python 3 to Arduino UNO: Easy Command Control and LED Demo
- Build a Morse Code Transmitter with Arduino – Easy DIY Guide
- Master Serial Communication with Arduino – Step-by-Step Video Guide
- Enhancing Smart Home Connectivity: Building Truly Intelligent and Autonomous Devices
- Arduino Serial Communication: Mastering UART Basics and Troubleshooting