-
Notifications
You must be signed in to change notification settings - Fork 16
/
wiring.c
62 lines (46 loc) · 1.28 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
#include "wiring.h"
#include <string.h>
void
wiring_init()
{
if (!bcm2835_init()) {
printf("bcm2835 init failed\n");
exit(errno);
}
bcm2835_spi_begin();
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // The default
bcm2835_spi_setDataMode(BCM2835_SPI_MODE0); // The default
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); // 4Mhz clock
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // The default
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // the default
bcm2835_gpio_fsel(WIRING_NRF_PROG_PIN, BCM2835_GPIO_FSEL_OUTP);
bcm2835_gpio_fsel(WIRING_NRF_RESET_PIN, BCM2835_GPIO_FSEL_OUTP);
}
uint8_t
wiring_write_then_read(uint8_t* out, uint16_t out_len,
uint8_t* in, uint16_t in_len)
{
uint8_t transfer_buf[out_len + in_len];
unsigned int ret = 0;
memset(transfer_buf, 0, out_len + in_len);
if (NULL != out) {
memcpy(transfer_buf, out, out_len);
ret += out_len;
}
if (NULL != in) {
ret += in_len;
}
bcm2835_spi_transfern((char*)transfer_buf, ret);
memcpy(in, &transfer_buf[out_len], in_len);
return ret;
}
void
wiring_set_gpio_value(uint8_t pin, uint8_t state)
{
bcm2835_gpio_write(pin, state);
}
void
wiring_destroy(void)
{
bcm2835_spi_end();
}