libsi24: small fixes for recv mode
[libsi24] / libsi24.c
1 /**
2  * File              : libsi24.c
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 <stdarg.h>
10 #include <stdio.h>
11 #include <stdint.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "libsi24.h"
16 #include "libsi24reg.h"
17
18 #define DEBUG 1
19
20 struct si24_t {
21         const si24_opts_t *opts;
22         const si24_ioctl_t *ctl;
23         si24_event_handler_t eh;
24 };
25
26 static uint8_t _reg_read(si24_t *si, uint8_t reg,
27                 uint8_t *data, int sz)
28 {
29         uint8_t buf[sz+1];
30         
31         memset(buf, 0, sz+1); 
32         buf[0] = reg | SI24_R_REGISTER;
33
34         
35         if (si->ctl->write_and_read(buf, sz+1) == -1) {
36                 si24_event_t ev;
37                 ev.type = EV_ERR_BUS;
38                 si->eh(si, &ev);
39                 return -1;
40         }
41         
42         memcpy(data, (buf+1), sz);
43         
44         return 0;
45 }
46
47 static uint8_t _reg_write(si24_t *si, uint8_t reg,
48                 const uint8_t *data, int sz)
49 {
50         uint8_t buf[sz+1];
51         
52         buf[0] = reg | SI24_W_REGISTER;
53         memcpy((buf+1), data, sz);
54         
55         if(DEBUG) {
56                 printf("REG 0x%x:\t", reg);
57                 for (int i = 1; i <= sz; ++i) {
58                         fprintf(stdout, "0x%x(%c)", buf[i], buf[i]);
59                 }
60                 fprintf(stdout, "\n");
61         }
62
63         if (si->ctl->write_and_read(buf, sz+1) == -1) {
64                 si24_event_t ev;
65                 ev.type = EV_ERR_BUS;
66                 si->eh(si, &ev);
67                 return -1;
68         }
69
70         return 0;
71 }
72
73 static int _config(si24_t * si)
74 {
75         int ret = 0;
76         uint8_t config_reg = (1 << PWR_UP);
77         uint8_t feature_reg = 0x0; /* default value */
78         uint8_t rf_setup_reg = 0xE; /* default value */
79         uint8_t setup_retr_reg = 0x3; /* default value */
80         const uint8_t rf_ch_reg = 0x40; /* default value */
81         const si24_opts_t * params = si->opts; 
82         
83         if (params->enable_crc) {
84                 config_reg |= (1 << EN_CRC);
85                 config_reg |= (si->opts->crc << CRCO);
86         }
87
88         if (params->enable_ack) {
89                 uint8_t dyn = (1 << DPL_P0);
90                 ret += _reg_write(si, SI24_REG_DYNPD, &dyn, 1);
91                 feature_reg |= (1 << EN_DPL);
92                 ret += _reg_write(si, SI24_REG_FEATURE, &feature_reg, 1);
93                 if (params->mode == SEND_MODE) {
94                         setup_retr_reg = ARD(params->timeout) | ARC(params->retries);
95                         ret += _reg_write(si, SI24_REG_SETUP_RETR, &setup_retr_reg, 1);
96                 }
97         } else {
98                 if (params->mode == SEND_MODE) {
99                         feature_reg |= (1 << EN_DYN_ACK);
100                         ret += _reg_write(si, SI24_REG_FEATURE, &feature_reg, 1);
101                 }
102         }
103
104         uint8_t aw;
105         if (params->mac_addr & 0xF0000) {
106                 aw = AW_5;
107                 ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
108         } else if (params->mac_addr & 0xF000) {
109                 aw = AW_4;
110                 ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
111         } else {
112                 aw = AW_3;
113                 ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
114         }
115
116         /* quick hack */
117         aw += 2;
118
119         if (params->mode == SEND_MODE && params->enable_ack) {
120                 ret += _reg_write(si, SI24_REG_RX_ADDR_P0, (uint8_t *) &params->mac_addr, aw);
121         }
122         
123         if (params->mode == RECV_MODE) {
124                 config_reg |= (1 << PRIM_RX);
125                 uint8_t ch = 0x1;
126                 ret += _reg_write(si, SI24_REG_EN_RXADDR, &ch, 1); 
127                 ret += _reg_write(si, SI24_REG_RX_ADDR_P0, (uint8_t *) &params->mac_addr, aw);
128                 ret += _reg_write(si, SI24_REG_RX_PW_P0, (uint8_t *) &params->payload, 1);
129         } else {
130                 ret += _reg_write(si, SI24_REG_TX_ADDR, (uint8_t *) &params->mac_addr, aw);
131         }
132
133         rf_setup_reg |= (params->speed << RF_DR_HIGH);
134         rf_setup_reg |= (params->txpwr << RF_PWR);
135         ret += _reg_write(si, SI24_REG_RF_SETUP, &rf_setup_reg, 1);
136
137
138         ret += _reg_write(si, SI24_REG_RF_CH, &rf_ch_reg, 1);
139         ret += _reg_write(si, SI24_REG_CONFIG, &config_reg, 1);
140
141         return ret;
142 }
143
144 si24_t* si24_init(const si24_opts_t *opts, si24_event_handler_t eh)
145 {
146         struct si24_t *si = (si24_t*) calloc(1, sizeof(si24_t));
147         if (si == 0)
148                 return 0;
149
150         si->opts = opts;
151         si->ctl = opts->ioctl;
152         si->eh = eh;
153
154         int ret = _config(si);
155         if (ret < 0) {
156                 free(si);
157                 return 0;
158         }
159
160         return si;
161 }
162
163 size_t si24_send(si24_t* si, const unsigned char * buf, size_t size)
164 {
165         si24_event_t ev;
166         uint16_t timeout = 0;
167         int sz;
168         uint8_t flags;
169         
170         if (si->opts->mode == RECV_MODE)
171                 return -1;
172
173         _reg_read(si, SI24_REG_STATUS, (uint8_t *) &flags, 1);
174
175         if (flags & (1 << TX_FULL)) {
176                 ev.type = EV_TX_FULL;
177                 si->eh(si, &ev);
178                 return -1;
179         }
180
181         for (size_t idx = 0; idx < size; idx += si->opts->payload) {
182                 sz = (size - idx) < si->opts->payload ? (size - idx) : si->opts->payload;  
183                 if (si->opts->enable_ack) {
184                         _reg_write(si, SI24_W_TX_PAYLOAD, buf + idx, sz);
185                         si->ctl->chip_enable(1);
186                         while ((!(flags & (1 << TX_DS)) && !(flags & (1 << MAX_RT))) && timeout < 1000) {
187                                 _reg_read(si, SI24_REG_STATUS, &flags, 1);
188                                 timeout++;
189                         }
190                         if (flags & (1 << MAX_RT)) {
191                                 ev.type = EV_ERR_MAX_RETRIES;
192                                 si->eh(si, &ev);
193                                 si24_reset(si);
194                                 return -1;
195                         }
196
197                 } else {
198                         _reg_write(si, SI24_W_TX_PAYLOAD_NO_ACK, buf + idx, sz);
199                         si->ctl->chip_enable(1);
200                         while (!(flags & (1 << TX_DS)) && timeout < 1000) {
201                                 _reg_read(si, SI24_REG_STATUS, &flags, 1);
202                                 timeout++;
203                         }
204                 }
205
206                 if (timeout >= 1000) {
207                         ev.type = EV_ERR_TIMEOUT;
208                         si->eh(si, &ev);
209                         si24_reset(si);
210                         return -1;
211                 }
212
213                 timeout = 0;
214         }
215
216         ev.type = EV_TX_COMPLETE;
217         si->eh(si, &ev);
218         si->ctl->chip_enable(0);
219
220         return 0;
221 }
222
223 size_t si24_recv(si24_t* si, unsigned char * buf, size_t size) 
224 {
225         (void) buf;
226         (void) size;
227         if (si->opts->mode == SEND_MODE)
228                 return -1;
229         
230         uint8_t flags;
231         _reg_read(si, 0x7, &flags, 1);
232         
233         return 0;
234 }
235
236 void si24_reset(si24_t* si)
237 {
238         if (si->opts->mode == RECV_MODE) {
239                 _reg_write(si, SI24_FLUSH_RX, 0, 0);
240         }
241         else if (si->opts->mode == SEND_MODE) {
242                 _reg_write(si, SI24_FLUSH_TX, 0, 0);
243         }
244
245         uint8_t status_reg = {0};
246         status_reg |= (1 << RX_DR);
247         status_reg |= (1 << TX_DS);
248         status_reg |= (1 << MAX_RT);
249
250         _reg_write(si, SI24_REG_STATUS, (uint8_t *) &status_reg, 1);
251
252         si->ctl->chip_enable(0);
253 }
254
255 void si24_free(si24_t * si)
256 {
257         free(si);
258 }
259
260 /* hardware linkage */
261 int spi_w_r(unsigned char *data, size_t sz)
262 {
263         if (sz >= 2)
264                 data[1] = (1 << TX_DS);
265         return sz;
266 }
267
268 void ce(unsigned val)
269 {
270 }
271
272 void eh(si24_t *si, si24_event_t * e)
273 {
274         switch(e->type) {
275                 case EV_TX_COMPLETE:
276                         printf("SENT SUCCESFUL\n");
277                         break;
278                 case EV_ERR_TIMEOUT:
279                         printf("TIMEOUT\n");
280                         break;
281                 default:
282                         printf("EVENT: %x\n", e->type);
283                         break;
284         }
285 }
286
287 int main(void)
288 {
289         const unsigned char buf[] = "THIS IS A WIRELESS TEST MESSAGE!";
290
291         si24_ioctl_t ctl = {
292                 .write_and_read = spi_w_r,
293                 .chip_enable = ce,
294         };
295
296         const si24_opts_t opts = {
297                 .mode = RECV_MODE,
298                 .enable_ack = 1,
299                 .non_blocking = 0,
300                 .enable_crc = 1,
301                 .enable_dynpd = 1,
302                 .crc = TWO_BYTE,
303                 .ioctl = &ctl,
304                 .speed = MBPS2,
305                 .txpwr = PLUS4DB,
306                 .payload = 5,
307                 .timeout = 1,
308                 .retries = 5,
309                 .mac_addr = 0xAAAAAAAAAA
310         };
311
312         struct si24_t * si = si24_init(&opts, eh);
313         si24_send(si, buf, sizeof(buf));
314         
315 }