define macros for nop blocks

This commit is contained in:
Daniel Schaal 2017-05-13 05:24:43 +02:00
parent b603217f47
commit 80915bdb2d
1 changed files with 24 additions and 59 deletions

View File

@ -3,6 +3,15 @@
#include <Arduino.h> #include <Arduino.h>
#include "pins_arduino.h" #include "pins_arduino.h"
#define NOP asm volatile ("nop")
#define NOP5 asm volatile ("nop\nnop\nnop\nnop\nnop\n")
#define NOP30 asm volatile ("nop\nnop\nnop\nnop\nnop\n" \
"nop\nnop\nnop\nnop\nnop\n" \
"nop\nnop\nnop\nnop\nnop\n" \
"nop\nnop\nnop\nnop\nnop\n" \
"nop\nnop\nnop\nnop\nnop\n" \
"nop\nnop\nnop\nnop\nnop\n")
// these two macros set arduino pin 2 to input or output, which with an // these two macros set arduino pin 2 to input or output, which with an
// external 1K pull-up resistor to the 3.3V rail, is like pulling it high or // external 1K pull-up resistor to the 3.3V rail, is like pulling it high or
// low. These operations translate to 1 op code, which takes 2 cycles // low. These operations translate to 1 op code, which takes 2 cycles
@ -149,7 +158,7 @@ inner_loop:
// 1 bit // 1 bit
// remain low for 1us, then go high for 3us // remain low for 1us, then go high for 3us
// nop block 1 // nop block 1
asm volatile ("nop\nnop\nnop\nnop\nnop\n"); NOP5;
asm volatile (";Setting line to high"); asm volatile (";Setting line to high");
N64_PIND_HIGH; N64_PIND_HIGH;
@ -157,27 +166,14 @@ inner_loop:
// nop block 2 // nop block 2
// we'll wait only 2us to sync up with both conditions // we'll wait only 2us to sync up with both conditions
// at the bottom of the if statement // at the bottom of the if statement
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP30;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
} else { } else {
asm volatile (";Bit is a 0"); asm volatile (";Bit is a 0");
// 0 bit // 0 bit
// remain low for 3us, then go high for 1us // remain low for 3us, then go high for 1us
// nop block 3 // nop block 3
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP30; NOP5; NOP;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
asm volatile (";Setting line to high"); asm volatile (";Setting line to high");
N64_PIND_HIGH; N64_PIND_HIGH;
@ -195,8 +191,8 @@ inner_loop:
if (bits != 0) { if (bits != 0) {
// nop block 4 // nop block 4
// this block is why a for loop was impossible // this block is why a for loop was impossible
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP5; NOP; NOP; NOP; NOP;
"nop\nnop\nnop\nnop\n");
// rotate bits // rotate bits
asm volatile (";rotating out bits"); asm volatile (";rotating out bits");
*buffer <<= 1; *buffer <<= 1;
@ -222,9 +218,8 @@ inner_loop:
// wait 1 us, 16 cycles, then raise the line // wait 1 us, 16 cycles, then raise the line
// 16-2=14 // 16-2=14
// nop block 6 // nop block 6
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP5; NOP5; NOP; NOP; NOP; NOP;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
N64_PIND_HIGH; N64_PIND_HIGH;
} }
@ -244,26 +239,13 @@ inner_loop:
asm volatile (";branching"); asm volatile (";branching");
if (*buffer >> 7) { if (*buffer >> 7) {
asm volatile (";Bit is a 1"); asm volatile (";Bit is a 1");
asm volatile ("nop\nnop\nnop\nnop\nnop\n"); NOP5;
asm volatile (";Setting line to high"); asm volatile (";Setting line to high");
N64_PINB_HIGH; N64_PINB_HIGH;
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP30;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
} else { } else {
asm volatile (";Bit is a 0"); asm volatile (";Bit is a 0");
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP30; NOP5; NOP;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\n");
asm volatile (";Setting line to high"); asm volatile (";Setting line to high");
N64_PINB_HIGH; N64_PINB_HIGH;
@ -272,8 +254,7 @@ inner_loop:
asm volatile (";finishing inner loop body"); asm volatile (";finishing inner loop body");
--bits; --bits;
if (bits != 0) { if (bits != 0) {
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP5; NOP; NOP; NOP; NOP;
"nop\nnop\nnop\nnop\n");
asm volatile (";rotating out bits"); asm volatile (";rotating out bits");
*buffer <<= 1; *buffer <<= 1;
goto inner_loop; goto inner_loop;
@ -286,11 +267,9 @@ inner_loop:
goto outer_loop; goto outer_loop;
} }
} }
asm volatile ("nop\nnop\nnop\nnop\n"); NOP; NOP; NOP; NOP;
N64_PINB_LOW; N64_PINB_LOW;
asm volatile ("nop\nnop\nnop\nnop\nnop\n" NOP5; NOP5; NOP; NOP; NOP; NOP;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\n");
N64_PINB_HIGH; N64_PINB_HIGH;
} }
@ -316,14 +295,7 @@ read_loop:
return; return;
} }
// wait approx 2us and poll the line // wait approx 2us and poll the line
asm volatile ( NOP30;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
*bitbin = N64_PIND_QUERY; *bitbin = N64_PIND_QUERY;
++bitbin; ++bitbin;
--bitcount; --bitcount;
@ -355,14 +327,7 @@ read_loop:
if (!--timeout) if (!--timeout)
return; return;
} }
asm volatile ( NOP30;
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
"nop\nnop\nnop\nnop\nnop\n"
);
*bitbin = N64_PINB_QUERY; *bitbin = N64_PINB_QUERY;
++bitbin; ++bitbin;
--bitcount; --bitcount;