Skip to content

Commit

Permalink
BHY2 interface adapt to RP2040
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Sep 7, 2023
1 parent f276954 commit cc80f3a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/bosch/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,24 +97,43 @@ bool setup_interfaces(bool reset_power, bhy_config_t config)
pinMode(config.irq, INPUT_PULLDOWN);
switch (config.intf) {
case BHY2_I2C_INTERFACE:
#if defined(ARDUINO_ARCH_RP2040)
config.u.i2c_dev.wire->end();
config.u.i2c_dev.wire->setSDA(config.u.i2c_dev.sda);
config.u.i2c_dev.wire->setSCL(config.u.i2c_dev.scl);
config.u.i2c_dev.wire->begin();
#else
config.u.i2c_dev.wire->begin(config.u.i2c_dev.sda, config.u.i2c_dev.scl);
#endif
config.u.i2c_dev.wire->beginTransmission(config.u.i2c_dev.addr);
if (config.u.i2c_dev.wire->endTransmission() != 0) {
return false;
}
break;
case BHY2_SPI_INTERFACE:
pinMode(config.u.spi_dev.cs, OUTPUT);

#if defined(ARDUINO_ARCH_RP2040)
config.u.spi_dev.spi->setSCK(config.u.spi_dev.sck);
config.u.spi_dev.spi->setRX(config.u.spi_dev.miso);
config.u.spi_dev.spi->setTX(config.u.spi_dev.mosi);
config.u.spi_dev.spi->begin();
#else
config.u.spi_dev.spi->begin(config.u.spi_dev.sck, config.u.spi_dev.miso, config.u.spi_dev.mosi);
#endif
break;
default:
break;
}
return true;
}


#if defined(ARDUINO_ARCH_RP2040)
static SPISettings __spiSetting = SPISettings();
#else
static SPISettings __spiSetting = SPISettings(4000000, SPI_MSBFIRST, SPI_MODE0);
#endif


int8_t bhy2_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t length, void *intf_ptr)
{
Expand Down Expand Up @@ -1079,6 +1098,5 @@ void check_bhy2_api(unsigned int line, const char *func, int8_t val)
get_api_error(val));
delay(1000);
}
esp_restart();
}

4 changes: 4 additions & 0 deletions src/bosch/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ typedef struct bhy_config {
int miso;
int mosi;
int sck;
#if defined(ARDUINO_ARCH_RP2040)
SPIClassRP2040 *spi;
#else
SPIClass *spi;
#endif
} spi_dev;
} u ;
} bhy_config_t;
Expand Down

0 comments on commit cc80f3a

Please sign in to comment.