I2C. Code cleanups.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/i2c_dev@2776 35acf78f-673a-0410-8e92-d51de3d6d3f4
This commit is contained in:
barthess 2011-02-27 15:22:18 +00:00
parent e96e10761e
commit 4f827c235a
4 changed files with 57 additions and 44 deletions

View File

@ -143,13 +143,9 @@ extern "C" {
void i2cStop(I2CDriver *i2cp); void i2cStop(I2CDriver *i2cp);
void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg); void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg); void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
void i2cMasterStart(I2CDriver *i2cp);
void i2cMasterStop(I2CDriver *i2cp);
void i2cMasterStartI(I2CDriver *i2cp,uint16_t header);
void i2cMasterStopI(I2CDriver *i2cp);
void i2cMasterRestartI(I2CDriver *i2cp);
void i2cMasterTransmitI(I2CDriver *i2cp, size_t n, const uint8_t *txbuf);
void i2cMasterReceiveI(I2CDriver *i2cp, size_t n, uint8_t *rxbuf);
#if I2C_USE_MUTUAL_EXCLUSION #if I2C_USE_MUTUAL_EXCLUSION
void i2cAcquireBus(I2CDriver *i2cp); void i2cAcquireBus(I2CDriver *i2cp);
void i2cReleaseBus(I2CDriver *i2cp); void i2cReleaseBus(I2CDriver *i2cp);

View File

@ -452,8 +452,6 @@ void i2c_lld_stop(I2CDriver *i2cp) {
} }
void i2c_lld_master_start(I2CDriver *i2cp){ void i2c_lld_master_start(I2CDriver *i2cp){
i2cp->id_i2c->CR1 |= I2C_CR1_START; i2cp->id_i2c->CR1 |= I2C_CR1_START;
while (i2cp->id_i2c->CR1 & I2C_CR1_START); while (i2cp->id_i2c->CR1 & I2C_CR1_START);
@ -468,8 +466,8 @@ void i2c_lld_master_stop(I2CDriver *i2cp){
} }
void i2c_lld_master_transmitI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg){ void i2c_lld_master_transmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg){
//TODO: check txbytes <= sizeof(i2cscfg->txbuf) here, or in hylevel API //TODO: check txbytes <= sizeof(i2cscfg->txbuf) here, or in hi level API
i2cp->id_slave_config = i2cscfg; i2cp->id_slave_config = i2cscfg;
i2cp->id_slave_config->rw_bit = I2C_WRITE; i2cp->id_slave_config->rw_bit = I2C_WRITE;
@ -478,8 +476,8 @@ void i2c_lld_master_transmitI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg){
i2c_lld_master_start(i2cp); i2c_lld_master_start(i2cp);
} }
void i2c_lld_master_receiveI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg){ void i2c_lld_master_receive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg){
//TODO: check txbytes <= sizeof(i2cscfg->txbuf) here, or in hylevel API //TODO: check txbytes <= sizeof(i2cscfg->txbuf) here, or in hi level API
i2cp->id_slave_config = i2cscfg; i2cp->id_slave_config = i2cscfg;
i2cp->id_slave_config->rw_bit = I2C_READ; i2cp->id_slave_config->rw_bit = I2C_READ;

View File

@ -110,28 +110,23 @@ typedef enum {
* @brief Driver configuration structure. * @brief Driver configuration structure.
*/ */
typedef struct { typedef struct {
I2C_opMode_t opMode; /*!< Specifies the I2C mode.*/ I2C_opMode_t opMode; /*!< Specifies the I2C mode.*/
uint32_t ClockSpeed; /*!< Specifies the clock frequency. Must be set to a value lower than 400kHz */ uint32_t ClockSpeed; /*!< Specifies the clock frequency. Must be set to a value lower than 400kHz */
I2C_DutyCycle_t FastModeDutyCycle;/*!< Specifies the I2C fast mode duty cycle */ I2C_DutyCycle_t FastModeDutyCycle;/*!< Specifies the I2C fast mode duty cycle */
uint8_t OwnAddress7; /*!< Specifies the first device 7-bit own address. */ uint8_t OwnAddress7; /*!< Specifies the first device 7-bit own address. */
uint16_t OwnAddress10; /*!< Specifies the second part of device own address in 10-bit mode. Set to NULL if not used. */ uint16_t OwnAddress10; /*!< Specifies the second part of device own address in 10-bit mode. Set to NULL if not used. */
} I2CConfig; } I2CConfig;
/**
* @brief TODO:
*/
typedef uint32_t i2cflags_t;
/** /**
* @brief TODO: * @brief I2C transmission data block size.
*/ */
typedef uint8_t i2cblock_t; typedef uint8_t i2cblock_t;
/** /**
* @brief Structure representing an I2C slave configuration. * @brief Structure representing an I2C slave configuration.
* @details Each slave has its own data buffers, adress, and error flags.
*/ */
struct I2CSlaveConfig{ struct I2CSlaveConfig{
/** /**
@ -141,6 +136,7 @@ struct I2CSlaveConfig{
* If set to @p NULL then the callback is disabled. * If set to @p NULL then the callback is disabled.
*/ */
i2ccallback_t id_callback; i2ccallback_t id_callback;
/** /**
* @brief Callback pointer. * @brief Callback pointer.
* @note This callback will be invoked when error condition occur. * @note This callback will be invoked when error condition occur.
@ -159,17 +155,22 @@ struct I2CSlaveConfig{
size_t txbufhead; size_t txbufhead;
/** /**
* @brief Address word. * @brief Contain slave address and some flags.
* @details The MSB used to switch between 10-bit and 7-bit modes * @details Bits 0..9 contain slave address in 10-bit mode.
* (0 denotes 7-bit mode). Bits 0..9 contain slave address. *
* Bits 10..14 ignores in 10-bit mode. * Bits 0..6 contain slave address in 7-bit mode.
* Bits 7..14 ignores in 7-bot mode. *
* Bits 10..14 are not used in 10-bit mode.
* Bits 7..14 are not used in 7-bit mode.
*
* Bit 15 is used to switch between 10-bit and 7-bit modes
* (0 denotes 7-bit mode).
*/ */
uint16_t address; uint16_t address;
//TODO: join rw_bit, restart in one word. //TODO: merge rw_bit, restart and address in one 16-bit variable.
uint8_t rw_bit; // this flag contain R/W bit uint8_t rw_bit;
bool_t restart; // send restart or stop event after complete data tx/rx bool_t restart; // send restart if TRUE. Else sent stop event after complete data tx/rx
#if I2C_USE_WAIT #if I2C_USE_WAIT
@ -249,11 +250,12 @@ void i2c_lld_set_own_address(I2CDriver *i2cp);
void i2c_lld_master_start(I2CDriver *i2cp); void i2c_lld_master_start(I2CDriver *i2cp);
void i2c_lld_master_stop(I2CDriver *i2cp); void i2c_lld_master_stop(I2CDriver *i2cp);
void i2c_lld_master_transmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
void i2c_lld_master_receive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
void i2c_lld_master_transmit_NI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t restart); void i2c_lld_master_transmit_NI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t restart);
void i2c_lld_master_transmitI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
void i2c_lld_master_receive_NI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg); void i2c_lld_master_receive_NI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
void i2c_lld_master_receiveI(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -116,14 +116,35 @@ void i2cStop(I2CDriver *i2cp) {
chSysUnlock(); chSysUnlock();
} }
/**
* @brief Generate (re)start on the bus.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2cMasterStart(I2CDriver *i2cp){
chDbgCheck((i2cp != NULL), "i2cMasterTransmit");
i2c_lld_master_start(i2cp);
}
/**
* @brief Generate stop on the bus.
*
* @param[in] i2cp pointer to the @p I2CDriver object
*/
void i2cMasterStop(I2CDriver *i2cp){
chDbgCheck((i2cp != NULL), "i2cMasterTransmit");
i2c_lld_master_stop(i2cp);
}
/** /**
* @brief Sends data ever the I2C bus. * @brief Sends data ever the I2C bus.
* *
* @param[in] i2cp pointer to the @p I2CDriver object * @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] slave_addr1 7-bit address of the slave * @param[in] i2cscfg pointer to the @p I2CSlaveConfig object
* @param[in] slave_addr1 used in 10-bit address mode
* @param[in] n number of words to send
* @param[in] txbuf the pointer to the transmit buffer
* *
*/ */
void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) { void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) {
@ -134,19 +155,15 @@ void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) {
"i2cMasterTransmit(), #1", "i2cMasterTransmit(), #1",
"not active"); "not active");
i2c_lld_master_transmitI(i2cp, i2cscfg); i2c_lld_master_transmit(i2cp, i2cscfg);
} }
/** /**
* @brief Receives data from the I2C bus. * @brief Receives data from the I2C bus.
* *
* @param[in] i2cp pointer to the @p I2CDriver object * @param[in] i2cp pointer to the @p I2CDriver object
* @param[in] slave_addr1 7-bit address of the slave * @param[in] i2cscfg pointer to the @p I2CSlaveConfig object
* @param[in] slave_addr1 used in 10-bit address mode
* @param[in] n number of words to receive
* @param[out] rxbuf the pointer to the receive buffer
*
*/ */
void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) { void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) {
@ -156,7 +173,7 @@ void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) {
"i2cMasterReceive(), #1", "i2cMasterReceive(), #1",
"not active"); "not active");
i2c_lld_master_receiveI(i2cp, i2cscfg); i2c_lld_master_receive(i2cp, i2cscfg);
} }