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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23 #if !HAS_SPI_0 && !HAS_SPI_1
28 #if HAS_SPI_0 && HAS_SPI_1
36 extern __xdata uint8_t ao_spi_mutex[N_SPI];
39 #define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[bus])
40 #define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[bus])
42 #define ao_spi_get(bus) ao_mutex_get(&ao_spi_mutex[0])
43 #define ao_spi_put(bus) ao_mutex_put(&ao_spi_mutex[0])
46 #define AO_SPI_SPEED_FAST 17
47 #define AO_SPI_SPEED_200kHz 13
50 #define ao_spi_set_speed(bus,speed) (*(bus ? &U1GCR : &U0GCR) =(UxGCR_CPOL_NEGATIVE | \
51 UxGCR_CPHA_FIRST_EDGE | \
53 ((speed) << UxGCR_BAUD_E_SHIFT)))
55 #define ao_spi_set_speed(bus,speed) (U0GCR = (UxGCR_CPOL_NEGATIVE | \
56 UxGCR_CPHA_FIRST_EDGE | \
58 ((speed) << UxGCR_BAUD_E_SHIFT)))
61 #define ao_spi_get_slave(bus) do { \
63 ao_spi_set_speed(bus,AO_SPI_SPEED_FAST); \
66 #define ao_spi_put_slave(bus) do { \
70 #define ao_spi_get_mask(reg,mask,bus,speed) do { \
72 ao_spi_set_speed(bus,speed); \
76 #define ao_spi_put_mask(reg,mask,bus) do { \
82 #define ao_spi_get_bit(reg,bit,pin,bus,speed) do { \
84 ao_spi_set_speed(bus,speed); \
88 #define ao_spi_put_bit(reg,bit,pin,bus) do { \
95 * The SPI mutex must be held to call either of these
96 * functions -- this mutex covers the entire SPI operation,
97 * from chip select low to chip select high
102 ao_spi_send(void __xdata *block, uint16_t len, uint8_t bus) __reentrant;
105 ao_spi_recv(void __xdata *block, uint16_t len, uint8_t bus) __reentrant;
108 ao_spi_send_bus(void __xdata *block, uint16_t len) __reentrant;
111 ao_spi_recv_bus(void __xdata *block, uint16_t len) __reentrant;
113 #define ao_spi_send(block, len, bus) ao_spi_send_bus(block, len)
114 #define ao_spi_recv(block, len, bus) ao_spi_recv_bus(block, len)
119 ao_spi_send_wait(void);
122 ao_spi_recv_wait(void);
128 #define token_paster(x,y) x ## y
129 #define token_paster3(x,y,z) x ## y ## z
130 #define token_evaluator(x,y) token_paster(x,y)
131 #define token_evaluator3(x,y,z) token_paster3(x,y,z)
133 #define ao_spi_init_cs(port, mask) do { \
135 token_evaluator(port,DIR) |= mask; \
136 token_evaluator(port,SEL) &= ~mask; \
139 #define cc1111_enable_output(port,dir,sel,pin,bit,v) do { \
142 sel &= ~(1 << bit); \
145 #define disable_unreachable _Pragma("disable_warning 126")
147 #define ao_enable_output(port,bit,pin,v) cc1111_enable_output(port,token_evaluator(port,DIR), token_evaluator(port,SEL), pin, bit, v)
148 #define ao_gpio_set(port, bit, pin, v) ((pin) = (v))
149 #define ao_gpio_get(port, bit, pin) (pin)
150 #define ao_gpio_get_bits(port) (port)
151 #define ao_gpio_set_bits(port, bits) ((port) |= bits)
152 #define ao_gpio_clr_bits(port, bits) ((port) &= ~bits)