Skip to content

Commit

Permalink
drivers: i2c: Add dts support for i2c slaves.
Browse files Browse the repository at this point in the history
Adding i2c slave requires overlay with node definitions and
proper aliases depending on driver implementation.

Modified i2c_slave_api test to use information from dts.

Signed-off-by: Mieszko Mierunski <[email protected]>
  • Loading branch information
Mieszko Mierunski committed Sep 24, 2018
1 parent a7ddb1f commit de228ec
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 82 deletions.
40 changes: 0 additions & 40 deletions drivers/i2c/slave/Kconfig.eeprom
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,6 @@ config I2C_EEPROM_SLAVE_0
depends on I2C_EEPROM_SLAVE
default y

config I2C_EEPROM_SLAVE_0_SIZE
int "I2C Slave EEPROM 0 Size in KiB"
depends on I2C_EEPROM_SLAVE_0
default 1

config I2C_EEPROM_SLAVE_0_NAME
string "I2C Slave EEPROM 0 device name"
depends on I2C_EEPROM_SLAVE_0
default "EEPROM_SLAVE_0"

config I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME
string "I2C Slave EEPROM 0 Controller device name"
depends on I2C_EEPROM_SLAVE_0
default "I2C_0"

config I2C_EEPROM_SLAVE_0_ADDRESS
hex "I2C Slave EEPROM 0 address"
depends on I2C_EEPROM_SLAVE_0
default 0x54

config I2C_EEPROM_SLAVE_1
bool "Enable I2C Slave EEPROM driver instance 1"
depends on I2C_EEPROM_SLAVE

config I2C_EEPROM_SLAVE_1_SIZE
int "I2C Slave EEPROM 1 Size in KiB"
depends on I2C_EEPROM_SLAVE_1
default 1

config I2C_EEPROM_SLAVE_1_NAME
string "I2C Slave EEPROM 1 device name"
depends on I2C_EEPROM_SLAVE_1
default "EEPROM_SLAVE_1"

config I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME
string "I2C Slave EEPROM 1 Controller device name"
depends on I2C_EEPROM_SLAVE_1
default "I2C_1"

config I2C_EEPROM_SLAVE_1_ADDRESS
hex "I2C Slave EEPROM 1 address"
depends on I2C_EEPROM_SLAVE_1
default 0x56
20 changes: 10 additions & 10 deletions drivers/i2c/slave/eeprom_slave.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,16 @@ static int i2c_eeprom_slave_init(struct device *dev)

static struct i2c_eeprom_slave_data i2c_eeprom_slave_0_dev_data;

static u8_t i2c_eeprom_slave_0_buffer[(CONFIG_I2C_EEPROM_SLAVE_0_SIZE * 1024)];
static u8_t i2c_eeprom_slave_0_buffer[(EEPROM_SLAVE_0_SIZE * 1024)];

static const struct i2c_eeprom_slave_config i2c_eeprom_slave_0_cfg = {
.controller_dev_name = CONFIG_I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME,
.address = CONFIG_I2C_EEPROM_SLAVE_0_ADDRESS,
.buffer_size = (CONFIG_I2C_EEPROM_SLAVE_0_SIZE * 1024),
.controller_dev_name = EEPROM_SLAVE_0_BUS_NAME,
.address = EEPROM_SLAVE_0_BASE_ADDRESS,
.buffer_size = (EEPROM_SLAVE_0_SIZE * 1024),
.buffer = i2c_eeprom_slave_0_buffer
};

DEVICE_AND_API_INIT(i2c_eeprom_slave_0, CONFIG_I2C_EEPROM_SLAVE_0_NAME,
DEVICE_AND_API_INIT(i2c_eeprom_slave_0, EEPROM_SLAVE_0_LABEL,
&i2c_eeprom_slave_init,
&i2c_eeprom_slave_0_dev_data, &i2c_eeprom_slave_0_cfg,
POST_KERNEL, CONFIG_I2C_SLAVE_INIT_PRIORITY,
Expand All @@ -227,16 +227,16 @@ DEVICE_AND_API_INIT(i2c_eeprom_slave_0, CONFIG_I2C_EEPROM_SLAVE_0_NAME,

static struct i2c_eeprom_slave_data i2c_eeprom_slave_1_dev_data;

static u8_t i2c_eeprom_slave_1_buffer[(CONFIG_I2C_EEPROM_SLAVE_1_SIZE * 1024)];
static u8_t i2c_eeprom_slave_1_buffer[(EEPROM_SLAVE_1_SIZE * 1024)];

static const struct i2c_eeprom_slave_config i2c_eeprom_slave_1_cfg = {
.controller_dev_name = CONFIG_I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME,
.address = CONFIG_I2C_EEPROM_SLAVE_1_ADDRESS,
.buffer_size = (CONFIG_I2C_EEPROM_SLAVE_1_SIZE * 1024),
.controller_dev_name = EEPROM_SLAVE_1_BUS_NAME,
.address = EEPROM_SLAVE_1_BASE_ADDRESS,
.buffer_size = (EEPROM_SLAVE_1_SIZE * 1024),
.buffer = i2c_eeprom_slave_1_buffer
};

DEVICE_AND_API_INIT(i2c_eeprom_slave_1, CONFIG_I2C_EEPROM_SLAVE_1_NAME,
DEVICE_AND_API_INIT(i2c_eeprom_slave_1, EEPROM_SLAVE_1_LABEL,
&i2c_eeprom_slave_init,
&i2c_eeprom_slave_1_dev_data, &i2c_eeprom_slave_1_cfg,
POST_KERNEL, CONFIG_I2C_SLAVE_INIT_PRIORITY,
Expand Down
26 changes: 26 additions & 0 deletions dts/bindings/slave/i2c,eeprom.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright (c) 2018, Nordic Semiconductor
#
# SPDX-License-Identifier: Apache-2.0
#
---
title: Virtual I2C slave eeprom
id: i2c,eeprom
version: 0.1

description: >
This binding gives a base representation of I2C slave eeprom
inherits:
!include i2c-device.yaml

properties:
compatible:
constraint: "i2c,eeprom"
size:
type: int
category: required
description: I2C Slave EEPROM Size in KiB
generation: define

...
2 changes: 0 additions & 2 deletions tests/drivers/i2c/i2c_slave_api/boards/nucleo_f091rc.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
CONFIG_I2C_STM32_V2=y
CONFIG_I2C_STM32_INTERRUPT=y
CONFIG_I2C_1=y
CONFIG_I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME="I2C_1"
CONFIG_I2C_2=y
CONFIG_I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME="I2C_2"
2 changes: 0 additions & 2 deletions tests/drivers/i2c/i2c_slave_api/boards/stm32f072b_disco.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
CONFIG_I2C_STM32_V2=y
CONFIG_I2C_STM32_INTERRUPT=y
CONFIG_I2C_1=y
CONFIG_I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME="I2C_1"
CONFIG_I2C_2=y
CONFIG_I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME="I2C_2"
25 changes: 25 additions & 0 deletions tests/drivers/i2c/i2c_slave_api/nucleo_f091rc.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/ {
aliases {
eeprom-slave-0 = &ep_0;
eeprom-slave-1 = &ep_1;
};
};

&i2c1 {
ep_0: eeprom@54 {
compatible = "i2c,eeprom";
reg = <0x54>;
label = "EEPROM_SLAVE_0";
size = <1>;
};
};


&i2c2 {
ep_1: eeprom@56 {
compatible = "i2c,eeprom";
reg = <0x56>;
label = "EEPROM_SLAVE_1";
size = <1>;
};
};
56 changes: 28 additions & 28 deletions tests/drivers/i2c/i2c_slave_api/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,76 +137,76 @@ void test_eeprom_slave(void)
int ret, offset;

i2c_0 = device_get_binding(
CONFIG_I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME);
EEPROM_SLAVE_0_BUS_NAME);
zassert_not_null(i2c_0, "I2C device %s not found",
CONFIG_I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME);
EEPROM_SLAVE_0_BUS_NAME);

SYS_LOG_INF("Found I2C Master device %s",
CONFIG_I2C_EEPROM_SLAVE_0_CONTROLLER_DEV_NAME);
EEPROM_SLAVE_0_BUS_NAME);

i2c_1 = device_get_binding(
CONFIG_I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME);
EEPROM_SLAVE_1_BUS_NAME);
zassert_not_null(i2c_1, "I2C device %s not found",
CONFIG_I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME);
EEPROM_SLAVE_1_BUS_NAME);

SYS_LOG_INF("Found I2C Master device %s",
CONFIG_I2C_EEPROM_SLAVE_1_CONTROLLER_DEV_NAME);
EEPROM_SLAVE_1_BUS_NAME);

eeprom_0 = device_get_binding(CONFIG_I2C_EEPROM_SLAVE_0_NAME);
eeprom_0 = device_get_binding(EEPROM_SLAVE_0_LABEL);
zassert_not_null(eeprom_0, "EEPROM device %s not found",
CONFIG_I2C_EEPROM_SLAVE_0_NAME);
EEPROM_SLAVE_0_LABEL);

SYS_LOG_INF("Found EEPROM device %s", CONFIG_I2C_EEPROM_SLAVE_0_NAME);
SYS_LOG_INF("Found EEPROM device %s", EEPROM_SLAVE_0_LABEL);

eeprom_1 = device_get_binding(CONFIG_I2C_EEPROM_SLAVE_1_NAME);
eeprom_1 = device_get_binding(EEPROM_SLAVE_1_LABEL);
zassert_not_null(eeprom_1, "EEPROM device %s not found",
CONFIG_I2C_EEPROM_SLAVE_1_NAME);
EEPROM_SLAVE_1_LABEL);

SYS_LOG_INF("Found EEPROM device %s", CONFIG_I2C_EEPROM_SLAVE_1_NAME);
SYS_LOG_INF("Found EEPROM device %s", EEPROM_SLAVE_1_LABEL);

