forked from derekstavis/nrf24le1-libbcm2835
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwiring.c
More file actions
86 lines (65 loc) · 1.41 KB
/
Copy pathwiring.c
File metadata and controls
86 lines (65 loc) · 1.41 KB
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
85
86
#include "wiring.h"
#include <string.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/spi/spidev.h>
#include "gpio.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 (gpio_init() != 0) {
goto exit;
}
gpio_oe(WIRING_NRF_PROG_PIN / 32, WIRING_NRF_PROG_PIN % 32);
gpio_oe(WIRING_NRF_RESET_PIN / 32, WIRING_NRF_RESET_PIN % 32);
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)
{
gpio_set(pin / 32, pin % 32, state);
}
void
wiring_destroy(void)
{
close(fd);
gpio_exit();
}