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; either version 2 of the License, or
7 * (at your option) any later version.
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.
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.
22 #define AO_ASYNC_BAUD 38400l
23 #define AO_ASYNC_DELAY (uint8_t) (1000000l / AO_ASYNC_BAUD)
25 #define LED_PORT PORTB
30 LED_PORT |= (1 << AO_LED_SERIAL);
36 LED_PORT &= ~(1 << AO_LED_SERIAL);
40 ao_async_byte(uint8_t byte)
49 w = (0x000 << 0) | (byte << 1) | (0x001 << 9);
54 ao_arch_block_interrupts();
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
66 " ldi %[b], 10\n" // loop value
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
77 #if AO_LED_SERIAL >= 2
78 " add %[bit],%[bit]\n" // shift by one
82 #if AO_LED_SERIAL >= 3
83 " add %[bit],%[bit]\n" // shift by one
87 #if AO_LED_SERIAL >= 4
88 " add %[bit],%[bit]\n" // shift by one
92 #if AO_LED_SERIAL >= 5
93 " add %[bit],%[bit]\n" // shift by one
97 #if AO_LED_SERIAL >= 6
98 " add %[bit],%[bit]\n" // shift by one
102 #if AO_LED_SERIAL >= 7
103 " add %[bit],%[bit]\n" // shift by one
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" // ...
120 " subi %[b], 1\n" // decrement bit count
121 " brne loop\n" // jump back to top
127 : [port] "I" (_SFR_IO_ADDR(LED_PORT)),
128 [led_mask] "M" ((~(1 << AO_LED_SERIAL)) & 0xff)
133 * Here's the equivalent C code to document
134 * what the above assembly code does
136 for (b = 0; b < 10; b++) {
137 uint8_t v = LED_PORT & ~(1 << AO_LED_SERIAL);
138 v |= (w & 1) << AO_LED_SERIAL;
142 /* Carefully timed to hit around 9600 baud */
143 asm volatile ("nop");
144 asm volatile ("nop");
145 asm volatile ("nop");
147 asm volatile ("nop");
148 asm volatile ("nop");
149 asm volatile ("nop");
150 asm volatile ("nop");
151 asm volatile ("nop");
153 asm volatile ("nop");
154 asm volatile ("nop");
155 asm volatile ("nop");
156 asm volatile ("nop");
157 asm volatile ("nop");
160 ao_arch_release_interrupts();