Skip to content
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

Unable to get a valid SPI interface on Raspberry Pi 5 #1425

Closed
hardcore-thinking opened this issue Feb 16, 2025 · 3 comments
Closed

Unable to get a valid SPI interface on Raspberry Pi 5 #1425

hardcore-thinking opened this issue Feb 16, 2025 · 3 comments

Comments

@hardcore-thinking
Copy link

Describe the bug
I'm still stuck with the Raspberry Pi example. I try to use it on my Raspberry Pi 5, but I am not able to get a valid SPI handle.

To Reproduce
I'm using a modified Raspberry Pi example.

#include <iostream>
#include <cstdlib>
#include <sstream>

#include <RadioLib.h>
#include "hal/RPi/PiHal.h"
#include <lgpio.h>

PiHal* hal = new PiHal(1);

SX1262 radio = new Module(hal, 7, 17, 22);

int main(int argc, char** argv) {
	std::cout << "[SX1262] Initializing ... ";
  	
	int state = radio.begin(868.0);;
  	if (state != RADIOLIB_ERR_NONE) {
		std::cout << "failed, code " << state << std::endl;
    		return EXIT_FAILURE;
  	}
  	
	std::cout << "success!" << std::endl;

  	int count = 0;
  	while (true) {
    		printf("[SX1262] Transmitting packet ... ");
    		char str[64];
    		sprintf(str, "Hello World! #%d", count++);
    		
		state = radio.transmit(str);
    		if(state == RADIOLIB_ERR_NONE) {
      			std::cout << "success!" << std::endl;

      			hal->delay(1000);
    		}
		
		else {
      			std::cout << "failed, code " << state << std::endl;
    		}
  	}

	delete hal;

  	return EXIT_SUCCESS;
}

Expected behavior
Just make the SX1262 work fine on my Raspberry Pi 5 using the example for the Raspberry Pi.

Screenshots
No screenshots but I do have the errors:

I get these when the SPI interface is turned off using raspi-config (which I can only assume is normal):

Could not open SPI handle on 0: can not open SPI device
Could not perform SPI transfer: unknown handle
Could not perform SPI transfer: unknown handle
Could not perform SPI transfer: unknown handle
Could not perform SPI transfer: unknown handle
Could not perform SPI transfer: unknown handle
Could not perform SPI transfer: unknown handle

And I get these when it is on (the same goes for the other SPI CS pin 8):

Could not claim pin 7 for mode 1: GPIO busy
Error writing value to pin 7: GPIO busy
Error writing value to pin 7: GPIO busy
Error writing value to pin 7: GPIO busy
Error writing value to pin 7: GPIO busy
Error writing value to pin 7: GPIO busy
Error writing value to pin 7: GPIO busy

Which I don't understand because, the SX1262 HAT excluded, nothing is on the GPIO. When checking the pins with lgpio, I get this though :

Handle 0 / Line 7's informations:
        - Offset: 7
        - Flags: 7
                - Kernel: In use by the kernel
                - Kernel: Output
                - Kernel: Active low
        - Name: GPIO7
        - User: spi0 CS1

Handle 0 / Line 8's informations:
        - Offset: 8
        - Flags: 7
                - Kernel: In use by the kernel
                - Kernel: Output
                - Kernel: Active low
        - Name: GPIO8
        - User: spi0 CS0

And I don't know what to do with these.

Additional info (please complete):

  • Host machine: Raspberry Pi 5
  • Wireless module type: SX1262
  • Library version: 7.1.2
@hardcore-thinking
Copy link
Author

Ok I think I got it working for the Raspberry Pi 5 (finally). So, for whoever is passing by in quest for a solution, here's what I did using informations from an old issue :

  • Check that you SPI interface is enabled in raspi-config ;
  • Use the correct pins: CS = 21, IRQ = 16, RST = 18 and BUSY = 20 (thanks past jgromes). It worked for my case, probably because of the specific model I use. Maybe those in the example will work for you ;
  • I don't know if it really helped but, unlike the example, I used spiChannel = 0 in PiHal's constructor ;
  • Once again, I don't know if this helped, but I added this line to the end of dtoverlay=spi1-0cs to /boot/firmware/config.txt

I still have a problem when running the program. I can't seem to get access back to previously enumerated pins. As if the program is still running in the background. But at least I managed to send something.

@hardcore-thinking
Copy link
Author

hardcore-thinking commented Feb 16, 2025

I forgot to post the code that worked (although not really different):

#include <iostream>
#include <cstdlib>
#include <sstream>

#include <RadioLib.h>
#include "hal/RPi/PiHal.h"
#include <lgpio.h>

PiHal* hal = new PiHal(0);
SX1262 radio = new Module(hal, 21, 16, 18, 20);

int main(int argc, char** argv) {
	std::cout << "[SX1262] Initializing ... ";
  	
	// forgot to mention these too lines
	// it's so that I don't get a -707 error
	// (cf. Troubleshooting section in the wiki)
	radio.XTAL = true;
	radio.setTCXO(0.0); 

	int state = radio.begin(868.0);;
  	if (state != RADIOLIB_ERR_NONE) {
		std::cout << "failed, code " << state << std::endl;
    		return EXIT_FAILURE;
  	}
  	
	std::cout << "success!" << std::endl;

  	int count = 0;
  	while (true) {
    		printf("[SX1262] Transmitting packet ... ");
    		char str[64];
    		sprintf(str, "Hello World! #%d", count++);
    		
		state = radio.transmit(str);
    		if(state == RADIOLIB_ERR_NONE) {
      			std::cout << "success!" << std::endl;

      			hal->delay(1000);
    		}
		
		else {
      			std::cout << "failed, code " << state << std::endl;
    		}
  	}

	delete hal;

  	return EXIT_SUCCESS;
}

@hardcore-thinking
Copy link
Author

Ok I'm gonna close the issue and start a discussion about joining TTN network.

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

No branches or pull requests

1 participant