Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
flxkrmr committed Feb 19, 2023
1 parent 91aa6cb commit a47b772
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
16 changes: 8 additions & 8 deletions src/Cart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void Cart::reset() {
frameNumber = 0;
wordBufferPointer = 0;
resetBuffer();

digitalWrite(pin_re, RE_READ);
}

void Cart::sendStartMessage() {
Expand Down Expand Up @@ -61,12 +63,10 @@ void Cart::setBaudrate(long baudrate) {


void Cart::loop() {

// if in diag param slot, we don't wait for bytes
// just send the parameter and return
if (mode == DIAG_PARAM_SLOT) {
if (enableDiagnosticParameterSending && diagnosticParameterPointer == (frameNumber-1)*2) {
//Serial.println("Sending diag params");
delayMicroseconds(delay.word);
digitalWrite(pin_re, RE_WRITE);
softwareSerial->write(diagnosticParameter[diagnosticParameterPointer]);
Expand Down Expand Up @@ -95,7 +95,7 @@ void Cart::loop() {
// buffer not full, wait for next byte
return;
}

// always look out for sync word and stop everything else if
// we find one
if (isBufferSync()) {
Expand All @@ -119,26 +119,26 @@ void Cart::loop() {
frameDone = true;
}

memcpy(idSlot, wordBuffer, 16);
memcpy(&idSlot, wordBuffer, 2);

// parity check
if (((idSlot->rpm & 0xF) ^ ((idSlot->rpm >> 4) & 0xF) ^ idSlot->frameNumber ^ 0xA) != idSlot->parity) {
if (((idSlot.rpm & 0xF) ^ ((idSlot.rpm >> 4) & 0xF) ^ idSlot.frameNumber ^ 0xA) != idSlot.parity) {
Serial.println("ID-Slot parity error");
frameNumber = 0;
mode = WAIT_SYNC;
break;
}

// if the we find the wrong frame number, we start again
if (frameNumber != idSlot->frameNumber) {
if (frameNumber != idSlot.frameNumber) {
frameNumber = 0;
mode = WAIT_SYNC;
break;
}

frameNumber++;

if (idSlot->frameNumber < 4) {
if (idSlot.frameNumber < 4) {
mode = DIAG_PARAM_SLOT;
} else {
mode = STATUS_SLOT;
Expand Down Expand Up @@ -168,7 +168,7 @@ void Cart::loop() {
}

void Cart::handleStatusSlot() {
switch (idSlot->frameNumber) {
switch (idSlot.frameNumber) {
case CURRENT_DIAGNOSTIC_MODE:
currentDiagnosticMode = wordBuffer[0];
break;
Expand Down
4 changes: 1 addition & 3 deletions src/Cart.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Cart {
unsigned int rpm : 8;
unsigned int frameNumber : 4;
unsigned int parity : 4;
};
} idSlot;

struct DclErrorFlagLow {
unsigned int loadAddrPartiy : 1;
Expand Down Expand Up @@ -56,8 +56,6 @@ class Cart {
// can be set to false, will be set true if current frame ends
bool frameDone = true;

IdSlot* idSlot;

Cart(SoftwareSerial* softwareSerial, uint8_t pin_re);
void reset();

Expand Down
7 changes: 5 additions & 2 deletions src/EecIv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void EecIv::mainLoop() {

case CHECK_IF_IN_DIAG_MODE:
cart->reset();
cart->setBaudrate(2400);
initTimeoutTimer();
currentState = WAIT_FOR_SYNC_2400;
break;
Expand All @@ -41,6 +42,7 @@ void EecIv::mainLoop() {

case CHANGE_BAUD_RATE_9600:
cart->setBaudrate(9600);
//cart->setBaudrate(19200);
initTimeoutTimer();
currentState = WAIT_FOR_SYNC_9600;
break;
Expand All @@ -50,7 +52,7 @@ void EecIv::mainLoop() {
currentState = REQUEST_BAUD_RATE_CHANGE;
} else {
if(exceededTimeout()) {
debugPrint("Exceeded fast sync timeout");
debugPrint("Exceeded waiting for sync in default baud");
currentState = SEND_START_MESSAGE;
}
}
Expand All @@ -72,14 +74,15 @@ void EecIv::mainLoop() {
break;
case CHANGE_BAUD_RATE_2400:
cart->setBaudrate(2400);
initTimeoutTimer();
currentState = WAIT_FOR_SYNC_2400;
break;
case WAIT_FOR_SYNC_2400:
if (cart->isSynced) {
currentState = REQUEST_CLEAR_DCL_ERRORS;
} else {
if(exceededTimeout()) {
debugPrint("Exceeded waiting for sync");
debugPrint("Exceeded waiting for sync 2400");
currentState = SEND_START_MESSAGE;
}
}
Expand Down

0 comments on commit a47b772

Please sign in to comment.