Fix floating pins bug, improve debug switches

This commit is contained in:
Raphaël Assénat 2013-10-26 20:26:58 +00:00
parent 1f9db3fdb4
commit c1181687a7
1 changed files with 60 additions and 11 deletions

View File

@ -21,6 +21,11 @@
#include <util/delay.h> #include <util/delay.h>
#include "usbdrv.h" #include "usbdrv.h"
#undef NOLRC
#undef TRACE_RX_START_END
#undef TRACE_DECODED
#undef TRACE_PIN1_BITS
// //
// //
// PORTC0 : Pin 1 // PORTC0 : Pin 1
@ -29,10 +34,14 @@
void maple_init(void) void maple_init(void)
{ {
DDRC &= ~0x03; DDRC = 0xFC;
PORTC = 0x03;
} }
#define transmitMode() do { PORTC |= 0x03; DDRC |= 0x03; } while(0)
#define inputMode() do { PORTC |= 0x03; DDRC &= ~0x03; } while(0)
#define MAPLE_BUF_SIZE 640
#define MAPLE_BUF_SIZE 641
static unsigned char maplebuf[MAPLE_BUF_SIZE]; static unsigned char maplebuf[MAPLE_BUF_SIZE];
static unsigned char buf_used; static unsigned char buf_used;
static unsigned char buf_phase; static unsigned char buf_phase;
@ -47,6 +56,8 @@ static void buf_reset(void)
static void buf_addBit(char value) static void buf_addBit(char value)
{ {
// The values in maplebuf will be written
// directly to PORTC. Unused bits will be low.
if (buf_phase & 0x01) { if (buf_phase & 0x01) {
maplebuf[buf_used] = PIN_5; maplebuf[buf_used] = PIN_5;
if (value) { if (value) {
@ -72,6 +83,15 @@ static int maplebus_decode(unsigned char *data, unsigned int maxlen)
unsigned char last_fell; unsigned char last_fell;
int i; int i;
#ifdef TRACE_DECODED
PORTB |= 0x10;
PORTB &= ~0x10;
PORTB |= 0x10;
PORTB &= ~0x10;
PORTB |= 0x10;
PORTB &= ~0x10;
#endif
// Look for the initial phase 1 (Pin 1 high, Pin 5 low). This // Look for the initial phase 1 (Pin 1 high, Pin 5 low). This
// is to skip what we got of the sync/start of frame sequence. // is to skip what we got of the sync/start of frame sequence.
// //
@ -90,7 +110,15 @@ static int maplebus_decode(unsigned char *data, unsigned int maxlen)
last_fell = 0; last_fell = 0;
for (; i<MAPLE_BUF_SIZE; i++) { for (; i<MAPLE_BUF_SIZE; i++) {
unsigned char fell; unsigned char fell;
unsigned char cur = maplebuf[i]; unsigned char cur = maplebuf[i] & 0x3;
#ifdef TRACE_PIN1_BITS
if (cur & 1) {
PORTB |= 0x10;
} else {
PORTB &= ~0x10;
}
#endif
if (cur == last) { if (cur == last) {
continue; // no change continue; // no change
@ -106,8 +134,14 @@ static int maplebus_decode(unsigned char *data, unsigned int maxlen)
if (fell == last_fell) { if (fell == last_fell) {
// two identical consecutive phases marks the end of the packet. // two identical consecutive phases marks the end of the packet.
#ifdef TRACE_DECODED
PORTB |= 0x10; PORTB |= 0x10;
PORTB &= ~0x10; PORTB &= ~0x10;
PORTB |= 0x10;
PORTB &= ~0x10;
PORTB |= 0x10;
PORTB &= ~0x10;
#endif
break; break;
} }
@ -122,8 +156,14 @@ static int maplebus_decode(unsigned char *data, unsigned int maxlen)
if (cur) { if (cur) {
data[dst_pos] |= dst_b; data[dst_pos] |= dst_b;
#ifdef TRACE_DECODED
PORTB |= 0x10;
#endif
} }
else { else {
#ifdef TRACE_DECODED
PORTB &= ~0x10;
#endif
} }
} }
@ -138,6 +178,10 @@ static int maplebus_decode(unsigned char *data, unsigned int maxlen)
last = cur; last = cur;
} }
#ifdef TRACE_DECODED
PORTB &= ~0x10;
#endif
return dst_pos; return dst_pos;
} }
@ -167,8 +211,8 @@ int maple_receivePacket(unsigned char *data, unsigned int maxlen)
" push r31 \n" // 2 " push r31 \n" // 2
" clr %1 \n" // 1 (result=0, no timeout) " clr %1 \n" // 1 (result=0, no timeout)
" sbi 0x5, 4 \n" // PB4 // " sbi 0x5, 4 \n" // PB4
" cbi 0x5, 4 \n" // " cbi 0x5, 4 \n"
// Loop until a change is detected. // Loop until a change is detected.
" ldi r18, 255 \n" " ldi r18, 255 \n"
@ -188,16 +232,20 @@ int maple_receivePacket(unsigned char *data, unsigned int maxlen)
" jmp done \n" " jmp done \n"
"start_rx: \n" "start_rx: \n"
#ifdef TRACE_RX_START_END
" sbi 0x5, 4 \n" // PB4 " sbi 0x5, 4 \n" // PB4
" cbi 0x5, 4 \n" " cbi 0x5, 4 \n"
#endif
// We will loose the first bit(s), but // We will loose the first bit(s), but
// it's only the start of frame. // it's only the start of frame.
#include "rxcode.asm" #include "rxcode.asm"
"done:\n" "done:\n"
#ifdef TRACE_RX_START_END
" sbi 0x5, 4 \n" // PB4 " sbi 0x5, 4 \n" // PB4
" cbi 0x5, 4 \n" " cbi 0x5, 4 \n"
#endif
" pop r31 \n" // 2 " pop r31 \n" // 2
" pop r30 \n" // 2 " pop r30 \n" // 2
: "=z"(tmp), "=r"(timeout) : "=z"(tmp), "=r"(timeout)
@ -211,16 +259,18 @@ int maple_receivePacket(unsigned char *data, unsigned int maxlen)
if (res<=0) if (res<=0)
return res; return res;
// A packet containts n groups of 4 bytes, plus 1 byte crc. // A packet contains n groups of 4 bytes, plus 1 byte crc.
if (((res-1) & 0x3) != 0) { if (((res-1) & 0x3) != 0) {
return -2; // frame error return -2; // frame error
} }
#ifndef NOLRC
for (lrc=0, i=0; i<res; i++) { for (lrc=0, i=0; i<res; i++) {
lrc ^= data[i]; lrc ^= data[i];
} }
if (lrc) if (lrc)
return -2; // LRC error return -2; // LRC error
#endif
/* Reverse each group of 4 bytes */ /* Reverse each group of 4 bytes */
for (i=0; i<(res-1); i+=4) { for (i=0; i<(res-1); i+=4) {
@ -252,8 +302,7 @@ void maple_sendPacket(unsigned char *data, unsigned char len)
} }
// Output // Output
PORTC |= 0x03; transmitMode();
DDRC |= 0x03;
// DC controller pin 1 and pin 5 // DC controller pin 1 and pin 5
#define SET_1 " sbi %0, 0\n" #define SET_1 " sbi %0, 0\n"
@ -286,14 +335,14 @@ void maple_sendPacket(unsigned char *data, unsigned char len)
"next_byte:\n" "next_byte:\n"
"out %0, r20 \n" // 1 initial phase 1 state "out %0, r20 \n" // 1 initial phase 1 state
"nop \n" // 1
"out %0, r16 \n" // 1 data "out %0, r16 \n" // 1 data
"cbi %0, 0 \n" // 1 falling edge on pin 1 "cbi %0, 0 \n" // 1 falling edge on pin 1
"ld r16, z+ \n" // 2 load phase 2 data "ld r16, z+ \n" // 2 load phase 2 data
"nop \n" // 1 "nop \n" // 1
"nop \n" // 1
"nop \n" // 1
"out %0, r21 \n" // 1 initial phase 2 state "out %0, r21 \n" // 1 initial phase 2 state
"nop \n"
"out %0, r16 \n" // 1 data "out %0, r16 \n" // 1 data
"cbi %0, 1 \n" // 1 falling edge on pin 5 "cbi %0, 1 \n" // 1 falling edge on pin 5
"ld r16, z+ \n" // 2 "ld r16, z+ \n" // 2
@ -314,7 +363,7 @@ void maple_sendPacket(unsigned char *data, unsigned char len)
); );
// back to input to receive the answer // back to input to receive the answer
DDRC &= ~0x03; inputMode();
} }