altos: stm i2c debug code was calling flush() even when disabled
[fw/altos] / src / stm / ao_i2c_stm.c
index e46211cd004e15c2c6c495abb9b2143d32e00ecc..51631cb73d8fc4a1b64d40058ddaf444805c8dd9 100644 (file)
@@ -136,49 +136,58 @@ ao_i2c_put(uint8_t index)
        ao_mutex_put(&ao_i2c_mutex[index]);
 }
 
+static inline void
+ao_i2c_delay(void)
+{
+       uint8_t i;
+
+       for (i = 0; i < 10; i++)
+               ao_arch_nop();
+}
+
 #define I2C_DEBUG      0
 #if I2C_DEBUG
-#define DBG(x...)      printf(x)
+#define DBG(x...)      do { printf(x); flush(); } while (0)
 #else
-#define DBG(x...)      
+#define DBG(x...)
 #endif
 
 static inline uint32_t in_sr1(char *where, struct stm_i2c *stm_i2c) {
        uint32_t        sr1 = stm_i2c->sr1;
-       DBG("%s: sr1: %x\n", where, sr1); flush();
+       DBG("%s: sr1: %x\n", where, sr1);
        return sr1;
 }
 
 static inline uint32_t in_sr2(char *where, struct stm_i2c *stm_i2c) {
        uint32_t        sr2 = stm_i2c->sr2;
-       DBG("%s: sr2: %x\n", where, sr2); flush();
+       DBG("%s: sr2: %x\n", where, sr2);
        return sr2;
 }
 
 static inline void out_cr1(char *where, struct stm_i2c *stm_i2c, uint32_t cr1) {
-       DBG("%s: cr1: %x\n", where, cr1); flush();
+       DBG("%s: cr1: %x\n", where, cr1);
        stm_i2c->cr1 = cr1;
 }
 
 static inline uint32_t in_cr1(char *where, struct stm_i2c *stm_i2c) {
        uint32_t        cr1 = stm_i2c->cr1;
-       DBG("%s: cr1: %x\n", where, cr1); flush();
+       DBG("%s: cr1: %x\n", where, cr1);
        return cr1;
 }
 
 static inline void out_cr2(char *where, struct stm_i2c *stm_i2c, uint32_t cr2) {
-       DBG("%s: cr2: %x\n", where, cr2); flush();
+       DBG("%s: cr2: %x\n", where, cr2);
        stm_i2c->cr2 = cr2;
 }
 
 static inline uint32_t in_dr(char *where, struct stm_i2c *stm_i2c) {
        uint32_t        dr = stm_i2c->dr;
-       DBG("%s: dr: %x\n", where, dr); flush();
+       DBG("%s: dr: %x\n", where, dr);
        return dr;
 }
 
 static inline void out_dr(char *where, struct stm_i2c *stm_i2c, uint32_t dr) {
-       DBG("%s: dr: %x\n", where, dr); flush();
+       DBG("%s: dr: %x\n", where, dr);
        stm_i2c->dr = dr;
 }
 
@@ -191,10 +200,10 @@ ao_i2c_start(uint8_t index, uint16_t addr)
 
        ao_i2c_state[index] = I2C_IDLE;
        ao_i2c_addr[index] = addr;
-       printf ("start sr1: %x\n", stm_i2c->sr1);
        out_cr2("start", stm_i2c, AO_STM_I2C_CR2);
        out_cr1("start", stm_i2c,
                AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_START));
+       ao_i2c_delay();
        out_cr2("start", stm_i2c,
                AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN));
        ao_alarm(1);
@@ -208,20 +217,17 @@ ao_i2c_start(uint8_t index, uint16_t addr)
 }
 
 static void
-ao_i2c_stop(uint8_t index)
+ao_i2c_wait_stop(uint8_t index)
 {
        struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
-       
-       ao_i2c_state[index] = I2C_IDLE;
-       out_cr2("enable isr", stm_i2c,
-               AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN));
-       ev_count = 0;
-       out_cr1("stop", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP));
+       int     t;
 
-       /* XXX check to see if there is an interrupt here */
-       while (in_cr1("stop", stm_i2c) & (1 << STM_I2C_CR1_STOP))
+       for (t = 0; t < I2C_TIMEOUT; t++) {
+               if (!(in_cr1("wait stop", stm_i2c) & (1 << STM_I2C_CR1_STOP)))
+                       break;
                ao_yield();
-       printf ("ev_count in stop: %d\n", ev_count);
+       }
+       ao_i2c_state[index] = I2C_IDLE;
 }
 
 uint8_t
@@ -230,8 +236,6 @@ ao_i2c_send(void *block, uint16_t len, uint8_t index, uint8_t stop)
        struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
        uint8_t         *b = block;
        uint32_t        sr1;
-       int             t;
-
        uint8_t         tx_dma_index = ao_i2c_stm_info[index].tx_dma_index;
 
        /* Clear any pending ADDR bit */
@@ -254,10 +258,9 @@ ao_i2c_send(void *block, uint16_t len, uint8_t index, uint8_t stop)
        ao_alarm(1 + len);
        cli();
        while (!ao_dma_done[tx_dma_index])
-               if (ao_sleep(&ao_dma_done[tx_dma_index])) {
-                       printf ("send timeout\n");
+               if (ao_sleep(&ao_dma_done[tx_dma_index]))
                        break;
-               }
+       ao_clear_alarm();
        ao_dma_done_transfer(tx_dma_index);
        out_cr2("send enable isr", stm_i2c,
                AO_STM_I2C_CR2 | (1 << STM_I2C_CR2_ITEVTEN) | (1 << STM_I2C_CR2_ITERREN));
@@ -266,8 +269,10 @@ ao_i2c_send(void *block, uint16_t len, uint8_t index, uint8_t stop)
                        break;
        out_cr2("send disable isr", stm_i2c, AO_STM_I2C_CR2);
        sei();
-       if (stop)
-               ao_i2c_stop(index);
+       if (stop) {
+               out_cr1("stop", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP));
+               ao_i2c_wait_stop(index);
+       }
        return TRUE;
 }
 
@@ -356,15 +361,8 @@ ao_i2c_recv(void *block, uint16_t len, uint8_t index, uint8_t stop)
                ao_dma_done_transfer(rx_dma_index);
                out_cr1("stop recv > 1", stm_i2c, AO_STM_I2C_CR1 | (1 << STM_I2C_CR1_STOP));
        }
-       if (stop) {
-               for (t = 0; t < I2C_TIMEOUT; t++) {
-                       if (!(in_cr1("recv stop", stm_i2c) & (1 << STM_I2C_CR1_STOP)))
-                               break;
-                       ao_yield();
-               }
-               if (t == I2C_TIMEOUT)
-                       return FALSE;
-       }
+       if (stop)
+               ao_i2c_wait_stop(index);
        return ret;
 }
 
@@ -372,8 +370,12 @@ void
 ao_i2c_channel_init(uint8_t index)
 {
        struct stm_i2c  *stm_i2c = ao_i2c_stm_info[index].stm_i2c;
+       int i;
 
        /* Turn I2C off while configuring */
+       stm_i2c->cr1 = (1 << STM_I2C_CR1_SWRST);
+       for (i = 0; i < 100; i++)
+               asm("nop");
        stm_i2c->cr1 = 0;
        stm_i2c->cr2 = AO_STM_I2C_CR2;
 
@@ -435,4 +437,3 @@ ao_i2c_init(void)
        stm_nvic_set_priority(STM_ISR_I2C2_ER_POS, 3);
 #endif
 }
-