aboutsummaryrefslogtreecommitdiff
path: root/apps/px4/sensors_bringup/sensors_main.c
blob: 3aa21ae14a640995268bbaf5ce7d68c1f1fc3645 (plain) (blame)
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/****************************************************************************
 *
 *   Copyright (C) 2012 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>

#include <sys/types.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <debug.h>

#include <arch/board/board.h>

#include <nuttx/spi.h>
#include <nuttx/i2c.h>

#include "sensors.h"

__EXPORT int sensors_bringup_main(int argc, char *argv[]);

/****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/****************************************************************************
 * Private Types
 ****************************************************************************/

/****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/****************************************************************************
 * Private Data
 ****************************************************************************/

/****************************************************************************
 * Public Data
 ****************************************************************************/

/****************************************************************************
 * Private Functions
 ****************************************************************************/

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * Name: user_start/adc_main
 ****************************************************************************/

int sensors_bringup_main(int argc, char *argv[])
{
	struct spi_dev_s *spi;
	int result = -1;

	spi = up_spiinitialize(1);
	if (!spi) {
		message("Failed to initialize SPI port 1\n");
		goto out;
	}

	struct i2c_dev_s *i2c;
	i2c = up_i2cinitialize(2);
	if (!i2c) {
		message("Failed to initialize I2C bus 2\n");
		goto out;
	}

	int ret;

#define EEPROM_ADDRESS		0x50
#define HMC5883L_ADDRESS	0x1E

	//uint8_t devaddr = EEPROM_ADDRESS;

	I2C_SETFREQUENCY(i2c, 100000);
//
//	uint8_t subaddr = 0x00;
//	int ret = 0;
//
//	// ATTEMPT HMC5883L CONFIG
//	I2C_SETADDRESS(i2c, HMC5883L_ADDRESS, 7);
//	subaddr = 0x02; // mode register
//	ret = I2C_WRITE(i2c, &subaddr, 0);
//	if (ret < 0)
//	{
//		message("I2C_WRITE failed: %d\n", ret);
//	}
//	else
//	{
//		message("I2C_WRITE SUCCEEDED: %d\n", ret);
//	}

	//fflush(stdout);
//
//
//


#define STATUS_REGISTER		0x09 // Of HMC5883L

	// ATTEMPT HMC5883L WRITE
	I2C_SETADDRESS(i2c, HMC5883L_ADDRESS, 7);
	uint8_t cmd = 0x09;
	uint8_t status_id[4] = {0, 0, 0, 0};


	ret = I2C_WRITEREAD(i2c, &cmd, 1, status_id, 4);

	if (ret >= 0 && status_id[1] == 'H' && status_id[2] == '4' && status_id[3] == '3')
	{
		message("HMC5883L identified, device status: %d\n", status_id[0]);
	} else {
		message("HMC5883L identification failed: %d\n", ret);
	}

#define HMC5883L_ADDR_CONF_A				0x00
#define HMC5883L_ADDR_CONF_B				0x01
#define HMC5883L_ADDR_MODE					0x02

#define HMC5883L_AVERAGING_1		(0 << 5) /* conf a register */
#define HMC5883L_AVERAGING_2		(1 << 5)
#define HMC5883L_AVERAGING_4		(2 << 5)
#define HMC5883L_AVERAGING_8		(3 << 5)

#define HMC5883L_RATE_75HZ			(6 << 2) /* 75 Hz */

#define HMC5883L_RANGE_0_88GA			(0 << 5)

	uint8_t rate_cmd[] = {HMC5883L_ADDR_CONF_A, HMC5883L_RATE_75HZ | HMC5883L_AVERAGING_8};
	ret = I2C_WRITE(i2c, rate_cmd, sizeof(rate_cmd));
	message("Wrote %d into register 0x00 of HMC, result: %d (0 = success)\n", HMC5883L_RATE_75HZ | HMC5883L_AVERAGING_8, ret);

	uint8_t range_cmd[] = {HMC5883L_ADDR_CONF_B, HMC5883L_RANGE_0_88GA};
	ret = I2C_WRITE(i2c, range_cmd, sizeof(range_cmd));
	message("Wrote %d into register 0x01 of HMC, result: %d (0 = success)\n", HMC5883L_RANGE_0_88GA, ret);

	// Set HMC into continous mode
	// First write address, then value
	uint8_t cont_address[] = {HMC5883L_ADDR_MODE, 0x00};
	ret = I2C_WRITE(i2c, cont_address, sizeof(cont_address));

	message("Wrote 0x00 into register 0x02 of HMC, result: %d (0 = success)\n", ret);


	// ATTEMPT HMC5883L READ
	int h = 0;

	I2C_SETADDRESS(i2c, HMC5883L_ADDRESS, 7);
	for (h = 0; h < 5; h++)
	{

		cont_address[0] = HMC5883L_ADDR_MODE;
		cont_address[1] = 0x01;
		ret = I2C_WRITE(i2c, cont_address, sizeof(cont_address));

		message("Wrote 0x01 into register 0x02 of HMC, result: %d (0 = success)\n", ret);

		usleep(100000);

		cont_address[1] = 0x00;
		uint8_t dummy;
		ret = I2C_WRITEREAD(i2c, cont_address, sizeof(cont_address), &dummy, 1);

		message("Wrote 0x00 into register 0x02 of HMC, result: %d (0 = success)\n", ret);

		usleep(100000);


		int16_t hmc5883l_data[3] = {0, 0, 0};
		uint8_t data_address = 0x03;
		uint8_t* data_ptr = (uint8_t*)hmc5883l_data;
		ret = I2C_WRITEREAD(i2c, &data_address, 1, data_ptr, 6);
		if (ret < 0)
		{
			message("HMC5883L READ failed: %d\n", ret);
		}
		else
		{
			// mask out top four bits as only 12 bits are valid
			hmc5883l_data[0] &= 0xFFF;
			hmc5883l_data[1] &= 0xFFF;
			hmc5883l_data[2] &= 0xFFF;

			message("HMC5883L READ SUCCEEDED: %d, val: %d %d %d\n", ret, hmc5883l_data[0], hmc5883l_data[1], hmc5883l_data[2]);
			uint8_t hmc_status;
			ret = I2C_WRITEREAD(i2c, &cmd, 1, &hmc_status, 1);

			message("\t status: %d\n", hmc_status);
		}
	}


	// Possible addresses: 0x77 or 0x76
#define MS5611_ADDRESS_1	0x76
#define MS5611_ADDRESS_2	0x77
	I2C_SETADDRESS(i2c, MS5611_ADDRESS_1, 7);
	// Reset cmd
	uint8_t ms5611_cmd[2] = {0x00, 0x1E};
	ret = I2C_WRITE(i2c, ms5611_cmd, 2);
	if (ret < 0)
	{
		message("MS5611 #1 WRITE failed: %d\n", ret);
	}
	else
	{
		message("MS5611 #1 WRITE SUCCEEDED: %d\n", ret);
	}

	fflush(stdout);

	I2C_SETADDRESS(i2c, MS5611_ADDRESS_2, 7);
	ret = I2C_WRITE(i2c, ms5611_cmd, 2);
	if (ret < 0)
	{
		message("MS5611 #2 WRITE failed: %d\n", ret);
	}
	else
	{
		message("MS5611 #2 WRITE SUCCEEDED: %d\n", ret);
	}

	fflush(stdout);


	// Wait for reset to complete (10 ms nominal, wait: 100 ms)
	usleep(100000);

	// Read PROM data
	uint8_t prom_buf[2] = {0,1};

	uint16_t calibration[6];

	int i = 0;

	prom_buf[0] = 0xA2 + (i*2);

	struct i2c_msg_s msgv[2] = {
	        {
	            .addr   = MS5611_ADDRESS_2,
	            .flags  = 0,
	            .buffer = prom_buf,
	            .length = 1
	        },
	        {
	            .addr   = MS5611_ADDRESS_2,
	            .flags  = I2C_M_READ,
	            .buffer = prom_buf,
	            .length = 1
	        }
	    };

	calibration[i] = prom_buf[0]*256;
	calibration[i]+= prom_buf[1];

	int retval;

	if ( (retval = I2C_TRANSFER(i2c, msgv, 2)) == OK )
	{
		printf("SUCCESS ACCESSING PROM OF MS5611: %d, value C1: %d\n", retval, (int)calibration[0]);
	}
	else
	{
		printf("FAIL ACCESSING PROM OF MS5611\n");
	}




	// TESTING CODE, EEPROM READ/WRITE
	uint8_t val[1] = {10};
	int retval_eeprom;
	uint8_t eeprom_subaddr[2] = {0, 0};

	struct i2c_msg_s msgv_eeprom[2] = {
	        {
	            .addr   = EEPROM_ADDRESS,
	            .flags  = 0,
	            .buffer = eeprom_subaddr,
	            .length = 2
	        },
	        {
	            .addr   = EEPROM_ADDRESS,
	            .flags  = I2C_M_READ,
	            .buffer = val,
	            .length = 1
	        }
	    };

	val[0] = 5;

	if ( (retval_eeprom = I2C_TRANSFER(i2c, msgv_eeprom, 2)) == OK )
	{
		printf("SUCCESS READING EEPROM: %d, value: %d\n", retval_eeprom, (int)val[0]);
	}
	else
	{
		printf("FAIL READING EEPROM: %d, value: %d\n", retval_eeprom, (int)val[0]);
	}

	// Increment val
	val[0] = val[0] + 1;

	struct i2c_msg_s msgv_eeprom_write[2] = {
		        {
		            .addr   = EEPROM_ADDRESS,
		            .flags  = I2C_M_NORESTART,
		            .buffer = eeprom_subaddr,
		            .length = 2
		        },
		        {
		            .addr   = EEPROM_ADDRESS,
		            .flags  = I2C_M_NORESTART,
		            .buffer = val,
		            .length = 1
		        }
	};


	if ( (retval_eeprom = I2C_TRANSFER(i2c, msgv_eeprom_write, 2)) == OK )
	{
		printf("SUCCESS WRITING EEPROM: %d\n", retval_eeprom);
	}

	usleep(10000);

	struct i2c_msg_s msgv_eeprom2[2] = {
	        {
	            .addr   = EEPROM_ADDRESS,
	            .flags  = 0,
	            .buffer = eeprom_subaddr,
	            .length = 2
	        },
	        {
	            .addr   = EEPROM_ADDRESS,
	            .flags  = I2C_M_READ,
	            .buffer = val,
	            .length = 1
	        }
	    };

	val[0] = 5;


	if ( (retval_eeprom = I2C_TRANSFER(i2c, msgv_eeprom2, 2)) == OK )
	{
		printf("SUCCESS READING WRITE RESULT EEPROM: %d, value: %d\n", retval_eeprom, (int)val[0]);
	}
	else
	{
		printf("FAIL READING WRITE RESULT EEPROM: %d, value: %d\n", retval_eeprom, (int)val[0]);
	}

	// Configure sensors
	l3gd20_test_configure(spi);
	bma180_test_configure(spi);

	for (int i = 0; i < 3; i++)
	{
		l3gd20_test_read(spi);
		bma180_test_read(spi);
		printf("# %d of 10\n", i+1);
		usleep(50000);
	}


	out:
   	msgflush();
	return result;
}