pikeyd165 – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | // spin.c |
2 | // |
||
3 | // Example program for bcm2835 library |
||
4 | // Shows how to interface with SPI to transfer a number of bytes to and from an SPI device |
||
5 | // |
||
6 | // After installing bcm2835, you can build this |
||
7 | // with something like: |
||
8 | // gcc -o spin spin.c -l bcm2835 |
||
9 | // sudo ./spin |
||
10 | // |
||
11 | // Or you can test it before installing with: |
||
12 | // gcc -o spin -I ../../src ../../src/bcm2835.c spin.c |
||
13 | // sudo ./spin |
||
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_begin(); |
||
40 | bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default |
||
41 | bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default |
||
42 | bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default |
||
43 | bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default |
||
44 | bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default |
||
45 | |||
46 | // Send a some bytes to the slave and simultaneously read |
||
47 | // some bytes back from the slave |
||
48 | // Most SPI devices expect one or 2 bytes of command, after which they will send back |
||
49 | // some data. In such a case you will have the command bytes first in the buffer, |
||
50 | // followed by as many 0 bytes as you expect returned data bytes. After the transfer, you |
||
51 | // Can the read the reply bytes from the buffer. |
||
52 | // If you tie MISO to MOSI, you should read back what was sent. |
||
53 | |||
54 | char buf[] = { 0x01, 0x02, 0x11, 0x33 }; // Data to send |
||
55 | bcm2835_spi_transfern(buf, sizeof(buf)); |
||
56 | // buf will now be filled with the data that was read from the slave |
||
57 | printf("Read from SPI: %02X %02X %02X %02X \n", buf[0], buf[1], buf[2], buf[3]); |
||
58 | |||
59 | bcm2835_spi_end(); |
||
60 | bcm2835_close(); |
||
61 | return 0; |
||
62 | } |
||
63 |