#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;
}
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.
#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;
}
| BCM2835_SPI_CS_NONE | No CS, control it yourself |