Edison MCU Project
 All Files Functions Typedefs Macros Pages
sample_i2c.c
Go to the documentation of this file.
1 /************************************************************************************
2  * Copyright (c) 2015, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  * list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
19  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
23  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
24  * OF THE POSSIBILITY OF SUCH DAMAGE.
25  ************************************************************************************/
26 
31 #include "mcu_api.h"
32 #include "mcu_errno.h"
33 
34 /* the I2C address of MPU6050 */
35 #define MPU6050_ADDR 0x68
36 /* the register address of WHOAMI register, which is used to verify the identity of the device */
37 #define MPU6050_WHOAMI ((unsigned char)0x75)
38 #define MPU6050_WHOAMI_VALUE ((unsigned char)0x68)
39 
40 
41 static unsigned char mpu6050_read_whoami()
42 {
43  unsigned char id;
44  int res;
45  res = i2c_read(MPU6050_ADDR, MPU6050_WHOAMI, &id, 1);
46  if (res)
47  {
48  debug_print(DBG_ERROR, "mpu6050_i2c_read whoami fail...\n");
49  return 0Xff;
50  }
51  if (id != MPU6050_WHOAMI_VALUE)
52  {
53  debug_print(DBG_ERROR, "wrong chip ID 0x%x, expected 0x%x\n", id, MPU6050_WHOAMI_VALUE);
54  return 0xff;
55  }
56  return id;
57 }
58 
59 
60 void mcu_main()
61 {
62  unsigned char device_id;
63  char buf[16];
64  int len;
65 
66  while (1)
67  {
68  mcu_sleep(300);
69  debug_print(DBG_INFO, "after sleep 3 seconds\n");
70  device_id = mpu6050_read_whoami();
71  len = mcu_snprintf(buf, 16, "id = %d\n", device_id);
72  host_send((unsigned char*)buf, len);
73  }
74 }
Intel MCU API definition.
void mcu_main()
Definition: sample_i2c.c:60
#define MPU6050_WHOAMI
Definition: sample_i2c.c:37
#define DBG_INFO
Definition: mcu_api.h:41
void debug_print(int level, const char *fmt,...)
void mcu_sleep(int ticks)
#define MPU6050_ADDR
Definition: sample_i2c.c:35
#define DBG_ERROR
Definition: mcu_api.h:39
#define MPU6050_WHOAMI_VALUE
Definition: sample_i2c.c:38
int i2c_read(unsigned char addr, unsigned char reg, unsigned char *buf, int len)
int host_send(unsigned char *buf, int length)
int mcu_snprintf(char *buf, unsigned int size, const char *fmt,...)
Intel MCU API error code definition.