Repository navigation
Description
Describe the bug
Extra data is being transmitted with SC16IS752 connected to I2C bus on Raspberry Pi running Jessie, Stretch, Buster or Bullseye. However, I have a version of Raspian that can best be described as a Frankenstein Monster version that I believe is the result of a failed distribution upgrade. In this Frankenstein version the problem described above and below does not exits.
Per the recommendation of Phil Elwell, I have captured runs of trying to transmit 256 bytes of data [0x00-0xFF] using a Saleae Logic Analyzer. I have done this both the working Frankenstein version of Raspian and an "out of the box" Buster (this run may actually come for an upgraded Jessie, but an out of the box version does the same thing).
Both files represents the same 256 bytes of data being transmitted yet the Buster-Bad file logged over 20K of I2C communications. The Frankenstein-Good version of the OS had in the neighborhood of 512 bytes of I2C data (which is what would be expected due to the heavy protocol overhead of I2C). Both of these files come from the same hardware setup and differ primarily by the OS that is running on them.
Buster-Bad I2C.csv
Frankenstein-Good no inter byte delay I2C.csv
While I have been trying to unravel this mystery I've been posting about it here: https://forums.raspberrypi.com/viewtopic.php?t=328190 but the summary in this issue is probably more concise that the thread. At least once while posting I fooled myself and posted what I now believe to be incorrect information.
I am invested heavily in finding and getting a solution rolled into Raspian so that we do not have to tip toe around the issue in the future nor be stuck using this Frankenstein image.
Steps to reproduce the behaviour
- Fresh install of jessie, stretch, buster, bullseye on a Raspberry Pi 3B with SC16IS752 installed connected to I2C1 (BCM2, BCM3, pins 3 &5) with the interrupt connected to (BCM24, pin 18). I'm currently using a custom board with two of these chips, but the waveshare board should be equivalent to my setup (I have seen the problem with both a single or double chip configuration).
- add ssh file to /boot to allow ssh'ing into the device
- Update /boot/config.txt to the following:
# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details
# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1
# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1
# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16
# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720
# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1
# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1
# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2
# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4
# uncomment for composite PAL
#sdtv_mode=2
#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800
# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
#dtparam=i2s=on
#dtparam=spi=on
# Uncomment this to enable infrared communication.
#dtoverlay=gpio-ir,gpio_pin=17
#dtoverlay=gpio-ir-tx,gpio_pin=18
# Additional overlays and parameters are documented /boot/overlays/README
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
[pi4]
# Enable DRM VC4 V3D driver on top of the dispmanx display stack
dtoverlay=vc4-fkms-v3d
max_framebuffers=2
[all]
#dtoverlay=vc4-fkms-v3d
dtoverlay=disable-bt
enable_uart=1
dtoverlay=sc16is752-i2c,int_pin=24,addr=0x48
dtoverlay=sc16is752-i2c,int_pin=23,addr=0x49
dtparam=i2c_arm_baudrate=400000
#dtoverlay=i2c-rtc,pcf8523
#dtoverlay=i2c-rtc,ds3231
- boot the pi
- ssh into p
- Clone this simple project to transmit 256 bytes of data [0x00-0xFF]
git clone https://github.com/ApiumJacob/rxtx - edit tx.c to set the port_name to /dev/ttySC0 if only one SC16IS752 is connected or /dev/ttySC2 if two SC16IS752's are connected.
char port_name[] = "/dev/ttySC0"; - Build rx program
cd rxtx
make
- run tx
./tx
Monitor the I2C bus or the incoming data with the rx program to see the excess data being transmitted.
Device (s)
Raspberry Pi 3 Mod. B
System
From the bad pi:
pi@SwarmBridge-09999:~ $ cat /etc/rpi-issue
Raspberry Pi reference 2020-02-13
Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 5f884374b6ac6e155330c58caa1fb7249b8badf1, stage2
pi@SwarmBridge-09999:~ $ vcgencmd version
Dec 1 2021 15:07:06
Copyright (c) 2012 Broadcom
version 71bd3109023a0c8575585ba87cbb374d2eeb038f (clean) (release) (start)
pi@SwarmBridge-09999:~ $ uname -a
Linux SwarmBridge-09999 5.10.63-v7+ #1496 SMP Wed Dec 1 15:58:11 GMT 2021 armv7l GNU/Linux
From the good pi:
pi@SwarmBridge-09999:~ $ cat /etc/rpi-issue
Raspberry Pi reference 2020-02-13
Generated using pi-gen, https://github.com/RPi-Distro/pi-gen, 5f884374b6ac6e155330c58caa1fb7249b8badf1, stage2
pi@SwarmBridge-09999:~ $ vcgencmd version
Feb 12 2020 12:38:08
Copyright (c) 2012 Broadcom
version 53a54c770c493957d99bf49762dfabc4eee00e45 (clean) (release) (start)
pi@SwarmBridge-09999:~ $ uname -a
Linux SwarmBridge-09999 4.19.97-v7+ #1294 SMP Thu Jan 30 13:15:58 GMT 2020 armv7l GNU/Linux
Logs
From the bad pi:
dmesg-bad.txt
From the good pi:
dmesg-good.txt
Additional context
No response
Activity
pelwell commentedon Feb 11, 2022
Sending data as individual bytes has a huge protocol overhead - ignoring starts and stops etc. I see 23 bytes being transferred for every single byte being sent to the UART, i.e. 22 overhead + 1 payload. Larger transfers are sent in groups of 30 bytes, with each group preceded by between 10 and about 30 bytes (it depends on the timing, since the driver is polling a FIFO status flag in the UART).
There is no evidence here of anything untoward, just the efficiency of sending data in bulk.
ApiumJacob commentedon Feb 11, 2022
My apologies, when I ran the data yesterday I was so confident (and an bit lazy) that there was an error that I didn't look at the output, if there is a long enough delay between inter byte data there are no errors. The last time I tested 2ms produced lots of errors so I just assumed they were there. Mea culpa.
Here is a run with with verify bad data and its easier to digest (as the data is sent in larger groups) so there is not 20k work of data. But in the past I have received 30k worth of data when only sending 256 bytes. In these runs the inter byte delay has been removed (because I know it very reliably produces errors):
output from tx program:
output from rx program:
Bytes 0x07 - 0x27 are received incorrectly (errors wrapped in {handle bars}).
Trace:


Start of bad data at byte 0x07:
The I2C data from the protocol analyzer which shows the incorrect data in the same places:
Buster-Bad I2C - 2022-02-11.csv
Here are some additional runs. The received data always seems to be offset from the expected value but the offset is not always the same.
Now for the fun stuff. Here I have set the inter byte delay to 100us and then transmit 256 bytes:
The reveive program (running on an Ubuntu laptop) report that 4352 byts have been received:
Here is the point where the first bad byte is detected with two additional good bytes:

Additionally, here is the entire sal file and exported csv of the I2C analyzer output zipped up:
Buster-Bad-I2C-2022-02-11-4352 bytes.zip
pelwell commentedon Feb 11, 2022
Those traces are illuminating because they clearly show incorrect data being sent. I hope to have some working test equipment soon.
pelwell commentedon Feb 14, 2022
The problem is easily reproducible with the Waveshare Serial "HAT" (it isn't a true HAT without an EEPROM) that has just arrived.
ApiumJacob commentedon Feb 14, 2022
Hopefully, its as easy to fix, let me know if there is anything I can do to support the effort.
What if your "HAT" has an EEPROM but it's blank? Does it get promoted from "HAT" to HAT or do you actually have to program it first? ;-)
pelwell commentedon Feb 15, 2022
The sc16is7xx driver does virtually no interlocking, as some added tracing shows:
The
to_sendlines show how many bytes (in hex) to send, and the first one in the block. Thesentline shows the content of the send buffer after the send has occurred, but here also serves to show the timing - in particular the overlapping of pairs ofto_sendandsentlines, meaning that two blocks of bytes are using the same buffer at the same time.Which means we're on stage 6 of the debugging process:

pelwell commentedon Feb 15, 2022
Adding the usual spinlocks breaks horribly because the driver uses the regmap abstraction to support both I2C and SPI modes. regmap does its own interlocking against multiple accesses using mutexes, which expect to be able to put the calling thread to sleep - something that isn't allowed while a spinlock is held because it disables interrupts - "scheduling while atomic" is the kernel error message.
It is possible to disable regmap's internal interlocking by adding the following line before the calls to
devm_regmap_init_*:Surprisingly this prevents data corruption, but causes a different set of warning messages:
As such, I don't think it is a suitable workaround, let alone a fix.
sc16is7xx: Fix for incorrect data being transmitted
pelwell commentedon Feb 15, 2022
The problem reentrancy is between the IRQ thread and work queue used for transmission. Claiming an existing mutex (
.efr_lock) insidesc16is7xx_tx_proceliminates any corruption in my test.Note that this doesn't solve the problem of dropped data when sent to the sc16is752, but that looks like overrun at the receive FIFO because it isn't drained fast enough - something that the use of RTS/CTS flow control would solve.
pelwell commentedon Feb 15, 2022
See de9c455 (before rebasing) for a patch containing the fix.
pelwell commentedon Feb 15, 2022
The dropped data mentioned above is cured with the
dtparam=i2c_arm_baudrate=400000, which I had omitted, although it's not impossible that a scheduling catastrophe could still result in lost data without flow control.sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
59 remaining items
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted
sc16is7xx: Fix for incorrect data being transmitted