Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ if(NOT CONFIG_RUST)
endif()

add_subdirectory(subsys)
add_subdirectory(drivers)
3 changes: 2 additions & 1 deletion Kconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
menu "Zephyr Playground"

rsource "subsys/Kconfig"
rsource "drivers/Kconfig"

endmenu
endmenu
6 changes: 5 additions & 1 deletion cmake/Util.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ function(ProjectSetup snippets)

list(APPEND SNIPPET_ROOT $ENV{NOTUS_ROOT})
set(SNIPPET_ROOT ${SNIPPET_ROOT} PARENT_SCOPE)

list(APPEND DTS_ROOT $ENV{NOTUS_ROOT})
set(DTS_ROOT ${DTS_ROOT} PARENT_SCOPE)

set(SNIPPET ${snippets} PARENT_SCOPE)

Expand All @@ -35,6 +38,7 @@ endfunction()

function(SetupTarget target_name)
target_include_directories(${target_name} PUBLIC $ENV{NOTUS_ROOT}/includes)
target_include_directories(${target_name} PUBLIC $ENV{NOTUS_ROOT})

target_compile_options(
${target_name}
Expand Down Expand Up @@ -74,4 +78,4 @@ function(RunClangdTidy target sources)
COMMENT "Running clangd-tidy"
)
endif()
endfunction()
endfunction()
2 changes: 2 additions & 0 deletions drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory_ifdef(CONFIG_SENSOR sensor)
message("Hello1")
5 changes: 5 additions & 0 deletions drivers/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
menu "Drivers"

rsource "sensor/Kconfig"

endmenu
2 changes: 2 additions & 0 deletions drivers/sensor/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_subdirectory_ifdef(CONFIG_EZO_EC ezo_ec)
message("Hello2")
1 change: 1 addition & 0 deletions drivers/sensor/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rsource "ezo_ec/Kconfig"
4 changes: 4 additions & 0 deletions drivers/sensor/ezo_ec/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
zephyr_library()
zephyr_library_sources(ezo_ec.c)
zephyr_library_sources_ifdef(CONFIG_EZO_EC_EMUL emul_ezo_ec.c)
message("Hello3")
14 changes: 14 additions & 0 deletions drivers/sensor/ezo_ec/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
config EZO_EC
bool "EZO-EC Sensor"
default n
depends on I2C
help
Enable EZO-EC


config EZO_EC_EMUL
bool "EZO-EC Emul"
default n
depends on I2C_EMUL
help
Enable EZO-EC Emulator
150 changes: 150 additions & 0 deletions drivers/sensor/ezo_ec/emul_ezo_ec.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
#define DT_DRV_COMPAT atlas_ezo_ec
#define LOG_LEVEL CONFIG_I2C_LOG_LEVEL

#include <stdint.h>
#include <string.h>
#include <time.h>

#include <zephyr/device.h>
#include <zephyr/drivers/emul.h>
#include <zephyr/drivers/i2c.h>
#include <zephyr/drivers/i2c_emul.h>
#include <zephyr/logging/log.h>
#include <zephyr/sys/printk.h>
#include <zephyr/sys/timeutil.h>

#include "emul_ezo_ec.h"
#include "ezo_ec_util.h"

LOG_MODULE_REGISTER(EMUL_EZO_EC); //NOLINT

// Future Updates:
// - Move to a circular buffer for input and output data
// - Add ability to set response code
// - Add support for the internal state?

// Runtime Data
struct emul_ezo_ec_data
{
struct i2c_emul emul;
char input[EZO_EC_BUFFER_SIZE];
char output[EZO_EC_BUFFER_SIZE];
};

void emul_ezo_ec_get_input(const struct emul *emul, char *buf, int len)
{
struct emul_ezo_ec_data *data = emul->data;
memcpy(buf, data->input, len);
}

void emul_ezo_ec_set_output(const struct emul *emul, const char *buf, int len)
{
struct emul_ezo_ec_data *data = emul->data;
memset(data->output, 0, EZO_EC_BUFFER_SIZE);
data->output[0] = 1; // Hardcode response code for now;
memcpy(data->output+1, buf, len);
}

void emul_ezo_ec_reset_buffers(const struct emul *emul)
{
struct emul_ezo_ec_data *data = emul->data;
memset(data->input,0, EZO_EC_BUFFER_SIZE);
memset(data->output, 0, EZO_EC_BUFFER_SIZE);
}

struct ezo_ec_config emul_ezo_ec_get_config(const struct emul *emul)
{
// Here we are casting away const which is BAD but is required for unit testing
// to allow us to inject custom configs that are normally created at compile time
const struct ezo_ec_config *config = (struct ezo_ec_config *)emul->dev->config;
return *config;
}

void emul_ezo_ec_set_config(const struct emul *emul, struct ezo_ec_config config)
{
struct ezo_ec_config *current_config = (struct ezo_ec_config *)emul->dev->config;
*current_config = config;
}

static int emul_ezo_ec_write(struct emul_ezo_ec_data *data, uint8_t len, const uint8_t *buf)
{
if (len > EZO_EC_BUFFER_SIZE)
{
LOG_ERR("Write too large");
return -ENXIO;
}

memcpy(data->input, buf, len);
return 0;
}

static int emul_ezo_ec_read(struct emul_ezo_ec_data *data, uint8_t len, uint8_t *buf)
{
if (len > EZO_EC_BUFFER_SIZE)
{
LOG_ERR("Read to large");
return -ENXIO;
}

memcpy(buf, data->output, len);

return 0;
}

static int emul_ezo_ec_transfer(const struct emul *target, struct i2c_msg *msgs, int num_msgs, int addr)
{
struct emul_ezo_ec_data *data = target->data;

i2c_dump_msgs(target->dev, msgs, num_msgs, addr);

if ((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_READ)
{
emul_ezo_ec_read(data, msgs[0].len, msgs[0].buf);
}
else if ((msgs[0].flags & I2C_MSG_RW_MASK) == I2C_MSG_WRITE)
{
emul_ezo_ec_write(data, msgs[0].len, &msgs[0].buf[0]);
}
else
{
LOG_ERR("Unknown transfer");
return -EIO;
}

return 0;
}

const static struct i2c_emul_api emul_ezo_ec_api = {
.transfer = emul_ezo_ec_transfer,
};

static int emul_ezo_ec_init(const struct emul *target, const struct device *parent)
{
LOG_INF("emul_ezo_ec_init");
const struct emul_ezo_ec_config *config = target->cfg;
struct emul_ezo_ec_data *data = target->data;

ARG_UNUSED(config);
ARG_UNUSED(data);
ARG_UNUSED(parent);

emul_ezo_ec_reset_buffers(target);

char info[] = "?i,EC,2.16";
emul_ezo_ec_set_output(target, info, (int)strlen(info));

return 0;
}

// clang-format off
#define ezo_ec_EMUL(inst) \
static struct emul_ezo_ec_data emul_ezo_ec_data_##inst; \
EMUL_DT_INST_DEFINE(inst, \
&emul_ezo_ec_init, \
&emul_ezo_ec_data_##inst, \
NULL, \
&emul_ezo_ec_api, \
NULL)

DT_INST_FOREACH_STATUS_OKAY(ezo_ec_EMUL) // NOLINT
// clang-format on
23 changes: 23 additions & 0 deletions drivers/sensor/ezo_ec/emul_ezo_ec.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef APP_INCLUDE_EMUL_ezo_ec_H_
#define APP_INCLUDE_EMUL_ezo_ec_H_

#ifdef __cplusplus
extern "C"
{
#endif

#include "ezo_ec_util.h"
#include <zephyr/drivers/i2c_emul.h>

void emul_ezo_ec_get_input(const struct emul *emul, char *buf, int len);
void emul_ezo_ec_set_output(const struct emul *emul, const char *buf, int len);
void emul_ezo_ec_reset_buffers(const struct emul *emul);

struct ezo_ec_config emul_ezo_ec_get_config(const struct emul *emul);
void emul_ezo_ec_set_config(const struct emul *emul, struct ezo_ec_config config);

#ifdef __cplusplus
}
#endif

#endif // APP_INCLUDE_EMUL_ezo_ec_H_
Loading
Loading