Using bcm2835 library with multiple SPI devices

818 views
Skip to first unread message

Maayan Dreamer

unread,
Jun 27, 2016, 12:03:18 AM6/27/16
to bcm2835
Hi,

I'm trying to hook up 10 SPI MCP3008 devices to my Pi. I'm tring to use the bcm2835 (http://www.airspayce.com/mikem/bcm2835/group__spi.html) library but with no success so far.
I managed to run it with one device using BCM2835_SPI_CS0 but when I try to control the SC/SS pins myself it doesn't work.
Can you guys look into this example code and figure out what I'm doing wrong?
Thanks
Maayan

P.S.
If you have another idea how I can easily sample about 100 piezo knock sensor I'm open to suggestions.

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

// Blinks on RPi Plug P1 pin 18 (which is GPIO pin 18)
#define PIN RPI_BPLUS_GPIO_J8_18

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

  if (!bcm2835_init()){
    return 1;
  }

  // Set the pin to be an output
  bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
  bcm2835_gpio_write(PIN, HIGH);
  bcm2835_spi_begin();
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //Data comes in on falling edge
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); //250MHz / 256 = 976.5kHz
  bcm2835_spi_chipSelect(BCM2835_SPI_CS_NONE); //Slave Select on CS0
  //bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);  
  // Turn it on
  //bcm2835_gpio_write(PIN, LOW);


  char send_data[3] = {0x01,0x80,0x01};
  char msb[3];
while (1)
{  
send_data[0] = 0x01;
send_data[1] = 0x80;
send_data[2] = 0x01;
bcm2835_gpio_write(PIN, LOW);
//usleep(100);
bcm2835_spi_transfern(send_data,3);
//usleep(100);
bcm2835_gpio_write(PIN, HIGH);
  //printf("%d\n", msb);
  printf("0: %d 1: %d 2: %d\n",send_data[0], send_data[1], send_data[2]);
usleep(100000);
}
  bcm2835_spi_end();
  bcm2835_close();
  return 0;
}


Mike McCauley

unread,
Jun 27, 2016, 12:27:07 AM6/27/16
to bcm...@googlegroups.com
Hi,

I dont think you have really told us enough to be able to help.

For example, how are your devices connected to the RPI?
What happens if you have only 2 devices connected?
What code do you use to talk to multiple devices?
What exactly do you mean by "doesnt work"
Have you considered what the fan-out of the SPI pins might be for the RPi? Is
it really likely you can drive many devices from the SPI pins?


Perhaps you might like to read:

http://en.wikipedia.org/wiki/Wikipedia:Reference_desk/How_to_ask_a_software_question
http://catb.org/~esr/faqs/smart-questions.html
http://www.chiark.greenend.org.uk/~shgtatham/bugs.html

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

Maayan Dreamer

unread,
Jun 27, 2016, 12:55:06 AM6/27/16
to bcm2835
Hi Mike,

Apologies for way I questioned and thank you for your time.

I'm trying to sample 88 piezo knock sensors using 10 MCP3008, the purpose is an interactive artwork. Every knock will trigger a was sound file that will be played using SDL2 library. As far as I understand I can use 10 SPI device. It will take 3(CLS, MISO, MOSI) +n (the number of SPI salve devices that will be used. It means 3+8 = 11 pins in total will be used. 3 of them to implement the MISO,MOSI and CLK and the rest for the ChipSelect/ChipEnable

As of now when I connect the MCP3008 using the regular SPI wiring like here:

When I connect the wiring and using CE0/CS0 it all works well with the bcm2835 code and I can read the piezo knock sensor. But, if I set the CS pin as pin 18, like in the code I attached in the first post, and manually pulls in up and down it doesn't work.

So far I tried to do it with only one device - the same one that worked with the CE0/CS0. When I meant that it doesn't work it means that I'm sending the same read command that works on the CS0/CE0 but this time I'm not getting any reply back.


This is the code with one slave/SPI device using CE0/SC0 that works well:
#include <stdio.h>
#include <bcm2835.h>
#include <time.h>
#include <unistd.h>

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

  if (!bcm2835_init()){
    return 1;
  }

  // Set the pin to be an output
  bcm2835_spi_begin();
  bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      
  bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); //Data comes in on falling edge
  bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_256); //250MHz / 256 = 976.5kHz
  bcm2835_spi_chipSelect(BCM2835_SPI_CS0); //Slave Select on CS0
  bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);  
 
  char send_data[3] = {0x01,0x80,0x01};
  char msb[3];
while (1)
{  
send_data[0] = 0x01;
send_data[1] = 0x80;
send_data[2] = 0x01;

bcm2835_spi_transfern(send_data,3);

  printf("0: %d 1: %d 2: %d\n",send_data[0], send_data[1], send_data[2]);
usleep(100000);
}
  bcm2835_spi_end();
  bcm2835_close();
  return 0;
}



