-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagdata.c
346 lines (332 loc) · 11.2 KB
/
magdata.c
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//=========================================================================
// magdata.c
//
// An interface for the RM3100 3-axis magnetometer from PNI Sensor Corp.
//
// Author: David Witten, KD0EAG
// Date: December 18, 2023
// License: GPL 3.0
// Note: replaces i2c.c (using file system calls to read(), write(), etc.
// with calls to pigpio.
// Also adding callbacks on GPIO27 for PPS rising edge.
//=========================================================================
//#include "i2c-pigpio.h"
#include "main.h"
#include "magdata.h"
//------------------------------------------
// Static variables
//------------------------------------------
// static char mSamples[9];
// extern char *version;
//#if(USE_PIPES)
//char outputPipeName[MAXPATHBUFLEN] = "/home/web/wsroot/pipein.fifo";
//char inputPipeName[MAXPATHBUFLEN] = "/home/web/wsroot/pipeout.fifo";
//#endif
//
////------------------------------------------
//// readTemp(ctlList *p)
////------------------------------------------
//int readTemp(ctlList *p)
//{
// int temp = -9999;
// char data[2] = {0};
// char reg[1] = {MCP9808_REG_AMBIENT_TEMP};
//
//// if(i2c_read_device(p->pi, data, 2) != 2)
//// {
//// fprintf(stderr, "Error : I/O error reading temp sensor at address: [0x%2X].\n", devAddr);
//// }
//// else
//// {
//// // Convert the data to 13-bits
//// temp = ((data[0] & 0x1F) * 256 + data[1]);
//// if(temp > 4095)
//// {
//// temp -= 8192;
//// }
//// }
// return temp;
//}
//
////------------------------------------------
//// readMagCMM()
////------------------------------------------
//int readMagCMM(ctlList *p, int32_t *XYZ)
//{
// int rv = 0;
// int bytes_read = 0;
//
// // i2c_setAddress(p->pi, devAddr);
// // Check if DRDY went high and wait unit high before reading results
//// while((rv = (i2c_read(p->pi, RM3100I2C_STATUS)) & RM3100I2C_READMASK) != RM3100I2C_READMASK)
//// {
//// }
// // Read the XYZ registers
//// if((bytes_read = i2c_readbuf(p->pi, RM3100I2C_XYZ, (unsigned char*) &mSamples, sizeof(mSamples)/sizeof(char))) != sizeof(mSamples)/sizeof(char))
//// {
//// perror("i2c transaction i2c_readbuf() failed.\n");
//// }
// XYZ[0] = ((signed char)mSamples[0]) * 256 * 256;
// XYZ[0] |= mSamples[1] * 256;
// XYZ[0] |= mSamples[2];
//
// XYZ[1] = ((signed char)mSamples[3]) * 256 * 256;
// XYZ[1] |= mSamples[4] * 256;
// XYZ[1] |= mSamples[5];
//
// XYZ[2] = ((signed char)mSamples[6]) * 256 * 256;
// XYZ[2] |= mSamples[7] * 256;
// XYZ[2] |= mSamples[8];
//
// return bytes_read;
//}
//
////------------------------------------------
//// readMagPOLL()
////------------------------------------------
//int readMagPOLL(ctlList *p, int32_t *XYZ)
//{
// int rv = 0;
// int bytes_read = 0;
// short pmMode = (PMMODE_ALL);
//
//// i2c_setAddress(p->pi, devAddr);
//// // Write command to use Continuous measurement Mode.
//// i2c_write(p->pi, RM3100_MAG_POLL, pmMode);
// // if a delay is specified after DRDY goes high, sleep it off.
// if(p->DRDYdelay)
// {
// usleep(p->DRDYdelay);
// }
// // Check if DRDY went high and wait unit high before reading results
//// while((rv = (i2c_read(p->pi, RM3100I2C_STATUS)) & RM3100I2C_READMASK) != RM3100I2C_READMASK)
//// {
//// }
// // Read the XYZ registers
//// if((bytes_read = i2c_readbuf(p->pi, RM3100I2C_XYZ, (unsigned char*) &mSamples, sizeof(mSamples)/sizeof(char))) != sizeof(mSamples)/sizeof(char))
//// {
//// perror("i2c transaction i2c_readbuf() failed.\n");
//// }
// XYZ[0] = ((signed char)mSamples[0]) * 256 * 256;
// XYZ[0] |= mSamples[1] * 256;
// XYZ[0] |= mSamples[2];
//
// XYZ[1] = ((signed char)mSamples[3]) * 256 * 256;
// XYZ[1] |= mSamples[4] * 256;
// XYZ[1] |= mSamples[5];
//
// XYZ[2] = ((signed char)mSamples[6]) * 256 * 256;
// XYZ[2] |= mSamples[7] * 256;
// XYZ[2] |= mSamples[8];
//
// return bytes_read;
//}
//
////------------------------------------------
//// openI2CBus()
////------------------------------------------
//int openI2CBus(ctlList *p)
//{
// p->pi = -1;
//
// char pathStr[64] = "";
// snprintf(pathStr, sizeof(pathStr), "/dev/i2c-%i", p->i2cBusNumber);
//
// if((p->pi = open(pathStr, O_RDWR)) < 0)
// {
// perror("Bus open failed\n");
// return -1;
// }
// return p->pi;
//}
//
////--------------------------------------------------------------------
//// closeI2CBus()
////
//// Close I2C bus
////--------------------------------------------------------------------
//void closeI2CBus(int pi)
//{
// close(pi);
//}
//
////------------------------------------------
//// setNOSReg()
////------------------------------------------
//int setNOSReg(ctlList *p)
//{
// int rv;
// printf("\nIn setNOSReg():: Setting undocumented NOS register to value: %2X\n", p->NOSRegValue);
//// rv = i2c_write(p->pi, RM3100I2C_NOS, p->NOSRegValue);
// return rv;
//}
//------------------------------------------
// setMagSampleRate()
//------------------------------------------
unsigned short setMagSampleRate(ctlList *p, unsigned short sample_rate)
{
int i;
const unsigned short int supported_rates[][2] =
{
/* [Hz], register value */
{ 2, 0x0A}, // up to 2Hz
{ 4, 0x09}, // up to 4Hz
{ 8, 0x08}, // up to 8Hz
{ 16, 0x07}, // up to 16Hz
{ 31, 0x06}, // up to 31Hz
{ 62, 0x05}, // up to 62Hz
{ 125, 0x04}, // up to 125Hz
{ 220, 0x03} // up to 250Hz
};
for(i = 0; i < sizeof(supported_rates)/(sizeof(unsigned short int) * 2) - 1; i++)
{
if(sample_rate <= supported_rates[i][0])
{
break;
}
}
p->CMMSampleRate = supported_rates[i][0];
// i2c_write(p->pi, RM3100I2C_TMRC, p->CMMSampleRate);
return p->CMMSampleRate;
}
//------------------------------------------
// getMagSampleRate();
// The actual sample rate of the sensor.
//------------------------------------------
unsigned short getMagSampleRate(ctlList *p)
{
return p->CMMSampleRate;
}
//
////------------------------------------------
//// getMagRev(ctlList *p)
////------------------------------------------
//int getMagRev(ctlList *p)
//{
// // Set address of the RM3100
//// i2c_setAddress(p->pi, p->magnetometerAddr);
//
// // Check Version
//// if((p->magRevId = i2c_read(p->pi, RM3100I2C_REVID)) != (uint8_t)RM3100_VER_EXPECTED)
//// {
//// // Fail, exit...
//// fprintf(stderr, "\nRM3100 REVID NOT CORRECT: ");
//// fprintf(stderr, "RM3100 REVID: 0x%X <> EXPECTED: 0x%X.\n\n", p->magRevId, RM3100_VER_EXPECTED);
//// fflush(stdout);
//// return 0;
//// }
//// else
//// {
//// fprintf(stdout,"RM3100 Detected Properly: ");
//// fprintf(stdout,"REVID: %x.\n", p->magRevId);
//// }
// return p->magRevId;
//}
//
////------------------------------------------
//// setup_mag()
////------------------------------------------
//int setup_mag(ctlList *p)
//{
// int rv = SensorOK;
//
// // Set address of the RM3100
//// i2c_setAddress(p->pi, p->magnetometerAddr);
// // Check Version
// if(!getMagRev(p))
// {
// exit (1);
// }
// // Setup the NOS register
// // setNOSReg(p);
// // Clear out these registers
//// i2c_write(p->pi, RM3100_MAG_POLL, 0);
//// i2c_write(p->pi, RM3100I2C_CMM, 0);
// // Initialize CC settings
// setCycleCountRegs(p);
// // Sleep for 1 second
// usleep(100000); // delay to help monitor DRDY pin on eval board
// return rv;
//}
//------------------------------------------
// runBIST()
// Runs the Built In Self Test.
//------------------------------------------
int runBIST(ctlList *p)
{
return 0;
//return i2c_read(p->pi, RM3100I2C_TMRC);
}
//------------------------------------------
// startCMM()
// Starts Continuous Measurement Mode
//------------------------------------------
int startCMM(ctlList *p)
{
int rv = 0;
// short cmmMode = (CMMMODE_ALL); // 71 d
// rv = i2c_write(p->pi, RM3100I2C_CMM, cmmMode);
return rv;
}
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Dave,
//
// Here is a general equation for gain taken directly from correspondence with PNI which I use in my Python scripts.
// Gn=(Aval*(0.3671*Cycnt+1.5)/1000)
// (0.3671*cycle count + 1.5) when divided into the X Y or Z result with no averaging gives the correct value in micro teslas
// Aval/1000 times (0.3671*cycle count + 1.5) when divided into the X Y or Z result gives the correct value in nano teslas.
// Conversely, you can multiply the X Y or Z values by 1000/(Aval*(0.3671*Cycnt+1.5))
// As far as I know, it is an exact gain equation for the RM3100 and works for ANY cycle count .... like 375, 405, 125, etc, etc. No error prone lookup tables.
//
// Jules
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//------------------------------------------
// getCCGainEquiv()
// Gn=(Aval*(0.3671*Cycnt+1.5)/1000)
//------------------------------------------
unsigned short getCCGainEquiv(unsigned short CCVal)
{
unsigned short gain = 0;
double dGain = (0.3671 * CCVal + 1.5);
gain = (unsigned short) dGain;
return gain;
}
//------------------------------------------
// setCycleCountRegs()
//------------------------------------------
void setCycleCountRegs(ctlList *p)
{
//int i = 0;
// i2c_write(p->pi, RM3100I2C_CCX_1, (p->cc_x >> 8));
// i2c_write(p->pi, RM3100I2C_CCX_0, (p->cc_x & 0xff));
p->x_gain = getCCGainEquiv(p->cc_x);
// i2c_write(p->pi, RM3100I2C_CCY_1, (p->cc_y >> 8));
// i2c_write(p->pi, RM3100I2C_CCY_0, (p->cc_y & 0xff));
p->y_gain = getCCGainEquiv(p->cc_y);
// i2c_write(p->pi, RM3100I2C_CCZ_1, (p->cc_y >> 8));
// i2c_write(p->pi, RM3100I2C_CCZ_0, (p->cc_y & 0xff));
p->z_gain = getCCGainEquiv(p->cc_z);
// Write NOSRegValue to register 0A
// i2c_write(p->pi, RM3100I2C_NOS, (uint8_t)(p->NOSRegValue));
// fprintf(stderr, "\nIn setCycleCountRegs():: Setting NOS register to value: %2X\n", p->NOSRegValue);
// fprintf(stderr, "CycleCounts - X: %u, Y: %u, Z: %u.\n", p->cc_x, p->cc_y, p->cc_x);
// fprintf(stderr, "Gains - X: %u, Y: %u, Z: %u.\n", p->x_gain, p->y_gain, p->z_gain);
// fprintf(stderr, "NOS Register - %2X.\n", p->NOSRegValue);
}
//------------------------------------------
// readCycleCountRegs()
//------------------------------------------
void readCycleCountRegs(ctlList *p)
{
uint8_t regCC[7]= { 0, 0, 0, 0, 0, 0, 0 };
// i2c_setAddress(p->pi, p->magnetometerAddr);
// Read register settings
// i2c_readbuf(p->pi, RM3100I2C_CCX_1, regCC, 7);
fprintf(stdout, "regCC[%i]: 0x%X\n", 0, (uint8_t)regCC[0]);
fprintf(stdout, "regCC[%i]: 0x%X\n", 1, (uint8_t)regCC[1]);
fprintf(stdout, "regCC[%i]: 0x%X\n", 2, (uint8_t)regCC[2]);
fprintf(stdout, "regCC[%i]: 0x%X\n", 3, (uint8_t)regCC[3]);
fprintf(stdout, "regCC[%i]: 0x%X\n", 4, (uint8_t)regCC[4]);
fprintf(stdout, "regCC[%i]: 0x%X\n", 5, (uint8_t)regCC[5]);
fprintf(stdout, "regCC[%i]: 0x%X\n\n", 6, (uint8_t)regCC[6]);
}