Połączymy dziś RPi z STM32 przez nRF24 -popularne karty radiowe.

Wykorzystamy dwie biblioteki, na RPi działającą świetnie:
https://tmrh20.github.io/RF24/classRF24.html

Zakładam, że mamy już zainstalowaną bibliotekę (są pliki make do kompilacji gcc). Do modułu nRF24l01 musieliśmy przylutować kondensator 10uF, bo inaczej nie chciał działać (nóżka – do GND, nóżka + do VCC) w celu filtrowania zakłóceń, po stronie STM32 o dziwo nie było to potrzebne (działa tak i tak w podobny sposób).

W obu przypadkach przypinamy nRF24l01 -> SPI1.

Obsługa po stronie RPi, korzystamy z przykładów

1
RF24/examples_linux/interrupts/gettingstarted.cpp

Oraz biblioteki takiego programisty:
https://msalamon.pl/komunikacja-radiowa-z-uzyciem-modulow-nrf24l01-cz-1/

Jest ona jeszcze w stanie niedokończonym (brakuje wielu funkcji z biblioteki RPi), ale działa. Część dopiszemy.

Najtrudniejszą rzeczą jest synchronizacja wszystkich ustawień – autorzy bibliotek zupełnie różnie podeszli do kwestii domyślnych parametrów połączeń, co sprawia sporo problemów: inne są długości adresów (3-5), szybkości połączeń, długości CRC (1B vs 2B), ustawienia różnych rejestrów, w drugiej bibliotece nie ma obsługi payloadów ACK, itp. itd.
Po kilku godzinach wszystko udało się zsynchronizować, strona RPi, zmodyfikowany przykład ww autorów biblioteki:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
TMRh20 2014 - Updated to work with optimized RF24 Arduino library
*/



/**
 * Example for efficient call-response using ack-payloads and interrupts
 *
 * This example continues to make use of all the normal functionality of the radios including
 * the auto-ack and auto-retry features, but allows ack-payloads to be written optionlly as well.
 * This allows very fast call-response communication, with the responding radio never having to
 * switch out of Primary Receiver mode to send back a payload, but having the option to switch to
 * primary transmitter if wanting to initiate communication instead of respond to a commmunication.
 */


#include <cstdlib>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <RF24/RF24.h>

using namespace std;

//
// Hardware configuration
// Configure the appropriate pins for your connections

/****************** Raspberry Pi ***********************/

//RF24 radio(22,0); //GPIO, SPI-BUS
RF24 radio(25,8,BCM2835_SPI_SPEED_8MHZ); //GPIO, SPI-BUS 22,0


/********** User Config *********/
// Assign a unique identifier for this node, 0 or 1. Arduino example uses radioNumber 0 by default.
bool radioNumber = 0;
int interruptPin = 5;
/********************************/


// Radio pipe addresses for the 2 nodes to communicate.
//const uint8_t addresses[][6] = {"1Node","2Node"};
const uint8_t addresses[][4] = {"ooo","nnn"}; //FX: takie ma STM32

volatile bool role_ping_out = 1, role_pong_back = 0, role = 0;
uint8_t counter = 1;                                                          // A single byte to keep track of the data being sent back and forth

volatile bool gotResponse = false;

void intHandler(){

  if ( role == role_pong_back ) {
    uint8_t pipeNo, gotByte;                            // Declare variables for the pipe and the byte received
    if( radio.available(&pipeNo)){                      // Read all available payloads
     radio.read( &gotByte, 1 );
                                                                                                        // Since this is a call-response. Respond directly with an ack payload.
          gotByte += 1;            
          gotByte += 1;                                                                 // Ack payloads are much more efficient than switching to transmit mode to respond to a call
          radio.writeAckPayload(pipeNo,&gotByte, 1 );   // This can be commented out to send empty payloads.
      printf("Loaded next response %d \n\r", gotByte);

    }
  }
}

int main(int argc, char** argv){


  cout << "RPi/RF24/examples/gettingstarted_call_response_int\n";
  radio.begin();
  radio.enableAckPayload();               // Allow optional ack payloads
  radio.enableDynamicAck();

  radio.enableDynamicPayloads();
  radio.setDataRate(RF24_250KBPS);
  radio.setCRCLength(RF24_CRC_8);
  radio.setRetries(150,7); //time and count of retries
  radio.setAddressWidth(3); //zamiast 5 będą stringi 3 znakowe
  radio.setAutoAck(true); //EN_AA rejestr wszędzie 0 false wszędzie 1 true
  radio.printDetails();                   // Dump the configuration of the rf unit for debugging

/********* Role chooser ***********/

  printf("\n ************ Role Setup ***********\n");
  string input = "";
  char myChar = {0};
  cout << "Choose a role: Enter 0 for pong_back, 1 for ping_out (CTRL+C to exit)\n>";
  getline(cin,input);

  if(input.length() == 1) {
        myChar = input[0];
        if(myChar == '0'){
                cout << "Role: Pong Back, awaiting transmission " << endl << endl;
        }else{  cout << "Role: Ping Out, starting transmission " << endl << endl;
                role = role_ping_out;
        }
  }
/***********************************/
  // This opens two pipes for these two nodes to communicate
  // back and forth.
    if ( !radioNumber )    { //tu jest błąd w programie powinno być zależne od  roli??? ^^^
        cerr <<"FX: open pipe: WRITTING=" << addresses[0] << ", READING =" << addresses[1] << "(pipe: 0), channel: 0x"<< hex << (int)radio.getChannel() << "\n";//FX
      radio.openWritingPipe(addresses[0]);
      radio.openReadingPipe(0,addresses[1]);//1,
    }else{
      radio.openWritingPipe(addresses[1]);
      radio.openReadingPipe(0,addresses[0]);//1
    }
        radio.startListening();
        radio.writeAckPayload(0,&counter,1);//1

    radio.maskIRQ(1,1,0); //Mask tx_ok & tx_fail interrupts
    attachInterrupt(interruptPin, INT_EDGE_FALLING, intHandler); //Attach interrupt to bcm pin

// forever loop
while (1){


/****************** Ping Out Role ***************************/

  if (role == role_ping_out){                               // Radio is in ping mode

    uint8_t gotByte;                                        // Initialize a variable for the incoming response

    radio.stopListening();                                  // First, stop listening so we can talk.
    printf("Now sending %d as payload. ",counter);          // Use a simple byte counter as     payload
    unsigned long time = millis();                          // Record the current microsecond count

   uint8_t wb = radio.writeBlocking(&counter,1,1);//1ms writting
   radio.txStandBy(1); //wstrzymannie wysyłania
    if ( wb)//radio.writeBlocking(&counter,1,200))//radio.write(&counter,1,       1) )
                         // Send the counter variable to the other radio
    {
        if(!radio.available()){                             // If nothing in the buffer, we got an ack but it is blank
            printf("Got blank response. round-trip delay: %lu ms\n\r",millis()-time);
counter++;
        }else{

            while(radio.available() ){                      // If an ack with payload was received
                radio.read( &gotByte, 1 );                  // Read it, and display the response time
                printf("Got response %d, round-trip delay: %lu ms\n\r",gotByte,millis()-time);
                counter++;                                  // Increment the counter variable
            }
        }

    }else{        printf("Sending failed.\n\r");
counter++;
 }          // If no ack response, sending failed

    sleep(1);  // Try again later
  }

/****************** Pong Back Role ***************************/



} //while 1
} //main

