Skip to content

[RPI] Refactor SPI and GPIO handling for XVF3510 initialization #213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

BohdanBuinich
Copy link
Contributor

This commit significantly overhauls the script for setting the XVF3510 board to boot from SPI slave mode and for loading a binary file. The refactor not only aims at improving code readability, maintainability, and robustness but also addresses compatibility issues with the latest Raspberry Pi models, specifically the Raspberry Pi 5, which does not support the RPi.GPIO library.

Changes made:

  • Switched from smbus to smbus2 for I2C communication, offering a more modern and robust interface.
  • Replaced RPi.GPIO and spidev with digitalio and busio from the adafruit_blinka library, enhancing cross-platform compatibility and providing a more Pythonic API for GPIO and SPI operations.
  • Introduced type annotations for function signatures, improving code readability and type safety.
  • Added exception handling around I2C operations and file reading, increasing the script's robustness by gracefully handling potential errors.
  • Defined global variables for GPIO pin configurations, making the code cleaner and easier to modify for different setups.
  • Encapsulated GPIO setup and reset logic into dedicated functions (setup_direct_gpio), streamlining the main logic flow and separating concerns.
  • Modularized SPI setup (setup_spi) and data transmission logic (send_data_over_spi and handle_block_transfer), enhancing code organization and maintainability.

Benefits:

  • The use of smbus2 and adafruit_blinka libraries modernizes the script and may improve compatibility with a wider range of devices and future Python versions.
  • Type annotations and structured exception handling make the script more understandable and safer to execute, reducing the risk of runtime errors.
  • The refactoring into more granular functions and the introduction of global variables for configuration parameters make the script easier to read, modify, and extend.
  • Improved error handling ensures that the script fails gracefully, providing clear error messages and avoiding potential resource leaks.

@BohdanBuinich BohdanBuinich marked this pull request as draft February 4, 2024 01:17
@j1nx
Copy link
Member

j1nx commented Feb 4, 2024

Very nice! I once started to implement the adafruit stuff for the Mark2 led, however never finished it. Good to see, you are picking those packages up.

About your changes to the flash script. That scrip originally came from the XMOS guys which I once downloaded when they where openly accessible;

https://www.xmos.com/xvf3510/

Perhaps, they are interested in it? Although they where also not very keen on my tip for the drivers to use the dtoverlay way;
xmos/vocalfusion-rpi-setup#33

Anyhow, for your convience I uploaded the full XMOS information pack with schematics, binaries, firmwares, tools and scripts here for you to have a look;
https://drive.google.com/file/d/15TynIKsM98CLk3JcZmuHyMzs1CZTGQOU/view?usp=sharing

It might help you understand certain things.

@BohdanBuinich
Copy link
Contributor Author

@j1nx Does the J201-Rev10 board work for you on the latest build?
For me (tested without my changes), it doesn't detect the board in the I2C list, and I don't see any significant changes related to I2C and the J201 board.
image

On the image from Dec.12, everything works fine.

image

@BohdanBuinich BohdanBuinich force-pushed the xvf3510-rpi5-compatibility branch from 44c4f55 to f6a2b4a Compare February 6, 2024 21:59
@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

@j1nx Does the J201-Rev10 board work for you on the latest build? For me (tested without my changes), it doesn't detect the board in the I2C list, and I don't see any significant changes related to I2C and the J201 board. image

On the image from Dec.12, everything works fine.

image

I am still in the middle of testing everything with the new 6.6.14 kernel.

I ran into some podman/quadlet issues that prevented me to run the test I wanted to run, however yesterday on a rpi4-2gb with the latest build, the

journalctl -u i2csound

Did not gave me any errors. It did found all the right i2c addresses and all configuration commands ran succesfully.

aplay -l
arecord -l

Above dis show the xvf-3510 card with all the right settings, however having a quick look at ncpamixer, no sound was captured on the microphone and no sound output as well.

That is obviously as the xvf3510 flash script should fail because the lack of RPi.GPIO support. Guess is silently fails. Will do some more testing soon when the podman/quadlet is fixed and the device keeps running without OOMD killing it.

EDIT: BTW, without loading the xvf3510 firmware, you will only see the 2f address which is the TAS5806. That is why we only use that specific address to "call it" a Mark2. Perhaps we should extend the script to check for the right XMOS addresses after loading the xvf3510 firmware and if not there, remove the dtoverlay again and report back that we do not have a mark2 but something else at that address.

Anyhow, will scan the addresses on my rpi4 this evening to check if I have the same problem as you.

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

OK, the new image was able to fully initialise again at first boot.

Below some testing;