Cheers
Maayan

Mike McCauley

unread,
Jun 27, 2016, 1:22:00 AM6/27/16
to bcm...@googlegroups.com
Hello,

looks like you sent the code which works but have not sent the code that
doesnt work. I think you should send that?

How are you specifying the chip select pins?

Cheers.

On Sunday, June 26, 2016 09:55:06 PM Maayan Dreamer wrote:
> Hi Mike,
>
> Apologies for way I questioned and thank you for your time.
>
> I'm trying to sample 88 piezo knock sensors using 10 MCP3008, the purpose
> is an interactive artwork. Every knock will trigger a was sound file that
> will be played using SDL2 library. As far as I understand I can use 10 SPI
> device. It will take 3(CLS, MISO, MOSI) +n (the number of SPI salve devices
> that will be used. It means 3+8 = 11 pins in total will be used. 3 of them
> to implement the MISO,MOSI and CLK and the rest for the
> ChipSelect/ChipEnable

Dont you mean 3+10 = 13 pins? You said 10 MCP3008?


>
> As of now when I connect the MCP3008 using the regular SPI wiring like here:
>
> <http://www.hertaville.com/files/uploads/2013/07/gg6.png>

Maayan Dreamer

unread,
Jun 27, 2016, 2:31:26 AM6/27/16
to bcm2835
Hi Mike,

In my first post I attached the code that doesn't work. Here it is again:
Yes, my bad, typo - its 80 piezo sensors as of now(not 88), as I said in my first post - eventually I need to read about 100 piezo knock sensors. As of now I design the artwork for 80 so its 10 MCPs with brings me to 3+10=13 not 3+8=11. I might want to use more in the future, but let's start with 10.

Thanks mate :)

Maayan

Mike McCauley

unread,
Jun 27, 2016, 2:44:16 AM6/27/16
to bcm...@googlegroups.com
Hi,

So if I understand you correctly, the code that works lets the bcm2835 library
code control the CS pin, and the code that doesnt work tries to control the CS
pin "manually" by setting it during the SPI trannsfer?

Are you sure your manually setting the CS pin is working? Have you observed
the voltage falling during the SPI access?

Cheers.

On Sunday, June 26, 2016 11:31:26 PM Maayan Dreamer wrote:
> Hi Mike,
>
> first post - eventually I need to read *about* 100 piezo knock sensors. As
> of now I design the artwork for 80 so its 10 MCPs with brings me to 3+10=13
> *not 3+8=11*. I might want to use more in the future, but let's start with

Maayan Dreamer

unread,
Jun 27, 2016, 3:16:03 AM6/27/16
to bcm2835
Hi Mike,

Correct. Because I don't have enough "CS" pins as part of the natural SPI use in Pi I thought it is a good idea to trigger the CS pins of the devices "manually" by myself during transfer.
I haven't checked that yet because I don't have a scope at home, I'm planning to do it tomorrow at work. I want to see that the timing of the falling edge and the CLK-MISO-MOSI is making sense.

If you are saying that my code look ok this is definitely the next place to look at. (Is it?)
I'll keep this thread posted anyway.

Cheers
Maayan

Mike McCauley

unread,
Jun 27, 2016, 3:26:40 AM6/27/16
to bcm...@googlegroups.com
Hi,

On Monday, June 27, 2016 12:16:03 AM Maayan Dreamer wrote:
> Hi Mike,
>
> Correct. Because I don't have enough "CS" pins as part of the natural SPI
> use in Pi I thought it is a good idea to trigger the CS pins of the devices
> "manually" by myself during transfer.
> I haven't checked that yet because I don't have a scope at home, I'm
> planning to do it tomorrow at work. I want to see that the timing of the
> falling edge and the CLK-MISO-MOSI is making sense.
>
> If you are saying that my code look ok this is definitely the next place to
> look at. (Is it?)

I have not tested driving an SPI device from Rpi that way. I have always use
the SPI peripheral controlled CS pin

Maayan Dreamer

unread,
Jun 27, 2016, 7:22:01 AM6/27/16
to bcm2835
Oh dear... After reading this:

I was sure it can be done easily:
BCM2835_SPI_CS_NONE 

No CS, control it yourself


I guess I gonna find out for all of us tomorrow :\ I gonna take some screenshot of the scope.

Cheers
Maayan

Maayan Dreamer

unread,
Jul 1, 2016, 7:33:25 AM7/1/16
to bcm2835
G'day :) 

Everything seems to be working well. I had a pinout mistake.
I tested it with one device. I'll connect the rest during the weekend.

Thanks for helping. Much appreciated.

Cheers
Maayn
Reply all
Reply to author
Forward
Message has been deleted
0 new messages