Teraz biblioteka Pana Michała Salamona. Autor nie umieścił jej w zwięzłej formie do pobrania, na cele moich projektów stworzyłem jeden zwarty plik wyłuskany z przykładów- wystarczy go wrzucić Core/Inc. Można sobie to rozdzielić na 3, ale rzecz w tym, że i tak w ustawieniach trzeba grzebać w pliku .c i będzie on za każdym razem przekompilowany, więc łatwiej mi było trzymać wszystko w jednym pliku:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
 * nRF24_Defs.h
 *
 *  Created on: Apr 26, 2020
 *      Author: Mateusz Salamon
 */


#ifndef INC_NRF24_NRF24_DEFS_H_
#define INC_NRF24_NRF24_DEFS_H_

//
// Registers
//
#define NRF24_CONFIG            0x00
#define NRF24_EN_AA             0x01
#define NRF24_EN_RXADDR 0x02
#define NRF24_SETUP_AW  0x03
#define NRF24_SETUP_RETR        0x04
#define NRF24_RF_CH             0x05
#define NRF24_RF_SETUP  0x06
#define NRF24_STATUS            0x07
#define NRF24_OBSERVE_TX        0x08
#define NRF24_CD                        0x09
#define NRF24_RX_ADDR_P0        0x0A
#define NRF24_RX_ADDR_P1        0x0B
#define NRF24_RX_ADDR_P2        0x0C
#define NRF24_RX_ADDR_P3        0x0D
#define NRF24_RX_ADDR_P4        0x0E
#define NRF24_RX_ADDR_P5        0x0F
#define NRF24_TX_ADDR           0x10
#define NRF24_RX_PW_P0  0x11
#define NRF24_RX_PW_P1  0x12
#define NRF24_RX_PW_P2  0x13
#define NRF24_RX_PW_P3  0x14
#define NRF24_RX_PW_P4  0x15
#define NRF24_RX_PW_P5  0x16
#define NRF24_FIFO_STATUS       0x17
#define NRF24_DYNPD             0x1C
#define NRF24_FEATURE           0x1D

//
// Commands
//
#define NRF24_CMD_R_REGISTER                    0x00
#define NRF24_CMD_W_REGISTER                    0x20
#define NRF24_CMD_R_RX_PAYLOAD          0x61
#define NRF24_CMD_W_TX_PAYLOAD          0xA0
#define NRF24_CMD_FLUSH_TX                      0xE1
#define NRF24_CMD_FLUSH_RX                      0xE2
#define NRF24_CMD_REUSE_TX_PL                   0xE3
#define NRF24_CMD_ACTIVATE                      0x50
#define NRF24_CMD_R_RX_PL_WID                   0x60
#define NRF24_CMD_W_ACK_PAYLOAD         0xA8
#define NRF24_CMD_W_TX_PAYLOAD_NOACK    0xB0
#define NRF24_CMD_NOP                                   0xFF

//
// Bit Mnemonics
//
#define NRF24_MASK_RX_DR  6
#define NRF24_MASK_TX_DS  5
#define NRF24_MASK_MAX_RT 4
#define NRF24_EN_CRC      3
#define NRF24_CRCO        2
#define NRF24_PWR_UP      1
#define NRF24_PRIM_RX     0
#define NRF24_ENAA_P5     5
#define NRF24_ENAA_P4     4
#define NRF24_ENAA_P3     3
#define NRF24_ENAA_P2     2
#define NRF24_ENAA_P1     1
#define NRF24_ENAA_P0     0
#define NRF24_ERX_P5      5
#define NRF24_ERX_P4      4
#define NRF24_ERX_P3      3
#define NRF24_ERX_P2      2
#define NRF24_ERX_P1      1
#define NRF24_ERX_P0      0
#define NRF24_AW          0
#define NRF24_ARD         4
#define NRF24_ARC         0
#define NRF24_PLL_LOCK    4
#define NRF24_RF_DR_HIGH  3
#define NRF24_RF_DR_LOW   5
#define NRF24_RF_PWR      1
#define NRF24_LNA_HCURR   0
#define NRF24_RX_DR       6
#define NRF24_TX_DS       5
#define NRF24_MAX_RT      4
#define NRF24_RX_P_NO     1
#define NRF24_TX_FULL     0
#define NRF24_PLOS_CNT    4
#define NRF24_ARC_CNT     0
#define NRF24_TX_REUSE    6
#define NRF24_FIFO_FULL   5
#define NRF24_TX_EMPTY    4
#define NRF24_RX_FULL     1
#define NRF24_RX_EMPTY    0
#define NRF24_RPD         0x09
#define NRF24_EN_DPL      2

