libsi24: added receive loop func
[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_RX_EMPTY,
65     EV_ERR_TIMEOUT,
66     EV_ERR_BUS,
67     EV_ERR_MAX_RETRIES,
68     EV_ERR_CRC,
69     EV_ERR_CONFIG
70 };
71
72 typedef enum si24_event_type_t si24_event_type_t;
73
74 union si24_event_t {
75         enum si24_event_type_t type;
76         struct error_t {
77                 enum si24_event_type_t _type;
78                 const char *file;
79                 const char *func;
80                 const char *msg;
81                 int line;
82         } error;
83 };
84
85 typedef union si24_event_t si24_event_t;
86
87 /* low level IO control */
88 typedef struct {
89     int (*write_and_read)(unsigned char *data, size_t sz);
90     void (*chip_enable)(unsigned val);
91 } si24_ioctl_t;
92
93 typedef struct {
94     si24_mode_t mode;
95     unsigned enable_ack;
96     unsigned non_blocking;
97     unsigned enable_crc;
98     unsigned enable_dynpd;
99     si24_crc_t crc;
100     si24_ioctl_t *ioctl;
101     si24_speed_t speed;
102     si24_txpower_t txpwr;
103     unsigned payload;
104     unsigned timeout; /* 1: 250 us, 15: 4000 us */
105     unsigned retries; /* 1 to 15 */
106     unsigned long mac_addr;
107 } si24_opts_t;
108
109 /* private data structure */
110 typedef struct si24_t si24_t;
111
112 typedef void (*si24_event_handler_t)(si24_t* si24, si24_event_t* event);
113
114 extern si24_t* si24_init(const si24_opts_t* si24opts, si24_event_handler_t eh);
115 extern void si24_free(si24_t* si24);
116 extern size_t si24_send(si24_t* si24, const unsigned char * buf, size_t size);
117 extern size_t si24_recv(si24_t* si24, unsigned char * buf, size_t size);
118 extern void si24_reset(si24_t* si24);
119
120 #if defined(__cplusplus)
121 }
122 #endif