diff options
Diffstat (limited to 'os')
-rw-r--r-- | os/hal/include/i2c.h | 5 | ||||
-rw-r--r-- | os/hal/platforms/STM32/i2c_lld.c | 49 | ||||
-rw-r--r-- | os/hal/src/i2c.c | 124 |
3 files changed, 21 insertions, 157 deletions
diff --git a/os/hal/include/i2c.h b/os/hal/include/i2c.h index 273c9f57a..73bab57f4 100644 --- a/os/hal/include/i2c.h +++ b/os/hal/include/i2c.h @@ -120,8 +120,9 @@ extern "C" { void i2cObjectInit(I2CDriver *i2cp);
void i2cStart(I2CDriver *i2cp, I2CConfig *config);
void i2cStop(I2CDriver *i2cp);
- void i2cMasterTransmit(I2CDriver *i2cp, uint8_t slave_addr1, uint8_t slave_addr2, size_t n, const void *txbuf);
- void i2cMasterReceive(I2CDriver *i2cp, uint8_t slave_addr1, uint8_t slave_addr2, size_t n, void *rxbuf);
+ void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t restart);
+ void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg);
+
void i2cMasterStartI(I2CDriver *i2cp,uint16_t header,i2ccallback_t callback);
void i2cMasterStopI(I2CDriver *i2cp, i2ccallback_t callback);
void i2cMasterRestartI(I2CDriver *i2cp, i2ccallback_t callback);
diff --git a/os/hal/platforms/STM32/i2c_lld.c b/os/hal/platforms/STM32/i2c_lld.c index 85ca16b2f..c1b932748 100644 --- a/os/hal/platforms/STM32/i2c_lld.c +++ b/os/hal/platforms/STM32/i2c_lld.c @@ -227,6 +227,7 @@ void i2c_lld_master_transmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t re while (!(i2cp->id_i2c->SR1 & I2C_SR1_SB)){ i++; // wait start bit } + i2cp->id_i2c->DR = (i2cp->id_slave_config->slave_addr1 << 1) | I2C_WRITE; // write slave addres in DR while (!(i2cp->id_i2c->SR1 & I2C_SR1_ADDR)){ i++; // wait Address sent @@ -249,6 +250,9 @@ void i2c_lld_master_transmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t re if (restart){ i2cp->id_i2c->CR1 |= I2C_CR1_START; // generate restart condition + while (!(i2cp->id_i2c->SR1 & I2C_SR1_SB)){ + i++; // wait start bit + } } else i2cp->id_i2c->CR1 |= I2C_CR1_STOP; // generate stop condition @@ -257,10 +261,8 @@ void i2c_lld_master_transmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t re /** * @brief Receives data from the I2C bus. - * @details To receive data from I2C slave you must sent them some - * control bytes first. Driver takes this data from @p I2CSlaveConfig - * structure (*txbuf and txbytes fields), so you must manually - * fill this fields before invocating receiving function + * @details Before receive data from I2C slave you must manually sent them some + * control bytes first (refer to you device datasheet). * * @param[in] i2cp pointer to the @p I2CDriver object * @param[in] i2cscfg pointer to the @p I2CSlaveConfig object @@ -274,45 +276,6 @@ void i2c_lld_master_receive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) { uint16_t i = 0; uint16_t tmp = 0; - // send control secuence to slave - //i2c_lld_master_transmit(i2cp, i2cscfg, TRUE); - - - - - - i2cp->id_i2c->CR1 |= I2C_CR1_START; // generate start condition - while (!(i2cp->id_i2c->SR1 & I2C_SR1_SB)){ - i++; // wait start bit - } - i2cp->id_i2c->DR = (i2cp->id_slave_config->slave_addr1 << 1) | I2C_WRITE; // write slave addres in DR - while (!(i2cp->id_i2c->SR1 & I2C_SR1_ADDR)){ - i++; // wait Address sent - } - i = i2cp->id_i2c->SR2; // TODO: check is it need to read this register for I2C to proper functionality - i = i2cp->id_i2c->SR1; //i2cp->id_i2c->SR1 &= (~I2C_SR1_ADDR); // clear ADDR bit - - // now write data byte by byte in DR register - uint32_t n = 0; - for (n = 0; n < i2cp->id_slave_config->txbytes; n++){ - i2cp->id_i2c->DR = i2cscfg->txbuf[n]; - while (!(i2cp->id_i2c->SR1 & I2C_SR1_TXE)){ - i++; - } - } - - while (!(i2cp->id_i2c->SR1 & I2C_SR1_BTF)){ - i++; - } - - i2cp->id_i2c->CR1 |= I2C_CR1_START; // generate restart condition - while (!(i2cp->id_i2c->SR1 & I2C_SR1_SB)){ - i++; // wait start bit - } - - - - // send slave addres with read-bit i2cp->id_i2c->DR = (i2cp->id_slave_config->slave_addr1 << 1) | I2C_READ; while (!(i2cp->id_i2c->SR1 & I2C_SR1_ADDR)){ diff --git a/os/hal/src/i2c.c b/os/hal/src/i2c.c index 273bb5933..e621c652d 100644 --- a/os/hal/src/i2c.c +++ b/os/hal/src/i2c.c @@ -115,6 +115,7 @@ void i2cStop(I2CDriver *i2cp) { i2cp->id_state = I2C_STOP;
chSysUnlock();
}
+
/**
* @brief Sends data ever the I2C bus.
*
@@ -125,18 +126,18 @@ void i2cStop(I2CDriver *i2cp) { * @param[in] txbuf the pointer to the transmit buffer
*
*/
-void i2cMasterTransmit(I2CDriver *i2cp, uint8_t slave_addr1, uint8_t slave_addr2, size_t n, const void *txbuf) {
+void i2cMasterTransmit(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg, bool_t restart) {
- chDbgCheck((i2cp != NULL) && (n > 0) && (txbuf != NULL),
- "i2cSend");
+ chDbgCheck((i2cp != NULL) && (i2cscfg != NULL),
+ "i2cMasterTransmit");
chDbgAssert(i2cp->id_state == I2C_READY,
- "i2cSend(), #1",
+ "i2cMasterTransmit(), #1",
"not active");
- //i2c_lld_master_transmit(i2cp, slave_addr1, slave_addr2, n, txbuf);
-
+ i2c_lld_master_transmit(i2cp, i2cscfg, restart);
}
+
/**
* @brief Receives data from the I2C bus.
*
@@ -147,122 +148,21 @@ void i2cMasterTransmit(I2CDriver *i2cp, uint8_t slave_addr1, uint8_t slave_addr2 * @param[out] rxbuf the pointer to the receive buffer
*
*/
-void i2cMasterReceive(I2CDriver *i2cp, uint8_t slave_addr1, uint8_t slave_addr2, size_t n, void *rxbuf) {
+void i2cMasterReceive(I2CDriver *i2cp, I2CSlaveConfig *i2cscfg) {
- chDbgCheck((i2cp != NULL) && (n > 0) && (rxbuf != NULL),
- "i2cReceive");
+ chDbgCheck((i2cp != NULL) && (i2cscfg != NULL),
+ "i2cMasterReceive");
chDbgAssert(i2cp->id_state == I2C_READY,
- "i2cReceive(), #1",
+ "i2cMasterReceive(), #1",
"not active");
- //i2c_lld_master_receive(i2cp, slave_addr1, slave_addr2, n, rxbuf);
-
+ i2c_lld_master_receive(i2cp, i2cscfg);
}
-/**
- * @brief Initiates a master bus transaction.
- * @details This function sends a start bit followed by an one or two bytes
- * header.
- *
- * @param[in] i2cp pointer to the @p I2CDriver object
- * @param[in] header transaction header
- * @param[in] callback operation complete callback
- *
- * @iclass
- */
-//void i2cMasterStartI(I2CDriver *i2cp,
-// uint16_t header,
-// i2ccallback_t callback) {
-//
-// chDbgCheck((i2cp != NULL) && (callback != NULL), "i2cMasterStartI");
-// chDbgAssert(i2cp->id_state == I2C_READY,
-// "i2cMasterStartI(), #1", "invalid state");
-//
-// i2cp->id_callback = callback;
-// i2c_lld_master_start(i2cp, header);
-//}
-
-/**
- * @brief Terminates a master bus transaction.
- *
- * @param[in] i2cp pointer to the @p I2CDriver object
- * @param[in] callback operation complete callback
- *
- * @iclass
- */
-//void i2cMasterStopI(I2CDriver *i2cp, i2ccallback_t callback) {
-//
-// chDbgCheck((i2cp != NULL) && (callback != NULL), "i2cMasterStopI");
-// chDbgAssert(i2cp->id_state == I2C_MREADY,
-// "i2cMasterStopI(), #1", "invalid state");
-//
-// i2cp->id_callback = callback;
-// i2c_lld_master_stop(i2cp);
-//}
-/**
- * @brief Sends a restart bit.
- * @details Restart bits are required by some types of I2C transactions.
- *
- * @param[in] i2cp pointer to the @p I2CDriver object
- * @param[in] callback operation complete callback
- *
- * @iclass
- */
-//void i2cMasterRestartI(I2CDriver *i2cp, i2ccallback_t callback) {
-//
-// chDbgCheck((i2cp != NULL) && (callback != NULL), "i2cMasterRestartI");
-// chDbgAssert(i2cp->id_state == I2C_MREADY,
-// "i2cMasterRestartI(), #1", "invalid state");
-//
-// i2cp->id_callback = callback;
-// i2c_lld_master_restart(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
- *
- * @iclass
- */
-//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->id_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
- *
- * @iclass
- */
-//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->id_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__)
/**
|