Difference between revisions of "Template:Serial Expansion HAT User Mnaual"

From Waveshare Wiki
Jump to: navigation, search
(Created page with "==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, n...")
 
Line 126: Line 126:
 
If you test the device with external serial device, the GND should be connected for properly using.
 
If you test the device with external serial device, the GND should be connected for properly using.
  
*python,默认波特率115200
+
*python, default baud rate 115200
 
<pre>
 
<pre>
 
cd python/Uart/
 
cd python/Uart/
#默认通过串口A发送数据
+
#Send data by Serial A
 
sudo python send.py
 
sudo python send.py
#默认通过串口B接收数据
+
#Receive data by Serial B
 
sudo python receive.py
 
sudo python receive.py
 
</pre>
 
</pre>
测试现象:若是接收端,会进入接收状态,终端打印接收到的程序。发送端会立即发送设置好的字符串。<br />
+
Expect result: After running the code. Receiver: keep receiving and print data received; Sender: send characters<br />
  
===测试扩展GPIO===
+
===Test GPIO===
 
*c语言
 
*c语言
 
<pre>
 
<pre>
Line 151: Line 151:
 
sudo python gpio.py
 
sudo python gpio.py
 
</pre>
 
</pre>
运行程序后,程序翻转GPIO0的电平几次,如果GPIO0上接上LED灯,会发现LED灯闪烁五次之后常亮,另外GPIO1被设置为输入模式,你可以将GPIO1分别接到3.3V和GND查看一下状态<br />
+
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.<br />
  
==叠加教程==
+
==Multi-devices==
如果你不仅需要一个,你需要扩展多个串口需要按照如下操作(适用于新版本)。<br />
+
If you want to stack multi devices for using more Serail port, please follow the guides below.<br />
安装i2c-tools,方便查看I2C地址,在控制台输入:
+
Install i2c-tools and check the i2c address:
 
<pre>
 
<pre>
 
sudo apt-get install i2c-tools
 
sudo apt-get install i2c-tools
 
</pre>
 
</pre>
等待安装成功,执行如下命令查看I2C地址。
+
Check i2c address with the following command:
 
<pre>
 
<pre>
 
sudo i2cdetect -y 1
 
sudo i2cdetect -y 1
 
</pre>
 
</pre>
如果默认已经插入了一个Serial Expansion HAT,那么执行如下命令会得到如下的效果:<br />
+
If there is only one Serial Expansion HAT, it should  be as figure:<br />
 
[[file:serial_expansion_hat_add0.png]] <br>
 
[[file:serial_expansion_hat_add0.png]] <br>
其中0X48的设备显示成了UU,说明系统已经加载成功了。运行ls /devttySC*可以查看到已经生成了两个串口设备了。<br />
+
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.其<br />
  
在叠加第二块模块的之前,需要修改相应的硬件设置,将板子上的默认INT与I2C address的配置改成与默认的不一致即可,这里将需要叠加的另一块板改了:INT PIN改成P23,I2C Address的A0改到了GND:<br />
+
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:<br />
 
[[file:serial_expansion_hat_add.png]] <br>
 
[[file:serial_expansion_hat_add.png]] <br>
修改完成之后,将模块叠加在之前的模块上,运行命令:<br />
+
After modifying, you can stack the boards and run the following commands<br />
 
[[file:serial_expansion_hat_add1.png]] <br>
 
[[file:serial_expansion_hat_add1.png]] <br>
新的模块的I2C地址为0X49,前面已经修改了中断管脚为23,那么我们就可以打开/boot/config.txt中加入如下配置:<br />
+
The new boards is recognized as 0x49. You also need to add setting line to config.txt file:<br />
 
<pre>
 
<pre>
 
sudo nano /boot/config.txt
 
sudo nano /boot/config.txt
 
</pre>
 
</pre>
在之前配置下方加入如下,保存并退出(ctrl +x,然后执行Y,并回车):<br />
+
Add the following line and save:<br />
 
<pre>
 
<pre>
 
dtoverlay=sc16is752-i2c,int_pin=23,addr=0x49
 
dtoverlay=sc16is752-i2c,int_pin=23,addr=0x49
 
</pre>
 
</pre>
 
[[file:serial_expansion_hat_add2.png]] <br>
 
[[file:serial_expansion_hat_add2.png]] <br>
重启树莓派<br />
+
Reboot<br />
 
<pre>
 
<pre>
 
sudo reboot
 
sudo reboot
 
</pre>
 
</pre>
执行如下命令,会出现4个设备号,叠加即成功了。<br />
+
Check the devices with commands:<br />
 
[[file:serial_expansion_hat_add3.png]] <br>
 
[[file:serial_expansion_hat_add3.png]] <br>
  
由于板载只可设置5个不同的电阻,因此最大叠加数为5个,如需要添加更多的,需要使用杜邦线连接<br />
+
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.<br />

Revision as of 07:08, 19 September 2020

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.