From 7f5e5c4563e069cbafc03b10d66b48d6e42b3b23 Mon Sep 17 00:00:00 2001 From: mtei <2170248+mtei@users.noreply.github.com> Date: Fri, 20 Jul 2018 01:33:54 +0900 Subject: [PATCH] add debug code to helix/serial.c --- keyboards/helix/serial.c | 27 ++++++++++++ keyboards/helix/serial.h | 94 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) diff --git a/keyboards/helix/serial.c b/keyboards/helix/serial.c index f2652c20bc..ec7da520eb 100644 --- a/keyboards/helix/serial.c +++ b/keyboards/helix/serial.c @@ -79,6 +79,7 @@ void serial_delay_half2(void) { inline static void serial_output(void) ALWAYS_INLINE; inline static void serial_output(void) { + debug_output_mode(); SERIAL_PIN_DDR |= SERIAL_PIN_MASK; } @@ -86,6 +87,7 @@ void serial_output(void) { inline static void serial_input_with_pullup(void) ALWAYS_INLINE; inline static void serial_input_with_pullup(void) { + debug_input_mode(); SERIAL_PIN_DDR &= ~SERIAL_PIN_MASK; SERIAL_PIN_PORT |= SERIAL_PIN_MASK; } @@ -108,11 +110,13 @@ void serial_high(void) { } void serial_master_init(void) { + serial_debug_init(); serial_output(); serial_high(); } void serial_slave_init(void) { + serial_debug_init(); serial_input_with_pullup(); #if SERIAL_PIN_MASK == _BV(PD0) @@ -133,19 +137,24 @@ void serial_slave_init(void) { // Used by the sender to synchronize timing with the reciver. static void sync_recv(void) { + debug_sync_start(); for (uint8_t i = 0; i < SERIAL_DELAY*5 && serial_read_pin(); i++ ) { + debug_sync_end(); debug_sync_start(); } // This shouldn't hang if the slave disconnects because the // serial line will float to high if the slave does disconnect. while (!serial_read_pin()); + debug_sync_end(); } // Used by the reciver to send a synchronization signal to the sender. static void sync_send(void) { + debug_sync_start(); serial_low(); serial_delay(); serial_high(); + debug_sync_end(); } // Reads a byte from the serial line @@ -155,9 +164,12 @@ uint8_t serial_read_byte(void) { _delay_sub_us(READ_WRITE_START_ADJUST); for ( uint8_t i = 0; i < 8; ++i) { serial_delay_half1(); // read the middle of pulses + debug_recvsample(); byte = (byte << 1) | serial_read_pin(); + debug_recvsample(); _delay_sub_us(READ_WRITE_WIDTH_ADJUST); serial_delay_half2(); + debug_dummy_delay_recv(); } return byte; } @@ -173,7 +185,10 @@ void serial_write_byte(uint8_t data) { serial_low(); } b >>= 1; + debug_recvsample(); serial_delay(); + debug_recvsample(); + debug_dummy_delay_send(); } serial_low(); // sync_send() / senc_recv() need raise edge } @@ -185,11 +200,15 @@ void serial_send_packet(uint8_t *buffer, uint8_t size) { uint8_t data; data = buffer[i]; sync_send(); + debug_bytewidth_start(); serial_write_byte(data); + debug_bytewidth_end(); checksum += data; } sync_send(); + debug_bytewidth_start(); serial_write_byte(checksum); + debug_bytewidth_end(); } static @@ -198,12 +217,16 @@ uint8_t serial_recive_packet(uint8_t *buffer, uint8_t size) { for (uint8_t i = 0; i < size; ++i) { uint8_t data; sync_recv(); + debug_bytewidth_start(); data = serial_read_byte(); + debug_bytewidth_end(); buffer[i] = data; checksum_computed += data; } sync_recv(); + debug_bytewidth_start(); uint8_t checksum_received = serial_read_byte(); + debug_bytewidth_end(); return checksum_computed != checksum_received; } @@ -228,6 +251,8 @@ void change_reciver2sender(void) { // interrupt handle to be used by the slave device ISR(SERIAL_PIN_INTERRUPT) { + debug_output_mode(); debug_input_mode(); // indicate intterupt entry + serial_low(); serial_output(); @@ -245,6 +270,7 @@ ISR(SERIAL_PIN_INTERRUPT) { } sync_recv(); //weit master output to high + debug_output_mode(); debug_input_mode(); // indicate intterupt exit } inline @@ -298,6 +324,7 @@ int serial_update_buffers(void) { // always, release the line when not in use sync_send(); + debug_input_mode(); debug_output_mode(); // indicate intterupt exit sei(); return 0; diff --git a/keyboards/helix/serial.h b/keyboards/helix/serial.h index c3c9569b2c..0c8be44fa8 100644 --- a/keyboards/helix/serial.h +++ b/keyboards/helix/serial.h @@ -24,4 +24,98 @@ void serial_slave_init(void); int serial_update_buffers(void); bool serial_slave_data_corrupt(void); + + + +// debug flags +#define SERIAL_DEBUG_MODE_WATCH_OUTMODE 0x1 +#define SERIAL_DEBUG_MODE_WATCH_RCVSAMPLE 0x2 +#define SERIAL_DEBUG_MODE_WATCH_BYTEWIDTH 0x4 +#define SERIAL_DEBUG_MODE_WATCH_SYNC 0x8 +#define SERIAL_DEBUG_MODE_WATCH_IOCHG 0x10 +#define SERIAL_DEBUG_MODE_WATCH_PARITY 0x20 + +//#define SERIAL_DEBUG_MODE SERIAL_DEBUG_MODE_WATCH_OUTMODE +//#define SERIAL_DEBUG_MODE SERIAL_DEBUG_MODE_WATCH_RCVSAMPLE +//#define SERIAL_DEBUG_MODE SERIAL_DEBUG_MODE_WATCH_BYTEWIDTH +//#define SERIAL_DEBUG_MODE (SERIAL_DEBUG_MODE_WATCH_RCVSAMPLE|SERIAL_DEBUG_MODE_WATCH_BYTEWIDTH) +//#define SERIAL_DEBUG_MODE SERIAL_DEBUG_MODE_WATCH_SYNC +//#define SERIAL_DEBUG_MODE (SERIAL_DEBUG_MODE_WATCH_RCVSAMPLE|SERIAL_DEBUG_MODE_WATCH_SYNC) +//#define SERIAL_DEBUG_MODE SERIAL_DEBUG_MODE_WATCH_IOCHG +//#define SERIAL_DEBUG_MODE (SERIAL_DEBUG_MODE_WATCH_IOCHG|SERIAL_DEBUG_MODE_WATCH_SYNC) +//#define SERIAL_DEBUG_MODE SERIAL_DEBUG_MODE_WATCH_PARITY + +// Helix keyboard unused port (for Logic analyzer or oscilloscope) +#ifdef SERIAL_DEBUG_MODE +#define SERIAL_DBGPIN_DDR DDRB +#define SERIAL_DBGPIN_PORT PORTB +#define SERIAL_DBGPIN_MASK _BV(PB5) +#endif + +#ifdef SERIAL_DEBUG_MODE + #define serial_debug_init() SERIAL_DBGPIN_DDR |= SERIAL_DBGPIN_MASK +#else + #define serial_debug_init() +#endif + +#if SERIAL_DEBUG_MODE & SERIAL_DEBUG_MODE_WATCH_OUTMODE + #define debug_output_mode() SERIAL_DBGPIN_PORT |= SERIAL_DBGPIN_MASK + #define debug_input_mode() SERIAL_DBGPIN_PORT &= ~SERIAL_DBGPIN_MASK +#else + #define debug_output_mode() + #define debug_input_mode() +#endif + +#if SERIAL_DEBUG_MODE & SERIAL_DEBUG_MODE_WATCH_RCVSAMPLE + #define debug_recvsample() SERIAL_DBGPIN_PORT ^= SERIAL_DBGPIN_MASK +#else + #define debug_recvsample() +#endif + +#if SERIAL_DEBUG_MODE & SERIAL_DEBUG_MODE_WATCH_BYTEWIDTH + #define debug_bytewidth_start() SERIAL_DBGPIN_PORT |= SERIAL_DBGPIN_MASK + #define debug_bytewidth_end() SERIAL_DBGPIN_PORT &= ~SERIAL_DBGPIN_MASK +#else + #define debug_bytewidth_start() + #define debug_bytewidth_end() +#endif + +#if SERIAL_DEBUG_MODE & SERIAL_DEBUG_MODE_WATCH_SYNC + #define debug_sync_start() SERIAL_DBGPIN_PORT |= SERIAL_DBGPIN_MASK + #define debug_sync_end() SERIAL_DBGPIN_PORT &= ~SERIAL_DBGPIN_MASK +#else + #define debug_sync_start() + #define debug_sync_end() +#endif + +#if SERIAL_DEBUG_MODE & SERIAL_DEBUG_MODE_WATCH_IOCHG + #define debug_iochg_on() SERIAL_DBGPIN_PORT |= SERIAL_DBGPIN_MASK + #define debug_iochg_off() SERIAL_DBGPIN_PORT &= ~SERIAL_DBGPIN_MASK +#else + #define debug_iochg_on() + #define debug_iochg_off() +#endif + +#if SERIAL_DEBUG_MODE & SERIAL_DEBUG_MODE_WATCH_PARITY + #define debug_parity_on() SERIAL_DBGPIN_PORT |= SERIAL_DBGPIN_MASK + #define debug_parity_off() SERIAL_DBGPIN_PORT &= ~SERIAL_DBGPIN_MASK +#else + #define debug_parity_on() + #define debug_parity_off() +#endif + +#define SYNC_DEBUG_MODE 0 +#if SYNC_DEBUG_MODE == 0 +#define debug_dummy_delay_recv() +#define debug_dummy_delay_send() +#endif +#if SYNC_DEBUG_MODE == 1 +#define debug_dummy_delay_recv() _delay_us(3); _delay_sub_us(2) +#define debug_dummy_delay_send() +#endif +#if SYNC_DEBUG_MODE == 2 +#define debug_dummy_delay_recv() +#define debug_dummy_delay_send() _delay_us(3); _delay_sub_us(2) +#endif + #endif /* SOFT_SERIAL_H */