ovos@OpenVoiceOS-840dca:~ $ uname -a
Linux OpenVoiceOS-840dca 6.6.14-rt21-ovos-buildroot #1 SMP PREEMPT_RT Mon Feb  5 20:32:26 UTC 2024 aarch64 GNU/Linux
ovos@OpenVoiceOS-840dca:~ $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 2f
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
ovos@OpenVoiceOS-840dca:~ $ journalctl -u i2csound
Dec 20 18:15:50 OpenVoiceOS systemd[1]: Starting I2C Sound configuration service...
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: SJ201LED detection result: false
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: TAS5806 detection result: true
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: ADAFRUIT detection result: false
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: AIYVOICEBONNET detection result: false
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: WM8XXX detection result: false
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: RESPEAKER6 detection result: false
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: RESPEAKER4 detection result: false
Dec 20 18:15:50 OpenVoiceOS ovos-i2csound[418]: Installing and configuring SJ-201 HAT
Dec 20 18:15:51 OpenVoiceOS-840dca ovos-i2csound[480]: Read file "/usr/lib/firmware/xvf3510/app_xvf3510_int_spi_boot_v4_1_0.bin" size: 196608 Bytes
Dec 20 18:15:51 OpenVoiceOS-840dca ovos-i2csound[480]: Sending complete
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: Deep sleep   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: Deep sleep   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: Deep sleep   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: Deep sleep   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Clock fault (reg: 0x71)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: HIZ   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Clock fault (reg: 0x71)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: HIZ   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: FS_MON: FS Error   (reg: 0x37)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Clock fault (reg: 0x71)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[542]: Run Status: HIZ   (reg: 0x68)
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[418]: Assume revision-10 SJ-201 board
Dec 20 18:15:54 OpenVoiceOS-840dca ovos-i2csound[418]: Configuring buttons
Dec 20 18:15:54 OpenVoiceOS-840dca systemd[1]: i2csound.service: Deactivated successfully.
Dec 20 18:15:54 OpenVoiceOS-840dca systemd[1]: Finished I2C Sound configuration service.

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

ovos@OpenVoiceOS-840dca:~ $ aplay -Ll
null
    Discard all samples (playback) or generate zero samples (capture)
oss
    Open Sound System
pipewire
    PipeWire Sound Server
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
default
    Default ALSA Output (currently PipeWire Media Server)
sysdefault:CARD=sndxmosvocalfus
    snd_xmos_vocalfusion_card, simple-card_codec_link snd-soc-dummy-dai-0
    Default Audio Device
sysdefault:CARD=Headphones
    bcm2835 Headphones, bcm2835 Headphones
    Default Audio Device
**** List of PLAYBACK Hardware Devices ****
card 0: sndxmosvocalfus [snd_xmos_vocalfusion_card], device 0: simple-card_codec_link snd-soc-dummy-dai-0 [simple-card_codec_link snd-soc-dummy-dai-0]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
card 3: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
  Subdevices: 8/8
  Subdevice #0: subdevice #0
  Subdevice #1: subdevice #1
  Subdevice #2: subdevice #2
  Subdevice #3: subdevice #3
  Subdevice #4: subdevice #4
  Subdevice #5: subdevice #5
  Subdevice #6: subdevice #6
  Subdevice #7: subdevice #7

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

ovos@OpenVoiceOS-840dca:~ $ arecord -lL
null
    Discard all samples (playback) or generate zero samples (capture)
oss
    Open Sound System
pipewire
    PipeWire Sound Server
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
default
    Default ALSA Output (currently PipeWire Media Server)
sysdefault:CARD=sndxmosvocalfus
    snd_xmos_vocalfusion_card, simple-card_codec_link snd-soc-dummy-dai-0
    Default Audio Device
**** List of CAPTURE Hardware Devices ****
card 0: sndxmosvocalfus [snd_xmos_vocalfusion_card], device 0: simple-card_codec_link snd-soc-dummy-dai-0 [simple-card_codec_link snd-soc-dummy-dai-0]
  Subdevices: 0/1
  Subdevice #0: subdevice #0

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

ovos@OpenVoiceOS-840dca:~ $ pactl list sinks short
79      alsa_output.platform-asoc-simple-card.0.xvf3510-stereo-output   PipeWire        s32le 2ch 48000Hz       IDLE
81      alsa_output.platform-bcm2835_audio.stereo-fallback      PipeWire        s16le 2ch 48000Hz       SUSPENDED
105     raop_sink.Sonos-347E5CFA6DF4.local.10.0.0.117.7000      PipeWire        s16le 2ch 44100Hz       SUSPENDED
ovos@OpenVoiceOS-840dca:~ $ pactl list sources short
36      openvoiceos_denoised_mic        PipeWire        float32le 2ch 48000Hz   SUSPENDED
79      alsa_output.platform-asoc-simple-card.0.xvf3510-stereo-output.monitor   PipeWire        s32le 2ch 48000Hz       IDLE
80      alsa_input.platform-asoc-simple-card.0.xvf3510-asr      PipeWire        s32le 2ch 48000Hz       RUNNING
81      alsa_output.platform-bcm2835_audio.stereo-fallback.monitor      PipeWire        s16le 2ch 48000Hz       SUSPENDED
105     raop_sink.Sonos-347E5CFA6DF4.local.10.0.0.117.7000.monitor      PipeWire        s16le 2ch 44100Hz       SUSPENDED

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

