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