0

I am a novice when it comes to programming. I am attempting to set up SPI communication between a module and my RPI3.

The following is my main.cpp code (I plan to neaten up the code once it works)

#include <QApplication>
#include "mainwindow.h"
#include "bcm2835.h"

#define PIN18 RPI_GPIO_18
#define PIN16 RPI_GPIO_16
#define WORD_SIZE 4U

int main(int argc, char *argv[])
{

  QApplication a(argc, argv);
  MainWindow w;
  if (!bcm2835_init()) {
              qDebug() << ("Could not init bcm2835") << endl;
             exit(EXIT_FAILURE);
      }

  bcm2835_gpio_fsel(PIN18, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_write(PIN18, LOW);
  delay(100);
  bcm2835_gpio_write(PIN18, HIGH);
  delay(100);
  bcm2835_spi_begin();
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_512);
  bcm2835_spi_chipSelect(BCM2835_SPI_CS0);
  bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);
  bcm2835_gpio_fsel(PIN16, BCM2835_GPIO_FSEL_INPT);

  w.show();

  return a.exec();
}

I get an segmentation fault at

bcm2835_spi_setDataMode (main.cpp) --->

bcm2835_peri_set_bits (bcm2835.c) --->

bcm2835_peri_read (bcm2835.c) --->

ret = *paddr (bcm2835.c)

I have reason to believe the pointer is attempting to access an address at "paddr" that I do not have permission to or that doesn't exist.

Please help, thanks in advance.

6
  • 1
    What version of the bcm2835 library are you using?
    – joan
    Oct 31 '16 at 14:10
  • The library version is bcm2835-1.50
    – Vitamin_G
    Oct 31 '16 at 14:17
  • I tested the code on Geany and it worked but since trying to create a GUI on Qt I have had problems
    – Vitamin_G
    Oct 31 '16 at 14:21
  • PIN16 isn't defined in your code. I presume that's a typo. I'd ask on the bcm2835 forum as it works in Geany but not Qt.
    – joan
    Oct 31 '16 at 14:39
  • Yes sorry it was a typo
    – Vitamin_G
    Oct 31 '16 at 14:40
1

I had the same problem and I discovered that was my fault, I mean you have to check the return value of a function and manage it in case of error.

In this case the function bcm2835_spi_begin(); returns operation status, as you can see in the documentation:

Start SPI operations. Forces RPi SPI0 pins P1-19 (MOSI), P1-21 (MISO), P1-23 (CLK), P1-24 (CE0) and P1-26 (CE1) to alternate function ALT0, which enables those pins for SPI interface. You should call bcm2835_spi_end() when all SPI funcitons are complete to return the pins to their default functions.

See also bcm2835_spi_end() Returns 1 if successful, 0 otherwise (perhaps because you are not running as root)

The most important thing of this explanation, is the last sentence where it says that you must run your appplication as root user. Otherwise bcm2835_spi_begin() will return you 0. As you do not check it, you have this segmentation fault error.

I hope it helps.

Regards.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.