git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@2132 35acf78f-673a-0410-8e92-d51de3d6d3f4

This commit is contained in:
gdisirio 2010-08-17 08:50:00 +00:00
parent ae70bb24cc
commit 8b45c58317
9 changed files with 606 additions and 0 deletions

View File

@ -177,6 +177,49 @@
* @ingroup SERIAL
*/
/**
* @defgroup I2C I2C Driver
* @brief Generic I2C Driver.
* @details This module implements a generic I2C driver. The driver implements
* a state machine internally:
* @dot
digraph example {
rankdir="LR";
node [shape=circle, fontname=Helvetica, fontsize=8, fixedsize="true", width="0.8", height="0.8"];
edge [fontname=Helvetica, fontsize=8];
uninit [label="SPI_UNINIT", style="bold"];
stop [label="SPI_STOP\nLow Power"];
ready [label="SPI_READY\nClock Enabled"];
active [label="SPI_ACTIVE\nBus Active"];
uninit -> stop [label="spiInit()"];
stop -> ready [label="spiStart()"];
ready -> ready [label="spiStart()"];
ready -> ready [label="spiIgnore()"];
ready -> stop [label="spiStop()"];
stop -> stop [label="spiStop()"];
ready -> active [label="spiSelect()"];
active -> active [label="spiSelect()"];
active -> ready [label="spiUnselect()"];
ready -> ready [label="spiUnselect()"];
active -> active [label="spiIgnore()\nspiExchange()\nspiSend()\nspiReceive()"];
}
* @enddot
*
* The driver is not thread safe for performance reasons, if you need to access
* the I2C bus from multiple thread then use the @p i2cAcquireBus() and
* @p i2cReleaseBus() APIs in order to gain exclusive access.
*
* @ingroup IO
*/
/**
* @defgroup I2C_LLD I2C Low Level Driver
* @brief @ref I2C low level driver template.
* @details This file is a template for an I2C low level driver.
*
* @ingroup I2C
*/
/**
* @defgroup SPI SPI Driver
* @brief Generic SPI Driver.

View File

@ -3,6 +3,7 @@
HALSRC = ${CHIBIOS}/os/hal/src/hal.c \
${CHIBIOS}/os/hal/src/adc.c \
${CHIBIOS}/os/hal/src/can.c \
${CHIBIOS}/os/hal/src/i2c.c \
${CHIBIOS}/os/hal/src/mac.c \
${CHIBIOS}/os/hal/src/pal.c \
${CHIBIOS}/os/hal/src/pwm.c \

View File

@ -36,6 +36,7 @@
#include "pal.h"
#include "adc.h"
#include "can.h"
#include "i2c.h"
#include "mac.h"
#include "pwm.h"
#include "serial.h"

103
os/hal/include/i2c.h Normal file
View File

@ -0,0 +1,103 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
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 i2c.h
* @brief I2C Driver macros and structures.
*
* @addtogroup I2C
* @{
*/
#ifndef _I2C_H_
#define _I2C_H_
#if CH_HAL_USE_I2C || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
/**
* @brief Enables the mutual exclusion APIs on the I2C bus.
*/
#if !defined(I2C_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
#define I2C_USE_MUTUAL_EXCLUSION TRUE
#endif
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
/**
* @brief Driver state machine possible states.
*/
typedef enum {
I2C_UNINIT = 0, /**< @brief Not initialized. */
I2C_STOP = 1, /**< @brief Stopped. */
I2C_READY = 2, /**< @brief Ready. */
I2C_MREADY = 3, /**< @brief START sent. */
I2C_MTRANSMIT = 4, /**< @brief Master transmitting. */
I2C_MRECEIVE = 5, /**< @brief Master receiving. */
} i2cstate_t;
#include "i2c_lld.h"
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
void i2cInit(void);
void i2cObjectInit(I2CDriver *i2cp);
void i2cStart(I2CDriver *i2cp, const I2CConfig *config);
void i2cStop(I2CDriver *i2cp);
void i2cMasterStartI(I2CDriver *i2cp, i2ccallback_t callback);
void i2cMasterStopI(I2CDriver *i2cp, i2ccallback_t callback);
void i2cMasterTransmitI(I2CDriver *i2cp, size_t n, const uint8_t *txbuf,
i2ccallback_t callback);
void i2cMasterReceiveI(I2CDriver *i2cp, size_t n, uint8_t *rxbuf,
i2ccallback_t callback);
#if I2C_USE_MUTUAL_EXCLUSION
void i2cAcquireBus(I2CDriver *i2cp);
void i2cReleaseBus(I2CDriver *i2cp);
#endif /* I2C_USE_MUTUAL_EXCLUSION */
#ifdef __cplusplus
}
#endif
#endif /* CH_HAL_USE_I2C */
#endif /* _I2C_H_ */
/** @} */

View File

