From 973c4588d7eba2672838e13a84f5ca4e9ce86258 Mon Sep 17 00:00:00 2001 From: JohSchneider Date: Thu, 4 Aug 2022 11:01:03 +0000 Subject: [PATCH] pico: i2c: align code with chibios, avr, documentation (#217) some defines and function signatures did not quite match the documented ones - which lead to compile issues while building the same code that uses an i2c-driver based vial-qmk/driver for multiple platforms Signed-off-by: Johannes Schneider Co-authored-by: Johannes Schneider --- platforms/pico/drivers/i2c_master.c | 23 +++++++++++++++++------ platforms/pico/drivers/i2c_master.h | 18 ++++++------------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/platforms/pico/drivers/i2c_master.c b/platforms/pico/drivers/i2c_master.c index d2559fa01f..dc8f6b7c22 100644 --- a/platforms/pico/drivers/i2c_master.c +++ b/platforms/pico/drivers/i2c_master.c @@ -7,6 +7,17 @@ #include "pico/binary_info.h" #include "hardware/i2c.h" +#ifndef I2C1_SCL_PIN +# define I2C1_SCL_PIN 3 +#endif +#ifndef I2C1_SDA_PIN +# define I2C1_SDA_PIN 2 +#endif + +#ifndef I2C_DRIVER +# define I2C_DRIVER i2c1 +#endif + static uint8_t i2c_address; static i2c_status_t pico_status_to_qmk(int res) { @@ -25,16 +36,16 @@ void i2c_init_pico(void) { is_initialised = true; i2c_init(I2C_DRIVER, 50 * 1000); - gpio_set_function(I2C1_SCL, GPIO_FUNC_I2C); - gpio_set_function(I2C1_SDA, GPIO_FUNC_I2C); - gpio_pull_up(I2C1_SCL); - gpio_pull_up(I2C1_SDA); + gpio_set_function(I2C1_SCL_PIN, GPIO_FUNC_I2C); + gpio_set_function(I2C1_SDA_PIN, GPIO_FUNC_I2C); + gpio_pull_up(I2C1_SCL_PIN); + gpio_pull_up(I2C1_SDA_PIN); // Make the I2C pins available to picotool - bi_decl(bi_2pins_with_func(I2C1_SDA, I2C1_SCL, GPIO_FUNC_I2C)); + bi_decl(bi_2pins_with_func(I2C1_SDA_PIN, I2C1_SCL_PIN, GPIO_FUNC_I2C)); } } -i2c_status_t i2c_start(uint8_t address) { +i2c_status_t i2c_start(uint8_t address, uint16_t timeout) { i2c_address = address; return I2C_STATUS_SUCCESS; } diff --git a/platforms/pico/drivers/i2c_master.h b/platforms/pico/drivers/i2c_master.h index 86de24918d..5c18609d6b 100644 --- a/platforms/pico/drivers/i2c_master.h +++ b/platforms/pico/drivers/i2c_master.h @@ -26,26 +26,20 @@ #include -#ifndef I2C1_SCL -# define I2C1_SCL 3 -#endif -#ifndef I2C1_SDA -# define I2C1_SDA 2 -#endif - -#ifndef I2C_DRIVER -# define I2C_DRIVER i2c1 -#endif - typedef int16_t i2c_status_t; #define I2C_STATUS_SUCCESS (0) #define I2C_STATUS_ERROR (-1) #define I2C_STATUS_TIMEOUT (-2) +/* collides with: + * pico-sdk/src/rp2_comon/hardware_i2c/include/hardware/i2c.h::i2c_init + * hence the bodge... + */ #define i2c_init() i2c_init_pico() void i2c_init_pico(void); -i2c_status_t i2c_start(uint8_t address); + +i2c_status_t i2c_start(uint8_t address, uint16_t timeout); i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout); i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout); i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout);