-
Notifications
You must be signed in to change notification settings - Fork 2
/
usbthing_bindings.c
70 lines (49 loc) · 1.41 KB
/
usbthing_bindings.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
#include "usbthing_bindings.h"
#include <stdint.h>
#include "usbthing/usbthing.h"
//#define DEBUG_INTERCEPT_SPI
#ifdef DEBUG_INTERCEPT_SPI
#include <stdio.h>
#endif
// SPI transfer binding
int spi_transfer(void* context, int len, uint8_t *data_out, uint8_t* data_in)
{
usbthing_t *usbthing = (usbthing_t *) context;
int res;
res = USBTHING_spi_transfer(*usbthing, len, data_out, data_in);
#ifdef DEBUG_INTERCEPT_SPI
// Intercept and print SPI commands for debugging
uint8_t reg = data_out[0] & 0x7F;
uint8_t is_write = ((data_out[0] & 0x80) == 0) ? 1 : 0;
printf("SPI (reg: 0x%x write: %d) MOSI: ", data_out[0] & 0x7F, is_write);
for (int i = 0; i < len; i++) {
printf("%.2x ", data_out[i]);
}
printf("MISO: ");
for (int i = 0; i < len; i++) {
printf("%.2x ", data_in[i]);
}
printf("\r\n");
#endif
return res;
}
int set_reset(void* context, uint8_t val)
{
usbthing_t *usbthing = (usbthing_t *) context;
int res = USBTHING_gpio_set(*usbthing, 0, val);
return res;
}
int set_slp_tr(void* context, uint8_t val)
{
usbthing_t *usbthing = (usbthing_t *) context;
int res = USBTHING_gpio_set(*usbthing, 1, val);
return res;
}
int get_irq(void* context, uint8_t *val)
{
usbthing_t *usbthing = (usbthing_t *) context;
int val_int;
int res = USBTHING_gpio_get(*usbthing, 2, &val_int);
*val = val_int;
return res;
}