@ -60,6 +60,9 @@ void halInit(void) {
#if CH_HAL_USE_CAN
canInit();
#endif
#if CH_HAL_USE_I2C
i2cInit();
#endif
#if CH_HAL_USE_MAC
macInit();
#endif

222
os/hal/src/i2c.c Normal file
View File

@ -0,0 +1,222 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
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 i2c.c
* @brief I2C Driver code.
*
* @addtogroup I2C
* @{
*/
#include "ch.h"
#include "hal.h"
#if CH_HAL_USE_I2C || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief I2C Driver initialization.
*/
void i2cInit(void) {
i2c_lld_init();
}
/**
* @brief Initializes the standard part of a @p I2CDriver structure.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2cObjectInit(I2CDriver *i2cp) {
i2cp->i2c_state = I2C_STOP;
i2cp->i2c_config = NULL;
}
/**
* @brief Configures and activates the I2C peripheral.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] config pointer to the @p I2CConfig object
*/
void i2cStart(I2CDriver *i2cp, const I2CConfig *config) {
chDbgCheck((i2cp != NULL) && (config != NULL), "i2cStart");
chSysLock();
chDbgAssert((i2cp->i2c_state == I2C_STOP) || (i2cp->i2c_state == I2C_READY),
"i2cStart(), #1",
"invalid state");
i2cp->i2c_config = config;
i2c_lld_start(i2cp);
i2cp->i2c_state = I2C_READY;
chSysUnlock();
}
/**
* @brief Deactivates the I2C peripheral.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2cStop(I2CDriver *i2cp) {
chDbgCheck(i2cp != NULL, "i2cStop");
chSysLock();
chDbgAssert((i2cp->i2c_state == I2C_STOP) || (i2cp->i2c_state == I2C_READY),
"i2cStop(), #1",
"invalid state");
i2c_lld_stop(i2cp);
i2cp->i2c_state = I2C_STOP;
chSysUnlock();
}
/**
* @brief Initiates a master bus transaction.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] callback operation complete callback
*/
void i2cMasterStartI(I2CDriver *i2cp, i2ccallback_t callback) {
chDbgCheck((i2cp != NULL) && (callback != NULL), "i2cMasterStartI");
chDbgAssert((i2cp->i2c_state == I2C_READY) || (i2cp->i2c_state == I2C_MREADY),
"i2cMasterStartI(), #1", "invalid state");
i2cp->id_callback = callback;
i2c_lld_master_start(i2cp);
}
/**
* @brief Terminates a master bus transaction.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] callback operation complete callback
*/
void i2cMasterStopI(I2CDriver *i2cp, i2ccallback_t callback) {
chDbgCheck((i2cp != NULL) && (callback != NULL), "i2cMasterStopI");
chDbgAssert(i2cp->i2c_state == I2C_MREADY,
"i2cMasterStopI(), #1", "invalid state");
i2cp->id_callback = callback;
i2c_lld_master_stop(i2cp);
}
/**
* @brief Master transmission.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] n number of bytes to be transmitted
* @param[in] txbuf transmit data buffer pointer
* @param[in] callback operation complete callback
*/
void i2cMasterTransmitI(I2CDriver *i2cp, size_t n, const uint8_t *txbuf,
i2ccallback_t callback) {
chDbgCheck((i2cp != NULL) && (n > 0) &&
(txbuf != NULL) && (callback != NULL), "i2cMasterTransmitI");
chDbgAssert(i2cp->i2c_state == I2C_MREADY,
"i2cMasterTransmitI(), #1", "invalid state");
i2cp->id_callback = callback;
i2c_lld_master_transmit(i2cp, n, txbuf);
}
/**
* @brief Master receive.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] n number of bytes to be transmitted
* @param[in] rxbuf receive data buffer pointer
* @param[in] callback operation complete callback
*/
void i2cMasterReceiveI(I2CDriver *i2cp, size_t n, uint8_t *rxbuf,
i2ccallback_t callback) {
chDbgCheck((i2cp != NULL) && (n > 0) &&
(rxbuf != NULL) && (callback != NULL), "i2cMasterReceiveI");
chDbgAssert(i2cp->i2c_state == I2C_MREADY,
"i2cMasterReceiveI(), #1", "invalid state");
i2cp->id_callback = callback;
i2c_lld_master_receive(i2cp, n, rxbuf);
}
#if I2C_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
/**
* @brief Gains exclusive access to the I2C bus.
* @details This function tries to gain ownership to the I2C bus, if the bus
* is already being used then the invoking thread is queued.
* @note This function is only available when the @p I2C_USE_MUTUAL_EXCLUSION
* option is set to @p TRUE.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*
*/
void i2cAcquireBus(I2CDriver *i2cp) {
chDbgCheck(i2cp != NULL, "i2cAcquireBus");
#if CH_USE_MUTEXES
chMtxLock(&i2cp->id_mutex);
#elif CH_USE_SEMAPHORES
chSemWait(&i2cp->id_semaphore);
#endif
}
/**
* @brief Releases exclusive access to the I2C bus.
* @note This function is only available when the @p I2C_USE_MUTUAL_EXCLUSION
* option is set to @p TRUE.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2cReleaseBus(I2CDriver *i2cp) {
chDbgCheck(i2cp != NULL, "i2cReleaseBus");
#if CH_USE_MUTEXES
(void)i2cp;
chMtxUnlock();
#elif CH_USE_SEMAPHORES
chSemSignal(&i2cp->id_semaphore);
#endif
}
#endif /* I2C_USE_MUTUAL_EXCLUSION */
#endif /* CH_HAL_USE_I2C */
/** @} */

125
os/hal/templates/i2c_lld.c Normal file
View File

@ -0,0 +1,125 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
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.
*
* @addtogroup I2C_LLD
* @{
*/
#include "ch.h"
#include "hal.h"
#if CH_HAL_USE_I2C || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver exported variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local variables. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver local functions. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver interrupt handlers. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver exported functions. */
/*===========================================================================*/
/**
* @brief Low level I2C driver initialization.
*/
void i2c_lld_init(void) {
}
/**
* @brief Configures and activates the I2C peripheral.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2c_lld_start(I2CDriver *i2cp) {
if (i2cp->i2c_state == I2C_STOP) {
/* Clock activation.*/
}
/* Configuration.*/
}
/**
* @brief Deactivates the I2C peripheral.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2c_lld_stop(I2CDriver *i2cp) {
}
/**
* @brief Initiates a master bus transaction.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2c_lld_master_start(I2CDriver *i2cp) {
}
/**
* @brief Terminates a master bus transaction.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2c_lld_master_stop(I2CDriver *i2cp) {
}
/**
* @brief Master transmission.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] n number of bytes to be transmitted
* @param[in] txbuf transmit data buffer pointer
*/
void i2c_lld_master_transmit(I2CDriver *i2cp, size_t n,
const uint8_t *txbuf) {
}
/**
* @brief Master receive.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] n number of bytes to be transmitted
* @param[in] rxbuf receive data buffer pointer
*/
void i2c_lld_master_receive(I2CDriver *i2cp, size_t n, uint8_t *rxbuf) {
}
#endif /* CH_HAL_USE_I2C */
/** @} */

107
os/hal/templates/i2c_lld.h Normal file
View File

@ -0,0 +1,107 @@
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010 Giovanni Di Sirio.
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.h
* @brief I2C Driver subsystem low level driver header template.
*
* @addtogroup I2C_LLD
* @{
*/
#ifndef _I2C_LLD_H_
#define _I2C_LLD_H_
#if CH_HAL_USE_I2C || defined(__DOXYGEN__)
/*===========================================================================*/
/* Driver constants. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver pre-compile time settings. */
/*===========================================================================*/
/*===========================================================================*/
/* Derived constants and error checks. */
/*===========================================================================*/
/*===========================================================================*/
/* Driver data structures and types. */
/*===========================================================================*/
/**
* @brief I2C completion callback type.
*
* @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] sts operation status
*/
typedef void (*i2ccallback_t)(I2CDriver *i2cp, i2cstatus_t sts);
/**
* @brief Driver configuration structure.
* @note It could be empty on some architectures.
*/
typedef struct {
/** @brief I2C bus bit rate.*/
uint32_t ic_speed;
/* End of the mandatory fields.*/
} I2CConfig;
/**
* @brief Structure representing an I2C driver.
*/
typedef struct {
/** @brief Driver state.*/
i2cstate_t id_state;
/** @brief Current configuration data.*/
const I2CConfig *id_config;
/** @brief Current callback.*/
i2ccallback_t id_callback;
/* End of the mandatory fields.*/
} I2CDriver;
/*===========================================================================*/
/* Driver macros. */
/*===========================================================================*/
/*===========================================================================*/
/* External declarations. */
/*===========================================================================*/
#ifdef __cplusplus
extern "C" {
#endif
void i2c_lld_init(void);
void i2c_lld_start(I2CDriver *i2cp);
void i2c_lld_stop(I2CDriver *i2cp);
void i2c_lld_master_start(I2CDriver *i2cp);
void i2c_lld_master_stop(I2CDriver *i2cp);
void i2c_lld_master_transmit(I2CDriver *i2cp, size_t n,
const uint8_t *txbuf);
void i2c_lld_master_receive(I2CDriver *i2cp, size_t n, uint8_t *rxbuf);
#ifdef __cplusplus
}
#endif
#endif /* CH_HAL_USE_I2C */
#endif /* _I2C_LLD_H_ */
/** @} */

View File

@ -71,6 +71,7 @@
2.0.3).
- FIX: Fixed a documentation error regarding the ADC driver function
adcStartConversion() (bug 3039890)(backported to 2.0.3).
- NEW: New I2C device driver model (no implementations yet).
- NEW: Added to the UART driver the capability to return the number of
not yet transferred frames when stopping an operation.
- NEW: Added more compile-time checks to the various STM32 device drivers.