1 | // Initialises nRF24L01+ registers with default values and puts it into power down state.
|
2 | // Note: this function can also be used to reset the nRF24L01+.
|
3 | // Parameters:
|
4 | // mode lib_nRF24L01_TXmode | lib_nRF24L01_RXmode
|
5 | // groupID the middle byte of the address of the nRF24L01+. This lib uses this byte as common address part of a multiceiver-group.
|
6 | // deviceID the LSByte of the address of the nRF24L01+. This lib uses default values from 1...6 for the transmitters of a multiceiver-group.
|
7 | // rf_channel RF frequency channel to use: F(RF) = 2400 + x [MHz], where x = 0..125
|
8 | void lib_nRF24L01_init_nRF24L01( uint8_t mode, uint8_t groupID, uint8_t deviceID, uint8_t rf_channel)
|
9 | {
|
10 | // the nRF24L01+ power on reset procedure needs 100ms after operating voltage is supplied
|
11 | if ( ! (currentState & STATE_BITMASK_INIT ) )
|
12 | _delay_us( nRF24L01_DELAY_POWERON_RESET);
|
13 |
|
14 | if ( (currentState & STATE_BITMASK_PWR_UP) )
|
15 | {
|
16 | // flush all data pipes, reset all interrupt flags (IRQ-pin), then go down
|
17 | lib_nRF24L01_flush_TX_FIFO();
|
18 | lib_nRF24L01_flush_RX_FIFO();
|
19 | lib_nRF24L01_clear_IRQ( nRF_REGBITMASK_STATUS_TX_DS);
|
20 | lib_nRF24L01_clear_IRQ( nRF_REGBITMASK_STATUS_RX_DR);
|
21 | lib_nRF24L01_clear_IRQ( nRF_REGBITMASK_STATUS_MAX_RT);
|
22 | lib_nRF24L01_power_down();
|
23 | }
|
24 |
|
25 | // Set RF channel
|
26 | lib_nRF24L01_set_regbyte( nRF_REG_RF_CH, rf_channel);
|
27 | // Set data speed & Output Power as preconfigured in lib_nRF24L01_RF_SETUP (set in lib_nRF24L01.h)
|
28 | lib_nRF24L01_set_regbyte( nRF_REG_RF_SETUP, lib_nRF24L01_RF_SETUP);
|
29 |
|
30 | // Set Device Address Width
|
31 | lib_nRF24L01_set_regbyte( nRF_REG_SETUP_AW, nRF_REGBITVAL_SETUP_AW_3BYTES);
|
32 |
|
33 | // Set device addresses for a "multiceiver group" (a star topology consisting of 1 receiver and up to 6 transmitters) with auto acknowledge
|
34 | if ( mode == lib_nRF24L01_TXmode )
|
35 | {
|
36 | // Set Device Address (which identifies the transmitter)
|
37 | lib_nRF24L01_set_tx_addr( groupID, deviceID);
|
38 | // Enable only RX data pipe 0 for receiving data (to receive acknowledge-packets from a listening receiver)
|
39 | lib_nRF24L01_set_regbyte( nRF_REG_EN_RXADDR, nRF_REGBITMASK_EN_RXADDR_ERX_P0);
|
40 | // addresses to listen to
|
41 | lib_nRF24L01_set_rx_addr( 0, groupID, deviceID); // Note: when using auto acknowledgement, RX pipe 0 of a nRF24L01+ in TX-mode has to be set to the own device address
|
42 | // just for tidiness: write unused (§ thats just my personal convention!) addresses to all unsed data pipes
|
43 | lib_nRF24L01_set_rx_addr( 1, 0xFF, 0xFF);
|
44 | lib_nRF24L01_set_rx_addr( 2, 0xFF, 0xFF);
|
45 | lib_nRF24L01_set_rx_addr( 3, 0xFF, 0xFF);
|
46 | lib_nRF24L01_set_rx_addr( 4, 0xFF, 0xFF);
|
47 | lib_nRF24L01_set_rx_addr( 5, 0xFF, 0xFF);
|
48 | }
|
49 | else // i.e. mode == lib_nRF24L01_RXmode (multiceiver)
|
50 | {
|
51 | // In order to avoid address conflicts which could occur when the PRX is switched to TX mode on the fly,
|
52 | // the Device Address is set even in RX-mode
|
53 | lib_nRF24L01_set_tx_addr( groupID, deviceID > 6 ? deviceID : 0); // ensure the deviceID does not collide with one of the transmitters of the same group
|
54 | // Enable all RX data pipe addresses for receiving data
|
55 | lib_nRF24L01_set_regbyte( nRF_REG_EN_RXADDR, nRF_REGBITMASK_EN_RXADDR_ERX_ALL);
|
56 | // addresses to listen to
|
57 | lib_nRF24L01_set_rx_addr( 0, groupID, 1);
|
58 | lib_nRF24L01_set_rx_addr( 1, groupID, 2);
|
59 | lib_nRF24L01_set_rx_addr( 2, groupID, 3);
|
60 | lib_nRF24L01_set_rx_addr( 3, groupID, 4);
|
61 | lib_nRF24L01_set_rx_addr( 4, groupID, 5);
|
62 | lib_nRF24L01_set_rx_addr( 5, groupID, 6);
|
63 | }
|
64 |
|
65 | // Enable the nice "Enhanced ShockBurst" features:
|
66 | // ... Automatic Retransmission
|
67 | // Note 1: use nRF_REG_SETUP_RETR ARD setting faster than 500us with caution: read note (d) at the end of the Register Map Table (page 60 of manual)!
|
68 | // Note 2: § in order to avoid repeated transmission collisions, ensure different values for each device working on the same RF-channel
|
69 | if ( mode == lib_nRF24L01_TXmode )
|
70 | {
|
71 | uint8_t delay = (0x10 * (1+deviceID)) & nRF_REGBITMASK_SETUP_RETR_ARD; // see nRF_REGVAL_SETUP_RETR_ARD_*-constants
|
72 | lib_nRF24L01_set_regbyte( nRF_REG_SETUP_RETR, (delay | nRF_REGVAL_SETUP_RETR_ARC_15) );
|
73 | }
|
74 | else
|
75 | {
|
76 | lib_nRF24L01_set_regbyte( nRF_REG_SETUP_RETR, (nRF_REGVAL_SETUP_RETR_ARD_500 | nRF_REGVAL_SETUP_RETR_ARC_15) );
|
77 | }
|
78 | // ... Dynamic Payload Length as well as Automatic Acknowledgement Payloads
|
79 | lib_nRF24L01_set_regbyte( nRF_REG_FEATURE, nRF_REGBITMASK_FEATURE_EN_DPL // enables Dynamic Payload Length
|
80 | | nRF_REGBITMASK_FEATURE_EN_DYN_ACK // enables use of nRF_CMD_W_TX_PAYLOAD_NOACK command
|
81 | | nRF_REGBITMASK_FEATURE_EN_ACK_PAY // enables "Acknowledge Payloads"
|
82 | );
|
83 | // ... enable Automatic Acknowledgement for all data pipes
|
84 | lib_nRF24L01_set_regbyte( nRF_REG_EN_AA, nRF_REGBITMASK_EN_AA_ENAA_ALL);
|
85 | // ... enable Dynamic Payload Length for all data pipes
|
86 | lib_nRF24L01_set_regbyte( nRF_REG_DYNPD, nRF_REGBITMASK_DYNPD_ALL);
|
87 |
|
88 | currentState = STATE_BITMASK_INIT; // initialised, powered down
|
89 | }
|