libsi24: correction for ack send 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                 setup_retr_reg = ARD(params->timeout) | ARC(params->retries);
94                 ret += _reg_write(si, SI24_REG_SETUP_RETR, &setup_retr_reg, 1);
95         } else {
96                 feature_reg |= (1 << EN_DYN_ACK);
97                 ret += _reg_write(si, SI24_REG_FEATURE, &feature_reg, 1);
98         }
99
100         uint8_t aw;
101         if (params->mac_addr & 0xF0000) {
102                 aw = AW_5;
103                 ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
104         } else if (params->mac_addr & 0xF000) {
105                 aw = AW_4;
106                 ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
107         } else {
108                 aw = AW_3;
109                 ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
110         }
111
112         /* quick hack */
113         aw += 2;
114
115         if (params->mode == SEND_MODE && params->enable_ack) {
116                 ret += _reg_write(si, SI24_REG_RX_ADDR_P0, (uint8_t *) &params->mac_addr, aw);
117         }
118         
119         if (params->mode == RECV_MODE) {
120                 config_reg |= (1 << PRIM_RX);
121                 uint8_t ch = 0x1;
122                 ret += _reg_write(si, SI24_REG_EN_RXADDR, &ch, 1); 
123                 ret += _reg_write(si, SI24_REG_RX_ADDR_P0, (uint8_t *) &params->mac_addr, aw);
124                 ret += _reg_write(si, SI24_REG_RX_PW_P0, (uint8_t *) &params->payload, 1);
125         } else {
126                 ret += _reg_write(si, SI24_REG_TX_ADDR, (uint8_t *) &params->mac_addr, aw);
127         }
128
129         rf_setup_reg |= (params->speed << RF_DR_HIGH);
130         rf_setup_reg |= (params->txpwr << RF_PWR);
131         ret += _reg_write(si, SI24_REG_RF_SETUP, &rf_setup_reg, 1);
132
133
134         ret += _reg_write(si, SI24_REG_RF_CH, &rf_ch_reg, 1);
135         ret += _reg_write(si, SI24_REG_CONFIG, &config_reg, 1);
136
137         return ret;
138 }
139
140 si24_t* si24_init(const si24_opts_t *opts, si24_event_handler_t eh)
141 {
142         struct si24_t *si = (si24_t*) calloc(1, sizeof(si24_t));
143         if (si == 0)
144                 return 0;
145
146         si->opts = opts;
147         si->ctl = opts->ioctl;
148         si->eh = eh;
149
150         int ret = _config(si);
151         if (ret < 0) {
152                 free(si);
153                 return 0;
154         }
155
156         return si;
157 }
158
159 size_t si24_send(si24_t* si, const unsigned char * buf, size_t size)
160 {
161         si24_event_t ev;
162         uint16_t timeout = 0;
163         int sz;
164         uint8_t flags;
165         
166         if (si->opts->mode == RECV_MODE)
167                 return -1;
168
169         _reg_read(si, SI24_REG_STATUS, (uint8_t *) &flags, 1);
170
171         if (flags & (1 << TX_FULL)) {
172                 ev.type = EV_TX_FULL;
173                 si->eh(si, &ev);
174                 return -1;
175         }
176
177         for (size_t idx = 0; idx < size; idx += si->opts->payload) {
178                 sz = (size - idx) < si->opts->payload ? (size - idx) : si->opts->payload;  
179                 if (si->opts->enable_ack) {
180                         _reg_write(si, SI24_W_TX_PAYLOAD, buf + idx, sz);
181                         si->ctl->chip_enable(1);
182                         while ((!(flags & (1 << TX_DS)) && !(flags & (1 << MAX_RT))) && timeout < 1000) {
183                                 _reg_read(si, SI24_REG_STATUS, &flags, 1);
184                                 timeout++;
185                         }
186                         if (flags & (1 << MAX_RT)) {
187                                 ev.type = EV_ERR_MAX_RETRIES;
188                                 si->eh(si, &ev);
189                                 si24_reset(si);
190                                 return -1;
191                         }
192
193                 } else {
194                         _reg_write(si, SI24_W_TX_PAYLOAD_NO_ACK, buf + idx, sz);
195                         si->ctl->chip_enable(1);
196                         while (!(flags & (1 << TX_DS)) && timeout < 1000) {
197                                 _reg_read(si, SI24_REG_STATUS, &flags, 1);
198                                 timeout++;
199                         }
200                 }
201
202                 if (timeout >= 1000) {
203                         ev.type = EV_ERR_TIMEOUT;
204                         si->eh(si, &ev);
205                         si24_reset(si);
206                         return -1;
207                 }
208
209                 timeout = 0;
210         }
211
212         ev.type = EV_TX_COMPLETE;
213         si->eh(si, &ev);
214         si->ctl->chip_enable(0);
215
216         return 0;
217 }
218
219 size_t si24_recv(si24_t* si, unsigned char * buf, size_t size) 
220 {
221         (void) buf;
222         (void) size;
223         if (si->opts->mode == SEND_MODE)
224                 return -1;
225         
226         uint8_t flags;
227         _reg_read(si, 0x7, &flags, 1);
228         
229         return 0;
230 }
231
232 void si24_reset(si24_t* si)
233 {
234         if (si->opts->mode == RECV_MODE) {
235                 _reg_write(si, SI24_FLUSH_RX, 0, 0);
236         }
237         else if (si->opts->mode == SEND_MODE) {
238                 _reg_write(si, SI24_FLUSH_TX, 0, 0);
239         }
240
241         uint8_t status_reg = {0};
242         status_reg |= (1 << RX_DR);
243         status_reg |= (1 << TX_DS);
244         status_reg |= (1 << MAX_RT);
245
246         _reg_write(si, SI24_REG_STATUS, (uint8_t *) &status_reg, 1);
247
248         si->ctl->chip_enable(0);
249 }
250
251 void si24_free(si24_t * si)
252 {
253         free(si);
254 }
255
256 /* hardware linkage */
257 int spi_w_r(unsigned char *data, size_t sz)
258 {
259         if (sz >= 2)
260                 data[1] = (1 << TX_DS);
261         return sz;
262 }
263
264 void ce(unsigned val)
265 {
266 }
267
268 void eh(si24_t *si, si24_event_t * e)
269 {
270         switch(e->type) {
271                 case EV_TX_COMPLETE:
272                         printf("SENT SUCCESFUL\n");
273                         break;
274                 case EV_ERR_TIMEOUT:
275                         printf("TIMEOUT\n");
276                         break;
277                 default:
278                         printf("EVENT: %x\n", e->type);
279                         break;
280         }
281 }
282
283 int main(void)
284 {
285         const unsigned char buf[] = "THIS IS A WIRELESS TEST MESSAGE!";
286
287         si24_ioctl_t ctl = {
288                 .write_and_read = spi_w_r,
289                 .chip_enable = ce,
290         };
291
292         const si24_opts_t opts = {
293                 .mode = SEND_MODE,
294                 .enable_ack = 1,
295                 .non_blocking = 0,
296                 .enable_crc = 1,
297                 .enable_dynpd = 1,
298                 .crc = TWO_BYTE,
299                 .ioctl = &ctl,
300                 .speed = MBPS2,
301                 .txpwr = PLUS4DB,
302                 .payload = 5,
303                 .timeout = 1,
304                 .retries = 5,
305                 .mac_addr = 0xAAAAAAAAAA
306         };
307
308         struct si24_t * si = si24_init(&opts, eh);
309         si24_send(si, buf, sizeof(buf));
310         
311 }