mm structure initialisation

Ideally we should clear down the mm structure every loop of our
ModeA/C/S bit detector. However, we're getting 2Mbits of data per
second, and the structure is several tens of bytes long. Clearing down
every loop would require us to zero up to 100Mb per second. The memset
function may be fast, but it's still going to take up valuable processor
time.

So instead of clearing the whole structure every loop, just clear the
important parts.
This commit is contained in:
Malcolm Robb 2013-05-22 11:49:03 +01:00
parent 4a3113f65a
commit b481c76909

View file

@ -998,7 +998,6 @@ int detectModeA(uint16_t *m, struct modesMessage *mm)
if ((ModeABits < 3) || (ModeABits & 0xFFFF8808) || (ModeAErrs) ) if ((ModeABits < 3) || (ModeABits & 0xFFFF8808) || (ModeAErrs) )
{return (ModeABits = 0);} {return (ModeABits = 0);}
memset(mm, 0, sizeof(*mm));
fSig = (fSig + 0x7F) >> 8; fSig = (fSig + 0x7F) >> 8;
mm->signalLevel = ((fSig < 255) ? fSig : 255); mm->signalLevel = ((fSig < 255) ? fSig : 255);
@ -1718,8 +1717,6 @@ void decodeModesMessage(struct modesMessage *mm, unsigned char *msg) {
// Get the message type ASAP as other operations depend on this // Get the message type ASAP as other operations depend on this
mm->msgtype = msg[0] >> 3; // Downlink Format mm->msgtype = msg[0] >> 3; // Downlink Format
mm->msgbits = modesMessageLenByType(mm->msgtype); mm->msgbits = modesMessageLenByType(mm->msgtype);
mm->correctedbits = 0; // No errors fixed
mm->phase_corrected = 0;
mm->crc = modesChecksum(msg, mm->msgbits); mm->crc = modesChecksum(msg, mm->msgbits);
if ((mm->crc) && (Modes.fix_errors) && ((mm->msgtype == 17) || (mm->msgtype == 18))) { if ((mm->crc) && (Modes.fix_errors) && ((mm->msgtype == 17) || (mm->msgtype == 18))) {
@ -2288,11 +2285,14 @@ void applyPhaseCorrection(uint16_t *pPayload) {
* size 'mlen' bytes. Every detected Mode S message is convert it into a * size 'mlen' bytes. Every detected Mode S message is convert it into a
* stream of bits and passed to the function to display it. */ * stream of bits and passed to the function to display it. */
void detectModeS(uint16_t *m, uint32_t mlen) { void detectModeS(uint16_t *m, uint32_t mlen) {
struct modesMessage mm;
unsigned char msg[MODES_LONG_MSG_BYTES], *pMsg; unsigned char msg[MODES_LONG_MSG_BYTES], *pMsg;
uint16_t aux[MODES_LONG_MSG_SAMPLES]; uint16_t aux[MODES_LONG_MSG_SAMPLES];
uint32_t j; uint32_t j;
int use_correction = 0; int use_correction = 0;
memset(&mm, 0, sizeof(mm));
/* The Mode S preamble is made of impulses of 0.5 microseconds at /* The Mode S preamble is made of impulses of 0.5 microseconds at
* the following time offsets: * the following time offsets:
* *
@ -2318,7 +2318,6 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
*/ */
for (j = 0; j < mlen; j++) { for (j = 0; j < mlen; j++) {
int high, i, errors, errors56, errorsTy; int high, i, errors, errors56, errorsTy;
int good_message = 0;
uint16_t *pPreamble, *pPayload, *pPtr; uint16_t *pPreamble, *pPayload, *pPtr;
uint8_t theByte, theErrs; uint8_t theByte, theErrs;
int msglen, scanlen, sigStrength; int msglen, scanlen, sigStrength;
@ -2326,12 +2325,18 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
pPreamble = &m[j]; pPreamble = &m[j];
pPayload = &m[j+MODES_PREAMBLE_SAMPLES]; pPayload = &m[j+MODES_PREAMBLE_SAMPLES];
// Rather than clear the whole mm structure, just clear the parts which are required. The clear
// is required for every bit of the input stream, and we don't want to be memset-ing the whole
// modesMessage structure two million times per second if we don't have to..
mm.bFlags =
mm.crcok =
mm.correctedbits = 0;
if (!use_correction) // This is not a re-try with phase correction if (!use_correction) // This is not a re-try with phase correction
{ // so try to find a new preamble { // so try to find a new preamble
if (Modes.mode_ac) if (Modes.mode_ac)
{ {
struct modesMessage mm;
int ModeA = detectModeA(pPreamble, &mm); int ModeA = detectModeA(pPreamble, &mm);
if (ModeA) // We have found a valid ModeA/C in the data if (ModeA) // We have found a valid ModeA/C in the data
@ -2529,13 +2534,12 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
if ( (msglen) if ( (msglen)
&& (sigStrength > MODES_MSG_SQUELCH_LEVEL) && (sigStrength > MODES_MSG_SQUELCH_LEVEL)
&& (errors <= MODES_MSG_ENCODER_ERRS) ) { && (errors <= MODES_MSG_ENCODER_ERRS) ) {
struct modesMessage mm;
memset(&mm, 0, sizeof(mm));
// Set initial mm structure details // Set initial mm structure details
mm.timestampMsg = Modes.timestampBlk + (j*6); mm.timestampMsg = Modes.timestampBlk + (j*6);
sigStrength = (sigStrength + 0x7F) >> 8; sigStrength = (sigStrength + 0x7F) >> 8;
mm.signalLevel = ((sigStrength < 255) ? sigStrength : 255); mm.signalLevel = ((sigStrength < 255) ? sigStrength : 255);
mm.phase_corrected = use_correction;
// Decode the received message // Decode the received message
decodeModesMessage(&mm, msg); decodeModesMessage(&mm, msg);
@ -2575,9 +2579,6 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
// Skip this message if we are sure it's fine // Skip this message if we are sure it's fine
if (mm.crcok) { if (mm.crcok) {
j += (MODES_PREAMBLE_US+msglen)*2; j += (MODES_PREAMBLE_US+msglen)*2;
good_message = 1;
if (use_correction)
mm.phase_corrected = 1;
} }
// Pass data to the next layer // Pass data to the next layer
@ -2591,7 +2592,7 @@ void detectModeS(uint16_t *m, uint32_t mlen) {
} }
// Retry with phase correction if possible. // Retry with phase correction if possible.
if (!good_message && !use_correction && j && detectOutOfPhase(pPreamble)) { if (!mm.crcok && !mm.correctedbits && !use_correction && j && detectOutOfPhase(pPreamble)) {
use_correction = 1; j--; use_correction = 1; j--;
} else { } else {
use_correction = 0; use_correction = 0;