Hardware Reference
In-Depth Information
If you remove the wire between MOSI and MISO, and connect the MISO to a high
(+3.3 V), you should be able to read 0xFF for all of the received bytes. If you then connect
MISO to ground, 0x00 will be received for each byte instead. (Be certain to apply to the
correct pin, since applying high or low to an output can damage it, and do not apply +5 V.)
1 /
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
2
spiloop.c − Example loop test
3
Connect MOSI (GPIO 10) to MISO (GPIO 9)
4
/
∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗∗
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdlib.h>
8 #include <stdint.h>
9 #include <fcntl.h>
10 #include <unistd.h>
11 #include <sys/ioctl.h>
12 #include <linux/types.h>
13 #include <linux/spi/spidev.h>
14
15 static int fd = −1;
16
17 static void
18 errxit(const char
msg) {
19 perror(msg);
20 exit(1);
21 }
22
23 int
24 main(int argc, char
argv) {
25 static uint8_t tx[] = {0x12, 0x23, 0x45, 0x67};
26 static uint8_t rx[] = {0xFF, 0xFF, 0xFF, 0xFF};
27 struct spi_ioc_transfer ioc = {
28 .tx_buf = (unsigned long) tx,
29 .rx_buf = (unsigned long) rx,
30 .len = 4,
31 .speed_hz = 100000,
32 .delay_usecs = 10,
33 .bits_per_word = 8,
34 .cs_change = 1
35 } ;
36 uint8_t mode = SPI_MODE_0;
37 int rc;
38
39 fd = open("/dev/spidev0.0",O_RDWR);
40 if ( fd < 0 )
41 errxit("Opening SPI device.");
42
∗∗
Search WWH ::




Custom Search