Skip to content

Instantly share code, notes, and snippets.

@alexanderhiam
Last active April 23, 2020 20:48
Show Gist options
  • Save alexanderhiam/65817fcf590ae77c7ff0 to your computer and use it in GitHub Desktop.
Save alexanderhiam/65817fcf590ae77c7ff0 to your computer and use it in GitHub Desktop.
Demonstrating the BeagleBone Black's Kernel driver working in RS485 mode with Michael Musset's gpio RTS patch
import serial, fcntl, struct, time
ser = serial.Serial(
port='/dev/ttyO4',
baudrate=9600,
timeout=1,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS
)
# Standard Linux RS485 ioctl:
TIOCSRS485 = 0x542F
# define serial_rs485 struct per Michael Musset's patch that adds gpio RE/DE
# control:
# (https://github.com/RobertCNelson/bb-kernel/blob/am33x-v3.8/patches/fixes/0007-omap-RS485-support-by-Michael-Musset.patch#L30)
SER_RS485_ENABLED = (1 << 0)
SER_RS485_RTS_ON_SEND = (1 << 1)
SER_RS485_RTS_AFTER_SEND = (1 << 2)
SER_RS485_RTS_BEFORE_SEND = (1 << 3)
SER_RS485_USE_GPIO = (1 << 5)
# Enable RS485 mode using a GPIO pin to control RE/DE:
RS485_FLAGS = SER_RS485_ENABLED | SER_RS485_USE_GPIO
# With this configuration the GPIO pin will be high when transmitting and low
# when not
# If SER_RS485_RTS_ON_SEND and SER_RS485_RTS_AFTER_SEND flags are included the
# RE/DE signal will be inverted, i.e. low while transmitting
# The GPIO pin to use, using the Kernel numbering:
RS485_RTS_GPIO_PIN = 48 # GPIO1_16 -> GPIO(1)_(16) = (1)*32+(16) = 48
# Pack the config into 8 consecutive unsigned 32-bit values:
# (per struct serial_rs485 in patched serial.h)
serial_rs485 = struct.pack('IIIIIIII',
RS485_FLAGS, # config flags
0, # delay in us before send
0, # delay in us after send
RS485_RTS_GPIO_PIN, # the pin number used for DE/RE
0, 0, 0, 0 # padding - space for more values
)
# Apply the ioctl to the open ttyO4 file descriptor:
fd=ser.fileno()
fcntl.ioctl(fd, TIOCSRS485, serial_rs485)
# Send some bytes:
# GPIO1_16 should be low here
time.sleep(0.2)
# GPIO1_16 should be high while this is being transmitted:
ser.write("RS485 test")
# GPIO1_16 should be low again after transmitting
time.sleep(0.2)
ser.close()
@sensors2cloud
Copy link

Is this patch merged with the latest debian image?
I tried on my bb and it cannot write.

@hanusek
Copy link

hanusek commented Apr 23, 2020

Is this patch merged with the latest debian image?
I tried on my bb and it cannot write.

I have the same problem.
Have you solved this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment