-
Notifications
You must be signed in to change notification settings - Fork 287
/
Copy pathmraa_i2c.c
57 lines (46 loc) · 1.11 KB
/
mraa_i2c.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
/*
* i2c example in c using mraa
*
* Author: Manivannan Sadhasivam <[email protected]>
*
* Usage: Checks for MPU6050 sensor at I2C bus 0
*
* Compilation: gcc mraa_i2c.c -o bin -lmraa
*
*/
/* standard headers */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* mraa header */
#include "mraa/i2c.h"
/* i2c bus */
#define I2C_BUS 0
/* sensor address */
#define MPU6050_ADDR 0x68
/* WHO_AM_I register address */
#define MPU6050_REG_WHO_AM_I 0x75
int main(void)
{
uint8_t data;
mraa_result_t status = MRAA_SUCCESS;
/* initialize i2c bus */
mraa_i2c_context i2c = mraa_i2c_init(I2C_BUS);
if (i2c == NULL)
goto err_exit;
/* set slave address */
status = mraa_i2c_address(i2c, MPU6050_ADDR);
if (status != MRAA_SUCCESS)
goto err_exit;
/* read WHO_AM_I register and compare its value */
data = mraa_i2c_read_byte_data(i2c, MPU6050_REG_WHO_AM_I);
if (data & 0x7E != MPU6050_ADDR) {
fprintf(stderr, "Device not found!\n");
goto err_exit;
}
fprintf(stdout, "MPU6050 found at: 0x%02x\n", data & 0x7E);
err_exit:
mraa_i2c_stop(i2c);
return status;
}