bcm2835 1.75
spin.c

Shows how to use SPI interface to transfer a number of bytes to and from an SPI device

// spin.c
//
// Example program for bcm2835 library
// Shows how to interface with SPI to transfer a number of bytes to and from an SPI device
//
// After installing bcm2835, you can build this
// with something like:
// gcc -o spin spin.c -l bcm2835
// sudo ./spin
//
// Or you can test it before installing with:
// gcc -o spin -I ../../src ../../src/bcm2835.c spin.c
// sudo ./spin
//
// Author: Mike McCauley
// Copyright (C) 2012 Mike McCauley
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
// If you call this, it will not actually access the GPIO
// Use for testing
// bcm2835_set_debug(1);
if (!bcm2835_init())
{
printf("bcm2835_init failed. Are you running as root??\n");
return 1;
}
{
printf("bcm2835_spi_begin failed. Are you running as root??\n");
return 1;
}
// Send a some bytes to the slave and simultaneously read
// some bytes back from the slave
// Most SPI devices expect one or 2 bytes of command, after which they will send back
// some data. In such a case you will have the command bytes first in the buffer,
// followed by as many 0 bytes as you expect returned data bytes. After the transfer, you
// Can the read the reply bytes from the buffer.
// If you tie MISO to MOSI, you should read back what was sent.
char buf[] = { 0x01, 0x02, 0x11, 0x33 }; // Data to send
bcm2835_spi_transfern(buf, sizeof(buf));
// buf will now be filled with the data that was read from the slave
printf("Read from SPI: %02X %02X %02X %02X \n", buf[0], buf[1], buf[2], buf[3]);
return 0;
}
#define LOW
Definition: bcm2835.h:687
@ BCM2835_SPI_BIT_ORDER_MSBFIRST
Definition: bcm2835.h:1138
@ BCM2835_SPI_CS0
Definition: bcm2835.h:1157
@ BCM2835_SPI_MODE0
Definition: bcm2835.h:1146
@ BCM2835_SPI_CLOCK_DIVIDER_65536
Definition: bcm2835.h:1179
int bcm2835_close(void)
Definition: bcm2835.c:2163
int bcm2835_init(void)
Definition: bcm2835.c:2008
void bcm2835_spi_setBitOrder(uint8_t order)
Definition: bcm2835.c:737
void bcm2835_spi_transfern(char *buf, uint32_t len)
Definition: bcm2835.c:893
void bcm2835_spi_chipSelect(uint8_t cs)
Definition: bcm2835.c:898
int bcm2835_spi_begin(void)
Definition: bcm2835.c:703
void bcm2835_spi_setDataMode(uint8_t mode)
Definition: bcm2835.c:760
void bcm2835_spi_end(void)
Definition: bcm2835.c:727
void bcm2835_spi_setClockDivider(uint16_t divider)
Definition: bcm2835.c:747
void bcm2835_spi_setChipSelectPolarity(uint8_t cs, uint8_t active)
Definition: bcm2835.c:905