altos: Replace C code attiny async output with inline asm
[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 #if AO_LED_SERIAL != 4
56 #error "expect AO_LED_SERIAL to be 4"
57 #endif
58
59         /* Ok, this is a bit painful.
60          * We need this loop to be precisely timed, which
61          * means knowing exactly how many instructions will
62          * be executed for each bit. It's easy to do that by
63          * compiling the C code and looking at the output,
64          * but we need this code to work even if the compiler
65          * changes. So, just hand-code the whole thing
66          */
67
68         asm volatile (
69                 "       ldi     %[b], 10\n"             // loop value
70                 "loop:\n"
71                 "       in      %[v], %[port]\n"        // read current value
72                 "       andi    %[v], %[led_mask]\n"    // mask to clear LED bit
73                 "       mov     %[bit], %[w_lo]\n"      // get current data byte
74                 "       andi    %[bit], 0x01\n"         // get current data bit
75                 "       swap    %[bit]\n"               // rotate by 4 (AO_LED_SERIAL is 4)
76                 "       andi    %[bit], 0xf0\n"         // mask off other 4 bits
77                 "       or      %[v], %[bit]\n"         // add to register
78                 "       out     %[port], %[v]\n"        // write current value
79                 "       lsr     %[w_hi]\n"              // shift data
80                 "       ror     %[w_lo]\n"              //  ...
81                 "       nop\n"
82                 "       nop\n"
83                 "       nop\n"
84                 "       nop\n"
85                 "       nop\n"
86
87                 "       nop\n"
88                 "       nop\n"
89                 "       nop\n"
90                 "       nop\n"
91                 "       nop\n"
92
93                 "       nop\n"
94                 "       nop\n"
95                 "       nop\n"
96                 "       subi    %[b], 1\n"              // decrement bit count
97                 "       brne    loop\n"                 // jump back to top
98                 : [v]        "=&r" (v),
99                   [bit]      "=&r" (bit),
100                   [b]        "=&r" (b),
101                   [w_lo]     "+r" (w_lo),
102                   [w_hi]     "+r" (w_hi)
103                 : [port]     "I"  (_SFR_IO_ADDR(LED_PORT)),
104                   [led_mask] "M"  ((~(1 << AO_LED_SERIAL)) & 0xff)
105                 );
106
107 #if 0
108         /*
109          * Here's the equivalent C code to document
110          * what the above assembly code does
111          */
112         for (b = 0; b < 10; b++) {
113                 uint8_t v = LED_PORT & ~(1 << AO_LED_SERIAL);
114                 v |= (w & 1) << AO_LED_SERIAL;
115                 LED_PORT = v;
116                 w >>= 1;
117
118                 /* Carefully timed to hit around 9600 baud */
119                 asm volatile ("nop");
120                 asm volatile ("nop");
121                 asm volatile ("nop");
122
123                 asm volatile ("nop");
124                 asm volatile ("nop");
125                 asm volatile ("nop");
126                 asm volatile ("nop");
127                 asm volatile ("nop");
128
129                 asm volatile ("nop");
130                 asm volatile ("nop");
131                 asm volatile ("nop");
132                 asm volatile ("nop");
133                 asm volatile ("nop");
134         }
135 #endif
136         ao_arch_release_interrupts();
137 }