license: update source file and licensing files
[libsi24] / libsi24.c
index ba6dee3..f4d6da5 100644 (file)
--- a/libsi24.c
+++ b/libsi24.c
@@ -1,11 +1,20 @@
-/**
- * File              : libsi24.c
- * Author            : Robin Krens <robin@robinkrens.nl>
- * Date              : 18.01.2023
- * Last Modified Date: 22.01.2023
- * Last Modified By  : Robin Krens <robin@robinkrens.nl>
+/* Copyright (C) 
+ * 2023 - Robin Krens
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * 
  */
-
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdint.h>
@@ -87,10 +96,6 @@ static int _config(si24_t * si)
        }
 
        if (params->enable_ack) {
-               uint8_t dyn = (1 << DPL_P0);
-               ret += _reg_write(si, SI24_REG_DYNPD, &dyn, 1);
-               feature_reg |= (1 << EN_DPL);
-               ret += _reg_write(si, SI24_REG_FEATURE, &feature_reg, 1);
                if (params->mode == SEND_MODE) {
                        setup_retr_reg = ARD(params->timeout) | ARC(params->retries);
                        ret += _reg_write(si, SI24_REG_SETUP_RETR, &setup_retr_reg, 1);
@@ -102,6 +107,17 @@ static int _config(si24_t * si)
                }
        }
 
+       if (params->enable_dynpd) {
+               uint8_t dyn = (1 << DPL_P0);
+               ret += _reg_write(si, SI24_REG_DYNPD, &dyn, 1);
+               feature_reg |= (1 << EN_DPL);
+               ret += _reg_write(si, SI24_REG_FEATURE, &feature_reg, 1);
+       } else { /* fixed payload size */
+               if (params->mode == RECV_MODE) {
+                       ret += _reg_write(si, SI24_REG_RX_PW_P0, (uint8_t *) &params->payload, 1);
+               }
+       }
+
        uint8_t aw = AW_5;
        ret += _reg_write(si, SI24_REG_SETUP_AW, &aw, 1); 
 
@@ -114,7 +130,6 @@ static int _config(si24_t * si)
                uint8_t ch = 0x1;
                ret += _reg_write(si, SI24_REG_EN_RXADDR, &ch, 1); 
                ret += _reg_write(si, SI24_REG_RX_ADDR_P0, params->mac_addr, sizeof(params->mac_addr));
-               ret += _reg_write(si, SI24_REG_RX_PW_P0, (uint8_t *) &params->payload, 1);
        } else {
                ret += _reg_write(si, SI24_REG_TX_ADDR, params->mac_addr, sizeof(params->mac_addr));
        }
@@ -174,8 +189,13 @@ size_t si24_send(si24_t* si, const unsigned char * buf, size_t size)
                return -1;
        }
 
-       for (size_t idx = 0; idx < size; idx += si->opts->payload) {
-               sz = (size - idx) < si->opts->payload ? (size - idx) : si->opts->payload;  
+       int payload = si->opts->payload;
+
+       if (si->opts->enable_dynpd)
+               payload = size > 32 ? 32 : size;
+
+       for (size_t idx = 0; idx < size; idx += payload) {
+               sz = (size - idx) < payload ? (size - idx) : payload;  
                if (si->opts->enable_ack) {
                        _reg_write(si, SI24_W_TX_PAYLOAD, buf + idx, sz);
                        si->ctl->chip_enable(1);
@@ -247,6 +267,11 @@ size_t si24_recv(si24_t* si, unsigned char * buf, size_t size)
        while(!(fifo_flags & (1 << RX_EMPTY)) &&
                        bytes_read < size) {
                
+               if (si->opts->enable_dynpd) {
+                       uint8_t d_sz = 0;
+                       _reg_read(si, SI24_RX_PL_WID, &d_sz, 1);
+                       p_size = d_sz;
+               }
                int m_size = (size - bytes_read) > p_size ? p_size : (size - bytes_read);
                _reg_read(si, SI24_R_RX_PAYLOAD, tmpbuf, m_size);