I2C slave support in HAL high level driver.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@16205 27425a3e-05d8-49a3-a47f-9c15f0e5edd8
This commit is contained in:
Giovanni Di Sirio 2023-04-04 09:53:16 +00:00
parent be00611a7b
commit c9f2cc4c73
3 changed files with 143 additions and 1 deletions

View File

@ -66,6 +66,11 @@
/* Derived constants and error checks. */ /* Derived constants and error checks. */
/*===========================================================================*/ /*===========================================================================*/
/* For compatibility, some LLDs could not export this.*/
#if !defined(I2C_SUPPORTS_SLAVE_MODE)
#define I2C_SUPPORTS_SLAVE_MODE FALSE
#endif
/*===========================================================================*/ /*===========================================================================*/
/* Driver data structures and types. */ /* Driver data structures and types. */
/*===========================================================================*/ /*===========================================================================*/
@ -154,7 +159,13 @@ extern "C" {
void i2cAcquireBus(I2CDriver *i2cp); void i2cAcquireBus(I2CDriver *i2cp);
void i2cReleaseBus(I2CDriver *i2cp); void i2cReleaseBus(I2CDriver *i2cp);
#endif #endif
#if I2C_SUPPORTS_SLAVE_MODE == TRUE
msg_t i2cSlaveMatchAddress(I2CDriver *i2cp, i2caddr_t i2cadr);
msg_t i2cSlaveReceiveTimeout(I2CDriver *i2cp, uint8_t *rxbuf,
size_t rxbytes, sysinterval_t timeout);
msg_t i2cSlaveTransmitTimeout(I2CDriver *i2cp, const uint8_t *txbuf,
size_t txbytes, sysinterval_t timeout);
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -299,6 +299,136 @@ void i2cReleaseBus(I2CDriver *i2cp) {
} }
#endif /* I2C_USE_MUTUAL_EXCLUSION == TRUE */ #endif /* I2C_USE_MUTUAL_EXCLUSION == TRUE */
#if (I2C_SUPPORTS_SLAVE_MODE == TRUE) || defined(__DOXYGEN__)
/**
* @brief Listen I2C bus for address match.
* @details Use 7 bit address (10 bit,dual and general call address dosn't implement yet) .
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] addr slave device address
* @return The operation status.
* @retval MSG_OK if the function succeeded.
* @retval MSG_RESET if one or more I2C errors occurred, the errors can
* be retrieved using @p i2cGetErrors().
*
* @notapi
*/
msg_t i2cSlaveMatchAddress(I2CDriver *i2cp, i2caddr_t addr) {
osalDbgCheck((i2cp != NULL) && (addr != 0x00));
chSysLock();
msg_t result = i2c_lld_match_address(i2cp, addr);
chSysUnlock();
return result;
}
/**
* @brief Receive data via the I2C bus as slave and call handler.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[out] rxbuf pointer to the receive buffer
* @param[in] rxbytes size of receive buffer
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_INFINITE no timeout.
* .
* @return The operation status.
* @retval MSG_OK if the function succeeded.
* @retval MSG_RESET if one or more I2C errors occurred, the errors can
* be retrieved using @p i2cGetErrors().
* @retval MSG_TIMEOUT if a timeout occurred before operation end. <b>After a
* timeout the driver must be stopped and restarted
* because the bus is in an uncertain state</b>.
*
* @api
*/
msg_t i2cSlaveReceiveTimeout(I2CDriver *i2cp,
uint8_t *rxbuf,
size_t rxbytes,
sysinterval_t timeout) {
msg_t rdymsg;
osalDbgCheck((i2cp != NULL) &&
(rxbytes > 0U) &&
(rxbuf != NULL) &&
(timeout != TIME_IMMEDIATE));
osalDbgAssert(i2cp->state == I2C_READY, "not ready");
osalSysLock();
i2cp->errors = I2C_NO_ERROR;
i2cp->state = I2C_ACTIVE_RX;
rdymsg = i2c_lld_slave_receive_timeout(i2cp, rxbuf, rxbytes, timeout);
if (rdymsg == MSG_TIMEOUT) {
i2cp->state = I2C_LOCKED;
}
else {
i2cp->state = I2C_READY;
}
osalSysUnlock();
return rdymsg;
}
/**
* @brief Transmits data via the I2C bus as slave.
* @details Call this function when Master request data (in request handler)
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] txbuf pointer to the transmit buffer
* @param[in] txbytes number of bytes to be transmitted
* @param[in] timeout the number of ticks before the operation timeouts,
* the following special values are allowed:
* - @a TIME_INFINITE no timeout.
* .
* @return The operation status.
* @retval MSG_OK if the function succeeded.
* @retval MSG_RESET if one or more I2C errors occurred, the errors can
* be retrieved using @p i2cGetErrors().
* @retval MSG_TIMEOUT if a timeout occurred before operation end. <b>After a
* timeout the driver must be stopped and restarted
* because the bus is in an uncertain state</b>.
*
* @api
*/
msg_t i2cSlaveTransmitTimeout(I2CDriver *i2cp,
const uint8_t *txbuf,
size_t txbytes,
sysinterval_t timeout){
msg_t rdymsg;
osalDbgCheck((i2cp != NULL) &&
(txbytes > 0U) &&
(txbuf != NULL) &&
(timeout != TIME_IMMEDIATE));
osalDbgAssert(i2cp->state == I2C_READY, "not ready");
osalSysLock();
i2cp->errors = I2C_NO_ERROR;
i2cp->state = I2C_ACTIVE_TX;
rdymsg = i2c_lld_slave_transmit_timeout(i2cp, txbuf, txbytes, timeout);
if (rdymsg == MSG_TIMEOUT) {
i2cp->state = I2C_LOCKED;
}
else {
i2cp->state = I2C_READY;
}
osalSysUnlock();
return rdymsg;
}
#endif /* I2C_SUPPORTS_SLAVE_MODE == TRUE */
#endif /* HAL_USE_I2C == TRUE */ #endif /* HAL_USE_I2C == TRUE */
/** @} */ /** @} */

View File

@ -74,6 +74,7 @@
***************************************************************************** *****************************************************************************
*** Next *** *** Next ***
- NEW: I2C slave support in HAL high level driver.
- NEW: Added settings for STM32 OCTOSPIv1 and OCTOSPIv2 TCR bits SSHIFT and - NEW: Added settings for STM32 OCTOSPIv1 and OCTOSPIv2 TCR bits SSHIFT and
DHQC. DHQC.
- NEW: Automatic removal of duplicated inclusion paths on make command lines. - NEW: Automatic removal of duplicated inclusion paths on make command lines.