Skip to content

Commit 64fd822

Browse files
hmhalvorsengalak
authored andcommitted
drivers: sensor: adxl362: Driver update and CS support
Updated to add support for CS. DT config names updated to adhere to the DTS naming convention. Init and SPI configuration now follows the device datasheet. Signed-off-by: Henrik Malvik Halvorsen <[email protected]>
1 parent 5cd9227 commit 64fd822

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

drivers/sensor/adxl362/adxl362.c

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,22 +574,44 @@ static int adxl362_init(struct device *dev)
574574
const struct adxl362_config *config = dev->config->config_info;
575575
struct adxl362_data *data = dev->driver_data;
576576
u8_t value;
577+
int err;
577578

578579
data->spi = device_get_binding(config->spi_name);
579580
if (!data->spi) {
580581
LOG_DBG("spi device not found: %s", config->spi_name);
581582
return -EINVAL;
582583
}
583584

584-
data->spi_cfg.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB |
585-
SPI_MODE_CPOL | SPI_MODE_CPHA;
585+
data->spi_cfg.operation = SPI_WORD_SET(8) | SPI_TRANSFER_MSB;
586586
data->spi_cfg.frequency = config->spi_max_frequency;
587587
data->spi_cfg.slave = config->spi_slave;
588588

589-
adxl362_software_reset(dev);
589+
#if defined(DT_ADI_ADXL362_0_CS_GPIO_CONTROLLER)
590+
data->adxl362_cs_ctrl.gpio_dev =
591+
device_get_binding(config->gpio_cs_port);
592+
if (!data->adxl362_cs_ctrl.gpio_dev) {
593+
LOG_ERR("Unable to get GPIO SPI CS device");
594+
return -ENODEV;
595+
}
596+
597+
data->adxl362_cs_ctrl.gpio_pin = config->cs_gpio;
598+
data->adxl362_cs_ctrl.delay = 0;
599+
600+
data->spi_cfg.cs = &data->adxl362_cs_ctrl;
601+
#endif
602+
603+
err = adxl362_software_reset(dev);
604+
605+
if (err) {
606+
LOG_ERR("adxl362_software_reset failed, error %d\n", err);
607+
return -ENODEV;
608+
}
609+
610+
k_sleep(5);
590611

591612
adxl362_get_reg(dev, &value, ADXL362_REG_PARTID, 1);
592613
if (value != ADXL362_PART_ID) {
614+
LOG_ERR("Failed: %d\n", value);
593615
return -ENODEV;
594616
}
595617

@@ -601,11 +623,15 @@ static int adxl362_init(struct device *dev)
601623
}
602624

603625
static const struct adxl362_config adxl362_config = {
604-
.spi_name = DT_ADXL362_SPI_DEV_NAME,
605-
.spi_slave = DT_ADXL362_SPI_DEV_SLAVE,
606-
.spi_max_frequency = DT_ADXL362_SPI_MAX_FREQUENCY,
626+
.spi_name = DT_ADI_ADXL362_0_BUS_NAME,
627+
.spi_slave = DT_ADI_ADXL362_0_BASE_ADDRESS,
628+
.spi_max_frequency = DT_ADI_ADXL362_0_SPI_MAX_FREQUENCY,
629+
#if defined(DT_ADI_ADXL362_0_CS_GPIO_CONTROLLER)
630+
.gpio_cs_port = DT_ADI_ADXL362_0_CS_GPIO_CONTROLLER,
631+
.cs_gpio = DT_ADI_ADXL362_0_CS_GPIO_PIN,
632+
#endif
607633
};
608634

609-
DEVICE_AND_API_INIT(adxl362, DT_ADXL362_DEV_NAME, adxl362_init,
635+
DEVICE_AND_API_INIT(adxl362, DT_ADI_ADXL362_0_LABEL, adxl362_init,
610636
&adxl362_data, &adxl362_config, POST_KERNEL,
611637
CONFIG_SENSOR_INIT_PRIORITY, &adxl362_api_funcs);

drivers/sensor/adxl362/adxl362.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,18 @@ struct adxl362_config {
158158
char *spi_name;
159159
u32_t spi_max_frequency;
160160
u16_t spi_slave;
161+
#if defined(DT_ADI_ADXL362_0_CS_GPIO_CONTROLLER)
162+
const char *gpio_cs_port;
163+
u8_t cs_gpio;
164+
#endif
161165
};
162166

163167
struct adxl362_data {
164168
struct device *spi;
165169
struct spi_config spi_cfg;
170+
#if defined(DT_ADI_ADXL362_0_CS_GPIO_CONTROLLER)
171+
struct spi_cs_control adxl362_cs_ctrl;
172+
#endif
166173
s32_t acc_x;
167174
s32_t acc_y;
168175
s32_t acc_z;

tests/drivers/build_all/dts_fixup.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
#define DT_ADI_ADT7420_0_INT_GPIOS_PIN 0
99
#endif
1010

11-
#ifndef DT_ADXL362_DEV_NAME
12-
#define DT_ADXL362_DEV_NAME ""
13-
#define DT_ADXL362_SPI_DEV_NAME ""
14-
#define DT_ADXL362_SPI_DEV_SLAVE 0
15-
#define DT_ADXL362_SPI_MAX_FREQUENCY 0
11+
#ifndef DT_ADI_ADXL362_0_LABEL
12+
#define DT_ADI_ADXL362_0_LABEL ""
13+
#define DT_ADI_ADXL362_0_BUS_NAME ""
14+
#define DT_ADI_ADXL362_0_BASE_ADDRESS 0
15+
#define DT_ADI_ADXL362_0_INT1_GPIOS_CONTROLLER ""
16+
#define DT_ADI_ADXL362_0_INT1_GPIOS_PIN 0
17+
#define DT_ADI_ADXL362_0_CS_GPIO_CONTROLLER 0
18+
#define DT_ADI_ADXL362_0_CS_GPIO_PIN 0
19+
#define DT_ADI_ADXL362_0_SPI_MAX_FREQUENCY 0
1620
#endif
1721

1822
#ifndef DT_ADI_ADXL372_0_LABEL

0 commit comments

Comments
 (0)