-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPruProxy.cpp
302 lines (265 loc) · 6.23 KB
/
PruProxy.cpp
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
// PruProxy.cpp
#include <stdio.h>
#include <iostream>
#include <sys/mman.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "PruProxy.h"
#define PRUSS_MAX_IRAM_SIZE 0x2000
using namespace std;
PruProxy::PruProxy()
{
Enabled = true;
mem_fd = -1;
}
PruProxy::~PruProxy()
{
if(mem_fd >= 0)
{
munmap(sharedMem, 0x400);
close(mem_fd);
}
}
// Disable the pru
void PruProxy::DisablePru()
{
Enabled = false;
}
// initialize the pru
bool PruProxy::Init()
{
Input1 = 1500;
Input2 = 1500;
Input3 = 1500;
Input4 = 1500;
Input5 = 1500;
Input6 = 1500;
if(Enabled)
{
/* open the device */
// map the device and init the varible before use it
mem_fd = open("/dev/mem", O_RDWR);
if (mem_fd < 0) {
printf("Failed to open /dev/mem (%s)\n", strerror(errno));
return false;
}
LoadImageToPru0("ReadPwmPru0.bin");
LoadImageToPru1("PwmOutPru1.bin");
/* map the shared memory */
sharedMem = mmap(0, 0x400, PROT_WRITE | PROT_READ, MAP_SHARED, mem_fd, 0x4a310000);
if (sharedMem == NULL) {
printf("Failed to map the device (%s)\n", strerror(errno));
close(mem_fd);
return false;
}
InitOutput();
RunPru0();
RunPru1();
}
return true;
}
// update input
bool PruProxy::UpdateInput()
{
if(Enabled)
{
Input1 = (*(unsigned long *)(sharedMem + 0x114))/200;
Input2 = (*(unsigned long *)(sharedMem + 0x124)/200);
Input3 = (*(unsigned long *)(sharedMem + 0x134)/200);
Input4 = (*(unsigned long *)(sharedMem + 0x144)/200);
Input5 = (*(unsigned long *)(sharedMem + 0x154)/200);
Input6 = (*(unsigned long *)(sharedMem + 0x164)/200);
// if(Input1 > 2000 || Input1 < 1000)
// {
// Input1 = 1050;
// }
// if(Input2 > 2000 || Input2 < 1000)
// {
// Input2 = 1050;
// }
// if(Input3 > 2000 || Input3 < 1000)
// {
// Input3 = 1050;
// }
// if(Input4 > 2000 || Input4 < 1000)
// {
// Input4 = 1050;
// }
}
return true;
}
// Init Output, set the esc to low throttle value as 1000
// set pulse width as 20 ms
// S1 pin 29 REAR_R S4
// S2 pin 30 FRONT_R S3
// S3 pin 27 READ)L S2
// S4 pin 28 FRONT_L S1
// Pin 20 21 won't work as it is high when reboot, esc wont' work
bool PruProxy::InitOutput()
{
if(Enabled)
{
*(unsigned long *)(sharedMem + 0x240) = 1000 * 200;
*(unsigned long *)(sharedMem + 0x250) = 1000 * 200;
*(unsigned long *)(sharedMem + 0x220) = 1000 * 200;
*(unsigned long *)(sharedMem + 0x230) = 1000 * 200;
*(unsigned long *)(sharedMem + 0x244) = 5000 * 200;
*(unsigned long *)(sharedMem + 0x254) = 5000 * 200;
*(unsigned long *)(sharedMem + 0x224) = 5000 * 200;
*(unsigned long *)(sharedMem + 0x234) = 5000 * 200;
}
return true;
}
bool PruProxy::UpdateOutput()
{
if(Enabled)
{
*(unsigned long *)(sharedMem + 0x240) = Output1;
*(unsigned long *)(sharedMem + 0x250) = Output2;
*(unsigned long *)(sharedMem + 0x220) = Output3;
*(unsigned long *)(sharedMem + 0x230) = Output4;
}
return true;
}
// load Image file to Pru0, the image will not run until ResetPru0 is called
bool PruProxy::LoadImageToPru0(char * filename)
{
if(Enabled)
{
if(!ResetPru0())
{
cout << "fail to reset pru0 " << endl;
return false;
}
return LoadImage(0x4a334000, filename); // 0x4a334000 is address of instruction in Pru0
}
return true;
}
// load Image file to Pru1, the image will not run until ResetPru1 is called
bool PruProxy::LoadImageToPru1(char * filename)
{
if(Enabled)
{
if(!ResetPru1())
{
cout << "Fail to reset Pru1 " << endl;
return false;
}
return LoadImage(0x4a338000, filename); // 0x4a334000 is address of instruction in Pru0
}
return true;
}
// Run image on Pru0
bool PruProxy::RunPru0()
{
return WriteUInt32(0x4a322000, 0xa);
}
// Run Image to Pru1
bool PruProxy::RunPru1()
{
return WriteUInt32(0x4a324000, 0xa);
}
// Reset Pru0,the image won't run
bool PruProxy::ResetPru0()
{
printf("Reset Pru0\n");
return WriteUInt32(0x4a322000, 0x10a);
}
// Reset Pru1, the image wont' run
bool PruProxy::ResetPru1()
{
return WriteUInt32(0x4a324000, 0x10a);
}
// write a unsigned long to memory
bool PruProxy::WriteUInt32(unsigned long addr, unsigned long data)
{
if(Enabled)
{
/* Initialize example */
/* map the shared memory */
void * pMem = mmap(0, 0x2000, PROT_WRITE | PROT_READ, MAP_SHARED, mem_fd, addr);
if (pMem == NULL)
{
printf("Failed to map the device (%s)\n", strerror(errno));
close(mem_fd);
return false;
}
*(unsigned long *)(pMem) = data;
cout << "write data to memory" << endl;
munmap(pMem, 0x2000);
return true;
}
return true;
}
// read a unsigned long from memroy
bool PruProxy::ReadUInt32(unsigned long addr, unsigned long & data)
{
if(Enabled)
{
/* Initialize example */
/* map the shared memory */
void * pMem = mmap(0, 0x4, PROT_WRITE | PROT_READ, MAP_SHARED, mem_fd, addr);
if (pMem == NULL)
{
printf("Failed to map the device (%s)\n", strerror(errno));
close(mem_fd);
return false;
}
data = *(unsigned long *)(pMem);
cout << "read data to memory" << endl;
munmap(pMem, 0x4);
return true;
}
return true;
}
// load image file
bool PruProxy::LoadImage(unsigned long addr, char * filename)
{
if(Enabled)
{
FILE *fPtr;
// Open an File from the hard drive
fPtr = fopen(filename, "rb");
if (fPtr == NULL) {
printf("Image File %s open failed\n", filename);
} else {
printf("Image File %s open passed\n", filename);
}
// Read file size
fseek(fPtr, 0, SEEK_END);
// read file
unsigned char fileDataArray[PRUSS_MAX_IRAM_SIZE];
int fileSize = 0;
fileSize = ftell(fPtr);
if (fileSize == 0) {
printf("File read failed.. Closing program\n");
fclose(fPtr);
return -1;
}
fseek(fPtr, 0, SEEK_SET);
if (fileSize !=
fread((unsigned char *) fileDataArray, 1, fileSize, fPtr)) {
printf("WARNING: File Size mismatch\n");
}
fclose(fPtr);
/* Initialize example */
/* map the shared memory */
void * pMem = mmap(0, 0x2000, PROT_WRITE | PROT_READ, MAP_SHARED, mem_fd, addr);
if (pMem == NULL) {
printf("Failed to map the device (%s)\n", strerror(errno));
close(mem_fd);
return false;
}
char * p = (char*)pMem;
for(int i = 0; i < fileSize; i ++)
{
*(p + i) = fileDataArray[i];
}
cout << "write file to memory" << endl;
munmap(pMem, 0x2000);
return true;
}
return true;
}