Template: Serial Expansion HAT User Mnaual

From Waveshare Wiki
Revision as of 07:08, 19 September 2020 by Waveshare-eng11 (talk | contribs)
Jump to: navigation, search

Features

  • Raspberry Pi connectivity, compatible with Raspberry Pi Zero/Zero W/Zero WH/2B/3B/3B+
  • Onboard SC16IS752 expands 2-ch UART and 8 programmable GPIO through I2C, no extra pin required
  • It is stackable up to 16 modules by setting the address jumper, that means up to 32-ch UART
  • Onboard multi LEDs for indicating the UART working status
  • Reserved I2C control pins, allows working with other control boards
  • Comes with development resources and manual (examples in C and python)

SPECIFICATION

  • operating voltage:3.3V
  • Expansion chip:SC16IS752
  • Control interface:I2C
  • Dimension:65mm × 30mm
  • Mounting hole size:3.0mm

Hardware

The SC16IS752 is an I2C-bus/SPI bus interface to a dual-channel high performance UART. It also provides the application with 8 additional programmable I/O pins. This module uses I2C interface by default, and its device address is hardware configurable by A0 and A1.

Serial-Expansion-HAT-intro.jpg


PINOUT

PIN Description
3V3 3.3V
GND Ground
TXDA Transmit pin of CHA
RXDA Receive pin of CHA
RTSA Receive request of CHA
CTSA Transmit request of CHA
TXDB Transmit pin of CHB
RXDB Receive pin of CHB
RTSB Receive request of CHB
CTSB Transmit request of CHB

LED

PWR: Power indicator
TXDA: Trasmint indicator of CHA
RXDA: Receive indicator of CHA
TXDB: Transmit indicator of CHB
RXDB: Receive indicator of CHB

I2C Address Configuration

A0, A1 are used to control the I2C address of device, you can change them by soldering 0ohm resistors acccording the table:
Serial expansion hat i2caddr.png
详见datasheet:Page39

The I2C addresses in the table are 8bits, however, the actual address is 7bits, you need to right-shift one bit to get the actual I2C address. For example, if you connect A1 and A0 to Vdd, the address of the module is 0x90 according to the table, to get the actual address you need to right-shift the data from 1001 000X to 100 1000, that is 0x48.
【Note】This module A0 and A1 are default welded to 3.3V, with I2C address 0x48
In theory, you can connect 32 devices to the same developer board by changing address.
If you bought the old version which is produced before 20191231, the interrupt pin is connected to GPIO of PI directly, you can only wiring the devices for stacking multi-devices.
If you buy the new version which is produced after 10291231, the interrupt pin is configurable by soldering resistors. Supports up to 5 devices by changing the pin, if you want to stack more than five devices, you need to wires them by cable.

Working with Raspberry Pi

Enable I2C Interface

Open a terminal and run the following commands:

sudo raspi-config 
Choose Interfacing Options -> I2C -> Yes.

Reboot Raspberry Pi:

sudo reboot

RPI open i2c.png

Install Libraries

Install Library

If you use the bookworm system, you can only use lgpio library, bcm2835 and wiringPi can't be installed and used.

BCM2835

#Open the Raspberry Pi terminal and run the following command
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz
tar zxvf bcm2835-1.71.tar.gz 
cd bcm2835-1.71/
sudo ./configure && sudo make && sudo make check && sudo make install
# For more, you can refer to the official website at: http://www.airspayce.com/mikem/bcm2835/

WiringPi

#Open the Raspberry Pi terminal and run the following command
cd
sudo apt-get install wiringpi
#For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required:
wget https://project-downloads.drogon.net/wiringpi-latest.deb
sudo dpkg -i wiringpi-latest.deb
gpio -v
# Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error

# Bullseye branch system using the following command:
git clone https://github.com/WiringPi/WiringPi
cd WiringPi
. /build
gpio -v
# Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error

lgpio

#Open the Raspberry Pi terminal and run the following command
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
sudo make install

# You can refer to the official website for more: https://github.com/gpiozero/lg
  • python
sudo apt-get install python-dev
sudo apt-get install python-rpi.gpio
sudo apt-get install python-smbus

Configuration

  • Append the line below to /boot/config.txt file:
sudo nano /boot/config.txt
# addr shoube the same as the actual setting of A0A1, default 0X48
dtoverlay=sc16is752-i2c,int_pin=24,addr=0x48
# reboot
sudo reboot
  • After rebooting, you can execute the command: ls /dev to check if SC16IS752 has been enabled to kernel:

Serial expansion hat finddev.png
GPIOchip is the driver of 8 GPIO.(It maybe gpiochip3 or gpiochip2 according to different versions of OS), ttysc0 and ttysc1 is the driver of serial ports.
You cannot directly stack the devices for multi-connecting because they use the same interrupt pin.
You need to wire the devices by cable or change the resistors, and modify the config.txt file:
dtoverlay=sc16is752-i2c,int_pin=xx,addr=0x48
xx is the actual BCM code of the interrupt pin.

Testing

Download demo ocdes

wget http://www.waveshare.com/w/upload/b/ba/Serial_Expansion_HAT_code.tar.gz
tar zxvf Serial_Expansion_HAT_code.tar.gz
sudo chmod 777 -R Serial_Expansion_HAT_code
cd Serial_Expansion_HAT_code

Test Serial port

  • c codes, baud rate is default 115200
#Send data via Serial A
cd c/uart/send
make clean 
make
sudo ./uart_send
#Receive data via Serial B
cd c/uart/receive/
make clean 
make
sudo ./uart_receive
  • Test

You can connect the TXDA to RXDB, RXDA to TXDB, open a terminal and one is set for transmit codes and another is set for receive codes.
Expected result:After running the code. Receiver: keep receiving and print data received; Sender: send characters。
Serial expansion hat test.png
If you test the device with external serial device, the GND should be connected for properly using.

  • python, default baud rate 115200
cd python/Uart/
#Send data by Serial A
sudo python send.py
#Receive data by Serial B
sudo python receive.py

Expect result: After running the code. Receiver: keep receiving and print data received; Sender: send characters

Test GPIO

  • c语言
cd c/gpio/
make clean 
make
sudo ./main

运行程序后,程序会翻转GPIO0的电平,如果在GPIO0上接上LED灯,会发现LED灯闪烁

  • python
cd python/GPIO
sudo python gpio.py

Expected result: The level of the GPIO0 is toggled for five times, GPIO1 is set as input, you can connect GPIO1 to 3.3V or GND to check the data.

Multi-devices

If you want to stack multi devices for using more Serail port, please follow the guides below.
Install i2c-tools and check the i2c address:

sudo apt-get install i2c-tools

Check i2c address with the following command:

sudo i2cdetect -y 1

If there is only one Serial Expansion HAT, it should be as figure:
Serial expansion hat add0.png
The address is UU but not 0x48, it means that the driver has been enabled in OS and you can find ttySC* devices in /dev directory.其

If you want to stack another Serial Expansion HAT, you need to modify the address setting and INT pin of the second boars firs. For example:INT PIN -> P23,I2C Address: A0->GND:
Serial expansion hat add.png
After modifying, you can stack the boards and run the following commands
Serial expansion hat add1.png
The new boards is recognized as 0x49. You also need to add setting line to config.txt file:

sudo nano /boot/config.txt

Add the following line and save:

dtoverlay=sc16is752-i2c,int_pin=23,addr=0x49

Serial expansion hat add2.png
Reboot

sudo reboot

Check the devices with commands:
Serial expansion hat add3.png

Because only five INT settings are available onboard, you can only stack five boards with the method above. If you want to connect more devices, you need to change and connect them by cable.