|
7 | 7 | #ifndef ZEPHYR_DRIVERS_SMBUS_SMBUS_UTILS_H_
|
8 | 8 | #define ZEPHYR_DRIVERS_SMBUS_SMBUS_UTILS_H_
|
9 | 9 |
|
| 10 | +#include <errno.h> |
10 | 11 | #include <stdint.h>
|
| 12 | + |
11 | 13 | #include <zephyr/device.h>
|
| 14 | +#include <zephyr/drivers/i2c.h> |
12 | 15 | #include <zephyr/drivers/smbus.h>
|
13 | 16 | #include <zephyr/sys/slist.h>
|
14 | 17 |
|
@@ -107,4 +110,82 @@ static inline void smbus_init_callback(struct smbus_callback *callback,
|
107 | 110 | */
|
108 | 111 | void smbus_loop_alert_devices(const struct device *dev, sys_slist_t *callbacks);
|
109 | 112 |
|
| 113 | +/** |
| 114 | + * @brief Compute the number of messages required for an SMBus transaction |
| 115 | + * |
| 116 | + * If @p flags indicates that the transaction requires packet error checking (PEC), |
| 117 | + * the number of messages is equal to @p num_msgs, otherwise the number of messages |
| 118 | + * required is @p num_msgs - 1, since a PEC byte is not required. |
| 119 | + * |
| 120 | + * Callers are expected to allocated an array of @ref i2c_msg objects to hold a number of |
| 121 | + * i2c messages, including one message dedicated to the a PEC byte, whether or not PEC is |
| 122 | + * being used. |
| 123 | + * |
| 124 | + * @note This function is intended for SMBus drivers that do not have hardware PEC support and |
| 125 | + * therefore must rely on software PEC calculation. |
| 126 | + * |
| 127 | + * @param flags SMBus flags. |
| 128 | + * @param num_msgs Number of allocated messages. |
| 129 | + * @return The number of required messages. |
| 130 | + */ |
| 131 | +uint8_t smbus_pec_num_msgs(uint32_t flags, uint8_t num_msgs); |
| 132 | + |
| 133 | +/** |
| 134 | + * @brief Compute the packet error checking (PEC) byte for an SMBus transaction |
| 135 | + * |
| 136 | + * @note This function is intended for SMBus drivers that do not have hardware PEC support and |
| 137 | + * therefore must rely on software PEC calculation. |
| 138 | + * |
| 139 | + * @note At this time, only 7-bit addresses are supported in @p addr. |
| 140 | + * |
| 141 | + * @param addr The address of the target device. |
| 142 | + * @param msgs Array of @ref i2c_msg that make up the transaction. |
| 143 | + * @param num_msgs Number of messages in the transaction. |
| 144 | + * @return the computed PEC byte. |
| 145 | + */ |
| 146 | +uint8_t smbus_pec(uint16_t addr, const struct i2c_msg *msgs, uint8_t num_msgs); |
| 147 | + |
| 148 | +/** |
| 149 | + * @brief Prepare the packet error checking (PEC) byte for an SMBus write transaction |
| 150 | + * |
| 151 | + * If the @p flags bitmask contains @ref SMBUS_MODE_PEC (i.e. PEC is enabled), the number of |
| 152 | + * messages is equal to @p num_msgs, otherwise the number of messages required is @p num_msgs - 1, |
| 153 | + * since a PEC byte is not required. |
| 154 | + * |
| 155 | + * @note This function is intended for SMBus drivers that do not have hardware PEC support and |
| 156 | + * therefore must rely on software PEC calculation. |
| 157 | + * |
| 158 | + * @note At this time, only 7-bit addresses are supported in @p addr. |
| 159 | + * |
| 160 | + * @param flags SMBus flags. |
| 161 | + * @param addr The address of the target device. |
| 162 | + * @param messages Array of @ref i2c_msg objects that make up the transaction. |
| 163 | + * @param num_msgs Number of messages in the transaction. |
| 164 | + */ |
| 165 | +void smbus_write_prepare_pec(uint32_t flags, uint16_t addr, struct i2c_msg *msgs, uint8_t num_msgs); |
| 166 | + |
| 167 | +/** |
| 168 | + * @brief Check the packet error checking (PEC) byte for an SMBus read transaction |
| 169 | + * |
| 170 | + * If the @p flags bitmask contains @ref SMBUS_MODE_PEC (i.e. PEC is enabled), the number of |
| 171 | + * messages is equal to @p num_msgs, otherwise the number of messages required is |
| 172 | + * @p num_msgs - 1, since a PEC byte is not required. |
| 173 | + * |
| 174 | + * When PEC is not enabled, this function returns 0. |
| 175 | + * |
| 176 | + * @note This function is intended for SMBus drivers that do not have hardware PEC support and |
| 177 | + * therefore must rely on software PEC calculation. |
| 178 | + * |
| 179 | + * @note At this time, only 7-bit addresses are supported in @p addr. |
| 180 | + * |
| 181 | + * @param flags SMBus flags. |
| 182 | + * @param addr The address of the target device. |
| 183 | + * @param messages Array of @ref i2c_msg objects that make up the transaction. |
| 184 | + * @param num_msgs Number of messages in the transaction. |
| 185 | + * @retval 0 on success. |
| 186 | + * @retval -EIO if the PEC byte does not match the computed value. |
| 187 | + */ |
| 188 | +int smbus_read_check_pec(uint32_t flags, uint16_t addr, const struct i2c_msg *msgs, |
| 189 | + uint8_t num_msgs); |
| 190 | + |
110 | 191 | #endif /* ZEPHYR_DRIVERS_SMBUS_SMBUS_UTILS_H_ */
|
0 commit comments