Skip to content

Commit

Permalink
- Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Kampi committed Oct 7, 2023
1 parent 930dfc3 commit e5394c8
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@
int1-gpios = <&gpio0 21 GPIO_ACTIVE_HIGH>;
int2-gpios = <&gpio0 14 GPIO_ACTIVE_HIGH>;
};

apds9306: apds9306@52 {
compatible = "avago,apds9306";
reg = <0x52>;
};
};

&spi4 {
Expand Down
1 change: 1 addition & 0 deletions app/drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(display)
add_subdirectory(input)
add_subdirectory(sensor/apds9306)
9 changes: 9 additions & 0 deletions app/drivers/sensor/apds9306/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) 2023, Daniel Kampert
#
# SPDX-License-Identifier: Apache-2.0
#

# TODO: Doesn´t work with CMakeList.txt of the drivers directory -> Check it
#zephyr_library()

zephyr_library_sources(apds9306.c)
12 changes: 12 additions & 0 deletions app/drivers/sensor/apds9306/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# APDS-9306 light sensor configuration options.

# Copyright (c) 2023, Daniel Kampert
#
# SPDX-License-Identifier: Apache-2.0

config APDS9306
bool "APDS9306 light sensor"
default y
select I2C
help
Enable the driver for the APDS9306 digital light sensor.
136 changes: 136 additions & 0 deletions app/drivers/sensor/apds9306/apds9306.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/* apds9306.c - Driver for Avago APDS-9306 light sensor. */

/*
* Copyright (c) 2023, Daniel Kampert
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT avago_apds9306

#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/sensor.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/pm/device.h>
#include <zephyr/pm/device_runtime.h>
#include <zephyr/logging/log.h>

//LOG_MODULE_REGISTER(apds9306, CONFIG_SENSOR_LOG_LEVEL);
LOG_MODULE_REGISTER(apds9306, LOG_LEVEL_INF);

#if(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0)
#warning "apds9306 driver enabled without any devices"
#endif

struct apds9306_data_t {
int16_t light;
};

struct apds9306_config_t {
struct i2c_dt_spec i2c;
};

static int apds9306_sample_fetch(const struct device* p_Dev, enum sensor_channel Channel)
{
struct apds9306_data *data = p_Dev->data;
const struct apds9306_config_t *cfg = p_Dev->config;
enum pm_device_state pm_state;
int ret;

(void)pm_device_state_get(p_Dev, &pm_state);
if(pm_state != PM_DEVICE_STATE_ACTIVE)
{
ret = -EIO;
return ret;
}

switch(Channel)
{
case SENSOR_CHAN_ALL:
case SENSOR_CHAN_LIGHT:
{
//ret = lm75_fetch_temp(cfg, data);
break;
}
default:
{
ret = -ENOTSUP;
break;
}
}

return ret;
}

static int apds9306_channel_get(const struct device* p_Dev, enum sensor_channel Channel, struct sensor_value* p_Value)
{
struct apds9306_data *data = p_Dev->data;

switch(Channel)
{
case SENSOR_CHAN_LIGHT:
{
return 0;
}
default:
{
return -ENOTSUP;
}
}
}

static const struct sensor_driver_api apds9306_driver_api = {
.sample_fetch = apds9306_sample_fetch,
.channel_get = apds9306_channel_get,
};

static int apds9306_init(const struct device* p_Dev)
{
int ret = 0;

LOG_INF("Init driver");

return ret;
}

#ifdef CONFIG_PM_DEVICE
static int apds9306_pm_action(const struct device* p_Dev, enum pm_device_action Action)
{
switch(Action)
{
case PM_DEVICE_ACTION_TURN_ON:
case PM_DEVICE_ACTION_RESUME:
case PM_DEVICE_ACTION_TURN_OFF:
case PM_DEVICE_ACTION_SUSPEND:
{
break;
}
default:
{
return -ENOTSUP;
}
}

return 0;
}
#endif

#define APDS9306_INIT(inst) \
static struct apds9306_data_t apds9306_data_##inst; \
\
static const struct apds9306_config_t apds9306_config_##inst = { \
.i2c = I2C_DT_SPEC_INST_GET(inst), \
}; \
\
PM_DEVICE_DT_INST_DEFINE(inst, apds9306_pm_action); \
\
SENSOR_DEVICE_DT_INST_DEFINE(inst, apds9306_init, \
PM_DEVICE_DT_INST_GET(inst), \
&apds9306_data_##inst, \
&apds9306_config_##inst, POST_KERNEL, \
CONFIG_SENSOR_INIT_PRIORITY, \
&apds9306_driver_api);

DT_INST_FOREACH_STATUS_OKAY(APDS9306_INIT)

0 comments on commit e5394c8

Please sign in to comment.