However no sound input nor sound output signal (silence)

Looks like the upload firmware indeed fails silently. Without the firmware being uploaded to the sj-201 sound will not work as the Mycroft guys did not add a SPI storage chip to hold it across boots/reboots.

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

ovos@OpenVoiceOS-840dca:/usr/libexec $ sudo xvf3510-flash --direct "/usr/lib/firmware/xvf3510/app_xvf3510_int_spi_boot_v4_1_0.bin"
Read file "/usr/lib/firmware/xvf3510/app_xvf3510_int_spi_boot_v4_1_0.bin" size: 196608 Bytes
Sending complete

I guess/assume the above actually does not do what is should be doing.

Will rebuild an image with this PR to test the new flash script.

@BohdanBuinich
Copy link
Contributor Author

However no sound input nor sound output signal (silence)

Looks like the upload firmware indeed fails silently. Without the firmware being uploaded to the sj-201 sound will not work as the Mycroft guys did not add a SPI storage chip to hold it across boots/reboots.

OK, this means that the device will only appear in the i2c list after FW flash, right?

Ok, I need to figure out what I missed in my script.
if I don't find out today, I'll have to connect a signal analyzer tomorrow and compare it on the image on which everything works

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

OK, this means that the device will only appear in the i2c list after FW flash, right?

Indeed, the firmware is what should be behing those adresses.

Just gave a command over ovos-cli-client and it appears I was wrong. Sound output does work without any issues, so it is only the microphone / xvf3510 chip/ input.

So guess, if we can get the firmware uploading fixed it should work.

Ok, I need to figure out what I missed in my script. if I don't find out today, I'll have to connect a signal analyzer tomorrow and compare it on the image on which everything works

Am I correct that the firmware upload with this PR also fails for you? If so, I will leave it to you for a bit.

Otherwise we have to dig into those docs I uploaded once again.

@BohdanBuinich
Copy link
Contributor Author

Am I correct that the firmware upload with this PR also fails for you? If so, I will leave it to you for a bit.

the uploading is working, but the device does not appear in the i2c list, the problem is that we do not have any information about the update status.

I'll try to experiment some more.

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

I will do the same.

there are some low level tools for the xmos chip in that zip file however never used them on the sj-201 or at all for that matter. I might as well give it a try.

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

As pointer with something I have in the back of my mind; i2c transfer speed is involved.

https://github.com/xmos/vocalfusion-rpi-setup/blob/master/setup.sh#L119

raspberrypi/linux#5653
raspberrypi/linux#5802
raspberrypi/linux#5916

@j1nx
Copy link
Member

j1nx commented Feb 7, 2024

I also see there are some changes in the dtoverlays for 6.6

https://github.com/OpenVoiceOS/VocalFusionDriver/blob/main/xvf3510.dts#L13

Should not be changed according to;
raspberrypi/linux@9e601fb#diff-14f742df77e9ba7c9f8bc4e492e8305df362d63fe872e160ed7352dc4fbdf0fe

@j1nx
Copy link
Member

j1nx commented Feb 15, 2024

Confirmed working on the RPI4

OpenVoiceOS/VocalFusionDriver#3 (comment)

@j1nx
Copy link
Member

j1nx commented Feb 17, 2024

Ah crap! @BohdanBuinich I screwed up by merging my gpio branch for the libgpiod changes as they where confimed working for the rpi4 however completely forgot that i merged your branch with this PR into it.

So now I kind of merged this PR without really merging this PR ????? while it is still in draft.

@BohdanBuinich BohdanBuinich force-pushed the xvf3510-rpi5-compatibility branch from 8f31a41 to 59d3088 Compare February 17, 2024 20:27
@BohdanBuinich
Copy link
Contributor Author

Ah crap! @BohdanBuinich I screwed up by merging my gpio branch for the libgpiod changes as they where confimed working for the rpi4 however completely forgot that i merged your branch with this PR into it.

So now I kind of merged this PR without really merging this PR ????? while it is still in draft.

no problem, you will be a tester now)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants