From 54c6d294afedbf778e7bdcd2e3182e46b5e2d847 Mon Sep 17 00:00:00 2001 From: "Antoine W. Campagna" Date: Sun, 25 Mar 2018 22:02:02 -0400 Subject: [PATCH] kx122: Allow faster SPI bus frequency The hardcoded frequency of 10kHz was much slower than the capacity of the device Signed-off-by: Antoine W. Campagna Signed-off-by: Noel Eck --- examples/c/kx122.c | 2 +- src/kx122/kx122.c | 10 ++++++++-- src/kx122/kx122.cxx | 3 ++- src/kx122/kx122.h | 5 +++-- src/kx122/kx122.hpp | 3 ++- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/examples/c/kx122.c b/examples/c/kx122.c index b6b0f560..bde030bc 100644 --- a/examples/c/kx122.c +++ b/examples/c/kx122.c @@ -43,7 +43,7 @@ int main(int argc, char **argv) signal(SIGINT,sig_handler); //! [Interesting] - kx122_context sensor = kx122_init(0,-1,24); + kx122_context sensor = kx122_init(0, -1, 24, 10000); if (!sensor) { printf("kx122_init() failed.\n"); diff --git a/src/kx122/kx122.c b/src/kx122/kx122.c index d8141f21..8a5718ad 100644 --- a/src/kx122/kx122.c +++ b/src/kx122/kx122.c @@ -161,7 +161,7 @@ Sets acceleration scale of sensor and the buffer. */ static void kx122_map_grange(const kx122_context dev, KX122_RANGE_T grange); -kx122_context kx122_init(int bus, int addr, int chip_select_pin) +kx122_context kx122_init(int bus, int addr, int chip_select_pin, int spi_bus_frequency) { kx122_context dev = (kx122_context)malloc(sizeof(struct _kx122_context)); @@ -190,6 +190,12 @@ kx122_context kx122_init(int bus, int addr, int chip_select_pin) if(dev->using_spi){ + if (spi_bus_frequency > 10000000){ // KX122 has a maximum SPI bus speed of 10MHz + printf("%s: bus frequency too high - KX122 has a maximum SPI bus speed of 10MHz.\n", __FUNCTION__); + kx122_close(dev); + return NULL; + } + if (!(dev->spi = mraa_spi_init(bus))){ printf("%s: mraa_spi_init() failed.\n", __FUNCTION__); kx122_close(dev); @@ -205,7 +211,7 @@ kx122_context kx122_init(int bus, int addr, int chip_select_pin) mraa_gpio_dir(dev->chip_select,MRAA_GPIO_OUT); mraa_spi_mode(dev->spi,MRAA_SPI_MODE0); - if (mraa_spi_frequency(dev->spi,SPI_FREQUENCY)){ + if (mraa_spi_frequency(dev->spi, spi_bus_frequency)){ printf("%s: mraa_spi_frequency() failed.\n", __FUNCTION__); kx122_close(dev); return NULL; diff --git a/src/kx122/kx122.cxx b/src/kx122/kx122.cxx index f80e68cb..38f55b71 100644 --- a/src/kx122/kx122.cxx +++ b/src/kx122/kx122.cxx @@ -32,7 +32,8 @@ using namespace upm; -KX122::KX122(int bus, int addr, int chip_select) : m_kx122(kx122_init(bus,addr,chip_select)) +KX122::KX122(int bus, int addr, int chip_select, int spi_bus_frequency) + : m_kx122(kx122_init(bus, addr, chip_select, spi_bus_frequency)) { if(!m_kx122){ throw std::runtime_error(std::string(__FUNCTION__) + "kx122_init() failed"); diff --git a/src/kx122/kx122.h b/src/kx122/kx122.h index 235bc540..c078e03e 100644 --- a/src/kx122/kx122.h +++ b/src/kx122/kx122.h @@ -47,7 +47,7 @@ extern "C"{ */ //Frequency of the SPI connection -#define SPI_FREQUENCY 10000 +#define DEFAULT_SPI_FREQUENCY 10000 //Default slave addresses for the sensor #define KX122_DEFAULT_SLAVE_ADDR_1 0x1F @@ -170,9 +170,10 @@ If no errors occur, the device gets initialized with default values and gets set @param bus I2C or SPI bus to use. @param addr I2C address of the sensor. @param chip_select Chip select pin for SPI. +@param spi_bus_frequency Speed of the SPI communication bus in Hz (ignored when using I2C). @return The device context, or NULL if an error occurs. */ -kx122_context kx122_init(int bus, int addr, int chip_select_pin); +kx122_context kx122_init(int bus, int addr, int chip_select_pin, int spi_bus_frequency); /** KX122 destructor diff --git a/src/kx122/kx122.hpp b/src/kx122/kx122.hpp index 4b474b96..1bcc8ec8 100644 --- a/src/kx122/kx122.hpp +++ b/src/kx122/kx122.hpp @@ -50,9 +50,10 @@ namespace upm{ @param bus I2C or SPI bus to use. @param addr I2C address of the sensor. @param chip_select Chip select pin for SPI. + @param spi_bus_frequency Speed of the SPI communication bus in Hz. @throws std::runtime_error on initialization failure. */ - KX122(int bus, int addr, int chip_select); + KX122(int bus, int addr, int chip_select, int spi_bus_frequency = DEFAULT_SPI_FREQUENCY); /** KX122 destructor