2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #if !HAS_SPI_0 && !HAS_SPI_1
27 #if HAS_SPI_0 && HAS_SPI_1
35 extern __xdata uint8_t ao_spi_mutex[N_SPI];
38 #define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[bus])
39 #define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[bus])
41 #define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[0])
42 #define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[0])
45 #define AO_SPI_SPEED_FAST 17
46 #define AO_SPI_SPEED_200kHz 13
49 #define ao_spi_set_speed(bus,speed) (*(bus ? &U1GCR : &U0GCR) =(UxGCR_CPOL_NEGATIVE | \
50 UxGCR_CPHA_FIRST_EDGE | \
52 ((speed) << UxGCR_BAUD_E_SHIFT)))
54 #define ao_spi_set_speed(bus,speed) (U0GCR = (UxGCR_CPOL_NEGATIVE | \
55 UxGCR_CPHA_FIRST_EDGE | \
57 ((speed) << UxGCR_BAUD_E_SHIFT)))
60 #define ao_spi_get_slave(bus) do { \
62 ao_spi_set_speed(bus,AO_SPI_SPEED_FAST); \
65 #define ao_spi_put_slave(bus) do { \
69 #define ao_spi_get_mask(reg,mask,bus,speed) do { \
71 ao_spi_set_speed(bus,speed); \
75 #define ao_spi_put_mask(reg,mask,bus) do { \
81 #define ao_spi_get_bit(reg,bit,pin,bus,speed) do { \
83 ao_spi_set_speed(bus,speed); \
87 #define ao_spi_put_bit(reg,bit,pin,bus) do { \
94 * The SPI mutex must be held to call either of these
95 * functions -- this mutex covers the entire SPI operation,
96 * from chip select low to chip select high
101 ao_spi_send(void __xdata *block, uint16_t len, uint8_t bus) __reentrant;
104 ao_spi_recv(void __xdata *block, uint16_t len, uint8_t bus) __reentrant;
107 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant;
110 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant;
112 #define ao_spi_send(block, len, bus) ao_spi_send_bus(block, len)
113 #define ao_spi_recv(block, len, bus) ao_spi_recv_bus(block, len)
118 ao_spi_send_wait(void);
121 ao_spi_recv_wait(void);
127 #define token_paster(x,y) x ## y
128 #define token_paster3(x,y,z) x ## y ## z
129 #define token_evaluator(x,y) token_paster(x,y)
130 #define token_evaluator3(x,y,z) token_paster3(x,y,z)
132 #define ao_spi_init_cs(port, mask) do { \
134 token_evaluator(port,DIR) |= mask; \
135 token_evaluator(port,SEL) &= ~mask; \
138 #define cc1111_enable_output(port,dir,sel,pin,bit,v) do { \
141 sel &= ~(1 << bit); \
144 #define disable_unreachable _Pragma("disable_warning 126")
146 #define ao_enable_output(port,bit,pin,v) cc1111_enable_output(port,token_evaluator(port,DIR), token_evaluator(port,SEL), pin, bit, v)
147 #define ao_gpio_set(port, bit, pin, v) ((pin) = (v))
148 #define ao_gpio_get(port, bit, pin) (pin)
149 #define ao_gpio_get_bits(port) (port)
150 #define ao_gpio_set_bits(port, bits) ((port) |= bits)
151 #define ao_gpio_clr_bits(port, bits) ((port) &= ~bits)