From 014b457794a2c3a9fb81d5822f7629d38649b41d Mon Sep 17 00:00:00 2001 From: Ed Gonzalez Date: Thu, 11 Jun 2015 11:31:29 -0500 Subject: [PATCH] Successfully decoding DSTAR Header information. Cleanup and full state machine work starting --- DSP_API/SmartSDR_Interface/sched_waveform.c | 42 +++++++++++++---- DSP_API/ThumbDV/bit_pattern_matcher.c | 12 +++-- DSP_API/ThumbDV/bit_pattern_matcher.h | 2 +- DSP_API/ThumbDV/dstar.c | 51 +++++++++++++-------- 4 files changed, 76 insertions(+), 31 deletions(-) diff --git a/DSP_API/SmartSDR_Interface/sched_waveform.c b/DSP_API/SmartSDR_Interface/sched_waveform.c index 970d459..6cc23d1 100644 --- a/DSP_API/SmartSDR_Interface/sched_waveform.c +++ b/DSP_API/SmartSDR_Interface/sched_waveform.c @@ -49,6 +49,7 @@ #include "thumbDV.h" #include "bit_pattern_matcher.h" #include "dstar.h" +#include "DStarDefines.h" //static Queue sched_fft_queue; @@ -207,6 +208,7 @@ static int _dv_serial_fd = 0; static GMSK_DEMOD _gmsk_demod = NULL; static GMSK_MOD _gmsk_mod = NULL; static BIT_PM _syn_pm = NULL; +static BIT_PM _data_sync_pm = NULL; static void* _sched_waveform_thread(void* param) { @@ -288,6 +290,7 @@ static void* _sched_waveform_thread(void* param) BOOL dstar_header[660] = {0}; uint32 header_count = 0; BOOL found_syn_bits = FALSE; + BOOL found_data_sync_bits = FALSE; // show that we are running BufferDescriptor buf_desc; @@ -380,7 +383,7 @@ static void* _sched_waveform_thread(void* param) output("FOUND SYN BITS\n"); bitPM_reset(_syn_pm); } - } else { + } else if ( header_count < 660 ){ /* We have already found syn-bits accumulate 660 bits */ if ( state == DEMOD_TRUE ) { dstar_header[header_count++] = TRUE; @@ -394,29 +397,49 @@ static void* _sched_waveform_thread(void* param) output("Found 660 bits - descrambling\n"); /* Found 660 bits of header */ unsigned char bytes[660/8 +1]; + gmsk_bitsToBytes(dstar_header, bytes, 660); thumbDV_dump("RAW:", bytes, 660/8); + uint32 scramble_count = 0; BOOL descrambled[660] = {0}; dstar_scramble(dstar_header, descrambled, header_count, &scramble_count); - gmsk_bitsToBytes(descrambled, bytes, 660); thumbDV_dump("DESCRAMBLE:", bytes, 660/8); + + BOOL out[660] = {0}; dstar_deinterleave(descrambled, out, 660); gmsk_bitsToBytes(out, bytes, 660); thumbDV_dump("DEINTERLEAVE:", bytes, 660/8); dstar_fec fec; memset(&fec, 0, sizeof(dstar_fec)); - unsigned int outLen = 0; - dstar_FECdecode(&fec, out, dstar_header, 660, &outLen); + unsigned int outLen = 660; + BOOL decoded[330] = {0}; + dstar_FECdecode(&fec, out, decoded, 660, &outLen); output("outLen = %d\n" ,outLen); - gmsk_bitsToBytes(dstar_header, bytes, outLen); - thumbDV_dump("FEC: ", dstar_header, outLen/8); + gmsk_bitsToBytes(decoded, bytes, outLen); + thumbDV_dump("FEC: ", bytes, outLen/8); - header_count = 0; - found_syn_bits = FALSE; } + } else { + if ( state == DEMOD_TRUE ) { + found_data_sync_bits = bitPM_addBit(_data_sync_pm, TRUE); + //output("%d ", 1); + } else if ( state == DEMOD_FALSE ) { + found_data_sync_bits = bitPM_addBit(_data_sync_pm, FALSE); + //output("%d ", 0); + } else { + //output("UNKNOWN DEMOD STATE"); + //bits[bit] = 0x00; + } + + if ( found_data_sync_bits ) { + output("FOUND DATA SYNC BITS\n"); + bitPM_reset(_syn_pm); + header_count = 0; + found_syn_bits = FALSE; + } } } @@ -664,7 +687,7 @@ static void* _sched_waveform_thread(void* param) void sched_waveform_Init(void) { - //dstar_FECTest(); + dstar_FECTest(); //exit(0); _gmsk_demod = gmsk_createDemodulator(); @@ -685,6 +708,7 @@ void sched_waveform_Init(void) } _syn_pm = bitPM_create( syn_bits, 64+15); + _data_sync_pm = bitPM_create( DATA_SYNC_BITS, 24); diff --git a/DSP_API/ThumbDV/bit_pattern_matcher.c b/DSP_API/ThumbDV/bit_pattern_matcher.c index 5f7282a..4a0ff24 100644 --- a/DSP_API/ThumbDV/bit_pattern_matcher.c +++ b/DSP_API/ThumbDV/bit_pattern_matcher.c @@ -38,7 +38,7 @@ #include "common.h" #include "bit_pattern_matcher.h" -BIT_PM bitPM_create(BOOL * to_match, uint32 length) +BIT_PM bitPM_create( const BOOL * to_match, uint32 length) { BIT_PM bpm = (BIT_PM) safe_malloc(sizeof(bit_pm)); @@ -108,10 +108,16 @@ BOOL bitPM_addBit(BIT_PM bpm, BOOL bit) } //#ifdef DEBUG_BIT_PM - output(ANSI_GREEN "Match Found\n"); + output(ANSI_GREEN "Match Found\nPat: "); for ( i = 0; i < bpm->length ; i++ ) { - output("Pat: %d Data %d\n", bpm->pattern[i], bpm->data[i]); + output("%d ", bpm->pattern[i]); } + output("\nMat: "); + for ( i = 0; i < bpm->length ; i++ ) { + output("%d ", bpm->data[i]); + } output("\n"); + + //#endif /* If we make it here all checks have passed */ diff --git a/DSP_API/ThumbDV/bit_pattern_matcher.h b/DSP_API/ThumbDV/bit_pattern_matcher.h index 8883190..45cb8d2 100644 --- a/DSP_API/ThumbDV/bit_pattern_matcher.h +++ b/DSP_API/ThumbDV/bit_pattern_matcher.h @@ -47,7 +47,7 @@ typedef struct _bit_pattern_matcher } bit_pm, * BIT_PM; void bitPM_destroy(BIT_PM bpm); -BIT_PM bitPM_create(BOOL * to_match, uint32 length); +BIT_PM bitPM_create(const BOOL * to_match, uint32 length); BOOL bitPM_addBit(BIT_PM bpm, BOOL bit); void bitPM_reset(BIT_PM bpm); diff --git a/DSP_API/ThumbDV/dstar.c b/DSP_API/ThumbDV/dstar.c index 914a3a7..fc01d71 100644 --- a/DSP_API/ThumbDV/dstar.c +++ b/DSP_API/ThumbDV/dstar.c @@ -141,8 +141,6 @@ static const unsigned int INTERLEAVE_TABLE[] = { 362, 389, 416, 443, 470, 497, 524, 551, 578, 605, 632, 659, 27, 55, 83, 111, 139, 167, 195, 223, 251, 279, 307, 335}; - - void dstar_scramble(BOOL * in, BOOL * out, uint32 length, uint32 * scramble_count) { if ( out == NULL || in == NULL || scramble_count == NULL) { @@ -168,15 +166,14 @@ void dstar_interleave(const BOOL * in, BOOL * out, unsigned int length) return; } -// if ( length != FEC_SECTION_LENGTH_BITS ) { -// output(ANSI_RED "length not correct interleave\n" ANSI_WHITE); -// return; -// } + if ( length != FEC_SECTION_LENGTH_BITS ) { + output(ANSI_RED "Wrong leangth in interleave\n" ANSI_WHITE); + } memset(out, 0, FEC_SECTION_LENGTH_BITS * sizeof(BOOL)); uint32 i = 0; for ( i = 0 ; i < FEC_SECTION_LENGTH_BITS ; i++ ) { - if (in[i]) { + if ( in[i] ) { unsigned int newi = INTERLEAVE_TABLE[i]; if ( newi >= FEC_SECTION_LENGTH_BITS ) { @@ -185,6 +182,7 @@ void dstar_interleave(const BOOL * in, BOOL * out, unsigned int length) out[newi] = TRUE; } + } } @@ -202,6 +200,8 @@ void dstar_deinterleave(const BOOL * in, BOOL * out, unsigned int length) if (in[i]) out[k] = TRUE; + else + out[k] = FALSE; k += 24U; if (k >= 672U) @@ -420,16 +420,17 @@ void dstar_FECTest(void) BOOL decoded[330] = {0}; uint32 i = 0; for ( i = 0 ; i < 327- 1 ; i += 2 ) { - test[i] = TRUE; - test[i+1] = FALSE; + test[i] = TRUE;//(rand() & 0x01) ? TRUE:FALSE; + test[i+1] = FALSE;//( rand() & 0x01 ) ? TRUE:FALSE ; } gmsk_bitsToBytes(test, bytes, 330); thumbDV_dump("TEST FEC IN:", bytes, 330/8); memset(bytes,0, 660/8 * sizeof(unsigned char)); uint32 outLen = 0; - dstar_FECencode(test, encoded, 327, &outLen); + dstar_FECencode(test, encoded, 300, &outLen); output("Encode outLen = %d\n", outLen); + outLen = 660; gmsk_bitsToBytes(encoded, bytes, outLen); thumbDV_dump("TEST FEC ENCODE", bytes, outLen/8);memset(bytes,0, 660/8 * sizeof(unsigned char)); @@ -454,17 +455,31 @@ void dstar_FECTest(void) dstar_fec fec; memset(&fec, 0, sizeof(dstar_fec)); + output("outLen = %d\n", outLen); dstar_FECdecode(&fec, deinterleaved, decoded, outLen, &outLen); output("Decode outLen = %d\n", outLen); gmsk_bitsToBytes(decoded, bytes, outLen); thumbDV_dump("TEST FEC Decode", bytes, outLen/8);memset(bytes,0, 660/8 * sizeof(unsigned char)); - - - output("True ^ True = %d\n", TRUE ^ TRUE); - output("True ^ False = %d\n", TRUE ^ FALSE); - output("False ^ True = %d\n", FALSE ^ TRUE); - output("False ^ False = %d\n", FALSE ^ FALSE); - - +// +// +// output("True ^ True = %d\n", TRUE ^ TRUE); +// output("True ^ False = %d\n", TRUE ^ FALSE); +// output("False ^ True = %d\n", FALSE ^ TRUE); +// output("False ^ False = %d\n", FALSE ^ FALSE); +// +// output("Generating Interleave Table\n"); +// +// uint32 j = 0; +// uint32 limit = 28; +// for ( j = 0 ; j < 24; j++ ) { +// +// if ( j < 12 ) limit = 28; +// else limit = 27; +// for ( i = 0; i < limit; i++ ) { +// +// output("%d,", j + (i * 24)); +// } +// } +// output("\nDONE\n"); }