From af0e40079ded13b8842e8d129fa6ed2f37fdf678 Mon Sep 17 00:00:00 2001
From: barthess <barthess@35acf78f-673a-0410-8e92-d51de3d6d3f4>
Date: Fri, 1 Jul 2011 13:36:59 +0000
Subject: I2C. Trying to add optional WAIT support. Driver broken.

git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/i2c_dev@3101 35acf78f-673a-0410-8e92-d51de3d6d3f4
---
 os/hal/include/i2c.h             | 11 +++++------
 os/hal/platforms/STM32/i2c_lld.c | 34 ++++++++++++++++++++--------------
 os/hal/platforms/STM32/i2c_lld.h |  2 +-
 os/hal/src/i2c.c                 | 40 ++++++++++++++++++----------------------
 4 files changed, 44 insertions(+), 43 deletions(-)

diff --git a/os/hal/include/i2c.h b/os/hal/include/i2c.h
index 60b2c322d..0a4ba8b53 100644
--- a/os/hal/include/i2c.h
+++ b/os/hal/include/i2c.h
@@ -165,9 +165,9 @@ struct I2CSlaveConfig{
  * @notapi
  */
 #define _i2c_wait_s(i2cp) {                                                 \
-  chDbgAssert((i2cp)->thread == NULL,                                       \
+  chDbgAssert((i2cp)->id_thread == NULL,                                       \
               "_i2c_wait(), #1", "already waiting");                        \
-  (i2cp)->thread = chThdSelf();                                             \
+  (i2cp)->id_thread = chThdSelf();                                             \
   chSchGoSleepS(THD_STATE_SUSPENDED);                                       \
 }
 
@@ -179,9 +179,9 @@ struct I2CSlaveConfig{
  * @notapi
  */
 #define _i2c_wakeup_isr(i2cp) {                                             \
-  if ((i2cp)->thread != NULL) {                                             \
-    Thread *tp = (i2cp)->thread;                                            \
-    (i2cp)->thread = NULL;                                                  \
+  if ((i2cp)->id_thread != NULL) {                                             \
+    Thread *tp = (i2cp)->id_thread;                                            \
+    (i2cp)->id_thread = NULL;                                                  \
     chSysLockFromIsr();                                                     \
     chSchReadyI(tp);                                                        \
     chSysUnlockFromIsr();                                                   \
@@ -216,7 +216,6 @@ struct I2CSlaveConfig{
   else                                                                 \
     (i2cp)->id_state = I2C_READY;                                      \
   _i2c_wakeup_isr(i2cp);                                               \
-  i2cReleaseBus(i2cp);                                                 \
 }
 
 
diff --git a/os/hal/platforms/STM32/i2c_lld.c b/os/hal/platforms/STM32/i2c_lld.c
index d9ca29a9e..a4aa2927d 100644
--- a/os/hal/platforms/STM32/i2c_lld.c
+++ b/os/hal/platforms/STM32/i2c_lld.c
@@ -561,13 +561,16 @@ void i2c_lld_master_transmit(I2CDriver *i2cp, uint16_t slave_addr,
 
   i2cp->id_i2c->CR1 |= I2C_CR1_START;               /* send start bit */
 
-#if !I2C_USE_WAIT
-  /* Wait until the START condition is generated on the bus:
-   * the START bit is cleared by hardware */
-  uint32_t timeout = 0xfffff;
-  while((i2cp->id_i2c->CR1 & I2C_CR1_START) && timeout--)
-    ;
-#endif /* I2C_USE_WAIT */
+//#if !I2C_USE_WAIT
+//  /* Wait until the START condition is generated on the bus:
+//   * the START bit is cleared by hardware */
+//  uint32_t timeout = 0xfffff;
+//  while((i2cp->id_i2c->CR1 & I2C_CR1_START) && timeout--)
+//    ;
+//#endif /* I2C_USE_WAIT */
+    uint32_t timeout = 0xfffff;
+    while((i2cp->id_i2c->CR1 & I2C_CR1_START) && timeout--)
+      ;
 }
 
 
@@ -623,13 +626,16 @@ void i2c_lld_master_receive(I2CDriver *i2cp, uint16_t slave_addr,
 
   i2cp->id_i2c->CR1 |= I2C_CR1_START;            /* send start bit */
 
-#if !I2C_USE_WAIT
-  /* Wait until the START condition is generated on the bus:
-   * the START bit is cleared by hardware */
-  uint32_t timeout = 0xfffff;
-  while((i2cp->id_i2c->CR1 & I2C_CR1_START) && timeout--)
-    ;
-#endif /* I2C_USE_WAIT */
+//#if !I2C_USE_WAIT
+//  /* Wait until the START condition is generated on the bus:
+//   * the START bit is cleared by hardware */
+//  uint32_t timeout = 0xfffff;
+//  while((i2cp->id_i2c->CR1 & I2C_CR1_START) && timeout--)
+//    ;
+//#endif /* I2C_USE_WAIT */
+    uint32_t timeout = 0xfffff;
+    while((i2cp->id_i2c->CR1 & I2C_CR1_START) && timeout--)
+      ;
 }
 
 
diff --git a/os/hal/platforms/STM32/i2c_lld.h b/os/hal/platforms/STM32/i2c_lld.h
index f1f065359..0b95a893d 100644
--- a/os/hal/platforms/STM32/i2c_lld.h
+++ b/os/hal/platforms/STM32/i2c_lld.h
@@ -142,7 +142,7 @@ struct I2CDriver{
   /**
    * @brief Thread waiting for I/O completion.
    */
-  Thread                *thread;
+  Thread                *id_thread;
 #endif /* I2C_USE_WAIT */
 #if I2C_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
 #if CH_USE_MUTEXES || defined(__DOXYGEN__)
diff --git a/os/hal/src/i2c.c b/os/hal/src/i2c.c
index 725e92d65..3f4095aa3 100644
--- a/os/hal/src/i2c.c
+++ b/os/hal/src/i2c.c
@@ -156,8 +156,6 @@ void i2cMasterTransmit(I2CDriver *i2cp,
                       uint8_t *rxbuf,
                       size_t rxbytes) {
 
-  i2cAcquireBus(i2cp);
-
   chDbgCheck((i2cp != NULL) && (i2cscfg != NULL) &&\
   		(slave_addr != 0) &&\
   		(txbytes > 0) &&\
@@ -167,15 +165,15 @@ void i2cMasterTransmit(I2CDriver *i2cp,
   /* init slave config field in driver */
   i2cp->id_slave_config = i2cscfg;
 
-#if I2C_USE_WAIT
-  i2c_lld_wait_bus_free(i2cp);
-  if(i2c_lld_bus_is_busy(i2cp)) {
-#ifdef PRINTTRACE
-    print("I2C Bus busy!\n");
-#endif
-    return;
-  };
-#endif
+//#if I2C_USE_WAIT
+//  i2c_lld_wait_bus_free(i2cp);
+//  if(i2c_lld_bus_is_busy(i2cp)) {
+//#ifdef PRINTTRACE
+//    print("I2C Bus busy!\n");
+//#endif
+//    return;
+//  };
+//#endif
 
   chSysLock();
   chDbgAssert(i2cp->id_state == I2C_READY,
@@ -204,8 +202,6 @@ void i2cMasterReceive(I2CDriver *i2cp,
                       uint8_t *rxbuf,
                       size_t rxbytes){
 
-  i2cAcquireBus(i2cp);
-
   chDbgCheck((i2cp != NULL) && (i2cscfg != NULL) &&\
   		(slave_addr != 0) &&\
   		(rxbytes > 0) && \
@@ -215,15 +211,15 @@ void i2cMasterReceive(I2CDriver *i2cp,
   /* init slave config field in driver */
   i2cp->id_slave_config = i2cscfg;
 
-#if I2C_USE_WAIT
-  i2c_lld_wait_bus_free(i2cp);
-  if(i2c_lld_bus_is_busy(i2cp)) {
-#ifdef PRINTTRACE
-    print("I2C Bus busy!\n");
-#endif
-    return;
-  };
-#endif
+//#if I2C_USE_WAIT
+//  i2c_lld_wait_bus_free(i2cp);
+//  if(i2c_lld_bus_is_busy(i2cp)) {
+//#ifdef PRINTTRACE
+//    print("I2C Bus busy!\n");
+//#endif
+//    return;
+//  };
+//#endif
 
   chSysLock();
   chDbgAssert(i2cp->id_state == I2C_READY,
-- 
cgit v1.2.3