altos/lpc: Create TX/RX busy macros for SPI driver
authorKeith Packard <keithp@keithp.com>
Mon, 20 May 2013 03:22:20 +0000 (20:22 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 20 May 2013 03:39:19 +0000 (20:39 -0700)
Check for both fifo status *and* device busy to make sure the device
is idle before we touch any registers.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/lpc/ao_spi_lpc.c

index 05688f5205f205ad72bd3a4861cda3c8d25f7b60..e7edca4c1a165e7a090339c20e7354d4ec75601c 100644 (file)
@@ -23,21 +23,25 @@ static struct lpc_ssp * const ao_lpc_ssp[LPC_NUM_SPI] = { &lpc_ssp0, &lpc_ssp1 }
 
 static uint8_t spi_dev_null;
 
+#define tx_busy(lpc_ssp) (lpc_ssp->sr & ((1 << LPC_SSP_SR_BSY) | (1 << LPC_SSP_SR_TNF))) != (1 << LPC_SSP_SR_TNF)
+#define rx_busy(lpc_ssp) (lpc_ssp->sr & ((1 << LPC_SSP_SR_BSY) | (1 << LPC_SSP_SR_RNE))) != (1 << LPC_SSP_SR_RNE)
+
 #define spi_loop(len, put, get) do {                                   \
                while (len--) {                                         \
                        /* Wait for space in the fifo */                \
-                       while ((lpc_ssp->sr & (1 << LPC_SSP_SR_TNF)) == 0) \
+                       while (tx_busy(lpc_ssp))                        \
                                ;                                       \
+                                                                       \
                        /* send a byte */                               \
                        lpc_ssp->dr = put;                              \
                                                                        \
+                       /* Wait for byte to appear in the fifo */       \
+                       while (rx_busy(lpc_ssp))                        \
+                               ;                                       \
+                                                                       \
                        /* recv a byte */                               \
                        get lpc_ssp->dr;                                \
                }                                                       \
-                                                                       \
-               /* Wait for the fifo to drain */                        \
-               while ((lpc_ssp->sr & (1 << LPC_SSP_SR_BSY)))           \
-                       ;                                               \
        } while (0);
 
 void