-
Notifications
You must be signed in to change notification settings - Fork 8k
drivers: smbus: stm32: support packet-error-checking (pec) #96691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -28,6 +28,14 @@ config SMBUS_INIT_PRIORITY | |||
help | ||||
SMBus device driver initialization priority. | ||||
|
||||
config SMBUS_SOFT_PEC | ||||
bool "SMBus software PEC support" | ||||
select CRC | ||||
help | ||||
Enable software Packet Error Checking (PEC) support. | ||||
|
||||
These generic functions can be used by SMBus drivers for transceivers that do not support | ||||
PEC in hardware. | ||||
|
||||
module = SMBUS | ||||
module-str = smbus | ||||
|
@@ -83,6 +91,7 @@ menuconfig SMBUS_STM32 | |||
depends on DT_HAS_ST_STM32_SMBUS_ENABLED | ||||
depends on I2C_STM32 | ||||
select PINCTRL | ||||
select SMBUS_SOFT_PEC | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose there are two ways to look at this:
With the select here it is more the latter one, although it should actually be the first one? But this then would also mean that everything has to be ifdef-ed in the driver itself, so I'm not sure if it is actually a good idea. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Slowly I start to understand it, you actually need this config-flag so that the utility functions for PEC (which themselves require CRC) are only pulled in when necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
"Should" is potentially debatable. E.g. zephyr/drivers/smbus/intel_pch_smbus.c Line 332 in d3e645e
The stm32 driver is the only other smbus driver in-tree. It's almost completely platform-independent, actually, with the exception of Personally, I would feel better if there was a uniform API, so that PEC can at least be configured by the application, but it isn't disabled by default here (the Zephyr convention), so I can see your point.
I'm ok to go either way. @benediktibk , @erwango , @etienne-lms - do you have preferences? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'd go for |
||||
help | ||||
Enable STM32 SMBus driver. | ||||
|
||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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); | ||
etienne-lms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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, | ||
|
@@ -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), | ||
|
@@ -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), | ||
|
@@ -272,20 +416,35 @@ 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to investigate capturing error statistics, in this block, in a subsequent PR. Also mentioned here |
||
} | ||
|
||
return res; | ||
return 0; | ||
} | ||
|
||
static DEVICE_API(smbus, smbus_stm32_api) = { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this option configurable by the project? It seems rather a driver private config.
... hmmm, i see at least the test sample needs to enable it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's configurable by the project. Technically could be used by any driver or other code.