[store seems to make this unstable
timo.lindfors@iki.fi**20090414215908] {
hunk ./calypso-debug-logger/CDC_task.c 5
-// 8000000/115200 = 69.44
-#define BITLENGTH 67
-// (8000000/115200)*1.5 = 104.1666
-#define BITLENGTH2 103
+#define DELAY30                                 \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"
hunk ./calypso-debug-logger/CDC_task.c 37
-static volatile uint8_t tmpbyte;
-static volatile uint8_t bits_left;
+/* 1 + 2 + 1 + 30 + 30 + 5 = 69 */
+#define READ_BIT                                \
+    "clc\n\t"                                   \
+    "sbic %0, 2\n\t" /* PD2 (RX) */             \
+    "sec\n\t"                                   \
+    "ror r18\n\t"                               \
+    DELAY30                                     \
+    DELAY30                                     \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"                                   \
+    "nop\n\t"
hunk ./calypso-debug-logger/CDC_task.c 51
-static volatile uint8_t rxbyte;
-static volatile uint8_t rxbyte_ready;
hunk ./calypso-debug-logger/CDC_task.c 52
+volatile static uint8_t __attribute__ ((aligned ((256)))) buf[256];
hunk ./calypso-debug-logger/CDC_task.c 54
-static volatile union {
-    uint16_t val16[256];
-    uint8_t val8[256*2];
-} debug;
+static void __attribute__ ((noinline)) receive_data(void) {
+    uint8_t sreg;
+    
+    sreg = SREG;
+    cli();
+    asm volatile(
+        "clr r30\n\t"
+        "ldi r31, hi8(buf)\n\t"
hunk ./calypso-debug-logger/CDC_task.c 63
-ISR(INT2_vect) {
-    uint8_t i;
+        ".wait:\n\t"
+        "sbis %0, 2\n\t" /* PD2 (RX) */
+        "rjmp .start_bit\n\t"
+        "sbic %1, 2\n\t" /* PE2 (HWB) */
+        "rjmp .wait\n\t"
+        "rjmp .button_pressed\n\t"
+        
+        ".start_bit:\n\t"
+        /* 1 + 2 + 30 + 4 + 2 + 30 + 30 = 99 (4 less than)*/
+        DELAY30
+        "nop\n\t"
+        "nop\n\t"
+        "nop\n\t"
+        "nop\n\t"
+        "sbic %0, 2\n\t" /* PD2 (RX) */
+        "rjmp .wait\n\t"
+        DELAY30
+        DELAY30
hunk ./calypso-debug-logger/CDC_task.c 82
-    debug.val16[0] = TCNT3;
-    EIMSK &= ~_BV(INT2); // disable int2
-    //OCR0A = TCNT0 + BITLENGTH2;
-    for (i = 0; i < 3; i++) {
-        if (PIND & _BV(PD2)) {
-            EIMSK |= _BV(INT2); // enable int2
-            debug.val16[0] = 0;
-            return;
-        }
-    }
-    TIMSK0 = _BV(OCIE0A); // enable timer0 compare A interrupt
-    bits_left = 8 + 1;
-    debug.val16[1] = TCNT3;
-}
+        READ_BIT
+        READ_BIT
+        READ_BIT 
+        READ_BIT
hunk ./calypso-debug-logger/CDC_task.c 87
-#if 1
-ISR(TIMER0_COMPA_vect) {
-    uint8_t bit;
-    static uint8_t i = 0;
+        READ_BIT
+        READ_BIT
+        READ_BIT
+        READ_BIT
hunk ./calypso-debug-logger/CDC_task.c 92
-    debug.val16[i++] = TCNT3;
-#if 0
-    bit = PIND & _BV(PD2);
-    bits_left--;
-    if (bits_left) {
-        tmpbyte <<= 1;
-        if (bit) {
-            tmpbyte |= 1;
-        }
-    } else {
-        TIMSK0 = _BV(OCIE0A); // disable timer0 compare A interrupt
-        //EIMSK |= _BV(INT2); // enable int2
-        if (bit) {
-            rxbyte = tmpbyte;
-            rxbyte_ready = 1;
-        }
-    }
-#endif
+        /* 2 + 30 + 2 = 34 */
+        "sbic %0, 2\n\t" /* PD2 (RX) */
+        "rjmp .stopok\n\t"
+        DELAY30
+        "rjmp .wait\n\t"
+        
+        ".stopok:\n\t"
+        "st Z, r18\n\t"
+        "add r30, 1\n\t"
+        "rjmp .wait\n\t"
+        
+        ".button_pressed:\n\t"
+        :
+        : /* 8-byte integer constant */ "M" (_SFR_IO_ADDR(PIND)),
+          /* 8-byte integer constant */ "M" (_SFR_IO_ADDR(PINE))
+        : "r18", "r30", "r31"
+        );
+    SREG = sreg;
hunk ./calypso-debug-logger/CDC_task.c 111
-#endif
hunk ./calypso-debug-logger/CDC_task.c 121
-    case 'd':
+    case 'w':
+        receive_data();
+        break;
+    case 'p':
hunk ./calypso-debug-logger/CDC_task.c 127
-        for (i = 0; i < 28; i++) {
-            put_byte('0' + i);
-            put_byte(' ');
-            print_bits(debug.val16[i] >> 8);
-            print_bits(debug.val16[i] & 0xff);
-            if (i > 0) {
-                uint16_t delta = debug.val16[i] - debug.val16[i-1];
-                put_byte(' ');
-                print_bits(delta >> 8);
-                print_bits(delta & 0xff);
-            }
+        for (i = 0; i < 256; i++) {
+            print_bits(buf[i]);
hunk ./calypso-debug-logger/CDC_task.c 133
-    case 'I':
-        EIMSK |= _BV(INT2); // enable int2
-        break;
-    case 'v':
-        print_bits(rxbyte >> 8);
-        print_bits(rxbyte & 0xff);
-        break;
-    case 'c':
-        print_bits(bits_left);
+    case 'z':
+        for (i = 0; i < 256; i++) {
+            buf[i] = 0xff;
+        }
hunk ./calypso-debug-logger/CDC_task.c 138
-    case 'T':
-        print_bits(TCNT0);
-        print_bits(TCNT3 >> 8);
-        print_bits(TCNT3 & 0xff);
+    case 'h':
+        print_bits(((uint16_t)buf) >> 8);
+        print_bits(((uint16_t)buf) & 0xff);
hunk ./calypso-debug-logger/CDC_task.c 143
+    
hunk ./calypso-debug-logger/CDC_task.c 152
-            if (Endpoint_BytesInEndpoint()) { // while?
+            if (Endpoint_BytesInEndpoint()) {
hunk ./calypso-debug-logger/CDC_task.c 165
-    EICRA |= _BV(ISC21); // int2 on falling edge
-    TCCR0A = _BV(WGM01); // clear on compare match (CTC)
-    TCCR0B = _BV(CS00); // CLK/1
-    TCCR3B = _BV(CS30); // CLK/1
-    OCR0A = BITLENGTH;
+    DDRE &= ~_BV(PE2); // PE2 (HWB) is input
}
