2010-08-17 08:50:00 +00:00
|
|
|
/*
|
2011-03-18 18:38:08 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
|
2013-02-02 10:58:09 +00:00
|
|
|
2011,2012,2013 Giovanni Di Sirio.
|
2010-08-17 08:50:00 +00:00
|
|
|
|
|
|
|
This file is part of ChibiOS/RT.
|
|
|
|
|
|
|
|
ChibiOS/RT is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
ChibiOS/RT is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file templates/i2c_lld.c
|
|
|
|
* @brief I2C Driver subsystem low level driver source template.
|
|
|
|
*
|
2010-10-25 18:48:13 +00:00
|
|
|
* @addtogroup I2C
|
2010-08-17 08:50:00 +00:00
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ch.h"
|
|
|
|
#include "hal.h"
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#if HAL_USE_I2C || defined(__DOXYGEN__)
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2011-08-26 13:47:22 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local definitions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2010-08-17 08:50:00 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
2013-02-25 11:58:40 +00:00
|
|
|
/**
|
|
|
|
* @brief I2C1 driver identifier.
|
|
|
|
*/
|
2013-02-24 10:07:54 +00:00
|
|
|
#if PLATFORM_I2C_USE_I2C1 || defined(__DOXYGEN__)
|
|
|
|
I2CDriver I2CD1;
|
|
|
|
#endif
|
|
|
|
|
2010-08-17 08:50:00 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local variables. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver local functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver interrupt handlers. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* Driver exported functions. */
|
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Low level I2C driver initialization.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-08-17 08:50:00 +00:00
|
|
|
*/
|
|
|
|
void i2c_lld_init(void) {
|
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
#if PLATFORM_I2C_USE_I2C1
|
|
|
|
i2cObjectInit(&I2CD1);
|
|
|
|
#endif /* PLATFORM_I2C_USE_I2C1 */
|
2010-08-17 08:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Configures and activates the I2C peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] i2cp pointer to the @p I2CDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-08-17 08:50:00 +00:00
|
|
|
*/
|
|
|
|
void i2c_lld_start(I2CDriver *i2cp) {
|
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
if (i2cp->state == I2C_STOP) {
|
2013-02-27 10:42:09 +00:00
|
|
|
/* Enables the peripheral.*/
|
2013-02-24 10:07:54 +00:00
|
|
|
#if PLATFORM_I2C_USE_I2C1
|
|
|
|
if (&I2CD1 == i2cp) {
|
|
|
|
|
|
|
|
}
|
|
|
|
#endif /* PLATFORM_I2C_USE_I2C1 */
|
2010-08-17 08:50:00 +00:00
|
|
|
}
|
2013-02-24 10:07:54 +00:00
|
|
|
/* Configures the peripheral.*/
|
|
|
|
|
2010-08-17 08:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Deactivates the I2C peripheral.
|
|
|
|
*
|
|
|
|
* @param[in] i2cp pointer to the @p I2CDriver object
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-08-17 08:50:00 +00:00
|
|
|
*/
|
|
|
|
void i2c_lld_stop(I2CDriver *i2cp) {
|
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
if (i2cp->state != I2C_STOP) {
|
|
|
|
/* Resets the peripheral.*/
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
/* Disables the peripheral.*/
|
|
|
|
#if PLATFORM_I2C_USE_I2C1
|
|
|
|
if (&I2CD1 == i2cp) {
|
2010-08-17 13:23:41 +00:00
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
}
|
|
|
|
#endif /* PLATFORM_I2C_USE_I2C1 */
|
|
|
|
}
|
2010-08-17 13:23:41 +00:00
|
|
|
}
|
|
|
|
|
2010-08-17 08:50:00 +00:00
|
|
|
/**
|
2013-02-24 10:07:54 +00:00
|
|
|
* @brief Receives data via the I2C bus as master.
|
|
|
|
* @details Number of receiving bytes must be more than 1 on STM32F1x. This is
|
|
|
|
* hardware restriction.
|
2010-08-17 08:50:00 +00:00
|
|
|
*
|
|
|
|
* @param[in] i2cp pointer to the @p I2CDriver object
|
2013-02-24 10:07:54 +00:00
|
|
|
* @param[in] addr slave device address
|
|
|
|
* @param[out] rxbuf pointer to the receive buffer
|
|
|
|
* @param[in] rxbytes number of bytes to be received
|
|
|
|
* @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 RDY_OK if the function succeeded.
|
|
|
|
* @retval RDY_RESET if one or more I2C errors occurred, the errors can
|
|
|
|
* be retrieved using @p i2cGetErrors().
|
|
|
|
* @retval RDY_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>.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-08-17 08:50:00 +00:00
|
|
|
*/
|
2013-02-24 10:07:54 +00:00
|
|
|
msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
|
|
|
|
uint8_t *rxbuf, size_t rxbytes,
|
|
|
|
systime_t timeout) {
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2013-02-25 11:58:40 +00:00
|
|
|
(void)i2cp;
|
|
|
|
(void)addr;
|
|
|
|
(void)rxbuf;
|
|
|
|
(void)rxbytes;
|
|
|
|
(void)timeout;
|
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
return RDY_OK;
|
2010-08-17 08:50:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-02-24 10:07:54 +00:00
|
|
|
* @brief Transmits data via the I2C bus as master.
|
|
|
|
* @details Number of receiving bytes must be 0 or more than 1 on STM32F1x.
|
|
|
|
* This is hardware restriction.
|
2010-08-17 08:50:00 +00:00
|
|
|
*
|
|
|
|
* @param[in] i2cp pointer to the @p I2CDriver object
|
2013-02-24 10:07:54 +00:00
|
|
|
* @param[in] addr slave device address
|
|
|
|
* @param[in] txbuf pointer to the transmit buffer
|
|
|
|
* @param[in] txbytes number of bytes to be transmitted
|
|
|
|
* @param[out] rxbuf pointer to the receive buffer
|
|
|
|
* @param[in] rxbytes number of bytes to be received
|
|
|
|
* @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 RDY_OK if the function succeeded.
|
|
|
|
* @retval RDY_RESET if one or more I2C errors occurred, the errors can
|
|
|
|
* be retrieved using @p i2cGetErrors().
|
|
|
|
* @retval RDY_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>.
|
2010-10-04 17:16:18 +00:00
|
|
|
*
|
|
|
|
* @notapi
|
2010-08-17 08:50:00 +00:00
|
|
|
*/
|
2013-02-24 10:07:54 +00:00
|
|
|
msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
|
|
|
|
const uint8_t *txbuf, size_t txbytes,
|
|
|
|
uint8_t *rxbuf, size_t rxbytes,
|
|
|
|
systime_t timeout) {
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2013-02-25 11:58:40 +00:00
|
|
|
(void)i2cp;
|
|
|
|
(void)addr;
|
|
|
|
(void)txbuf;
|
|
|
|
(void)txbytes;
|
|
|
|
(void)rxbuf;
|
|
|
|
(void)rxbytes;
|
|
|
|
(void)timeout;
|
|
|
|
|
2013-02-24 10:07:54 +00:00
|
|
|
return RDY_OK;
|
2010-08-17 08:50:00 +00:00
|
|
|
}
|
|
|
|
|
2010-11-01 17:29:56 +00:00
|
|
|
#endif /* HAL_USE_I2C */
|
2010-08-17 08:50:00 +00:00
|
|
|
|
|
|
|
/** @} */
|