2010-08-17 08:50:00 +00:00
|
|
|
/*
|
2013-03-30 10:32:37 +00:00
|
|
|
ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2010-08-17 08:50:00 +00:00
|
|
|
|
2013-03-30 10:32:37 +00:00
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
2010-08-17 08:50:00 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
/*===========================================================================*/
|
2013-02-28 16:23:19 +00:00
|
|
|
/* Driver local variables and types. */
|
2010-08-17 08:50:00 +00:00
|
|
|
/*===========================================================================*/
|
|
|
|
|
|
|
|
/*===========================================================================*/
|
|
|
|
/* 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
|
|
|
|
|
|
|
/** @} */
|