Class: HardsploitAPI_NRF24L01

Inherits:
Object
  • Object
show all
Defined in:
lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb

Constant Summary collapse

R_REGISTER =

Instruction Mnemonics

0x00
W_REGISTER =
0x20
REGISTER_MASK =
0x1F
ACTIVATE =
0x50
R_RX_PL_WID =
0x60
R_RX_PAYLOAD =
0x61
W_TX_PAYLOAD =
0xA0
W_ACK_PAYLOAD =
0xA8
FLUSH_TX =
0xE1
FLUSH_RX =
0xE2
REUSE_TX_PL =
0xE3
NOP =
0xFF
NRF24L01_00_CONFIG =

Register map

0x00
NRF24L01_01_EN_AA =
0x01
NRF24L01_02_EN_RXADDR =
0x02
NRF24L01_03_SETUP_AW =
0x03
NRF24L01_04_SETUP_RETR =
0x04
NRF24L01_05_RF_CH =
0x05
NRF24L01_06_RF_SETUP =
0x06
NRF24L01_07_STATUS =
0x07
NRF24L01_08_OBSERVE_TX =
0x08
NRF24L01_09_CD =
0x09
NRF24L01_0A_RX_ADDR_P0 =
0x0A
NRF24L01_0B_RX_ADDR_P1 =
0x0B
NRF24L01_0C_RX_ADDR_P2 =
0x0C
NRF24L01_0D_RX_ADDR_P3 =
0x0D
NRF24L01_0E_RX_ADDR_P4 =
0x0E
NRF24L01_0F_RX_ADDR_P5 =
0x0F
NRF24L01_10_TX_ADDR =
0x10
NRF24L01_11_RX_PW_P0 =
0x11
NRF24L01_12_RX_PW_P1 =
0x12
NRF24L01_13_RX_PW_P2 =
0x13
NRF24L01_14_RX_PW_P3 =
0x14
NRF24L01_15_RX_PW_P4 =
0x15
NRF24L01_16_RX_PW_P5 =
0x16
NRF24L01_17_FIFO_STATUS =
0x17
NRF24L01_1C_DYNPD =
0x1C
NRF24L01_1D_FEATURE =
0x1D
NRF24L01_00_MASK_RX_DR =

Bit mnemonics

6
NRF24L01_00_MASK_TX_DS =
5
NRF24L01_00_MASK_MAX_RT =
4
NRF24L01_00_EN_CRC =
3
NRF24L01_00_CRCO =
2
NRF24L01_00_PWR_UP =
1
NRF24L01_00_PRIM_RX =
0
NRF24L01_07_RX_DR =
6
NRF24L01_07_TX_DS =
5
NRF24L01_07_MAX_RT =
4
NRF24L01_BR_1M =

Bitrates

0
NRF24L01_BR_2M =
1
NRF24L01_BR_250K =
2
NRF24L01_BR_RSVD =
3
TXRX_OFF =
0
TX_EN =
1
RX_EN =
2

Instance Method Summary collapse

Constructor Details

#initializeHardsploitAPI_NRF24L01

Returns a new instance of HardsploitAPI_NRF24L01.



91
92
93
94
95
96
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 91

def initialize()
	#Speed Range 1-255  SPI clock =  150Mhz / (2*speed) tested from 3 to 255 (25Mhz to about 0.3Khz)
	@spi = HardsploitAPI_SPI.new(speed:8,mode:0) # 150/(2*8) = 9.3Mhz
	@rf_setup = 0x0F
	@tout	=0
end

Instance Method Details

#BV(x) ⇒ Object



77
78
79
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 77

def BV(x)
 return (1 << x)
end

#CE_hiObject



250
251
252
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 250

def CE_hi
	@spi.pulse = 1
end

#CE_loObject



246
247
248
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 246

def CE_lo
	@spi.pulse = 0
end

#changeChannel(channel:) ⇒ Object



222
223
224
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 222

def changeChannel(channel:)
	NRF24L01_WriteReg(NRF24L01_05_RF_CH, channel)
end

#dataAvailableObject



213
214
215
216
217
218
219
220
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 213

def dataAvailable()
	result = sendAndReceiveSPI([R_REGISTER ,HardsploitAPI_NRF24L01::NRF24L01_07_STATUS])
	if ((result[0] & BV(HardsploitAPI_NRF24L01::NRF24L01_07_RX_DR))>>6)==1
		return true
	else
		return false
	end
end

#initDrone(channel:, address:) ⇒ Object



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
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 98

def initDrone(channel:,address:)
	config = BV(NRF24L01_00_EN_CRC) | BV(NRF24L01_00_CRCO) | BV(NRF24L01_00_PRIM_RX)
	NRF24L01_WriteReg(NRF24L01_00_CONFIG, config);
	NRF24L01_WriteReg(NRF24L01_01_EN_AA, 0x0f);      # Auto Acknoledgement
	NRF24L01_Activate(0x73);                         #Allow write feature reg
	NRF24L01_WriteReg( NRF24L01_1D_FEATURE,0x06); #enableDynamicPayloads
	NRF24L01_WriteReg( NRF24L01_1C_DYNPD,0x3f);   #enableDynamicPayloads
   NRF24L01_WriteReg(NRF24L01_02_EN_RXADDR, 0x01);  # Enable data pipe 0
	NRF24L01_WriteReg(NRF24L01_03_SETUP_AW, 0x03);   # 5-byte RX/TX address
 		#NRF24L01_WriteReg(NRF24L01_04_SETUP_RETR, 0xFF); # 4ms retransmit t/o, 15 tries
	NRF24L01_WriteReg(NRF24L01_05_RF_CH, channel);      # Channel  - bind
	setBitrate(NRF24L01_BR_250K)
	setPower(3) #Max power
	NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70); 	# Clear data ready, data
	NRF24L01_WriteReg(NRF24L01_11_RX_PW_P0, 16);
	NRF24L01_WriteReg(NRF24L01_17_FIFO_STATUS, 0x00);
	NRF24L01_WriteRegisterMulti(NRF24L01_0A_RX_ADDR_P0,address);
	NRF24L01_WriteRegisterMulti(NRF24L01_10_TX_ADDR, address);

	initialize();

	config |= BV(NRF24L01_00_PWR_UP);
  NRF24L01_WriteReg(NRF24L01_00_CONFIG, config);

	valid_packets = missed_packets = bad_packets = 0;

	NRF24L01_SetTxRxMode(TXRX_OFF);
	NRF24L01_SetTxRxMode(RX_EN);
	# puts "EN_AA"
	# p NRF24L01_ReadReg(NRF24L01_01_EN_AA)
	# puts "EN_RXADDR"
	# p NRF24L01_ReadReg(NRF24L01_02_EN_RXADDR)
	# puts "SETUP_AW"
	# p NRF24L01_ReadReg(NRF24L01_03_SETUP_AW)
	# puts "RF_CH"
	# p NRF24L01_ReadReg(NRF24L01_05_RF_CH)
	# puts "RX_PW_P0"
	# p NRF24L01_ReadRegisterMulti(NRF24L01_0A_RX_ADDR_P0,5)
	# puts "TX_PW_P0"
	# p NRF24L01_ReadRegisterMulti(NRF24L01_10_TX_ADDR,5)
	# puts "RX_ADDR_P0"
	# p NRF24L01_ReadReg(NRF24L01_0A_RX_ADDR_P0)
	# puts "TX_ADDR"
	# p NRF24L01_ReadReg(NRF24L01_10_TX_ADDR)
	# puts "config"
	# p config
end

#NRF24L01_Activate(code) ⇒ Object



208
209
210
211
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 208

def NRF24L01_Activate(code)
	result = sendAndReceiveSPI([ACTIVATE ,code])
	return result[0];
