-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRN2483LVP.cpp
More file actions
324 lines (277 loc) · 7.95 KB
/
RN2483LVP.cpp
File metadata and controls
324 lines (277 loc) · 7.95 KB
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
#include "RN2483LVP.h"
RN2483LVP::RN2483LVP()
{
this->pgd = 5;
this->pgc = 6;
this->mclr = 4;
this->inLVP = false;
}
//Constructor
void RN2483LVP::initRN2483LVP(int pgd, int pgc, int mclr, Stream &usbserial)
{
this->pgd = pgd;
this->pgc = pgc;
this->mclr = mclr;
this->usbserial = &usbserial;
this->inLVP = false;
}
// Enter low-voltage program/verify mode
void RN2483LVP::enterLVP()
{
if (not(inLVP))
{
inLVP = true;
usbserial->write("Entering low-voltage program/verify mode\r\n");
// Pull all programming pins Low
digitalWrite(mclr, LOW);
digitalWrite(pgd, LOW);
digitalWrite(pgc, LOW);
// Wait atleast 100ns (P13)
delayMicroseconds(1);
// Set mclr High then Low
digitalWrite(mclr, HIGH);
delayMicroseconds(1);
digitalWrite(mclr, LOW);
// Wait atleast 1ms (P18)
delayMicroseconds(2);
// Enter key sequence on pgd
for (int n = 0; n < 32; n++)
{
//Lower pgc and write bit to pgd
digitalWrite(pgc, LOW);
digitalWrite(pgd, keyseq[n]);
delayMicroseconds(1);
//Raise pgc to send bit
digitalWrite(pgc, HIGH);
delayMicroseconds(1);
}
digitalWrite(pgc, LOW);
// Wait atleast 40ns
delayMicroseconds(1);
// Set mclr High
digitalWrite(mclr, HIGH);
// Wait atleast 400us
delayMicroseconds(450);
}
}
// Exit low-voltage program/verify mode
void RN2483LVP::exitLVP()
{
inLVP = false;
usbserial->write("Exiting low-voltage program/verify mode\r\n");
delayMicroseconds(1);
digitalWrite(mclr, LOW);
delayMicroseconds(10);
digitalWrite(mclr, HIGH);
}
// Serial program/verify operation
int RN2483LVP::sendOp(int cmd, int payload)
{
bool valid = true;
bool isread = false;
// Check if command is valid and if it contains a read
switch (cmd)
{
case B0000:
break;
case B0010:
isread = true;
break;
case B1000:
isread = true;
break;
case B1001:
isread = true;
break;
case B1010:
isread = true;
break;
case B1011:
isread = true;
break;
case B1100:
break;
case B1101:
break;
case B1110:
break;
case B1111:
break;
default:
valid = false;
}
// Check if data payload is the right size
if ((isread && (payload > 255)) || (!isread && (payload > 65535)))
{
usbserial->write("Invalid number of payload bits\r\n");
return false;
}
if (valid)
{
// Send command bits to the RN2483
for (int n = 0; n < 4; n++)
{
//Lower pgc and write bit to pgd
digitalWrite(pgc, LOW);
digitalWrite(pgd, bitRead(cmd, n));
delayMicroseconds(1);
//Raise pgc to send bit
digitalWrite(pgc, HIGH);
delayMicroseconds(1);
}
digitalWrite(pgc, LOW);
// Wait atleast 40ns (P5)
delayMicroseconds(1);
if (isread)
{
// Write 8 payload bits to RN2483
for (int n = 0; n < 8; n++)
{
//Lower pgc and write bit to pgd
digitalWrite(pgc, LOW);
digitalWrite(pgd, bitRead(payload, n));
delayMicroseconds(1);
//Raise pgc to send bit
digitalWrite(pgc, HIGH);
delayMicroseconds(1);
}
digitalWrite(pgc, LOW);
// Set pgd to input
pinMode(pgd, INPUT);
// Wait atleast 20ns (P6)
delayMicroseconds(1);
// Read 8 response bits from RN2483
int response = 255;
for (int n = 0; n < 8; n++)
{
//Raise pgc to recieve bit
digitalWrite(pgc, HIGH);
delayMicroseconds(1);
//Lower pgc and read bit from pgd
digitalWrite(pgc, LOW);
bitWrite(response, n, digitalRead(pgd));
delayMicroseconds(1);
}
// Set pgd to output
pinMode(pgd, OUTPUT);
// Wait atleast 40ns (P5A)
delayMicroseconds(1);
return response;
}
else
{
// Write all 16 bits from the data payload
for (int n = 0; n < 16; n++)
{
//Lower pgc and write bit to pgd
digitalWrite(pgc, LOW);
digitalWrite(pgd, bitRead(payload, n));
delayMicroseconds(1);
//Raise pgc to send bit
digitalWrite(pgc, HIGH);
delayMicroseconds(1);
}
digitalWrite(pgc, LOW);
// Wait atleast 40ns (P5A)
delayMicroseconds(1);
return true;
}
}
else
{
usbserial->write("Command not valid\r\n");
return false;
}
}
// Sets the TBLPTR to the address to be read/written
void RN2483LVP::setTBLPTR(int addru, int addrh, int addrl)
{
sendOp(B0000, 0x0E00 + addru); // MOVLW <Addr[21:16]>
sendOp(B0000, 0x6EF8); // MOVWF TBLPTRU
sendOp(B0000, 0x0E00 + addrh); // MOVLW <Addr[15:8]>
sendOp(B0000, 0x6EF7); // MOVWF TBLPTRH
sendOp(B0000, 0x0E00 + addrl); // MOVLW <Addr[7:0]>
sendOp(B0000, 0x6EF6); // MOVWF TBLPTRL
}
// Read value at address
int RN2483LVP::readAddress(int addru, int addrh, int addrl)
{
setTBLPTR(addru, addrh, addrl);
return sendOp(B1001, 0x0000); // TBLRD *+
}
// Print device IDs on serial
void RN2483LVP::readDeviceID()
{
int DEVID1 = readAddress(0x3F, 0xFF, 0xFE);
int DEVID2 = readAddress(0x3F, 0xFF, 0xFF);
usbserial->print("DEVID1: " + String(DEVID1, HEX) + "h\r\n");
usbserial->print("DEVID2: " + String(DEVID2, HEX) + "h\r\n");
}
// Write value to address in config block
void RN2483LVP::writeAddressConfig(int addru, int addrh, int addrl, int value)
{
setTBLPTR(addru, addrh, addrl);
sendOp(B1100, value);
}
// Bulk erase the entire device
void RN2483LVP::bulkErase()
{
// Write 0F0F to 3C0005h
writeAddressConfig(0x3C, 0x00, 0x05, 0x0F0F);
// Write 8F8F to 3C0004h
writeAddressConfig(0x3C, 0x00, 0x04, 0x8F8F);
sendOp(B0000, 0x0000);
sendOp(B0000, 0x0000);
// Wait atleast 15ms (P11) to let the erase complete
delay(20);
}
// Writes a block of bytes to code memory.
// bytes.length <= 64, bytes[n] <= 0xFFFF
void RN2483LVP::writeCodeBlock(int addru, int addrh, int addrl, int bytes[])
{
// Point to row to write.
setTBLPTR(addru, addrh, addrl);
// Load write buffer. Repeat for all but the last two bytes.
for (int n = 0; n < SIZE - 2; n = n + 2)
{
sendOp(B1101, (bytes[n + 1] << 8) + bytes[n]);
}
// Load write buffer for last two bytes and start programming
sendOp(B1111, (bytes[SIZE - 2] << 8) + bytes[SIZE - 1]);
// Send NOP and Hold 4th pgc HIGH for atleast 1ms (P9)
digitalWrite(pgd, LOW);
for (int n = 0; n < 4; n++)
{
digitalWrite(pgc, LOW);
delayMicroseconds(1);
digitalWrite(pgc, HIGH);
delayMicroseconds(1);
}
delay(2);
// Hold pgc LOW for atleast 200us (P10)
digitalWrite(pgc, LOW);
delayMicroseconds(250);
}
// Writes a sequence of bytes into the code memory
void RN2483LVP::writeFlash(int bytes[])
{
int LoopCount = 0;
// Bulk erase chip
bulkErase();
// Direct access to code memory
sendOp(B0000, 0x8EA6); // BSF EECON1, EEpgd
sendOp(B0000, 0x9CA6); // BCF EECON1, CFGS
sendOp(B0000, 0x84A6); // BSF EECON1, WREN
writeCodeBlock(0, 0, 0, bytes);
exitLVP();
enterLVP();
}
void RN2483LVP::printCodeMemory(int start, int stop)
{
int response;
for (int n = start; n < stop; n++)
{
response = readAddress(0, 0, n);
usbserial->print(String(response, HEX) + "\r\n");
}
}