/* Program dummy bytes */
ret = eeprom_slave_program(eeprom_0, eeprom_0_data, TEST_DATA_SIZE);
zassert_equal(ret, 0, "Failed to program EEPROM %s",
CONFIG_I2C_EEPROM_SLAVE_0_NAME);
EEPROM_SLAVE_0_LABEL);

ret = eeprom_slave_program(eeprom_1, eeprom_1_data, TEST_DATA_SIZE);
zassert_equal(ret, 0, "Failed to program EEPROM %s",
CONFIG_I2C_EEPROM_SLAVE_1_NAME);
EEPROM_SLAVE_1_LABEL);

/* Attach EEPROM */
ret = i2c_slave_driver_register(eeprom_0);
zassert_equal(ret, 0, "Failed to register EEPROM %s",
CONFIG_I2C_EEPROM_SLAVE_0_NAME);
EEPROM_SLAVE_0_LABEL);

SYS_LOG_INF("EEPROM %s Attached !", CONFIG_I2C_EEPROM_SLAVE_0_NAME);
SYS_LOG_INF("EEPROM %s Attached !", EEPROM_SLAVE_0_LABEL);

ret = i2c_slave_driver_register(eeprom_1);
zassert_equal(ret, 0, "Failed to register EEPROM %s",
CONFIG_I2C_EEPROM_SLAVE_1_NAME);
EEPROM_SLAVE_1_LABEL);

SYS_LOG_INF("EEPROM %s Attached !", CONFIG_I2C_EEPROM_SLAVE_1_NAME);
SYS_LOG_INF("EEPROM %s Attached !", EEPROM_SLAVE_1_LABEL);

/* Run Tests without bus access conflicts */
run_full_read(i2c_0, CONFIG_I2C_EEPROM_SLAVE_1_ADDRESS, eeprom_1_data);
run_full_read(i2c_1, CONFIG_I2C_EEPROM_SLAVE_0_ADDRESS, eeprom_0_data);
run_full_read(i2c_0, EEPROM_SLAVE_1_BASE_ADDRESS, eeprom_1_data);
run_full_read(i2c_1, EEPROM_SLAVE_0_BASE_ADDRESS, eeprom_0_data);

for (offset = 0 ; offset < TEST_DATA_SIZE-1 ; ++offset) {
run_partial_read(i2c_0, CONFIG_I2C_EEPROM_SLAVE_1_ADDRESS,
run_partial_read(i2c_0, EEPROM_SLAVE_1_BASE_ADDRESS,
eeprom_1_data, offset);
}

for (offset = 0 ; offset < TEST_DATA_SIZE-1 ; ++offset) {
run_partial_read(i2c_1, CONFIG_I2C_EEPROM_SLAVE_0_ADDRESS,
run_partial_read(i2c_1, EEPROM_SLAVE_0_BASE_ADDRESS,
eeprom_0_data, offset);
}

for (offset = 0 ; offset < TEST_DATA_SIZE-1 ; ++offset) {
run_program_read(i2c_0, CONFIG_I2C_EEPROM_SLAVE_1_ADDRESS,
run_program_read(i2c_0, EEPROM_SLAVE_1_BASE_ADDRESS,
offset);
}

for (offset = 0 ; offset < TEST_DATA_SIZE-1 ; ++offset) {
run_program_read(i2c_1, CONFIG_I2C_EEPROM_SLAVE_0_ADDRESS,
run_program_read(i2c_1, EEPROM_SLAVE_0_BASE_ADDRESS,
offset);
}

Expand All @@ -215,17 +215,17 @@ void test_eeprom_slave(void)
/* Detach EEPROM */
ret = i2c_slave_driver_unregister(eeprom_0);
zassert_equal(ret, 0, "Failed to unregister EEPROM %s",
CONFIG_I2C_EEPROM_SLAVE_0_NAME);
EEPROM_SLAVE_0_LABEL);

SYS_LOG_INF("EEPROM %s Detached !",
CONFIG_I2C_EEPROM_SLAVE_0_NAME);
EEPROM_SLAVE_0_LABEL);

ret = i2c_slave_driver_unregister(eeprom_1);
zassert_equal(ret, 0, "Failed to unregister EEPROM %s",
CONFIG_I2C_EEPROM_SLAVE_1_NAME);
EEPROM_SLAVE_1_LABEL);

SYS_LOG_INF("EEPROM %s Detached !",
CONFIG_I2C_EEPROM_SLAVE_1_NAME);
EEPROM_SLAVE_1_LABEL);
}

void test_main(void)
Expand Down
25 changes: 25 additions & 0 deletions tests/drivers/i2c/i2c_slave_api/stm32f072b_disco.overlay
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/ {
aliases {
eeprom-slave-0 = &ep_0;
eeprom-slave-1 = &ep_1;
};
};

&i2c1 {
ep_0: eeprom@54 {
compatible = "i2c,eeprom";
reg = <0x54>;
label = "EEPROM_SLAVE_0";
size = <1>;
};
};


&i2c2 {
ep_1: eeprom@56 {
compatible = "i2c,eeprom";
reg = <0x56>;
label = "EEPROM_SLAVE_1";
size = <1>;
};
};

0 comments on commit de228ec

Please sign in to comment.