User avatar
SpazzTechTom
Posts: 15
Joined: Sat Jan 31, 2015 12:50 am
Location: Glen Rock, PA. USA
Contact: Website

SPI with bcm2835 library on RPi 2

Fri Apr 03, 2015 8:38 pm

Sorry if this is a repeat. A search did not uncover an answer for me.

Has anyone used successfully used SPI with the bcm2835 library with the RPi 2 yet? My program is exiting from the bcm2835_spi_begin() function with code 139. I have not found anything in the bcm2835.c or bcm2835.h code that would return 139. I have used raspi-config to make sure the SPI module is enabled and have rebooted. I'm wondering if anyone else has had this problem. I traced the problem to the bcm2835_spi_begin() function by surrounding each function call with printf messages.

I am using version 1.42 of bcm2835.

Thanks in advance.
SpazzTechTom
Having fun with technology!

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI with bcm2835 library on RPi 2

Fri Apr 03, 2015 9:01 pm

All the libraries which access the gpios at a low-level needed to be updated for the Pi2. Make sure you are using the latest version.

User avatar
SpazzTechTom
Posts: 15
Joined: Sat Jan 31, 2015 12:50 am
Location: Glen Rock, PA. USA
Contact: Website

Re: SPI with bcm2835 library on RPi 2

Sun Apr 05, 2015 11:36 pm

joan wrote:All the libraries which access the gpios at a low-level needed to be updated for the Pi2. Make sure you are using the latest version.
I am running the latest version of Rasbian just downloaded and installed today and the latest version of the bcm2835 library (1.44). My program is still exiting with code 139.

I have ensured that /proc/device-tree/soc/ranges is present as stated in the comments of bcm3835.h and have tried all available options under raspi-config for SPI with reboots in between each option change.
SpazzTechTom
Having fun with technology!

User avatar
SpazzTechTom
Posts: 15
Joined: Sat Jan 31, 2015 12:50 am
Location: Glen Rock, PA. USA
Contact: Website

Re: SPI with bcm2835 library on RPi 2

Mon Apr 06, 2015 1:29 am

I found the issue. The bcm2835 library failed to map the register addresses for the peripherals. On lines 45 and 49 - 56 I am seeing "MAP_FAILED" instead of a register address.

BTW, I downloaded and tried using SPI with wiringPi and the spiSpeed.c example worked out of the box.

I'm tempted to just use wiringPi or make making my own driver, but now that I have found where the issue is, I'm inclined to fix it. I'm just trying to find all the proper register addresses now. :)

Is there a peripheral interface reference document yet for the RPi 2?
SpazzTechTom
Having fun with technology!

User avatar
joan
Posts: 14936
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: SPI with bcm2835 library on RPi 2

Mon Apr 06, 2015 8:56 am

SpazzTechTom wrote:I found the issue. The bcm2835 library failed to map the register addresses for the peripherals. On lines 45 and 49 - 56 I am seeing "MAP_FAILED" instead of a register address.

BTW, I downloaded and tried using SPI with wiringPi and the spiSpeed.c example worked out of the box.

I'm tempted to just use wiringPi or make making my own driver, but now that I have found where the issue is, I'm inclined to fix it. I'm just trying to find all the proper register addresses now. :)

Is there a peripheral interface reference document yet for the RPi 2?
It's the same as the bcm2835 with the peripherals re-based at physical address 0x3F000000.

Code: Select all

#define AUX_BASE   (pi_peri_phys + 0x00215000)
#define CLK_BASE   (pi_peri_phys + 0x00101000)
#define DMA_BASE   (pi_peri_phys + 0x00007000)
#define DMA15_BASE (pi_peri_phys + 0x00E05000)
#define GPIO_BASE  (pi_peri_phys + 0x00200000)
#define PCM_BASE   (pi_peri_phys + 0x00203000)
#define PWM_BASE   (pi_peri_phys + 0x0020C000)
#define SPI_BASE   (pi_peri_phys + 0x00204000)
#define SYST_BASE  (pi_peri_phys + 0x00003000)

#define AUX_LEN   0xD8
#define CLK_LEN   0xA8
#define DMA_LEN   0x1000 /* allow access to all channels */
#define GPIO_LEN  0xB4
#define PCM_LEN   0x24
#define PWM_LEN   0x28
#define SPI_LEN   0x18
#define SYST_LEN  0x1C

User avatar
rpdom
Posts: 17174
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: SPI with bcm2835 library on RPi 2

Mon Apr 06, 2015 9:28 am

SpazzTechTom wrote:Is there a peripheral interface reference document yet for the RPi 2?
That would be the BCM2836 ARM-local peripherals manual at http://www.raspberrypi.org/documentatio ... /README.md

User avatar
AndyD
Posts: 2334
Joined: Sat Jan 21, 2012 8:13 am
Location: Melbourne, Australia
Contact: Website

Re: SPI with bcm2835 library on RPi 2

Mon Apr 06, 2015 9:49 am

SpazzTechTom wrote:I found the issue. The bcm2835 library failed to map the register addresses for the peripherals. On lines 45 and 49 - 56 I am seeing "MAP_FAILED" instead of a register address.

BTW, I downloaded and tried using SPI with wiringPi and the spiSpeed.c example worked out of the box.

I'm tempted to just use wiringPi or make making my own driver, but now that I have found where the issue is, I'm inclined to fix it. I'm just trying to find all the proper register addresses now. :)

Is there a peripheral interface reference document yet for the RPi 2?
Are you calling bcm2835_init()? In the bcm2835 library all the addresses are set to MAP_FAILED until bcm2835_init() is called.

Alternatively, are you seeing a message bcm2835: gpio mmap failed: ...? If so what is ...? Most common reason the mmap fails is privileges (i.e. you can only mmap /dev/mem as root).

User avatar
SpazzTechTom
Posts: 15
Joined: Sat Jan 31, 2015 12:50 am
Location: Glen Rock, PA. USA
Contact: Website

Re: SPI with bcm2835 library on RPi 2

Fri May 01, 2015 11:06 pm

I just wanted to post what I found was causing my problem. I was just about to give up, but decided to try one more time from scratch. To my surprise it worked on the first try with no errors. That has happened to me before, but in this case I can not figure out what was happening exactly.

The error I was running into was that my program would exit with code 139 ever time the bcm2835_spi_begin() function was called. (program existed with code: 139).

I am using bcm2835.h version 1.44 on RPi 2. Raspbian 2015-02-16.

I went through my original code line by line comparing to the code that worked and traced the problem to a function I had defined. I declared a function called "void close();" ahead of main and defined it after main. It called "bcm2835_spi_close();" and "bcm2835_close();". That's it. With this in my program, bcm2835_spi_begin() will not work. I dug a little be further and found that if I comment out bcm2835_close() it works. So why does having this line in a function that is not even being called cause th bcm2835_spi_begin() function to fail?

I'm happy I have the rest of my program working, but am not sure why this caused bcm2835_spi_begin(); to cause my program to exit with code 139. Its just one of those weird things that is going to bug me until I understand it.

By the way, I love the bcm2835.h library. Especially how well it is documented!
SpazzTechTom
Having fun with technology!

Return to “Interfacing (DSI, CSI, I2C, etc.)”