This is an automated email from the ASF dual-hosted git repository. vipulrahane pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
The following commit(s) were added to refs/heads/master by this push: new b91942a Add SPI to lps33hw driver (#991) b91942a is described below commit b91942a8b5d269c5da403a40cddd9cb3b3a59e5d Author: Jamirsen <20569324+jamir...@users.noreply.github.com> AuthorDate: Wed Apr 4 17:27:39 2018 -0700 Add SPI to lps33hw driver (#991) - adding set_config function for lps33hw sensor driver - adding and setting SPI configuration - adding documentation --- .../sensors/lps33hw/include/lps33hw/lps33hw.h | 2 + hw/drivers/sensors/lps33hw/src/lps33hw.c | 210 ++++++++++++++++++++- 2 files changed, 205 insertions(+), 7 deletions(-) diff --git a/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h b/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h index ae2ecea..6648e74 100644 --- a/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h +++ b/hw/drivers/sensors/lps33hw/include/lps33hw/lps33hw.h @@ -30,6 +30,8 @@ extern "C" { #define LPS33HW_I2C_ADDR (0x5C) +#define LPS33HW_SPI_READ_CMD_BIT (0x80) + #define LPS33HW_INT_LEVEL (0x80) #define LPS33HW_INT_OPEN (0x40) #define LPS33HW_INT_LATCH_EN (0x20) diff --git a/hw/drivers/sensors/lps33hw/src/lps33hw.c b/hw/drivers/sensors/lps33hw/src/lps33hw.c index 9955dfb..c3b5872 100644 --- a/hw/drivers/sensors/lps33hw/src/lps33hw.c +++ b/hw/drivers/sensors/lps33hw/src/lps33hw.c @@ -24,6 +24,8 @@ #include "os/mynewt.h" #include "hal/hal_i2c.h" +#include "hal/hal_spi.h" +#include "hal/hal_gpio.h" #include "sensor/sensor.h" #include "sensor/pressure.h" #include "sensor/temperature.h" @@ -32,7 +34,12 @@ #include "log/log.h" #include "stats/stats.h" -#include "lps33hw_priv.h" +static struct hal_spi_settings spi_lps33hw_settings = { + .data_order = HAL_SPI_MSB_FIRST, + .data_mode = HAL_SPI_MODE3, + .baudrate = 4000, + .word_size = HAL_SPI_WORD_SIZE_8BIT, +}; /* Define the stats section and records */ STATS_SECT_START(lps33hw_stat_section) @@ -63,6 +70,7 @@ static int lps33hw_sensor_read(struct sensor *, sensor_type_t, sensor_data_func_t, void *, uint32_t); static int lps33hw_sensor_get_config(struct sensor *, sensor_type_t, struct sensor_cfg *); +static int lps33hw_sensor_set_config(struct sensor *, void *); static int lps33hw_sensor_set_trigger_thresh(struct sensor *sensor, sensor_type_t sensor_type, struct sensor_type_traits *stt); static int lps33hw_sensor_handle_interrupt(struct sensor *sensor); @@ -74,6 +82,7 @@ static int lps33hw_sensor_clear_high_thresh(struct sensor *sensor, static const struct sensor_driver g_lps33hw_sensor_driver = { .sd_read = lps33hw_sensor_read, .sd_get_config = lps33hw_sensor_get_config, + .sd_set_config = lps33hw_sensor_set_config, .sd_set_trigger_thresh = lps33hw_sensor_set_trigger_thresh, .sd_handle_interrupt = lps33hw_sensor_handle_interrupt, .sd_clear_low_trigger_thresh = lps33hw_sensor_clear_low_thresh, @@ -145,9 +154,9 @@ lps33hw_reg_to_degc(int16_t reg) return reg / LPS33HW_TEMP_OUT_DIV; } - /** - * Writes a single byte to the specified register + * Writes a single byte to the specified register using i2c + * interface * * @param The sensor interface * @param The register address to write to @@ -156,7 +165,7 @@ lps33hw_reg_to_degc(int16_t reg) * @return 0 on success, non-zero error on failure. */ static int -lps33hw_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value) +lps33hw_i2c_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value) { int rc; uint8_t payload[2] = { reg, value }; @@ -180,16 +189,145 @@ lps33hw_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value) } /** - * Read bytes from the specified register + * Writes a single byte to the specified register using SPI + * interface + * + * @param The sensor interface + * @param The register address to write to + * @param The value to write + * + * @return 0 on success, non-zero error on failure. + */ +static int +lps33hw_spi_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value) +{ + int rc; + + /* Select the device */ + hal_gpio_write(itf->si_cs_pin, 0); + + /* Send the register address w/write command */ + rc = hal_spi_tx_val(itf->si_num, reg & ~LPS33HW_SPI_READ_CMD_BIT); + if (rc == 0xFFFF) { + rc = SYS_EINVAL; + LPS33HW_ERR("SPI_%u register write failed addr:0x%02X\n", + itf->si_num, reg); + STATS_INC(g_lps33hwstats, write_errors); + goto err; + } + + /* Write data */ + rc = hal_spi_tx_val(itf->si_num, value); + if (rc == 0xFFFF) { + rc = SYS_EINVAL; + LPS33HW_ERR("SPI_%u write failed addr:0x%02X:0x%02X\n", + itf->si_num, reg); + STATS_INC(g_lps33hwstats, write_errors); + goto err; + } + + rc = 0; + +err: + /* De-select the device */ + hal_gpio_write(itf->si_cs_pin, 1); + + os_time_delay((OS_TICKS_PER_SEC * 30)/1000 + 1); + + return rc; +} + +/** + * Writes a single byte to the specified register using specified + * interface + * + * @param The sensor interface + * @param The register address to write to + * @param The value to write + * + * @return 0 on success, non-zero error on failure. + */ +static int +lps33hw_set_reg(struct sensor_itf *itf, uint8_t reg, uint8_t value) +{ + int rc; + + if (itf->si_type == SENSOR_ITF_I2C) { + rc = lps33hw_i2c_set_reg(itf, reg, value); + } else { + rc = lps33hw_spi_set_reg(itf, reg, value); + } + + return rc; +} + +/** + * + * Read bytes from the specified register using SPI interface * * @param The sensor interface * @param The register address to read from + * @param The number of bytes to read * @param Pointer to where the register value should be written * * @return 0 on success, non-zero error on failure. */ static int -lps33hw_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size, +lps33hw_spi_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size, + uint8_t *buffer) +{ + int i; + uint16_t retval; + int rc; + rc = 0; + + /* Select the device */ + hal_gpio_write(itf->si_cs_pin, 0); + + /* Send the address */ + retval = hal_spi_tx_val(itf->si_num, reg | LPS33HW_SPI_READ_CMD_BIT); + if (retval == 0xFFFF) { + rc = SYS_EINVAL; + LPS33HW_ERR("SPI_%u register write failed addr:0x%02X\n", + itf->si_num, reg); + STATS_INC(g_lps33hwstats, read_errors); + goto err; + } + + for (i = 0; i < size; i++) { + /* Read data */ + retval = hal_spi_tx_val(itf->si_num, 0); + if (retval == 0xFFFF) { + rc = SYS_EINVAL; + LPS33HW_ERR("SPI_%u read failed addr:0x%02X\n", + itf->si_num, reg); + STATS_INC(g_lps33hwstats, read_errors); + goto err; + } + buffer[i] = retval; + } + + rc = 0; + +err: + /* De-select the device */ + hal_gpio_write(itf->si_cs_pin, 1); + + return rc; +} + +/** + * Read bytes from the specified register using i2c interface + * + * @param The sensor interface + * @param The register address to read from + * @param The number of bytes to read + * @param Pointer to where the register value should be written + * + * @return 0 on success, non-zero error on failure. + */ +static int +lps33hw_i2c_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size, uint8_t *buffer) { int rc; @@ -222,6 +360,31 @@ lps33hw_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size, return rc; } +/** + * Read bytes from the specified register using specified interface + * + * @param The sensor interface + * @param The register address to read from + * @param The number of bytes to read + * @param Pointer to where the register value should be written + * + * @return 0 on success, non-zero error on failure. + */ +static int +lps33hw_get_regs(struct sensor_itf *itf, uint8_t reg, uint8_t size, + uint8_t *buffer) +{ + int rc; + + if (itf->si_type == SENSOR_ITF_I2C) { + rc = lps33hw_i2c_get_regs(itf, reg, size, buffer); + } else { + rc = lps33hw_spi_get_regs(itf, reg, size, buffer); + } + + return rc; +} + static int lps33hw_apply_value(struct lps33hw_register_value addr, uint8_t value, uint8_t *reg) @@ -723,7 +886,32 @@ lps33hw_init(struct os_dev *dev, void *arg) return rc; } - return sensor_mgr_register(sensor); + rc = sensor_mgr_register(sensor); + if (rc) { + return rc; + } + + if (sensor->s_itf.si_type == SENSOR_ITF_SPI) { + rc = hal_spi_config(sensor->s_itf.si_num, &spi_lps33hw_settings); + if (rc == EINVAL) { + /* If spi is already enabled, for nrf52, it returns -1, We should not + * fail if the spi is already enabled + */ + return rc; + } + + rc = hal_spi_enable(sensor->s_itf.si_num); + if (rc) { + return rc; + } + + rc = hal_gpio_init_out(sensor->s_itf.si_cs_pin, 1); + if (rc) { + return rc; + } + } + + return rc; } int @@ -858,6 +1046,14 @@ lps33hw_sensor_read(struct sensor *sensor, sensor_type_t type, } static int +lps33hw_sensor_set_config(struct sensor *sensor, void *cfg) +{ + struct lps33hw* lps33hw = (struct lps33hw *)SENSOR_GET_DEVICE(sensor); + + return lps33hw_config(lps33hw, (struct lps33hw_cfg*)cfg); +} + +static int lps33hw_sensor_get_config(struct sensor *sensor, sensor_type_t type, struct sensor_cfg *cfg) { -- To stop receiving notification emails like this one, please contact vipulrah...@apache.org.