end

#NRF24L01_FlushRxObject



204
205
206
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 204

def NRF24L01_FlushRx()
	return Strobe(FLUSH_RX);
end

#NRF24L01_FlushTxObject



200
201
202
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 200

def NRF24L01_FlushTx()
	return Strobe(FLUSH_TX);
end

#NRF24L01_ReadReg(reg) ⇒ Object



167
168
169
170
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 167

def NRF24L01_ReadReg(reg)
	result = sendAndReceiveSPI([R_REGISTER | (REGISTER_MASK & reg) ,0xFF ])
	return result[1]
end

#NRF24L01_ReadRegisterMulti(reg, length) ⇒ Object



177
178
179
180
181
182
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 177

def NRF24L01_ReadRegisterMulti(reg,length)
	tab = Array.new
	tab.push (R_REGISTER | (REGISTER_MASK & reg))
	tab.push *Array.new(length, 0xFF)
	return sendAndReceiveSPI(tab).drop(1) #remove the first byte
end

#NRF24L01_SetTxRxMode(mode) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 254

def NRF24L01_SetTxRxMode(mode)
	if(mode == TX_EN) then
		CE_lo()
		#sleep(0.5)
		NRF24L01_WriteReg(NRF24L01_07_STATUS, (1 << NRF24L01_07_RX_DR) | (1 << NRF24L01_07_TX_DS) 	| (1 << NRF24L01_07_MAX_RT))   #reset the flag(s)
		NRF24L01_WriteReg(NRF24L01_00_CONFIG, (1 << NRF24L01_00_EN_CRC)| (1 << NRF24L01_00_CRCO) | (1 << NRF24L01_00_PWR_UP))  #switch to TX mode
		#sleep(0.5)
		CE_hi()
	elsif (mode == RX_EN) then
		CE_lo()
	#	sleep(0.5)
		NRF24L01_WriteReg(NRF24L01_07_STATUS, 0x70)        # reset the flag(s)
		NRF24L01_WriteReg(NRF24L01_00_CONFIG, 0x0F)        # switch to RX mode
		NRF24L01_WriteReg(NRF24L01_07_STATUS, (1 << NRF24L01_07_RX_DR) | (1 << NRF24L01_07_TX_DS) | (1 << NRF24L01_07_MAX_RT)) #reset the flag(s)
		NRF24L01_WriteReg(NRF24L01_00_CONFIG, (1 << NRF24L01_00_EN_CRC)| (1 << NRF24L01_00_CRCO) 	| (1 << NRF24L01_00_PWR_UP) | (1 << NRF24L01_00_PRIM_RX)) #switch to RX mode
	#	sleep(0.5)
		CE_hi()
	else
		NRF24L01_WriteReg(NRF24L01_00_CONFIG, (1 << NRF24L01_00_EN_CRC)) #PowerDown
		CE_lo()
	end
end

#NRF24L01_WritePayload(payload) ⇒ Object



159
160
161
162
163
164
165
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 159

def NRF24L01_WritePayload(payload)
	tmpWpayload = Array.new
	tmpWpayload.push (W_TX_PAYLOAD)
	tmpWpayload.push *payload
	result = sendAndReceiveSPI(tmpWpayload)
	return result[0]
end

#NRF24L01_WriteReg(reg, data) ⇒ Object



146
147
148
149
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 146

def NRF24L01_WriteReg(reg, data)
	result = sendAndReceiveSPI([ (W_REGISTER | (REGISTER_MASK & reg)),data ])
	return result[1]
end

#NRF24L01_WriteRegisterMulti(reg, payload) ⇒ Object



151
152
153
154
155
156
157
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 151

def NRF24L01_WriteRegisterMulti(reg, payload)
	tmppayload = Array.new
	tmppayload.push (W_REGISTER | (REGISTER_MASK & reg))
	tmppayload.push *payload
	result = sendAndReceiveSPI(tmppayload)
	return result[0]
