pikeyd165 – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 // spi.c
2 //
3 // Example program for bcm2835 library
4 // Shows how to interface with SPI to transfer a byte to and from an SPI device
5 //
6 // After installing bcm2835, you can build this
7 // with something like:
8 // gcc -o spi spi.c -l bcm2835
9 // sudo ./spi
10 //
11 // Or you can test it before installing with:
12 // gcc -o spi -I ../../src ../../src/bcm2835.c spi.c
13 // sudo ./spi
14 //
15 // Author: Mike McCauley
16 // Copyright (C) 2012 Mike McCauley
17 // $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
18  
19 #include <bcm2835.h>
20 #include <stdio.h>
21  
22 int main(int argc, char **argv)
23 {
24 // If you call this, it will not actually access the GPIO
25 // Use for testing
26 // bcm2835_set_debug(1);
27  
28 if (!bcm2835_init())
29 {
30 printf("bcm2835_init failed. Are you running as root??\n");
31 return 1;
32 }
33  
34 if (!bcm2835_spi_begin())
35 {
36 printf("bcm2835_spi_begin failed. Are you running as root??\n");
37 return 1;
38 }
39 bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
40 bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
41 bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
42 bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
43 bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
44  
45 // Send a byte to the slave and simultaneously read a byte back from the slave
46 // If you tie MISO to MOSI, you should read back what was sent
47 uint8_t send_data = 0x23;
48 uint8_t read_data = bcm2835_spi_transfer(send_data);
49 printf("Sent to SPI: 0x%02X. Read back from SPI: 0x%02X.\n", send_data, read_data);
50 if (send_data != read_data)
51 printf("Do you have the loopback from MOSI to MISO connected?\n");
52 bcm2835_spi_end();
53 bcm2835_close();
54 return 0;
55 }
56