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
70 changes: 33 additions & 37 deletions drivers/i2c/target/eeprom_target.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ struct i2c_eeprom_target_data {
uint32_t buffer_idx;
uint32_t idx_write_cnt;
uint8_t address_width;
bool read_only;
};

struct i2c_eeprom_target_config {
Expand Down Expand Up @@ -115,22 +116,26 @@ static int eeprom_target_write_received(struct i2c_target_config *config,
struct i2c_eeprom_target_data,
config);

LOG_DBG("eeprom: write done, val=0x%x", val);

/* In case EEPROM wants to be R/O, return !0 here could trigger
* a NACK to the I2C controller, support depends on the
* I2C controller support
*/

if (data->idx_write_cnt < (data->address_width >> 3)) {
LOG_DBG("eeprom: write addr, val=0x%x", val);

if (data->idx_write_cnt == 0) {
data->buffer_idx = 0;
}

data->buffer_idx = val | (data->buffer_idx << 8);
data->idx_write_cnt++;
} else {
} else if (!data->read_only) {
LOG_DBG("eeprom: write data, val=0x%x", val);

data->buffer[data->buffer_idx++] = val;
} else {
LOG_DBG("eeprom: write attempt to read-only EEPROM");

/* In case the EEPROM is read-only, an error code is returned
* to trigger a NACK on I2C controllers that support it.
*/
return -EIO;
}

data->buffer_idx = data->buffer_idx % data->buffer_size;
Expand Down Expand Up @@ -249,34 +254,25 @@ static int i2c_eeprom_target_init(const struct device *dev)
return 0;
}

#define I2C_EEPROM_INIT(inst) \
static struct i2c_eeprom_target_data \
i2c_eeprom_target_##inst##_dev_data = { \
.address_width = DT_INST_PROP_OR(inst, \
address_width, 8), \
}; \
\
static uint8_t \
i2c_eeprom_target_##inst##_buffer[(DT_INST_PROP(inst, size))]; \
\
BUILD_ASSERT(DT_INST_PROP(inst, size) <= \
(1 << DT_INST_PROP_OR(inst, address_width, 8)), \
"size must be <= than 2^address_width"); \
\
static const struct i2c_eeprom_target_config \
i2c_eeprom_target_##inst##_cfg = { \
.bus = I2C_DT_SPEC_INST_GET(inst), \
.buffer_size = DT_INST_PROP(inst, size), \
.buffer = i2c_eeprom_target_##inst##_buffer \
}; \
\
DEVICE_DT_INST_DEFINE(inst, \
&i2c_eeprom_target_init, \
NULL, \
&i2c_eeprom_target_##inst##_dev_data, \
&i2c_eeprom_target_##inst##_cfg, \
POST_KERNEL, \
CONFIG_I2C_TARGET_INIT_PRIORITY, \
&api_funcs);
#define I2C_EEPROM_INIT(inst) \
static struct i2c_eeprom_target_data i2c_eeprom_target_##inst##_dev_data = { \
.address_width = DT_INST_PROP_OR(inst, address_width, 8), \
.read_only = DT_INST_PROP_OR(inst, read_only, 8), \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a copy-paste error. The read-only property is always present due to its type being boolean so there is no reason to give it a default value.

Suggested change
.read_only = DT_INST_PROP_OR(inst, read_only, 8), \
.read_only = DT_INST_PROP(inst, read_only), \

}; \
\
static uint8_t i2c_eeprom_target_##inst##_buffer[(DT_INST_PROP(inst, size))]; \
\
BUILD_ASSERT(DT_INST_PROP(inst, size) <= (1 << DT_INST_PROP_OR(inst, address_width, 8)), \
"size must be <= than 2^address_width"); \
\
static const struct i2c_eeprom_target_config i2c_eeprom_target_##inst##_cfg = { \
.bus = I2C_DT_SPEC_INST_GET(inst), \
.buffer_size = DT_INST_PROP(inst, size), \
.buffer = i2c_eeprom_target_##inst##_buffer}; \
\
DEVICE_DT_INST_DEFINE(inst, &i2c_eeprom_target_init, NULL, \
&i2c_eeprom_target_##inst##_dev_data, \
&i2c_eeprom_target_##inst##_cfg, POST_KERNEL, \
CONFIG_I2C_TARGET_INIT_PRIORITY, &api_funcs);

DT_INST_FOREACH_STATUS_OKAY(I2C_EEPROM_INIT)
3 changes: 3 additions & 0 deletions dts/bindings/mtd/zephyr,i2c-target-eeprom.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ properties:
description: |
Number of address bits used to address the EEPROM. If not specified
the EEPROM is assumed to have a 8-bit address.
read-only:
type: boolean
description: set this property if the EERPOM is read-only.
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/frdm_ke17z512.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c1 {
Expand Down
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/frdm_mcxn236.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
size = <1024>;
address-width = <16>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&flexcomm5_lpi2c5 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&flexcomm2_lpi2c2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
reg = <0x54>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&flexcomm2_lpi2c2 {
Expand Down
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/it8xxx2_evb.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,11 @@
reg = <0x52>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c1 {
Expand Down
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/max32662evkit.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c2 {
Expand Down
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/max32670evkit.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c1 {
Expand Down
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/max32672evkit.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c1 {
Expand Down
7 changes: 7 additions & 0 deletions tests/drivers/i2c/i2c_target_api/boards/max32675evkit.overlay
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&i2c2 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
address-width = <16>;
size = <1024>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c3 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c3 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c5 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c5 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c5 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c3 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
reg = <0x54>;
size = <256>;
};
eeprom2: eeprom@58 {
compatible = "zephyr,i2c-target-eeprom";
reg = <0x58>;
size = <4096>;
address-width = <16>;
read-only;
};
};

&lpi2c3 {
Expand Down
Loading
Loading