Pavel_47
Posts: 46
Joined: Mon Jun 12, 2017 8:34 am
Location: Switzerland

"Segmentation fault" error while using bcm2835 librarry

Tue Jun 04, 2019 10:09 am

Hello,

I've just tried simple example on using bcm2835 library.
The run-time error Segmentation fault occurs on this instruction:

Code: Select all

bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);
Any suggestions ?

LdB
Posts: 1281
Joined: Wed Dec 07, 2016 2:29 pm

Re: "Segmentation fault" error while using bcm2835 librarry

Tue Jun 04, 2019 11:54 am

At a guess you are running under linux and haven't mapped access to the SPI registers.

There is probably an Init call somewhere you are supposed to make before doing anything with the library, so did you read the instructions?.

Pavel_47
Posts: 46
Joined: Mon Jun 12, 2017 8:34 am
Location: Switzerland

Re: "Segmentation fault" error while using bcm2835 librarry

Tue Jun 04, 2019 12:04 pm

Here is whole example (I added printf to see where it happens):

Code: Select all

#include <bcm2835.h>
#include <stdio.h>

int main(int argc, char **argv) {
        printf("starting ...\n");

        if (!bcm2835_init()) {
                printf("init failed\n");
                return 1;
        }

        printf("Init Ok\n");

        bcm2835_spi_begin();
        printf("SPI begin Ok\n");

        bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
        printf("Set bit order Ok \n");

        bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
        printf("Set SPI mode Ok \n");

        bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64);    // ~ 4 MHz
        bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
        bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default

        printf("SPI setup Ok\n");

        uint8_t mosi[4] = { 0x20, 0x00, 0x08, 0x00 };
        uint8_t miso[4] = { 0, 0, 0, 0 };
        bcm2835_spi_transfernb(mosi, miso, 4);
        printf("miso[0] = %04x\nmiso[1] = %04x\nmiso[2] = %04x\nmiso[3] = %04x\n", miso[0], miso[1], miso[2], miso[3]);

        bcm2835_spi_end();
        bcm2835_close();
        return 0;
}
First 3 instruction pass, on the 4th the runtime occurs.
But I've just found workaround.
When example is executed with sudo, whole code works !!!

User avatar
Paeryn
Posts: 2701
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: "Segmentation fault" error while using bcm2835 librarry

Tue Jun 04, 2019 3:23 pm

The docs for the library say that you only get GPIO access if you aren't running as root. Moreover, you should have checked the return value of bcm2835_spi_begin() which, when not run as root, would have returned false telling you that SPI access wasn't available.
http://www.airspayce.com/mikem/bcm2835/ wrote: Prior to the release of Raspbian Jessie in Feb 2016, access to any peripheral device via /dev/mem on the RPi required the process to run as root. Raspbian Jessie permits non-root users to access the GPIO peripheral (only) via /dev/gpiomem, and this library supports that limited mode of operation.

If the library runs with effective UID of 0 (ie root), then bcm2835_init() will attempt to open /dev/mem, and, if successful, it will permit use of all peripherals and library functions.

If the library runs with any other effective UID (ie not root), then bcm2835_init() will attempt to open /dev/gpiomem, and, if successful, will only permit GPIO operations. In particular, bcm2835_spi_begin() and bcm2835_i2c_begin() will return false and all other non-gpio operations may fail silently or crash.
She who travels light — forgot something.

Return to “C/C++”