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; version 2 of the License.
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.
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.
21 #define AO_ASYNC_BAUD 38400l
22 #define AO_ASYNC_DELAY (uint8_t) (1000000l / AO_ASYNC_BAUD)
24 #define LED_PORT PORTB
29 LED_PORT |= (1 << AO_LED_SERIAL);
35 LED_PORT &= ~(1 << AO_LED_SERIAL);
39 ao_async_byte(uint8_t byte)
48 w = (0x000 << 0) | (byte << 1) | (0x001 << 9);
53 ao_arch_block_interrupts();
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
65 " ldi %[b], 10\n" // loop value
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
76 #if AO_LED_SERIAL >= 2
77 " add %[bit],%[bit]\n" // shift by one
81 #if AO_LED_SERIAL >= 3
82 " add %[bit],%[bit]\n" // shift by one
86 #if AO_LED_SERIAL >= 4
87 " add %[bit],%[bit]\n" // shift by one
91 #if AO_LED_SERIAL >= 5
92 " add %[bit],%[bit]\n" // shift by one
96 #if AO_LED_SERIAL >= 6
97 " add %[bit],%[bit]\n" // shift by one
101 #if AO_LED_SERIAL >= 7
102 " add %[bit],%[bit]\n" // shift by one
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" // ...
119 " subi %[b], 1\n" // decrement bit count
120 " brne loop\n" // jump back to top
126 : [port] "I" (_SFR_IO_ADDR(LED_PORT)),
127 [led_mask] "M" ((~(1 << AO_LED_SERIAL)) & 0xff)
132 * Here's the equivalent C code to document
133 * what the above assembly code does
135 for (b = 0; b < 10; b++) {
136 uint8_t v = LED_PORT & ~(1 << AO_LED_SERIAL);
137 v |= (w & 1) << AO_LED_SERIAL;
141 /* Carefully timed to hit around 9600 baud */
142 asm volatile ("nop");
143 asm volatile ("nop");
144 asm volatile ("nop");
146 asm volatile ("nop");
147 asm volatile ("nop");
148 asm volatile ("nop");
149 asm volatile ("nop");
150 asm volatile ("nop");
152 asm volatile ("nop");
153 asm volatile ("nop");
154 asm volatile ("nop");
155 asm volatile ("nop");
156 asm volatile ("nop");
159 ao_arch_release_interrupts();