1996fcd9e98a9cee798aaaf3ace662c2b623924a
[fw/altos] / src / avr / ao_spi_usart.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 #include "ao.h"
19
20 /*
21  * Atmega32u4 USART in MSPIM (master SPI mode)
22  */
23
24 __xdata uint8_t ao_spi_mutex;
25
26 /* Send bytes over SPI.
27  *
28  * This just polls; the SPI is set to go as fast as possible,
29  * so using interrupts would take way too long
30  */
31 void
32 ao_spi_send(void __xdata *block, uint16_t len) __reentrant
33 {
34         uint8_t *d = block;
35
36         while (len--) {
37                 while (!(UCSR1A & (1 << UDRE1)));
38                 UDR1 = *d++;
39                 while (!(UCSR1A & (1 << RXC1)));
40                 (void) UDR1;
41         }
42 }
43
44 /* Receive bytes over SPI.
45  *
46  * This sets up tow DMA engines, one reading the data and another
47  * writing constant values to the SPI transmitter as that is what
48  * clocks the data coming in.
49  */
50 void
51 ao_spi_recv(void __xdata *block, uint16_t len) __reentrant
52 {
53         uint8_t *d = block;
54
55         while (len--) {
56                 while (!(UCSR1A & (1 << UDRE1)));
57                 UDR1 = 0;
58                 while (!(UCSR1A & (1 << RXC1)));
59                 *d++ = UDR1;
60         }
61 }
62
63 /*
64  * Initialize USART0 for SPI using config alt 2
65  *
66  *      MO      P1_5
67  *      MI      P1_4
68  *      CLK     P1_3
69  *
70  * Chip select is the responsibility of the caller
71  */
72
73 #define XCK1_DDR        DDRD
74 #define XCK1_PORT       PORTD
75 #define XCK1            PORTD5
76 #define XMS1_DDR        DDRE
77 #define XMS1_PORT       PORTE
78 #define XMS1            PORTE6
79
80 void
81 ao_spi_init(void)
82 {
83         /* Ensure the USART is powered */
84         PRR1 &= ~(1 << PRUSART1);
85
86         /*
87          * Set pin directions
88          */
89         XCK1_DDR |= (1 << XCK1);
90
91         /* Clear chip select (which is negated) */
92         XMS1_PORT |= (1 < XMS1);
93         XMS1_DDR |= (1 << XMS1);
94
95         /* Set baud register to zero (required before turning transmitter on) */
96         UBRR1 = 0;
97
98         UCSR1C = ((0x3 << UMSEL10) |    /* Master SPI mode */
99                   (0 << UCSZ10) |       /* SPI mode 0 */
100                   (0 << UCPOL1));       /* SPI mode 0 */
101
102         /* Enable transmitter and receiver */
103         UCSR1B = ((1 << RXEN1) |
104                   (1 << TXEN1));
105
106         /* It says that 0 is a legal value; we'll see... */
107         UBRR1 = 0;
108 }