end

#ReadObject



284
285
286
287
288
289
290
291
292
293
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 284

def Read()
	tabdataread = Array.new
	if dataAvailable()
		NRF24L01_WriteReg(0x07,BV(HardsploitAPI_NRF24L01::NRF24L01_07_RX_DR))
		tabdataread.push *readPayload(16)
   	return tabdataread
	else
		return tabdataread
	end
end

#readAvailableDataObject



191
192
193
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 191

def readAvailableData
		return readPayload(readPayloadSize)
end

#readPayload(length) ⇒ Object



184
185
186
187
188
189
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 184

def readPayload(length)
	tab = Array.new
	tab.push R_RX_PAYLOAD
	tab.push *Array.new(length, 0xFF)
	return sendAndReceiveSPI(tab).drop(1) #remove the first byte
end

#readPayloadSizeObject



172
173
174
175
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 172

def readPayloadSize
	result = sendAndReceiveSPI([R_RX_PL_WID ,0xFF ])
	return result[1]
end

#resetObject



277
278
279
280
281
282
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 277

def reset()
	NRF24L01_SetTxRxMode(TXRX_OFF)
 	NRF24L01_FlushTx()
 	NRF24L01_FlushRx()
	return true
end

#Send(dataSend) ⇒ Object



295
296
297
298
299
300
301
302
303
304
305
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 295

def Send(dataSend)
	NRF24L01_SetTxRxMode(TXRX_OFF)
	NRF24L01_FlushTx()
	NRF24L01_WritePayload(dataSend)
	NRF24L01_SetTxRxMode(TX_EN)
 	sleep(0.1)
	NRF24L01_SetTxRxMode(TXRX_OFF)
	NRF24L01_FlushTx()
	NRF24L01_FlushRx()
	NRF24L01_SetTxRxMode(RX_EN);
end

#sendAndReceiveSPI(packet) ⇒ Object



81
82
83
84
85
86
87
88
89
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 81

def sendAndReceiveSPI(packet)
	begin
		return @spi.spi_Interact(payload:packet)
	rescue HardsploitAPI::ERROR::HARDSPLOIT_NOT_FOUND
		puts "Hardsploit not found"
  rescue HardsploitAPI::ERROR::USB_ERROR
		puts "USB ERROR"
	end
end

#setBitrate(bitrate) ⇒ Object



226
227
228
229
230
231
232
233
234
235
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 226

def setBitrate(bitrate)
	 #Note that bitrate 250kbps (and bit RF_DR_LOW) is valid only
	 #for nRF24L01+. There is no way to programmatically tell it from
	 #older version, nRF24L01, but the older is practically phased out
	 #by Nordic, so we assume that we deal with with modern version.

	# Bit 0 goes to RF_DR_HIGH, bit 1 - to RF_DR_LOW
	@rf_setup = (@rf_setup & 0xD7) | ((bitrate & 0x02) << 4) | ((bitrate & 0x01) << 3);
	return NRF24L01_WriteReg(NRF24L01_06_RF_SETUP, @rf_setup);
end

#setPower(nrf_power) ⇒ Object

Power setting is 0..3 for nRF24L01



238
239
240
241
242
243
244
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 238

def setPower(nrf_power)
	if (nrf_power < 0) or (nrf_power > 3) then
		raise "NRF setPower, wrong must be between 0 and 3"
	end
	@rf_setup = (@rf_setup & 0xF9) | ((nrf_power & 0x03) << 1)
	return NRF24L01_WriteReg(NRF24L01_06_RF_SETUP, @rf_setup)
end

#Strobe(state) ⇒ Object



195
196
197
198
# File 'lib/HardsploitAPI/Modules/NRF24L01/HardsploitAPI_NRF24L01.rb', line 195

def Strobe(state)
	result = sendAndReceiveSPI([state])
	return result[0];
end