#define NRF24_PAYLOAD_SIZE 1

#define NRF24_ADDR_SIZE 3
//3 ^^ było 5
#define NRF24_CRC_WIDTH_1B   0
#define NRF24_CRC_WIDTH_2B  1

#define NRF24_RF_DR_250KBPS   2
#define NRF24_RF_DR_1MBPS  0
#define NRF24_RF_DR_2MBPS  1

#define NRF24_PA_PWR_M18dBM  0
#define NRF24_PA_PWR_M12dBM 1
#define NRF24_PA_PWR_M6dBM  2
#define NRF24_PA_PWR_0dBM 3

#endif /* INC_NRF24_NRF24_DEFS_H_ */
/////////////////////////////////////////////////////////////////////////////////////////////

/*
 * nRF24.h
 *
 *  Created on: Apr 26, 2020
 *      Author: Mateusz Salamon
 */


#ifndef INC_NRF24_NRF24_H_
#define INC_NRF24_NRF24_H_

#include "main.h"

//
//      Configuration
//

#define NRF24_DYNAMIC_PAYLOAD   1
#define NRF24_INTERRUPT_MODE    1

//
// Enums
//
typedef enum
{
        NRF24_RECEIVED_PACKET,          // 0
        NRF24_NO_RECEIVED_PACKET,       // 1
} nRF24_RX_Status;

typedef enum
{
        NRF24_TRANSMITTED_PACKET,               // 0
        NRF24_NO_TRANSMITTED_PACKET,    // 1
} nRF24_TX_Status;


//
// Init
//

void nRF24_Init(SPI_HandleTypeDef *hspi);

//
// READ/WRITE REGISTERS
//
uint8_t nRF24_ReadConfig(void);
void nRF24_WriteConfig(uint8_t conf);
uint8_t nRF24_ReadStatus();
void nRF24_WriteStatus(uint8_t st);

//
// FIFO Status register
//
uint8_t nRF24_IsTxReuse(void);
uint8_t nRF24_IsTxFull(void);
uint8_t nRF24_IsTxEmpty(void);
uint8_t nRF24_IsRxFull(void);
uint8_t nRF24_IsRxEmpty(void);

//
// SWITCHING BETWEEN RX AND TX
//
void nRF24_RX_Mode(void);
void nRF24_TX_Mode(void);

//
// RADIO SETTINGS
//
void nRF24_SetPALevel(uint8_t lev);
void nRF24_SetDataRate(uint8_t dr);
void nRF24_EnableCRC(uint8_t onoff);
void nRF24_SetCRCLength(uint8_t crcl);
void nRF24_SetRetries(uint8_t ard, uint8_t arc);
void nRF24_SetRFChannel(uint8_t channel);
void nRF24_SetPayloadSize(uint8_t pipe, uint8_t size);
void nRF24_EnablePipe(uint8_t pipe, uint8_t onoff);
void nRF24_AutoACK(uint8_t pipe, uint8_t onoff);
void nRF24_SetRXAddress(uint8_t pipe, uint8_t* address); // Remember to define RX address before TX
void nRF24_SetTXAddress(uint8_t* address);
void nRF24_SetAddressWidth(uint8_t size);
void nRF24_SetPayloadSize(uint8_t pipe, uint8_t size);

//
// INTERRUPT CONTROL
//
void nRF24_ClearInterrupts(void);
void nRF24_EnableRXDataReadyIRQ(uint8_t onoff);
void nRF24_EnableTXDataSentIRQ(uint8_t onoff);
void nRF24_EnableMaxRetransmitIRQ(uint8_t onoff);

//
// PUSH/PULL DATA TO PAYLOAD
//
void nRF24_WriteTXPayload(uint8_t * data, uint8_t size);
void nRF24_WaitTX();
void nRF24_ReadRXPaylaod(uint8_t *data, uint8_t *size);

//
// TRANSMITTING DATA
//
nRF24_TX_Status nRF24_SendPacket(uint8_t* Data, uint8_t Size);
nRF24_RX_Status nRF24_ReceivePacket(uint8_t* Data, uint8_t *Size);

//
// FLUSHING FIFOs
//
void nRF24_FlushRX(void);
void nRF24_FlushTX(void);

//
// Interrupt mode
//
void nRF24_IRQ_Handler(void);

void nRF24_EventRxCallback(void);
void nRF24_EventTxCallback(void);
void nRF24_EventMrCallback(void);

void nRF24_Event(void);

//
// POLLING METHOD
//
uint8_t nRF24_RXAvailible(void);

#endif /* INC_NRF24_NRF24_H_ */




/////////////////////////////////////////////////////////////
/*
 * nRF24.c
 *
 *  Created on: Apr 26, 2020
 *      Author: Mateusz Salamon
 */


#include "main.h"
//#include "spi.h"

//#include "nRF24/nRF24.h"
//#include "nRF24/nRF24_Defs.h"

static SPI_HandleTypeDef *hspi_nrf;

static uint8_t addr_p0_backup[NRF24_ADDR_SIZE];

static uint8_t nrf24_rx_flag, nrf24_tx_flag, nrf24_mr_flag;
static volatile uint8_t Nrf24InterruptFlag;

