Skip to content
Draft
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 drivers/smbus/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

menuconfig SMBUS
bool "System Management Bus (SMBus) drivers"
imply CRC
help
Enable SMBus Driver Configuration

Expand Down
297 changes: 260 additions & 37 deletions drivers/smbus/smbus_stm32.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ static int smbus_stm32_configure(const struct device *dev, uint32_t config_value
const struct smbus_stm32_config *config = dev->config;
struct smbus_stm32_data *data = dev->data;

if (config_value & SMBUS_MODE_PEC) {
LOG_ERR("%s: not implemented", dev->name);
return -EINVAL;
}

if (config_value & SMBUS_MODE_HOST_NOTIFY) {
LOG_ERR("%s: not available", dev->name);
return -EINVAL;
Expand Down Expand Up @@ -152,62 +147,200 @@ static int smbus_stm32_quick(const struct device *dev, uint16_t periph_addr,

static int smbus_stm32_byte_write(const struct device *dev, uint16_t periph_addr, uint8_t command)
{
uint8_t pec;
uint8_t num_msgs;
struct i2c_msg msgs[] = {
{
.buf = &command,
.len = sizeof(command),
.flags = I2C_MSG_WRITE,
},
{
.buf = &pec,
.len = sizeof(pec),
.flags = I2C_MSG_WRITE,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

return i2c_write(config->i2c_dev, &command, sizeof(command), periph_addr);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
smbus_write_prepare_pec(data->config, periph_addr, msgs, num_msgs);
return i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
}

static int smbus_stm32_byte_read(const struct device *dev, uint16_t periph_addr, uint8_t *byte)
{
int ret;
uint8_t pec = 0;
uint8_t num_msgs;
struct i2c_msg msgs[] = {
{
.buf = byte,
.len = sizeof(*byte),
.flags = I2C_MSG_READ,
},
{
.buf = &pec,
.len = sizeof(pec),
.flags = I2C_MSG_READ,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

return i2c_read(config->i2c_dev, byte, sizeof(*byte), periph_addr);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
ret = i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
if (ret < 0) {
return ret;
}

ret = smbus_read_check_pec(data->config, periph_addr, msgs, num_msgs);
if (ret < 0) {
return ret;
}

return 0;
}

static int smbus_stm32_byte_data_write(const struct device *dev, uint16_t periph_addr,
uint8_t command, uint8_t byte)
{
const struct smbus_stm32_config *config = dev->config;
uint8_t buffer[] = {
command,
byte,
uint8_t pec;
uint8_t num_msgs;
struct i2c_msg msgs[] = {
{
.buf = &command,
.len = sizeof(command),
.flags = I2C_MSG_WRITE,
},
{
.buf = &byte,
.len = sizeof(byte),
.flags = I2C_MSG_WRITE,
},
{
.buf = &pec,
.len = sizeof(pec),
.flags = I2C_MSG_WRITE,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

return i2c_write(config->i2c_dev, buffer, ARRAY_SIZE(buffer), periph_addr);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
smbus_write_prepare_pec(data->config, periph_addr, msgs, num_msgs);
return i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
}

static int smbus_stm32_byte_data_read(const struct device *dev, uint16_t periph_addr,
uint8_t command, uint8_t *byte)
{
int ret;
uint8_t pec;
uint8_t num_msgs;
struct i2c_msg msgs[] = {
{
.buf = &command,
.len = sizeof(command),
.flags = I2C_MSG_WRITE,
},
{
.buf = byte,
.len = sizeof(*byte),
.flags = I2C_MSG_READ | I2C_MSG_RESTART,
},
{
.buf = &pec,
.len = sizeof(pec),
.flags = I2C_MSG_READ,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

return i2c_write_read(config->i2c_dev, periph_addr, &command, sizeof(command), byte,
sizeof(*byte));
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
ret = i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
if (ret < 0) {
return ret;
}

ret = smbus_read_check_pec(data->config, periph_addr, msgs, num_msgs);
if (ret < 0) {
return ret;
}

return 0;
}

static int smbus_stm32_word_data_write(const struct device *dev, uint16_t periph_addr,
uint8_t command, uint16_t word)
{
uint8_t pec;
uint8_t num_msgs;
struct i2c_msg msgs[] = {
{
.buf = &command,
.len = sizeof(command),
.flags = I2C_MSG_WRITE,
},
{
.buf = (uint8_t *)&word,
.len = sizeof(word),
.flags = I2C_MSG_WRITE,
},
{
.buf = &pec,
.len = sizeof(pec),
.flags = I2C_MSG_WRITE,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;
uint8_t buffer[sizeof(command) + sizeof(word)];

buffer[0] = command;
sys_put_le16(word, buffer + 1);

return i2c_write(config->i2c_dev, buffer, ARRAY_SIZE(buffer), periph_addr);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
smbus_write_prepare_pec(data->config, periph_addr, msgs, num_msgs);
return i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
}

static int smbus_stm32_word_data_read(const struct device *dev, uint16_t periph_addr,
uint8_t command, uint16_t *word)
{
int ret;
uint8_t pec;
uint8_t num_msgs;
struct i2c_msg messages[] = {
{
.buf = &command,
.len = sizeof(command),
.flags = I2C_MSG_WRITE,
},
{
.buf = (uint8_t *)word,
.len = sizeof(*word),
.flags = I2C_MSG_READ | I2C_MSG_RESTART,
},
{
.buf = &pec,
.len = sizeof(pec),
.flags = I2C_MSG_READ,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;
int result;

result = i2c_write_read(config->i2c_dev, periph_addr, &command, sizeof(command), word,
sizeof(*word));
*word = sys_le16_to_cpu(*word);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(messages));
ret = i2c_transfer(config->i2c_dev, messages, num_msgs, periph_addr);
if (ret < 0) {
return ret;
}

return result;
ret = smbus_read_check_pec(data->config, periph_addr, messages, num_msgs);
if (ret < 0) {
return ret;
}

return 0;
}

static int smbus_stm32_pcall(const struct device *dev, uint16_t periph_addr, uint8_t command,
Expand All @@ -230,8 +363,9 @@ static int smbus_stm32_pcall(const struct device *dev, uint16_t periph_addr, uin
static int smbus_stm32_block_write(const struct device *dev, uint16_t periph_addr, uint8_t command,
uint8_t count, uint8_t *buf)
{
const struct smbus_stm32_config *config = dev->config;
struct i2c_msg messages[] = {
uint8_t pec;
uint8_t num_msgs;
struct i2c_msg msgs[] = {
{
.buf = &command,
.len = sizeof(command),
Expand All @@ -246,18 +380,28 @@ static int smbus_stm32_block_write(const struct device *dev, uint16_t periph_add
.buf = buf,
.len = count,
.flags = I2C_MSG_WRITE,
}
},
{
.buf = &pec,
.len = 1,
.flags = I2C_MSG_WRITE,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

return i2c_transfer(config->i2c_dev, messages, ARRAY_SIZE(messages), periph_addr);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
smbus_write_prepare_pec(data->config, periph_addr, msgs, ARRAY_SIZE(msgs));
return i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
}

static int smbus_stm32_block_read(const struct device *dev, uint16_t periph_addr, uint8_t command,
uint8_t *count, uint8_t *buf)
{
const struct smbus_stm32_config *config = dev->config;

struct i2c_msg messages[] = {
int ret;
uint8_t num_msgs;
uint8_t received_pec;
struct i2c_msg msgs[] = {
{
.buf = &command,
.len = sizeof(command),
Expand All @@ -272,20 +416,99 @@ static int smbus_stm32_block_read(const struct device *dev, uint16_t periph_addr
.buf = buf,
.len = 0, /* written by previous message! */
.flags = I2C_MSG_READ,
}
},
{
.buf = &received_pec,
.len = 1,
.flags = I2C_MSG_READ,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

/* Count is read in msg 1 and stored in the len of msg 2.
* This works because the STM I2C driver processes each message serially.
* The addressing math assumes little-endian.
*/
messages[1].buf = (uint8_t *)&messages[2].len;
msgs[1].buf = (uint8_t *)&msgs[2].len;

int res = i2c_transfer(config->i2c_dev, messages, ARRAY_SIZE(messages), periph_addr);
num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
ret = i2c_transfer(config->i2c_dev, msgs, num_msgs, periph_addr);
if (ret < 0) {
return ret;
}

*count = messages[2].len;
*count = msgs[2].len;
ret = smbus_read_check_pec(data->config, periph_addr, msgs, num_msgs);
if (ret < 0) {
return ret;
}

return res;
return 0;
}

int smbus_stm32_block_pcall(const struct device *dev, uint16_t addr, uint8_t cmd,
uint8_t send_count, uint8_t *send_buf, uint8_t *recv_count,
uint8_t *recv_buf)
{
int ret;
uint8_t num_msgs;
uint8_t received_pec;
struct i2c_msg msgs[] = {
{
.buf = &cmd,
.len = sizeof(cmd),
.flags = I2C_MSG_WRITE,
},
{
.buf = &send_count,
.len = sizeof(send_count),
.flags = I2C_MSG_WRITE,
},
{
.buf = send_buf,
.len = send_count,
.flags = I2C_MSG_WRITE,
},
{
.buf = NULL, /* will point to next message's len field */
.len = 1,
.flags = I2C_MSG_READ | I2C_MSG_RESTART,
},
{
.buf = recv_buf,
.len = 0, /* written by previous message! */
.flags = I2C_MSG_READ,
},
{
.buf = &received_pec,
.len = 1,
.flags = I2C_MSG_READ,
},
};
struct smbus_stm32_data *data = dev->data;
const struct smbus_stm32_config *config = dev->config;

/* Count is read in msg 3 and stored in the len of msg 4.
* This works because the STM I2C driver processes each message serially.
* The addressing math assumes little-endian.
*/
msgs[3].buf = (uint8_t *)&msgs[4].len;

num_msgs = smbus_pec_num_msgs(data->config, ARRAY_SIZE(msgs));
ret = i2c_transfer(config->i2c_dev, msgs, num_msgs, addr);
if (ret < 0) {
return ret;
}

ret = smbus_read_check_pec(data->config, addr, msgs, num_msgs);
if (ret < 0) {
return ret;
}

*recv_count = msgs[4].len;

return 0;
}

static DEVICE_API(smbus, smbus_stm32_api) = {
Expand All @@ -308,7 +531,7 @@ static DEVICE_API(smbus, smbus_stm32_api) = {
.smbus_smbalert_set_cb = NULL,
.smbus_smbalert_remove_cb = NULL,
#endif /* CONFIG_SMBUS_STM32_SMBALERT */
.smbus_block_pcall = NULL,
.smbus_block_pcall = smbus_stm32_block_pcall,
.smbus_host_notify_set_cb = NULL,
.smbus_host_notify_remove_cb = NULL,
};
Expand Down
Loading