altos: Fix nanopeak compile
[fw/altos] / src / attiny / ao_async.c
1 /*
2  * Copyright © 2012 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 #include <ao_async.h>
20
21 #define AO_ASYNC_BAUD   38400l
22 #define AO_ASYNC_DELAY  (uint8_t) (1000000l / AO_ASYNC_BAUD)
23
24 #define LED_PORT        PORTB
25
26 void
27 ao_async_start(void)
28 {
29         LED_PORT |= (1 << AO_LED_SERIAL);
30 }
31
32 void
33 ao_async_stop(void)
34 {
35         LED_PORT &= ~(1 << AO_LED_SERIAL);
36 }
37
38 void
39 ao_async_byte(uint8_t byte)
40 {
41         uint8_t         b;
42         uint16_t        w;
43         uint8_t         v;
44         uint8_t         bit;
45         uint8_t         w_hi, w_lo;
46
47         /*    start           data           stop */
48         w = (0x000 << 0) | (byte << 1) | (0x001 << 9);
49
50         w_hi = w >> 8;
51         w_lo = w;
52
53         ao_arch_block_interrupts();
54
55         /* Ok, this is a bit painful.
56          * We need this loop to be precisely timed, which
57          * means knowing exactly how many instructions will
58          * be executed for each bit. It's easy to do that by
59          * compiling the C code and looking at the output,
60          * but we need this code to work even if the compiler
61          * changes. So, just hand-code the whole thing
62          */
63
64         asm volatile (
65                 "       ldi     %[b], 10\n"             // loop value
66                 "loop:\n"
67                 "       in      %[v], %[port]\n"        // read current value
68                 "       andi    %[v], %[led_mask]\n"    // mask to clear LED bit
69                 "       mov     %[bit], %[w_lo]\n"      // get current data byte
70                 "       andi    %[bit], 0x01\n"         // get current data bit
71 #if AO_LED_SERIAL >= 1
72                 "       add     %[bit],%[bit]\n"        // shift by one
73 #else
74                 "       nop\n"
75 #endif
76 #if AO_LED_SERIAL >= 2
77                 "       add     %[bit],%[bit]\n"        // shift by one
78 #else
79                 "       nop\n"
80 #endif
81 #if AO_LED_SERIAL >= 3
82                 "       add     %[bit],%[bit]\n"        // shift by one
83 #else
84                 "       nop\n"
85 #endif
86 #if AO_LED_SERIAL >= 4
87                 "       add     %[bit],%[bit]\n"        // shift by one
88 #else
89                 "       nop\n"
90 #endif
91 #if AO_LED_SERIAL >= 5
92                 "       add     %[bit],%[bit]\n"        // shift by one
93 #else
94                 "       nop\n"
95 #endif
96 #if AO_LED_SERIAL >= 6
97                 "       add     %[bit],%[bit]\n"        // shift by one
98 #else
99                 "       nop\n"
100 #endif
101 #if AO_LED_SERIAL >= 7
102                 "       add     %[bit],%[bit]\n"        // shift by one
103 #else
104                 "       nop\n"
105 #endif
106                 "       or      %[v], %[bit]\n"         // add to register
107                 "       out     %[port], %[v]\n"        // write current value
108                 "       lsr     %[w_hi]\n"              // shift data
109                 "       ror     %[w_lo]\n"              //  ...
110                 "       nop\n"
111                 "       nop\n"
112                 "       nop\n"
113                 "       nop\n"
114                 "       nop\n"
115
116                 "       nop\n"
117                 "       nop\n"
118                 "       nop\n"
119                 "       subi    %[b], 1\n"              // decrement bit count
120                 "       brne    loop\n"                 // jump back to top
121                 : [v]        "=&r" (v),
122                   [bit]      "=&r" (bit),
123                   [b]        "=&r" (b),
124                   [w_lo]     "+r" (w_lo),
125                   [w_hi]     "+r" (w_hi)
126                 : [port]     "I"  (_SFR_IO_ADDR(LED_PORT)),
127                   [led_mask] "M"  ((~(1 << AO_LED_SERIAL)) & 0xff)
128                 );
129
130 #if 0
131         /*
132          * Here's the equivalent C code to document
133          * what the above assembly code does
134          */
135         for (b = 0; b < 10; b++) {
136                 uint8_t v = LED_PORT & ~(1 << AO_LED_SERIAL);
137                 v |= (w & 1) << AO_LED_SERIAL;
138                 LED_PORT = v;
139                 w >>= 1;
140
141                 /* Carefully timed to hit around 9600 baud */
142                 asm volatile ("nop");
143                 asm volatile ("nop");
144                 asm volatile ("nop");
145
146                 asm volatile ("nop");
147                 asm volatile ("nop");
148                 asm volatile ("nop");
149                 asm volatile ("nop");
150                 asm volatile ("nop");
151
152                 asm volatile ("nop");
153                 asm volatile ("nop");
154                 asm volatile ("nop");
155                 asm volatile ("nop");
156                 asm volatile ("nop");
157         }
158 #endif
159         ao_arch_release_interrupts();
160 }