reformatted the NmraDcc.h & NmraDcc.cpp files using the astyle rules in the new file: .astyle as per the PR from David Zuhn (davidzuhn)

This commit is contained in:
Alex Shepherd
2021-05-09 16:56:08 +12:00
parent ad5ce96253
commit 0a72fc610b
2 changed files with 2518 additions and 2465 deletions

View File

@@ -369,7 +369,8 @@ void ExternalInterruptHandler(void)
static byte halfBit, DCC_IrqRunning, preambleBitCount;
unsigned int actMicros, bitMicros;
#ifdef ALLOW_NESTED_IRQ
if ( DCC_IrqRunning ) {
if (DCC_IrqRunning)
{
// nested DCC IRQ - obviously there are glitches
// ignore this interrupt and increment glitchcounter
CLR_TP3;
@@ -383,23 +384,28 @@ void ExternalInterruptHandler(void)
actMicros = micros();
bitMicros = actMicros-lastMicros;
CLR_TP3; SET_TP3;
CLR_TP3;
SET_TP3;
#ifdef __AVR_MEGA__
if ( bitMicros < bitMin || ( DccRx.State != WAIT_START_BIT && (*DccProcState.ExtIntPort & DccProcState.ExtIntMask) != (ISRLevel) ) ) {
if (bitMicros < bitMin || (DccRx.State != WAIT_START_BIT && (*DccProcState.ExtIntPort & DccProcState.ExtIntMask) != (ISRLevel)))
{
#else
if ( bitMicros < bitMin || ( DccRx.State != WAIT_START_BIT && digitalRead( DccProcState.ExtIntPinNum ) != (ISRLevel) ) ) {
if (bitMicros < bitMin || (DccRx.State != WAIT_START_BIT && digitalRead (DccProcState.ExtIntPinNum) != (ISRLevel)))
{
#endif
// too short - my be false interrupt due to glitch or false protocol or level does not match RISING / FALLING edge -> ignore this IRQ
CLR_TP3;
SET_TP4; /*delayMicroseconds(1); */ CLR_TP4;
return; //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> abort IRQ
}
CLR_TP3; SET_TP3;
CLR_TP3;
SET_TP3;
lastMicros = actMicros;
#ifndef SUPPORT_ZERO_BIT_STRETCHING
//if ( bitMicros > MAX_ZEROBITFULL ) {
if ( bitMicros > (bitMax*2) ) {
if (bitMicros > (bitMax*2))
{
// too long - my be false protocol -> start over
DccRx.State = WAIT_PREAMBLE ;
DccRx.BitCount = 0 ;
@@ -446,9 +452,11 @@ void ExternalInterruptHandler(void)
#ifndef SYNC_ALWAYS
case WAIT_START_BIT_FULL:
// wait for startbit without level checking
if ( !DccBitVal ) {
if (!DccBitVal)
{
// we got the startbit
CLR_TP2;CLR_TP1;
CLR_TP2;
CLR_TP1;
DccRx.State = WAIT_DATA ;
CLR_TP1;
// initialize packet buffer
@@ -465,25 +473,31 @@ void ExternalInterruptHandler(void)
#endif
case WAIT_START_BIT:
// we are looking for first half "0" bit after preamble
switch ( halfBit ) {
switch (halfBit)
{
case 0:
// check first part
if ( DccBitVal ) {
if (DccBitVal)
{
// is still 1-bit (Preamble)
halfBit=1;
bit1=bitMicros;
} else {
}
else
{
// was "0" half bit, maybe the startbit
halfBit = 4;
}
break;
case 1: // previous halfbit was '1'
if ( DccBitVal ) {
if (DccBitVal)
{
// its a '1' halfBit -> we are still in the preamble
halfBit = 0;
bit2=bitMicros;
preambleBitCount++;
if( abs(bit2-bit1) > MAX_BITDIFF ) {
if (abs (bit2-bit1) > MAX_BITDIFF)
{
// the length of the 2 halfbits differ too much -> wrong protokoll
DccRx.State = WAIT_PREAMBLE;
bitMax = MAX_PRAEAMBEL;
@@ -506,7 +520,9 @@ void ExternalInterruptHandler(void)
SET_TP3;
CLR_TP4;
}
} else {
}
else
{
// first '0' half detected in second halfBit
// wrong sync or not a DCC protokoll
CLR_TP3;
@@ -515,18 +531,23 @@ void ExternalInterruptHandler(void)
}
break;
case 3: // previous halfbit was '0' in second halfbit
if ( DccBitVal ) {
if (DccBitVal)
{
// its a '1' halfbit -> we got only a half '0' bit -> cannot be DCC
DccRx.State = WAIT_PREAMBLE;
bitMax = MAX_PRAEAMBEL;
bitMin = MIN_ONEBITFULL;
preambleBitCount = 0;
// SET_TP2; CLR_TP2;
} else {
}
else
{
// we got two '0' halfbits -> it's the startbit
// but sync is NOT ok, change IRQ edge.
CLR_TP2;CLR_TP1;
if ( ISREdge == RISING ) ISREdge = FALLING; else ISREdge = RISING;
CLR_TP2;
CLR_TP1;
if (ISREdge == RISING) ISREdge = FALLING;
else ISREdge = RISING;
DccRx.State = WAIT_DATA ;
CLR_TP1;
bitMax = MAX_ONEBITFULL;
@@ -559,17 +580,22 @@ void ExternalInterruptHandler(void)
break;
case 4: // previous (first) halfbit was 0
// if this halfbit is 0 too, we got the startbit
if ( DccBitVal ) {
if (DccBitVal)
{
// second halfbit is 1 -> unknown protokoll
DccRx.State = WAIT_PREAMBLE;
bitMax = MAX_PRAEAMBEL;
bitMin = MIN_ONEBITFULL;
preambleBitCount = 0;
CLR_TP2;CLR_TP1;
CLR_TP2;
CLR_TP1;
DccRx.BitCount = 0;
} else {
}
else
{
// we got the startbit
CLR_TP2;CLR_TP1;
CLR_TP2;
CLR_TP1;
DccRx.State = WAIT_DATA ;
CLR_TP1;
bitMax = MAX_ONEBITFULL;
@@ -634,16 +660,20 @@ void ExternalInterruptHandler(void)
break;
case WAIT_END_BIT:
SET_TP2;CLR_TP2;
SET_TP2;
CLR_TP2;
DccRx.BitCount++;
if( DccBitVal ) { // End of packet?
CLR_TP3; SET_TP4;
if (DccBitVal) // End of packet?
{
CLR_TP3;
SET_TP4;
DccRx.State = WAIT_PREAMBLE ;
DccRx.BitCount = 0 ;
bitMax = MAX_PRAEAMBEL;
bitMin = MIN_ONEBITFULL;
SET_TP1;
if ( DccRx.chkSum == 0 ) {
if (DccRx.chkSum == 0)
{
// Packet is valid
#ifdef ESP32
portENTER_CRITICAL_ISR (&mux);
@@ -655,7 +685,9 @@ void ExternalInterruptHandler(void)
#endif
// SET_TP2; CLR_TP2;
preambleBitCount = 0 ;
} else {
}
else
{
// Wrong checksum
CLR_TP1;
#ifdef DCC_DBGVAR
@@ -664,8 +696,11 @@ void ExternalInterruptHandler(void)
#endif
}
SET_TP3; CLR_TP4;
} else { // Get next Byte
SET_TP3;
CLR_TP4;
}
else // Get next Byte
{
// KGW - Abort immediately if packet is too long.
if (DccRx.PacketBuf.Size == MAX_DCC_MESSAGE_LEN) // Packet is too long - abort
{
@@ -688,18 +723,23 @@ void ExternalInterruptHandler(void)
// we always search for a preamble ( ( 10 or more consecutive 1 bits )
// if we found it within a packet, the packet decoding is aborted because
// that much one bits cannot be valid in a packet.
if ( DccRx.State != WAIT_START_BIT ) {
if (DccRx.State != WAIT_START_BIT)
{
if (DccBitVal)
{
preambleBitCount++;
//SET_TP2;
if( preambleBitCount > 10 ) {
if (preambleBitCount > 10)
{
CLR_TP2;
#ifndef SYNC_ALWAYS
if ( DccRx.chkSum == 0 ) {
if (DccRx.chkSum == 0)
{
// sync must be correct if chksum was ok, no need to check sync
DccRx.State = WAIT_START_BIT_FULL;
} else {
}
else
{
#endif
DccRx.State = WAIT_START_BIT ;
SET_TP2;
@@ -724,7 +764,9 @@ void ExternalInterruptHandler(void)
}
#endif
}
} else {
}
else
{
CLR_TP1;
preambleBitCount = 0 ;
// SET_TP2; CLR_TP2;
@@ -1357,14 +1399,19 @@ void execDccProcessor( DCC_MSG * pDccMsg )
// If we're filtering addresses, does the address match our address or is it a broadcast address? If NOT then return
if (DccProcState.Flags & FLAGS_MY_ADDRESS_ONLY)
{
if( DccProcState.Flags & FLAGS_OUTPUT_ADDRESS_MODE ) {
if (DccProcState.Flags & FLAGS_OUTPUT_ADDRESS_MODE)
{
DB_PRINT (" AddrChk: OAddr:%d, BAddr:%d, myAddr:%d Chk=%d", OutputAddress, BoardAddress, getMyAddr(), OutputAddress != getMyAddr());
if ( OutputAddress != getMyAddr() && OutputAddress < 2045 ) {
if (OutputAddress != getMyAddr() && OutputAddress < 2045)
{
DB_PRINT (" eDP: OAddr:%d, myAddr:%d - no match", OutputAddress, getMyAddr());
return;
}
} else {
if( ( BoardAddress != getMyAddr() ) && ( BoardAddress < 511 ) ) {
}
else
{
if ( (BoardAddress != getMyAddr()) && (BoardAddress < 511))
{
DB_PRINT (" eDP: BAddr:%d, myAddr:%d - no match", BoardAddress, getMyAddr());
return;
}
@@ -1517,7 +1564,8 @@ void NmraDcc::pin( uint8_t ExtIntNum, uint8_t ExtIntPinNum, uint8_t EnablePullup
// because STM32F1 has a NVIC we must set interuptpriorities
const nvic_irq_num irqNum2nvic[] = { NVIC_EXTI0, NVIC_EXTI1, NVIC_EXTI2, NVIC_EXTI3, NVIC_EXTI4,
NVIC_EXTI_9_5, NVIC_EXTI_9_5, NVIC_EXTI_9_5, NVIC_EXTI_9_5, NVIC_EXTI_9_5,
NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10 };
NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10, NVIC_EXTI_15_10
};
exti_num irqNum = (exti_num) (PIN_MAP[ExtIntPinNum].gpio_bit);
// DCC-Input IRQ must be able to interrupt other long low priority ( level15 ) IRQ's

View File

@@ -132,7 +132,8 @@ typedef struct
#define MAXCV E2END // the upper limit of the CV value currently defined to max memory.
#endif
typedef enum {
typedef enum
{
CV29_LOCO_DIR = 0b00000001, /** bit 0: Locomotive Direction: "0" = normal, "1" = reversed */
CV29_F0_LOCATION = 0b00000010, /** bit 1: F0 location: "0" = bit 4 in Speed and Direction instructions, "1" = bit 4 in function group one instruction */
CV29_APS = 0b00000100, /** bit 2: Alternate Power Source (APS) "0" = NMRA Digital only, "1" = Alternate power source set by CV12 */
@@ -143,7 +144,8 @@ typedef enum {
CV29_ACCESSORY_DECODER = 0b10000000, /** bit 7: "0" = Multi-Function Decoder Mode "1" = Accessory Decoder Mode */
} CV_29_BITS;
typedef enum {
typedef enum
{
#ifdef NMRA_DCC_ENABLE_14_SPEED_STEP_MODE
SPEED_STEP_14 = 15, /**< ESTOP=0, 1 to 15 */
#endif
@@ -151,12 +153,14 @@ typedef enum {
SPEED_STEP_128 = 127 /**< ESTOP=0, 1 to 127 */
} DCC_SPEED_STEPS;
typedef enum {
typedef enum
{
DCC_DIR_REV = 0, /** The locomotive to go in the reverse direction */
DCC_DIR_FWD = 1, /** The locomotive should move in the forward direction */
} DCC_DIRECTION;
typedef enum {
typedef enum
{
DCC_ADDR_SHORT, /** Short address is used. The range is 0 to 127. */
DCC_ADDR_LONG, /** Long Address is used. The range is 1 to 10239 */
} DCC_ADDR_TYPE;
@@ -209,7 +213,8 @@ typedef enum
//#define DCC_DBGVAR
#ifdef DCC_DBGVAR
typedef struct countOf_t {
typedef struct countOf_t
{
unsigned long Tel;
unsigned long Err;
} countOf_t ;