libsi24: uniform registers + debugging setup
[libsi24] / libsi24.h
1 /**
2  * File              : libsi24.h
3  * Author            : Robin Krens <robin@robinkrens.nl>
4  * Date              : 18.01.2023
5  * Last Modified Date: 22.01.2023
6  * Last Modified By  : Robin Krens <robin@robinkrens.nl>
7  */
8
9 #include <stddef.h>
10
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14
15 enum si24_status_t {
16     SHUTDOWN = 0,
17     STANDBY,
18     IDLETX,
19     TX,
20     RX
21 };
22
23 typedef enum si24_status_t si24_status_t;
24
25 enum si24_mode_t {
26     RECV_MODE = 0,
27     SEND_MODE
28 };
29
30 typedef enum si24_mode_t si24_mode_t;
31
32 enum si24_crc_t {
33     ONE_BYTE = 0,
34     TWO_BYTE = 1
35 };
36
37 typedef enum si24_crc_t si24_crc_t;
38
39 enum si24_speed_t {
40         MBPS1 = 0,
41         MBPS2 = 1,
42         KBPS250 = 2
43 };
44
45 typedef enum si24_speed_t si24_speed_t;
46
47 enum si24_txpower_t {
48         MINUS12DB = 0,
49         MINUS6DB,
50         MINUS4DB,
51         ZERODB,
52         PLUS1DB,
53         PLUS3DB,
54         PLUS4DB,
55         PLUS7DB
56 };
57
58 typedef enum si24_txpower_t si24_txpower_t;
59
60 enum si24_event_type_t {
61     EV_RX_COMPLETE = 0,
62     EV_TX_COMPLETE,
63     EV_TX_FULL,
64     EV_ERR_TIMEOUT,
65     EV_ERR_BUS,
66     EV_ERR_MAX_RETRIES,
67     EV_ERR_CRC,
68     EV_ERR_CONFIG
69 };
70
71 typedef enum si24_event_type_t si24_event_type_t;
72
73 union si24_event_t {
74         enum si24_event_type_t type;
75         struct error_t {
76                 enum si24_event_type_t _type;
77                 const char *file;
78                 const char *func;
79                 const char *msg;
80                 int line;
81         } error;
82 };
83
84 typedef union si24_event_t si24_event_t;
85
86 /* low level IO control */
87 typedef struct {
88     int (*write_and_read)(unsigned char *data, size_t sz);
89     void (*chip_enable)(unsigned val);
90 } si24_ioctl_t;
91
92 typedef struct {
93     si24_mode_t mode;
94     unsigned enable_ack;
95     unsigned non_blocking;
96     unsigned enable_crc;
97     unsigned enable_dynpd;
98     si24_crc_t crc;
99     si24_ioctl_t *ioctl;
100     si24_speed_t speed;
101     si24_txpower_t txpwr;
102     unsigned payload;
103     unsigned timeout; /* 1: 250 us, 15: 4000 us */
104     unsigned retries; /* 1 to 15 */
105     unsigned long mac_addr;
106 } si24_opts_t;
107
108 /* private data structure */
109 typedef struct si24_t si24_t;
110
111 typedef void (*si24_event_handler_t)(si24_t* si24, si24_event_t* event);
112
113 extern si24_t* si24_init(const si24_opts_t* si24opts, si24_event_handler_t eh);
114 extern void si24_free(si24_t* si24);
115 extern size_t si24_send(si24_t* si24, const unsigned char * buf, size_t size);
116 extern size_t si24_recv(si24_t* si24, unsigned char * buf, size_t size);
117 extern void si24_reset(si24_t* si24);
118
119 #if defined(__cplusplus)
120 }
121 #endif