Last active
November 9, 2024 18:53
-
-
Save todbot/1b215cad99d26af3a93fdca3c3b6c244 to your computer and use it in GitHub Desktop.
Test pico-sdk i2c functions in Arduino, in this case reading TouchwheelSAO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pico_i2c_test.ino -- test pico-sdk i2c functions in Arduino, in this case reading TouchwheelSAO | |
// 9 Nov 2024 - @todbot / Tod Kurt | |
// demo using pico-sdk i2c functions inside of Arduino sketch | |
// copies from https://github.com/raspberrypi/pico-examples/blob/master/i2c/lis3dh_i2c/lis3dh_i2c.c | |
// assumes compiling with arduino-pico Arduino core for Pico/PicoW | |
#include <Wire.h> | |
#define _i2c_dev i2c0 | |
#define _clkHz 400000 | |
#define _scl_pin 17 | |
#define _sda_pin 16 | |
// TouchwheelSAO is normally on address 0x54 | |
// but 0x56 is tod's second board set to a different address | |
const int i2c_addr = 0x54; | |
//const int i2c_addr = 0x56; | |
void i2c_begin() { | |
Wire.end(); // release i2c hardware so we can do it by hand | |
i2c_init(_i2c_dev, _clkHz); | |
i2c_set_slave_mode(_i2c_dev, false, 0); | |
gpio_set_function(_sda_pin, GPIO_FUNC_I2C); | |
gpio_pull_up(_sda_pin); | |
gpio_set_function(_scl_pin, GPIO_FUNC_I2C); | |
gpio_pull_up(_scl_pin); | |
} | |
void i2c_setreg(uint8_t reg, uint8_t val) { | |
uint8_t regval[2] = {reg, val}; | |
i2c_write_blocking(_i2c_dev, i2c_addr, regval, 2, false); | |
} | |
int i2c_readreg(uint8_t reg) { | |
uint8_t val = 0; | |
i2c_write_blocking(_i2c_dev, i2c_addr, ®, 1, true); // 'true' == retain bus control, but 'false' works too | |
i2c_read_blocking(_i2c_dev, i2c_addr, &val, 1, false); | |
return val; | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(1000); // wait a bit for USB serial to negotiate (actually takes about 6 secs so we blink too) | |
Serial.println("top of setup!"); | |
i2c_begin(); | |
for(int i=0; i<10; i++) { | |
Serial.println("blink on!"); | |
i2c_setreg(14, 1); // touchwheel status led on | |
delay(300); | |
Serial.println("blink off!"); | |
i2c_setreg(14, 0); // touchwheel status led off | |
delay(300); | |
} | |
Serial.println("end of setup"); | |
} | |
void loop() { | |
int val = i2c_readreg(0); | |
Serial.printf("loop! pos:%d\n", val); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment