How to setup and use RS-232 interface on SP7021 BPi-F2P board of Banana Pi

The goal of the document is to illustrate how to setup and use RS-232 interface of BPi-F2P board of Banana Pi. BPi-F2P board of Banana Pi is based on Sunplus SP7021 SoC and is designed for industrial control. Refer to picture below, SP7021 is covered under black heat-sink.

BPi-F2P board has one channel of RS-232 interface which is connected to a DB-9 connector (on the most left side).

1. Schematics

Refer to schematic of RS-232 of BPi-F2P board below, signal SOC-UART-RX-3 is connected to GPIO_P5_2 (G_MX[42]) of SP7021. Signal SOC-UART-TX-3 is connected to GPIO_P5_5 (G_MX[45]) of SP7021. Signal SOC-UART-CTS-3 is connected to GPIO_P6_5 (G_MX[53]) of SP7021. Signal SOC-UART-RTS-3 is connected to GPIO_P4_0 (G_MX[32]) of SP7021. U13 and U15 are dual-channel digital isolator from Novosense Microelectronics. One channel for input and the other for output. They are added for electric isolation between internal digital circuits and output drivers. U14 is a multi-channel line-drivers and receivers of RS-232 from Texas Instruments. It converts 0V/5V-level signals to RS-232 line-signal (+10V/-10V). RS-232 line signals are then connected to a DB-9 connector. U17 is an isolated DC-DC converter from MORNSUN Guangzhou Science & Technology. It is used to supply +5V power (VDD5V-ISO) of output drivers.

2. Modify device-tree source

From schematics, we list connections of all RS-232 signals in the following table:

Signals of RS-232

Mux-Pins of SP7021

Signals of RS-232

Mux-Pins of SP7021

SOC-UART-TX-3

45 (GPIO_P5_5/G_MX[45])

SOC-UART-RX-3

42 (GPIO_P5_2/G_MX[42])

SOC-UART-RTS-3

32 (GPIO_P4_0/G_MX[32])

SOC-UART-CTS-3

53 (GPIO_P6_5/G_MX[53])

Modify device-tree node uart3 in device-tree source file linux/kernel/arch/arm/boot/dts/sp7021-bpi-f2p.dts to setup channel 3 of UART for RS-232 interface as shown below:

&uart3 { pinctrl-names = "default"; pinctrl-0 = <&pins_uart3>; status = "okay"; };

Note that uart3 is a label to node serial@9c000880.

Also, modify dts node pinmux_uart3-pins to setup pins for channel 3 of UART as shown below:

pins_uart3: pinmux_uart3-pins { sunplus,pins = < SPPCTL_IOPAD(45, SPPCTL_PCTL_G_PMUX, MUXF_UA3_TX, 0) SPPCTL_IOPAD(42, SPPCTL_PCTL_G_PMUX, MUXF_UA3_RX, 0) SPPCTL_IOPAD(32, SPPCTL_PCTL_G_PMUX, MUXF_UA3_RTS, 0) SPPCTL_IOPAD(53, SPPCTL_PCTL_G_PMUX, MUXF_UA3_CTS, 0) >; };

Note that node pinmux_uart3-pins is sub-node of node pctl.

Please note that to comply with Linux rules, after version 5.10.59, 4 property-names of pin node of SP7021 are changed as shown in table below:

5.4.35

5.10.59

5.4.35

5.10.59

sppctl,function

function

sppctl,groups

groups

sppctl,pins

sunplus,pins

sppctl,zero_func

sunplus,zerofunc

3. Enable Linux UART drivers

By default, UART module is enabled. Refer to screenshot of “Linux kernel Configuration Menu”:

“Sunplus UART serial port support” is enabled.

4. Build Linux image

Go to top folder. Run make config to select to build image for “BPi-F2P Board”. After make config completes, run make all to build Linux image.

5. Boot Linux

Boot Linux with the built image.

6. Serial port setup, write and read

Use stty command to setup serial port. For example,

~ # stty -F /dev/ttyS3 115200 cs8 cstopb -parenb

It sets channel 3 of serial port (UART) to 115,200 Baud (bps), 8-bit data, 2-bit stop-bit, no parity.

Use echo command to transmit a character string to serial port. For example,

Character ‘A', CR and LF will be transmitted. ASCII code of character 'A’ is 0x41. ASCII code of CR is 0x0D. ASCII code of LF is 0x0A. Refer to snapshot of oscilloscope which was captured when the command is run:

where C1 (yellow) is signal SOC-UART-TX-3, C2 (red) is signal SOC-UART-RTS-3, C3 (blue) is signal UART-232-TX-3, and C4 (green) is signal UART-232-RTS-3. Z1, Z2, Z3 and Z4 are zoom-in of C1, C2, C3 and C4, respectively.

Note that less significant bit (LSB) is sent first.

You can use command-line option -n to suppress CR and LF.

Character ‘A', ‘B' and 'C’ will be transmitted. There is no CR and LF. Refer to snapshot of oscilloscope which was captured when the command is run:

where C1 (yellow) is signal SOC-UART-TX-3, C2 (red) is signal SOC-UART-RTS-3, C3 (blue) is signal UART-232-TX-3, and C4 (green) is signal UART-232-RTS-3. Z1, Z2, Z3 and Z4 are zoom-in of C1, C2, C3 and C4, respectively.

ASCII code 0x41, 0x42 and 0x43 are transmitted successively. LSB is sent first.

Use cat command to receive characters from serial port. For example,

A character string ‘HELLO!’ is received. It is sent by (another BPi-F2P board):

Refer to snapshot of oscilloscope which was captured when the command is run:

where C1 (yellow) is signal UART-232-RX-3, C2 (red) is signal UART-232-CTS-3, C3 (blue) is signal SOC-UART-RX-3, and C4 (green) is signal SOC-UART-CTS-3. Z1, Z2, Z3 and Z4 are zoom-in of C1, C2, C3 and C4, respectively.

ASCII code 0x48, 0x45, 0x4C, 0x4C, 0x4F, 0x21, 0x0D and 0x0A are received successively.

Refer to snapshot of oscilloscope which was captured while 'A', CR and LF are received:

where C1 (yellow) is signal UART-232-RX-3, C2 (red) is signal UART-232-CTS-3, C3 (blue) is signal SOC-UART-RX-3, and C4 (green) is signal SOC-UART-CTS-3. Z1, Z2, Z3 and Z4 are zoom-in of C1, C2, C3 and C4, respectively.

ASCII code 0x41, 0x0D and 0x0A are received successively.

7. An example application of serial port programming in C

A. Read a string from serial port

B. Write a string to serial port

Use write function to send data to serial port. For example,