-#define ao_spi_get_mask(reg,mask,bus) do { \
- ao_mutex_get(&ao_spi_mutex); \
- (reg) &= ~(mask); \
+#if HAS_SPI_0 && HAS_SPI_1
+#define MULTI_SPI 1
+#define N_SPI 2
+#else
+#define MULTI_SPI 0
+#define N_SPI 1
+#endif
+
+extern __xdata uint8_t ao_spi_mutex[N_SPI];
+
+#if MULTI_SPI
+#define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[bus])
+#define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[bus])
+#else
+#define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[0])
+#define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[0])
+#endif
+
+#define AO_SPI_SPEED_FAST 17
+#define AO_SPI_SPEED_200kHz 13
+
+#if MULTI_SPI
+#define ao_spi_set_speed(bus,speed) (*(bus ? &U1GCR : &U0GCR) =(UxGCR_CPOL_NEGATIVE | \
+ UxGCR_CPHA_FIRST_EDGE | \
+ UxGCR_ORDER_MSB | \
+ ((speed) << UxGCR_BAUD_E_SHIFT)))
+#else
+#define ao_spi_set_speed(bus,speed) (U0GCR = (UxGCR_CPOL_NEGATIVE | \
+ UxGCR_CPHA_FIRST_EDGE | \
+ UxGCR_ORDER_MSB | \
+ ((speed) << UxGCR_BAUD_E_SHIFT)))
+#endif
+
+#define ao_spi_get_slave(bus) do { \
+ ao_spi_get(bus); \
+ ao_spi_set_speed(bus,AO_SPI_SPEED_FAST); \
+ } while (0)
+
+#define ao_spi_put_slave(bus) do { \
+ ao_spi_put(bus); \
+ } while (0)
+
+#define ao_spi_get_mask(reg,mask,bus,speed) do { \
+ ao_spi_get(bus); \
+ ao_spi_set_speed(bus,speed); \
+ (reg) &= ~(mask); \