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 #if AO_LED_SERIAL != 4
56 #error "expect AO_LED_SERIAL to be 4"
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
69 " ldi %[b], 10\n" // loop value
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" // ...
96 " subi %[b], 1\n" // decrement bit count
97 " brne loop\n" // jump back to top
103 : [port] "I" (_SFR_IO_ADDR(LED_PORT)),
104 [led_mask] "M" ((~(1 << AO_LED_SERIAL)) & 0xff)
109 * Here's the equivalent C code to document
110 * what the above assembly code does
112 for (b = 0; b < 10; b++) {
113 uint8_t v = LED_PORT & ~(1 << AO_LED_SERIAL);
114 v |= (w & 1) << AO_LED_SERIAL;
118 /* Carefully timed to hit around 9600 baud */
119 asm volatile ("nop");
120 asm volatile ("nop");
121 asm volatile ("nop");
123 asm volatile ("nop");
124 asm volatile ("nop");
125 asm volatile ("nop");
126 asm volatile ("nop");
127 asm volatile ("nop");
129 asm volatile ("nop");
130 asm volatile ("nop");
131 asm volatile ("nop");
132 asm volatile ("nop");
133 asm volatile ("nop");
136 ao_arch_release_interrupts();