7
7
#include <stdio.h>
8
8
#include <string.h>
9
9
#include "pico/stdlib.h"
10
+ #include "pico/binary_info.h"
10
11
#include "hardware/i2c.h"
11
12
12
13
/* Example code to talk to a MPU6050 MEMS accelerometer and gyroscope
23
24
24
25
Connections on Raspberry Pi Pico board, other boards may vary.
25
26
26
- GPIO 4 (pin 6)-> SDA on MPU6050 board
27
- GPIO 5 (pin 7)-> SCL on MPU6050 board
27
+ GPIO PICO_DEFAULT_I2C_SDA_PIN (On Pico this is 4 (pin 6)) -> SDA on MPU6050 board
28
+ GPIO PICO_DEFAULT_I2C_SCK_PIN (On Pico this is 5 (pin 7)) -> SCL on MPU6050 board
28
29
3.3v (pin 36) -> VCC on MPU6050 board
29
30
GND (pin 38) -> GND on MPU6050 board
30
31
*/
31
32
32
33
// By default these devices are on bus address 0x68
33
34
static int addr = 0x68 ;
34
35
35
- #define I2C_PORT i2c0
36
-
36
+ #ifdef i2c_default
37
37
static void mpu6050_reset () {
38
38
// Two byte reset. First byte register, second byte data
39
39
// There are a load more options to set up the device in different ways that could be added here
40
40
uint8_t buf [] = {0x6B , 0x00 };
41
- i2c_write_blocking (I2C_PORT , addr , buf , 2 , false);
41
+ i2c_write_blocking (i2c_default , addr , buf , 2 , false);
42
42
}
43
43
44
44
static void mpu6050_read_raw (int16_t accel [3 ], int16_t gyro [3 ], int16_t * temp ) {
@@ -50,8 +50,8 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
50
50
51
51
// Start reading acceleration registers from register 0x3B for 6 bytes
52
52
uint8_t val = 0x3B ;
53
- i2c_write_blocking (I2C_PORT , addr , & val , 1 , true); // true to keep master control of bus
54
- i2c_read_blocking (I2C_PORT , addr , buffer , 6 , false);
53
+ i2c_write_blocking (i2c_default , addr , & val , 1 , true); // true to keep master control of bus
54
+ i2c_read_blocking (i2c_default , addr , buffer , 6 , false);
55
55
56
56
for (int i = 0 ; i < 3 ; i ++ ) {
57
57
accel [i ] = (buffer [i * 2 ] << 8 | buffer [(i * 2 ) + 1 ]);
@@ -60,8 +60,8 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
60
60
// Now gyro data from reg 0x43 for 6 bytes
61
61
// The register is auto incrementing on each read
62
62
val = 0x43 ;
63
- i2c_write_blocking (I2C_PORT , addr , & val , 1 , true);
64
- i2c_read_blocking (I2C_PORT , addr , buffer , 6 , false); // False - finished with bus
63
+ i2c_write_blocking (i2c_default , addr , & val , 1 , true);
64
+ i2c_read_blocking (i2c_default , addr , buffer , 6 , false); // False - finished with bus
65
65
66
66
for (int i = 0 ; i < 3 ; i ++ ) {
67
67
gyro [i ] = (buffer [i * 2 ] << 8 | buffer [(i * 2 ) + 1 ]);;
@@ -70,23 +70,29 @@ static void mpu6050_read_raw(int16_t accel[3], int16_t gyro[3], int16_t *temp) {
70
70
// Now temperature from reg 0x41 for 2 bytes
71
71
// The register is auto incrementing on each read
72
72
val = 0x41 ;
73
- i2c_write_blocking (I2C_PORT , addr , & val , 1 , true);
74
- i2c_read_blocking (I2C_PORT , addr , buffer , 2 , false); // False - finished with bus
73
+ i2c_write_blocking (i2c_default , addr , & val , 1 , true);
74
+ i2c_read_blocking (i2c_default , addr , buffer , 2 , false); // False - finished with bus
75
75
76
76
* temp = buffer [0 ] << 8 | buffer [1 ];
77
77
}
78
+ #endif
78
79
79
80
int main () {
80
81
stdio_init_all ();
81
-
82
+ #if !defined(i2c_default ) || !defined(PICO_DEFAULT_I2C_SDA_PIN ) || !defined(PICO_DEFAULT_I2C_SCL_PIN )
83
+ #warning i2c/bus_scane example requires a board with I2C pins
84
+ puts ("Default I2C pins were not defined');
85
+ #else
82
86
printf ("Hello, MPU6050! Reading raw data from registers...\n" );
83
87
84
- // This example will use I2C0 on GPIO4 (SDA) and GPIO5 (SCL) running at 400kHz.
85
- i2c_init (I2C_PORT , 400 * 1000 );
86
- gpio_set_function (4 , GPIO_FUNC_I2C );
87
- gpio_set_function (5 , GPIO_FUNC_I2C );
88
- gpio_pull_up (4 );
89
- gpio_pull_up (5 );
88
+ // This example will use I2C0 on the default SDA and SCL pins (4, 5 on a Pico)
89
+ i2c_init (i2c_default , 400 * 1000 );
90
+ gpio_set_function (PICO_DEFAULT_I2C_SDA_PIN , GPIO_FUNC_I2C );
91
+ gpio_set_function (PICO_DEFAULT_I2C_SCL_PIN , GPIO_FUNC_I2C );
92
+ gpio_pull_up (PICO_DEFAULT_I2C_SDA_PIN );
93
+ gpio_pull_up (PICO_DEFAULT_I2C_SCL_PIN );
94
+ // Make the I2C pins available to picotool
95
+ bi_decl (bi_2pins_with_func (PICO_DEFAULT_I2C_SDA_PIN , PICO_DEFAULT_I2C_SCL_PIN , GPIO_FUNC_I2C ));
90
96
91
97
mpu6050_reset ();
92
98
@@ -106,5 +112,6 @@ int main() {
106
112
sleep_ms (100 );
107
113
}
108
114
115
+ #endif
109
116
return 0 ;
110
117
}
0 commit comments