forked from dwery/nrf24le1-bbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiring.c
84 lines (64 loc) · 1.38 KB
/
wiring.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "wiring.h"
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/spi/spidev.h>
static int fd = -1;
bool
wiring_init(const char *device)
{
int speed = 4000000;
uint8_t mode = SPI_MODE_0;
fd = open(device, O_RDWR);
if (fd < 0) {
printf("Failed to open the %s\n", device);
return 0;
}
if (ioctl(fd, SPI_IOC_WR_MODE, &mode)<0) {
perror("can't set spi mode");
goto exit;
}
if (ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed)<0) {
perror("can't set max speed hz");
goto exit;
}
if (SETUP_OK != sunxi_gpio_init()) {
goto exit;
}
sunxi_gpio_set_cfgpin(SUNXI_GPA(13), OUTPUT);
sunxi_gpio_set_cfgpin(SUNXI_GPA(14), OUTPUT);
return 1;
exit:
close(fd);
return 0;
}
uint8_t
wiring_write_then_read(uint8_t* out, uint16_t out_len,
uint8_t* in, uint16_t in_len)
{
struct spi_ioc_transfer xfer[2];
memset(xfer, 0, sizeof(xfer));
xfer[0].tx_buf = (unsigned long)out;
xfer[0].len = out_len;
xfer[0].bits_per_word = 8;
xfer[1].rx_buf = (unsigned long)in;
xfer[1].len = in_len;
xfer[1].bits_per_word = 8;
int status = ioctl(fd, SPI_IOC_MESSAGE(in_len > 0 ? 2 : 1), &xfer[0]);
if (status < 0) {
perror("SPI_IOC_MESSAGE");
return 0;
}
return in_len + out_len;
}
void
wiring_set_gpio_value(uint8_t pin, uint8_t state)
{
sunxi_gpio_output(pin, state);
}
void
wiring_destroy(void)
{
close(fd);
sunxi_gpio_cleanup();
}