//
// BASIC READ/WRITE FUNCTIONS
//
// Define these function for your MCU
//

#define NRF24_CSN_HIGH          HAL_GPIO_WritePin(NRF24_CSN_GPIO_Port, NRF24_CSN_Pin, GPIO_PIN_SET)
#define NRF24_CSN_LOW           HAL_GPIO_WritePin(NRF24_CSN_GPIO_Port, NRF24_CSN_Pin, GPIO_PIN_RESET)

#define NRF24_CE_HIGH           HAL_GPIO_WritePin(NRF24_CE_GPIO_Port, NRF24_CE_Pin, GPIO_PIN_SET)
#define NRF24_CE_LOW            HAL_GPIO_WritePin(NRF24_CE_GPIO_Port, NRF24_CE_Pin, GPIO_PIN_RESET)

static void nRF24_Delay_ms(uint8_t Time)
{
        HAL_Delay(Time);
}

static void nRF24_SendSpi(uint8_t *Data, uint8_t Length)
{
        HAL_SPI_Transmit(hspi_nrf, Data, Length, 1000);
}

static void nRF24_ReadSpi(uint8_t *Data, uint8_t Length)
{
        HAL_SPI_Receive(hspi_nrf, Data, Length, 1000);
}

//
// END OF BASIC READ/WRITE FUNCTIONS
//

static uint8_t nRF24_ReadRegister(uint8_t reg)
{
        uint8_t result;

        reg = NRF24_CMD_R_REGISTER | reg;

        NRF24_CSN_LOW;
        nRF24_SendSpi(&reg, 1);
        nRF24_ReadSpi(&result, 1);
        NRF24_CSN_HIGH;

        return result;
}

static void nRF24_ReadRegisters(uint8_t reg, uint8_t* ret, uint8_t len)
{
        reg = NRF24_CMD_R_REGISTER | reg;

        NRF24_CSN_LOW;

        nRF24_SendSpi(&reg, 1);
        nRF24_ReadSpi(ret, len);

        NRF24_CSN_HIGH;
}

static void nRF24_WriteRegister(uint8_t reg, uint8_t val)
{
        uint8_t tmp[2];

        tmp[0] = NRF24_CMD_W_REGISTER | reg;
        tmp[1] = val;

        NRF24_CSN_LOW;

        nRF24_SendSpi(tmp, 2);

        NRF24_CSN_HIGH;
}

static void nRF24_WriteRegisters(uint8_t reg, uint8_t* val, uint8_t len)
{
        reg = NRF24_CMD_W_REGISTER | reg;

        NRF24_CSN_LOW;

        nRF24_SendSpi(&reg, 1);
        nRF24_SendSpi(val, len);

        NRF24_CSN_HIGH;
}

void nRF24_RX_Mode(void)
{
        uint8_t config = nRF24_ReadConfig();
        // Restore pipe 0 adress after comeback from TX mode
        nRF24_SetRXAddress(0, addr_p0_backup);
        // PWR_UP bit set
        config |= (1<<NRF24_PWR_UP);
        // PRIM_RX bit set
        config |= (1<<NRF24_PRIM_RX);
        nRF24_WriteConfig(config);
        // Reset status
        nRF24_WriteStatus((1<<NRF24_RX_DR)|(1<<NRF24_TX_DS)|(1<<NRF24_MAX_RT));
        // Flush RX
        nRF24_FlushRX();
        // Flush TX
        nRF24_FlushTX();

        NRF24_CE_HIGH;
        nRF24_Delay_ms(1);
}

void nRF24_TX_Mode(void)
{
        NRF24_CE_LOW;

        uint8_t config = nRF24_ReadConfig();
        // PWR_UP bit set
        config |= (1<<NRF24_PWR_UP);
        // PRIM_RX bit low
        config &= ~(1<<NRF24_PRIM_RX);
        nRF24_WriteConfig(config);
        // Reset status
        nRF24_WriteStatus((1<<NRF24_RX_DR)|(1<<NRF24_TX_DS)|(1<<NRF24_MAX_RT));
        // Flush RX
        nRF24_FlushRX();
        // Flush TX
        nRF24_FlushTX();

        nRF24_Delay_ms(1);
}



uint8_t nRF24_ReadConfig(void)
{
        return (nRF24_ReadRegister(NRF24_CONFIG));
}

void nRF24_WriteConfig(uint8_t conf)
{
        nRF24_WriteRegister(NRF24_CONFIG, conf);
}

void nRF24_SetPALevel(uint8_t lev)
{
        uint8_t rf_setup = nRF24_ReadRegister(NRF24_RF_SETUP);
        rf_setup &= 0xF8; // Clear PWR bits
        rf_setup |= (lev<<1);
        nRF24_WriteRegister(NRF24_RF_SETUP, rf_setup);
}

void nRF24_SetDataRate(uint8_t dr)
{
        uint8_t rf_setup = nRF24_ReadRegister(NRF24_RF_SETUP);
        rf_setup &= 0xD7; // Clear DR bits (1MBPS)
        if(dr == NRF24_RF_DR_250KBPS)
                rf_setup |= (1<<NRF24_RF_DR_LOW);
        else if(dr == NRF24_RF_DR_2MBPS)
                rf_setup |= (1<<NRF24_RF_DR_HIGH);
        nRF24_WriteRegister(NRF24_RF_SETUP, rf_setup);
}

uint8_t nRF24_ReadStatus(void)
{
        return (nRF24_ReadRegister(NRF24_STATUS));
}

void nRF24_WriteStatus(uint8_t st)
{
        nRF24_WriteRegister(NRF24_STATUS, st);
}

//
// FIFO Status
//

uint8_t nRF24_ReadFifoStatus(void)
{
        return (nRF24_ReadRegister(NRF24_FIFO_STATUS));
}

void nRF24_WriteFifoStatus(uint8_t st)
{
        nRF24_WriteRegister(NRF24_FIFO_STATUS, st);
}

uint8_t nRF24_IsBitSetInFifoStatus(uint8_t Bit)
{
        uint8_t FifoStatus;

        FifoStatus = nRF24_ReadFifoStatus();

        if(FifoStatus & (1<<Bit))
        {
                return 1;
        }

        return 0;
}

uint8_t nRF24_IsTxReuse(void)
{
        return nRF24_IsBitSetInFifoStatus(NRF24_TX_REUSE);
}

uint8_t nRF24_IsTxFull(void)
{
        return nRF24_IsBitSetInFifoStatus(NRF24_TX_FULL);
}

uint8_t nRF24_IsTxEmpty(void)
{
        return nRF24_IsBitSetInFifoStatus(NRF24_TX_EMPTY);
}

uint8_t nRF24_IsRxFull(void)
{
        return nRF24_IsBitSetInFifoStatus(NRF24_RX_FULL);
}

uint8_t nRF24_IsRxEmpty(void)
{
        return nRF24_IsBitSetInFifoStatus(NRF24_RX_EMPTY);
}

void nRF24_FlushRX(void)
{
        uint8_t command = NRF24_CMD_FLUSH_RX;

        NRF24_CSN_LOW;
        nRF24_SendSpi(&command, 1);
        NRF24_CSN_HIGH;
}

void nRF24_FlushTX(void)
{
        uint8_t command = NRF24_CMD_FLUSH_TX;

        NRF24_CSN_LOW;
        nRF24_SendSpi(&command, 1);
        NRF24_CSN_HIGH;
}

void nRF24_EnableCRC(uint8_t onoff)
{
        uint8_t config = nRF24_ReadConfig();

        if(onoff)
                config |= (1<<NRF24_EN_CRC);
        else
                config &= ~(1<<NRF24_EN_CRC);
        nRF24_WriteConfig(config);
}

void nRF24_SetCRCLength(uint8_t crcl)
{
        uint8_t config = nRF24_ReadConfig();
        if(crcl == NRF24_CRC_WIDTH_2B)
                config |= (1<<NRF24_CRCO);
        else
                config &= ~(1<<NRF24_CRCO);
        nRF24_WriteConfig(config);
}

void nRF24_SetRetries(uint8_t ard, uint8_t arc)
{ //ard = delay   arc = count (ilość i odległość czasowa
        // ard * 250us, arc repeats
        nRF24_WriteRegister(NRF24_SETUP_RETR, (((ard & 0x0F)<<NRF24_ARD) | ((arc & 0x0F)<<NRF24_ARC)));
}

void nRF24_SetRFChannel(uint8_t channel)
{
        nRF24_WriteRegister(NRF24_RF_CH, (channel & 0x7F));
}

void nRF24_SetPayloadSize(uint8_t pipe, uint8_t size)
{
        if(pipe > 5)
                pipe = 5; // Block too high pipe number
        nRF24_WriteRegister(NRF24_RX_PW_P0 + pipe , (size & 0x3F));
}

void nRF24_EnablePipe(uint8_t pipe, uint8_t onoff)
{
        if(pipe > 5)
                pipe = 5; // Block too high pipe number
        uint8_t enable_pipe = nRF24_ReadRegister(NRF24_EN_RXADDR);
        if(onoff == 1)
                enable_pipe |= (1<<pipe);
        else
                enable_pipe &= ~(1<<pipe);
        nRF24_WriteRegister(NRF24_EN_RXADDR, enable_pipe);
}

void nRF24_AutoACK(uint8_t pipe, uint8_t onoff)
{
        if(pipe > 5)
                pipe = 5; // Block too high pipe number
        uint8_t enaa = nRF24_ReadRegister(NRF24_EN_AA);
        if(onoff == 1)
                enaa |= (1<<pipe);
        else
                enaa &= ~(1<<pipe);
        nRF24_WriteRegister(NRF24_EN_AA, enaa);
}

void nRF24_SetAddressWidth(uint8_t size)
{
        if(size > 5)
                size = 5; // Maximum are 5 bytes
        if(size < 3)
                size = 3; // Minimum are 3 bytes
        nRF24_WriteRegister(NRF24_SETUP_AW, ((size-2) & 0x03));
}

void nRF24_SetRXAddress(uint8_t pipe, uint8_t* address)
{
        // pipe 0 and pipe 1 are fully 40-bits storaged
        // pipe 2-5 is storaged only with last byte. Rest are as same as pipe 1
        // pipe 0 and 1 are LSByte first so they are needed to reverse address
        if((pipe == 0) || (pipe == 1))
        {
                uint8_t i;
                uint8_t address_rev[NRF24_ADDR_SIZE];
                for(i = 0; i<NRF24_ADDR_SIZE; i++)
                        address_rev[NRF24_ADDR_SIZE - 1 - i] = address[i];
                nRF24_WriteRegisters(NRF24_RX_ADDR_P0 + pipe, address_rev, NRF24_ADDR_SIZE);
        }
        else
                nRF24_WriteRegister(NRF24_RX_ADDR_P0 + pipe, address[NRF24_ADDR_SIZE-1]);
}

void nRF24_SetTXAddress(uint8_t* address)
{
        // TX address is storaged similar to RX pipe 0 - LSByte first
        uint8_t i;
        uint8_t address_rev[NRF24_ADDR_SIZE];

        nRF24_ReadRegisters(NRF24_RX_ADDR_P0, address_rev, NRF24_ADDR_SIZE); // Backup P0 address
        for(i = 0; i<NRF24_ADDR_SIZE; i++)
                addr_p0_backup[NRF24_ADDR_SIZE - 1 - i] = address_rev[i]; //Reverse P0 address

        for(i = 0; i<NRF24_ADDR_SIZE; i++)
                address_rev[NRF24_ADDR_SIZE - 1 - i] = address[i];
        //make pipe 0 address backup;

        nRF24_WriteRegisters(NRF24_RX_ADDR_P0, address_rev, NRF24_ADDR_SIZE); // Pipe 0 must be same for auto ACk
        nRF24_WriteRegisters(NRF24_TX_ADDR, address_rev, NRF24_ADDR_SIZE);

}

void nRF24_ClearInterrupts(void)
{
        uint8_t status = nRF24_ReadStatus();
        status |= (7<<4); // Clear bits 4, 5, 6.
        nRF24_WriteStatus(status);
}

uint8_t nRF24_GetDynamicPayloadSize(void)
{
    uint8_t result = 0;

    result = nRF24_ReadRegister(NRF24_CMD_R_RX_PL_WID);

    if (result > 32) // Something went wrong :)
    {
        nRF24_FlushRX();
        nRF24_Delay_ms(2);
        return 0;
    }
    return result;
}

void nRF24_EnableRXDataReadyIRQ(uint8_t onoff)
{
        uint8_t config = nRF24_ReadConfig();

        if(!onoff)
                config |= (1<<NRF24_RX_DR);
        else
                config &= ~(1<<NRF24_RX_DR);

        nRF24_WriteConfig(config);
}

void nRF24_EnableTXDataSentIRQ(uint8_t onoff)
{
        uint8_t config = nRF24_ReadConfig();

        if(!onoff)
                config |= (1<<NRF24_TX_DS);
        else
                config &= ~(1<<NRF24_TX_DS);

        nRF24_WriteConfig(config);
}

void nRF24_EnableMaxRetransmitIRQ(uint8_t onoff)
{
        uint8_t config = nRF24_ReadConfig();

        if(!onoff)
                config |= (1<<NRF24_MAX_RT);
        else
                config &= ~(1<<NRF24_MAX_RT);

        nRF24_WriteConfig(config);
}

void nRF24_WriteTXPayload(uint8_t * data, uint8_t size)
{
#if (NRF24_DYNAMIC_PAYLOAD == 1)
        nRF24_WriteRegisters(NRF24_CMD_W_TX_PAYLOAD, data, size);
#else
        nRF24_WriteRegisters(NRF24_CMD_W_TX_PAYLOAD, data, NRF24_PAYLOAD_SIZE);
#endif
}

void nRF24_WaitTX()
{
        uint8_t status;
        NRF24_CE_HIGH;
        nRF24_Delay_ms(1);
        NRF24_CE_LOW;
        do
        {
                nRF24_Delay_ms(1);
                status = nRF24_ReadStatus();
        }while(!((status & (1<<NRF24_MAX_RT)) || (status & (1<<NRF24_TX_DS))));

}

void nRF24_ReadRXPaylaod(uint8_t *data, uint8_t *size)
{
#if (NRF24_DYNAMIC_PAYLOAD == 1)
        *size = nRF24_GetDynamicPayloadSize();
        nRF24_ReadRegisters(NRF24_CMD_R_RX_PAYLOAD, data, *size);
#else
        nRF24_ReadRegisters(NRF24_CMD_R_RX_PAYLOAD, data, NRF24_PAYLOAD_SIZE);
#endif
#if (NRF24_INTERRUPT_MODE == 0)
        nRF24_WriteRegister(NRF24_STATUS, (1<NRF24_RX_DR));
        if(nRF24_ReadStatus() & (1<<NRF24_TX_DS))
                nRF24_WriteRegister(NRF24_STATUS, (1<<NRF24_TX_DS));
#endif
}

nRF24_TX_Status nRF24_SendPacket(uint8_t* Data, uint8_t Size)
{
        if(Size > 32)
                return NRF24_NO_TRANSMITTED_PACKET;

        nRF24_WriteTXPayload(Data, Size);
        nRF24_WaitTX();

        return NRF24_TRANSMITTED_PACKET;
}

nRF24_RX_Status nRF24_ReceivePacket(uint8_t* Data, uint8_t *Size)
{
#if (NRF24_INTERRUPT_MODE == 0)
        if(nRF24_RXAvailible())
        {
#endif
                nRF24_ReadRXPaylaod(Data, Size);
#if (NRF24_INTERRUPT_MODE == 0)
                return NRF24_RECEIVED_PACKET;
        }
        return NRF24_NO_RECEIVED_PACKET;
#endif
        return NRF24_RECEIVED_PACKET; //FX??
}

uint8_t nRF24_RXAvailible(void)
{
        uint8_t status = nRF24_ReadStatus();

        // RX FIFO Interrupt
        if ((status & (1 << 6)))
        {
                nrf24_rx_flag = 1;
                status |= (1<<6); // Interrupt flag clear
                nRF24_WriteStatus(status);
                return 1;
        }
        return 0;
}

void nRF24_IRQ_Handler(void)
{

        Nrf24InterruptFlag = 1;
}

void nRF24_IRQ_Read(void)
{
        if(Nrf24InterruptFlag == 1)
        {
                Nrf24InterruptFlag = 0;

                uint8_t status = nRF24_ReadStatus();
                uint8_t ClearIrq = 0;
                // RX FIFO Interrupt
                if ((status & (1 << NRF24_RX_DR)))
                {
                        nrf24_rx_flag = 1;
                        ClearIrq |= (1<<NRF24_RX_DR); // Interrupt flag clear
                }
                // TX Data Sent interrupt
                if ((status & (1 << NRF24_TX_DS)))
                {
                        nrf24_tx_flag = 1;
                        ClearIrq |= (1<<NRF24_TX_DS); // Interrupt flag clear
                }
                // Max Retransmits interrupt
                if ((status & (1 << NRF24_MAX_RT)))
                {
                        nrf24_mr_flag = 1;
                        ClearIrq |= (1<<NRF24_MAX_RT); // Interrupt flag clear
                }

                nRF24_WriteStatus(ClearIrq);
        }
}

//
// nRF24 Event for Interrupt mode
//

__weak void nRF24_EventRxCallback(void)
{

}

__weak void nRF24_EventTxCallback(void)
{

}

__weak void nRF24_EventMrCallback(void)
{

}

void nRF24_Event(void)
{
        nRF24_IRQ_Read(); // Check if there was any interrupt

        if(nrf24_rx_flag)
        {
                nRF24_EventRxCallback();
                nrf24_rx_flag = 0;
        }

        if(nrf24_tx_flag)
        {
                nRF24_EventTxCallback();
                nrf24_tx_flag = 0;
        }

        if(nrf24_mr_flag)
        {
                nRF24_EventMrCallback();
                nrf24_mr_flag = 0;
        }
}

void nRF24_Init(SPI_HandleTypeDef *hspi)
{
        hspi_nrf = hspi;

        NRF24_CE_LOW;
        NRF24_CSN_HIGH;

        nRF24_Delay_ms(5); // Wait for radio power up

        nRF24_SetPALevel( NRF24_PA_PWR_0dBM); // Radio power NRF24_PA_PWR_0dBM
        nRF24_SetDataRate(NRF24_RF_DR_250KBPS); // Data Rate
        nRF24_EnableCRC(1); // Enable CRC
        nRF24_SetCRCLength(NRF24_CRC_WIDTH_1B);//1==NRF24_CRC_WIDTH_2B); // CRC Length 1 byte _1B
//      nRF24_SetRetries(0x04, 0x07); // 1000us, 7 times
        nRF24_SetRetries(5, 15); // 1000us, 7 times //5,15 lub 4,7

#if (NRF24_DYNAMIC_PAYLOAD == 1)
        nRF24_WriteRegister(NRF24_FEATURE, nRF24_ReadRegister(NRF24_FEATURE) | (1<<NRF24_EN_DPL)); // Enable dynamic payload feature
        nRF24_WriteRegister(NRF24_DYNPD, 0x3F); // Enable dynamic payloads for all pipes
#else
        nRF24_WriteRegister(NRF24_DYNPD, 0); // Disable dynamic payloads for all pipes
        nRF24_SetPayloadSize(0, NRF24_PAYLOAD_SIZE); // Set 32 bytes payload for pipe 0
#endif
        nRF24_SetRFChannel(0x4c); // Set RF channel for transmission 10
        nRF24_EnablePipe(0, 1); // Enable pipe 0
        nRF24_SetAddressWidth(NRF24_ADDR_SIZE); // Set address size
        nRF24_AutoACK(0, 1); // Enable auto ACK for pipe 0

        nRF24_WriteRegister(NRF24_RF_SETUP,0x5); //skopiowane z RPi ustawienia rejestrów
        nRF24_WriteRegister(NRF24_FEATURE,0x6); //skopiowane z RPi ustawienia rejestrów (ważne, aby były te same)
        nRF24_SetDataRate(NRF24_RF_DR_250KBPS); // Data Rate
    nRF24_SetAddressWidth(3); //adres 3 znaki a nie 5

        nRF24_Delay_ms(1);

        nRF24_EnableRXDataReadyIRQ(1); //receiver irq
        nRF24_EnableTXDataSentIRQ(0);
        nRF24_EnableMaxRetransmitIRQ(0);

        nRF24_Delay_ms(1);

        nRF24_ClearInterrupts();
}

Teraz main.c – istotne dodane fragmenty kodu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* USER CODE BEGIN PFP */
//printf mój:
char buf_printf[100]; //max printf length
#define printfx(f_, ...) snprintf(buf_printf, 100, (f_), ##__VA_ARGS__); \
HAL_UART_Transmit(&huart3, (uint8_t*)buf_printf, strlen(buf_printf), 1000);


////////

void nRF24_PrintDetails();
void nRF24_WriteAckPayload(uint8_t, const void *, uint8_t);
uint8_t nRF24_Available(uint8_t*);

/* USER CODE END PFP */
// .....................

  /* USER CODE BEGIN 2 */

 nRF24_Init(&hspi1); //init nRF24 na SPI1 w tym INIT ustawić parametry transmisji!

  //PIPE:
   nRF24_SetRXAddress(0,(uint8_t *)"ooo"); //Pipe 1
   nRF24_SetTXAddress((uint8_t *)"nnn");
   nRF24_RX_Mode();
  /* USER CODE END 2 */

//..........................

  /* USER CODE BEGIN 3 */

  if(nRF24_Available(&pipeNo))
             {
               nRF24_ReadRXPaylaod(Nrf24_Message, messageSize); //wczytuje do tablicy odp
              // nRF24_WriteRegister()
               nRF24_WriteAckPayload(pipeNo, &ackPayloadData, 1);//pipe,dane,ile bajtów

               printfx("Message: %i pipe:%i\n\r", Nrf24_Message[0], pipeNo);
              // HAL_UART_Transmit(&huart2, Message, MessageLength, 1000);
             }

 /* USER CODE END 3 */
.........................

/* USER CODE BEGIN 4*/
// Dopisałem kilka funkcji do libki, wrzuciłem tu (jako że autor inny)
////////////
// NRF24l01
////////////


void nRF24_PrintAddressRegister(uint8_t ileRejestrow)
{ //wypisuje rejestru z adresami transmisji (3, 4, lub 5 znaków, teraz 3)
        printfx("register P0-P%i:", ileRejestrow);
          uint8_t buffer[NRF24_ADDR_SIZE]; //addr_width
          uint8_t reg =  NRF24_RX_ADDR_P0;
          for(int i=0; i<=ileRejestrow; i++)
          {
            nRF24_ReadRegisters(reg++,buffer,sizeof buffer);
            printfx(" 0x");
            uint8_t* bufptr = buffer + sizeof buffer;
            while( --bufptr >= buffer )
            {
                printfx("%02x",*bufptr);
            }
            printfx("   ");
          }
          printfx(" TX_ADDR: ");
          reg =  NRF24_TX_ADDR; //TX

                    nRF24_ReadRegisters(reg++,buffer,sizeof buffer);
                    printfx(" 0x");
                    uint8_t* bufptr = buffer + sizeof buffer;
                    while( --bufptr >= buffer )
                    {
                        printfx("%02x",*bufptr);
                    }
                    printfx("   ");

          printfx("\r\n");
}

void nRF24_PrintByteRegisterX(const char *text, uint8_t reg, uint8_t count)
{//wypisuje kilka rejestrów (count)
        printfx("%s \t", text);
        while (count--)
        {
                printfx(" 0x%02x", nRF24_ReadRegister(reg));
        }
        printfx("\r\n");
}

void nRF24_PrintByteRegister(const char *text, uint8_t reg)
{//wypisz jednobajtowy rejestr
        printfx("%s  ", text);
                printfx(" 0x%02x", nRF24_ReadRegister(reg));
        printfx("\r\n");
}


typedef enum { RF24_CRC_DISABLED = 0, RF24_CRC_8, RF24_CRC_16 } rf24_crclength_e;
#define _BV(x) (1<<(x))
#define CRCO 2
#define EN_CRC 3

void RF24_PrintCRCLength(void)
{ //wypisz ustawienie CRC Length (bity parzystości)
//rf24_crclength_e result = RF24_CRC_DISABLED;

 uint8_t config = nRF24_ReadRegister(NRF24_CONFIG) & ( _BV(CRCO) | _BV(NRF24_EN_CRC));
 uint8_t AA = nRF24_ReadRegister(NRF24_EN_AA);

 if ( config & _BV(EN_CRC ) || AA)
 {
   if ( config & _BV(CRCO) )
   {
           printfx("CRC: 16 bits\r\n");
   }
   else
   {
           printfx("CRC: 8 bits\r\n");
   }
 }
 else
 {
         printfx("CRC disabled\r\n");
 }
}


void nRF24_PrintDetails()
{
        printfx("nRF24 settings:\r\n");
        nRF24_PrintAddressRegister(1); //RX_ADDR_P0-1 , RX_ADDR_P0, 2 // 2 rej od RX_ADDR_P0
    nRF24_PrintByteRegisterX("RX_PW_P0-6\t", NRF24_RX_PW_P0, 6);

    nRF24_PrintByteRegister("(bit 0-5 - pipes# AckPayload) EN_AA\t", NRF24_EN_AA);
    nRF24_PrintByteRegister("(channel) RF_CH\t", NRF24_RF_CH);
    nRF24_PrintByteRegister("(speed, power, LNA) RF_SETUP\t", NRF24_RF_SETUP);
    nRF24_PrintByteRegister("(irq) RF_CONFIG\t", NRF24_CONFIG );
    nRF24_PrintByteRegister("DYNAMIC PAYLOAD\t", NRF24_DYNPD );
    nRF24_PrintByteRegister("(dynamic payload,ack)FEATURE\t", NRF24_FEATURE);
    nRF24_PrintByteRegister("SETUP_AW (3=5 chars pipe name,1=3pipe chars): ", NRF24_SETUP_AW);
    RF24_PrintCRCLength();
}

///////////////////////


#define RET_SIZE 1
uint8_t nRF24_GetStatus(void)
{
        uint8_t reg = NRF24_CMD_NOP,
                        ret[RET_SIZE];//return z SPI

        NRF24_CSN_LOW; //pin z linią wysyłania

        //status = transfer(RF24_NOP)

        nRF24_SendSpi(&reg, 1);
        nRF24_ReadSpi(ret, RET_SIZE);

        NRF24_CSN_HIGH; //pin z linią wysyłania

                        return ret[0];
}


uint8_t nRF24_Available(uint8_t* pipe_num)
{ //bool for pipe
  if (!( nRF24_ReadRegister(NRF24_FIFO_STATUS) & _BV(NRF24_RX_EMPTY) )){

    // If the caller wants the pipe number, include that
    if ( pipe_num ){
      uint8_t status = nRF24_GetStatus();
      *pipe_num = ( status >> NRF24_RX_P_NO ) & 0x07;
        }
        return 1;
  }

  return 0;


}

////////////////////////

//#define W_ACK_PAYLOAD 0xA8
//NRF24_CMD_W_ACK_PAYLOAD
void nRF24_WriteAckPayload(uint8_t pipe, const void *buf, uint8_t len)
{ //wysyła odpowiedź na pakiet, under construction

        uint8_t* current = (uint8_t *)buf; //wskaźnik na wysyłany aktualnie bajt
        uint8_t data_len = len>32 ? 32 : len; //max 32 bajty w 1 pakiecie
        uint8_t writeAckPayloadCommand = NRF24_CMD_W_ACK_PAYLOAD | (pipe & 0x07); //CMD z przodu a trzy ostatnie bity to nr rury
        uint8_t bufferToSend[33];
        bufferToSend[0] = writeAckPayloadCommand;
        for(int i=0; i<data_len; i++)
        {
                bufferToSend[i+1] = current[i];
        }
        NRF24_CSN_LOW; //pin z linią wysyłania

        nRF24_SendSpi(bufferToSend, data_len+1); //max 32 bajty PAYLOAD

        NRF24_CSN_HIGH; //wyłączamy wysyłanie SPI
}

/* USER CODE END 4 */