-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMirf.h
206 lines (166 loc) · 5.77 KB
/
Mirf.h
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
#ifndef _MIRF_H_
#define _MIRF_H_
#include <stdint.h>
#include <avr/io.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#include "spilib.h"
#include "packet_defs.h"
#include "Mirf_nRF24L01.h"
// Nrf24l settings
#define mirf_ADDR_LEN 3 //5
const char mirf_ADDR[] = "honza";
#define ADDR_TYPE uint8_t
#define MULTICAST_ADDR 0xFF //for uint16 0xffff;
#define DEFAULT_RF_CHANNEL 1
#define MAX_RX_PACKET_QUEUE 6
#define MAX_TX_PACKET_QUEUE 1
#define MAX_ACK_PACKET_QUEUE 6
#define MAX_TX_ATTEMPTS 4
#define MAX_ACK_WAIT_TIME 2 //25
#define NOP_ASM __asm__("nop\n\t");
typedef struct mirfPacket{
ADDR_TYPE volatile txAddr;
ADDR_TYPE volatile rxAddr;
//ADDR_TYPE origAddr;
uint8_t volatile type;
uint8_t volatile counter;
uint8_t volatile payload[7];
//mirfPacket& operator = (volatile mirfPacket& a) { return *this; }
} mirfPacket;
#define NRF_PAYLOAD_SIZE sizeof(mirfPacket)
#define CE_PIN PB1
#define CSN_PIN PB2
#define CE_CSN_PORT PORTB
#define CE_CSN_DDR DDRB
/*
#define ceHi() PORTB |= (1<<1)
#define ceLow() PORTB &= (~(1<<1))
#define csnHi() PORTB |= (1<<2)
#define csnLow() PORTB &= (~(1<<2))
*/
//==========================================================================
class Nrf24l {
public:
Nrf24l();
void init();
void config();
//void send(uint8_t *value);
void setRfChannel(uint8_t new_channel);
uint8_t getRfChannel(void);
void setADDR(void);
void setDevAddr(ADDR_TYPE);
bool dataReady();
bool isSending();
bool rxFifoEmpty();
bool txFifoEmpty();
void getData(uint8_t * data);
uint8_t getCarrier();
uint8_t getStatus();
void removePacketfromTxQueue(void);
void removePacketfromAckQueue(void);
void handleRxLoop(void);
void handleTxLoop(void);
void readPacket(mirfPacket* paket);
uint8_t sendPacket(mirfPacket* paket);
void createAck(mirfPacket* paket);
//,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
void configRegister(uint8_t reg, uint8_t value);
void readRegister(uint8_t reg, uint8_t * value, uint8_t len);
void writeRegister(uint8_t reg, uint8_t * value, uint8_t len);
void flushTx();
void flushRx();
void powerUpRx();
void powerUpTx();
void powerDown();
void nrfSpiWrite(uint8_t reg, uint8_t *data = NULL, bool readData = false, uint8_t len = 0);
void nrfSpiWrite2(uint8_t reg, uint8_t *data = NULL, bool readData = false, uint8_t len = 0);
void csnHi(); //__attribute__((noinline))
void csnLow();
void ceHi();
void ceLow();
//-----------------------------------------------------------------------
//------------------ Variables ------------------------------------------
//--- setup values ------------------------------------------------------
/**
* In sending mode.
*/
volatile uint8_t PTX;
/**
* CE Pin controls RX / TX, default D9.
*/
//uint8_t cePin;
/**
* CSN Pin Chip Select Not, default D10.
*/
//uint8_t csnPin;
/**
* Channel 0 - 127 or 0 - 84 in the US.
*/
uint8_t channel;
//set by first call to setRfChannel() from config()
/**
* The base config register.
* When required PWR_UP and/or PRIM_RX will be OR'ed with this.
*
* NOTE: Use "_BV(EN_CRC) | _BV(CRCO)" here if you want to
* connect to a device using the RF24 library.
*/
uint8_t baseConfig;
//address of device
//unique for every node
//1 for master node
ADDR_TYPE devAddr;
/**
* Spi interface (must extend spi).
*/
SPIlib *spi;
//MirfSpiDriver *spi; //MirfSpiDriver
//-------------------------------------------------------------------------
mirfPacket pendingPacket;
mirfPacket rxQueue[MAX_RX_PACKET_QUEUE];
uint8_t volatile rxPosBeg;
uint8_t volatile rxPosEnd;
mirfPacket txQueue[MAX_TX_PACKET_QUEUE];
uint8_t volatile txPosBeg;
uint8_t volatile txPosEnd;
uint8_t volatile txQueueSize;
uint8_t volatile txAttempt;
mirfPacket ackQueue[MAX_ACK_PACKET_QUEUE];
uint8_t volatile ackPosBeg;
uint8_t volatile ackPosEnd;
uint8_t volatile ackQueueSize;
//uint8_t volatile confirmedPackets[MAX_TX_PACKET_QUEUE];
//uint8_t volatile confirmedStackSize;
// sign that there is received packet in buffer adressed to this device
// 0 means no packets
// 1..x means number of packets ready
uint8_t volatile inPacketReady;
// sign that there is sending in progress
//0 means - no packet to be sent (ready)
//1 means - packet in fifo, waiting for Tx
//2 means unsent - waiting for free air
//3 means packet sent, but waiting for ACK
//4 means timeout
uint8_t volatile sendingStatus;
uint8_t volatile sendResult; //sending status is copied here, to be able to send next packets
//incremented with every new sent packet (used for identification of ACKs)
uint8_t packetCounter;
//not fully implemented now
//address of last transmitter from which packet was received and processed
//used for detecting of same packet, which ACK was probably not received by transmitter
//so it is sending the same packet again
//so we send ack again, but not process the packet because it was already processed
//uint8_t last_addr_in;
//uint8_t last_packetCounter_in;
//increments with every call of periodic Rx and Tx handle loop functions
//dedicated to recognize timeouts in waiting for ACK
//and to measure random time between send attempts
uint8_t volatile Timer;
uint8_t volatile TimerNewAttempt;
uint8_t volatile ackTimeoutTimer; //timer state when timeout will occur
//whether there is to be sent some ack
//volatile bool sendAck;
};
extern Nrf24l Mirf;
#endif /* _MIRF_H_ */