multigcs / LinuxCNC-RIO

RealtimeIO for LinuxCNC based on an FPGA
https://forum.linuxcnc.org/18-computer/49142-linuxcnc-rio-realtimeio-for-linuxcnc-based-on-fpga-ice40-ecp5
GNU General Public License v2.0
72 stars 17 forks source link

linuxcnc crash makes SPI no workie #33

Closed jschoch closed 11 months ago

jschoch commented 11 months ago

Through some trials and tribulations I will skip here I discovered that if linuxcnc crashes then the SPI setup of my pi 4 would get into some strange state and I could no longer connect to the tang 9k.

The naive fix was to reboot, the less hair pulling fix is to remove and restart the spi module

sudo modprobe spi-bcm2835 -r;sudo modprobe spi-bcm2835

This seems to have something to do with the set_pud command here

https://github.com/multigcs/LinuxCNC-RIO/blob/2507c97df8856cc6f45211df77ebed181ab243cc/generators/linuxcnc_component/rio.c#L282

Also, noticed that this is hard coded to CE0 in the initialization but elsewhere it is using a CE1 pin

https://github.com/multigcs/LinuxCNC-RIO/blob/2507c97df8856cc6f45211df77ebed181ab243cc/generators/linuxcnc_component/rio.c#L1175

this manifests as per below

image

In the unstuck state GPIO 7 doens't have the SPI alt state and a crashing linuxcnc doesn't lock SPI up. The other way to see the stuck state is with qt_spitest.py where all the rx values are the same and modprobe remove/restart the spi module allows the qt_spitest.py to work again.

I can do a PR to make the CS pin a define or variable if you are open to it, may also be worthwhile to make the other pins easier to update with some notes on how to set the overlay to enable spi1..4

multigcs commented 11 months ago

the whole rio.c part needs to be rewritten, but unfortunately we have too many other things to do at the moment.

CS1 is used for SPI communication and CS0 was intended for flashing. However, there is probably a bug in the SPI, so I can't use CS1 in the normal way and have to set it manually.

jschoch commented 11 months ago

I can make a PR to fix make CS a define for variable and to fix the issue with the crash, just let me know if you are ok and I'll submit it for review.

multigcs commented 11 months ago

PR's are always welcome

jschoch commented 11 months ago

is this the template for the rio header? tests/data/tangnano9k_1/rio.h

multigcs commented 11 months ago

no, the header is build by this python script:

generators/linuxcnc_component/linuxcnc_component.py

jschoch commented 11 months ago

do you want the PR against master, or should I make a branch?

multigcs commented 11 months ago

main (is the master) is ok

jschoch commented 11 months ago

I wonder if you've considered using a template library like mako or something for the header generation. I'm not really much of a python programmer but it seems like it maybe would be worthwhile for readability.

jschoch commented 11 months ago

also, you may not need to do anything to MISO/MOSI since the RPI overlay seems to be setting all that up (including CLK) It may be cleaner to just toggle the CS pin and ignore the rest...

multigcs commented 11 months ago

I wonder if you've considered using a template library like mako or something for the header generation. I'm not really much of a python programmer but it seems like it maybe would be worthwhile for readability.

the python code generates a little bit more then only the header file and there is not realy a benefit to use an extra template system for it

multigcs commented 11 months ago

also, you may not need to do anything to MISO/MOSI since the RPI overlay seems to be setting all that up (including CLK) It may be cleaner to just toggle the CS pin and ignore the rest...

I would be happy if someone could clean up the code :)

the c-code is copied from the remora project and i'm glad that it works. as i said, everything needs to be reworked.

multigcs commented 11 months ago

is merged and worked, man thanks !!!

but can not see you as a contributor :( https://github.com/multigcs/LinuxCNC-RIO/graphs/contributors

sorry for that, maybe because i merged it via the console and over the github button, or i have changed to much :(