diff --git a/speeduino/board_same51.h b/speeduino/board_same51.h index e2d8b54f..bd0f3ecd 100644 --- a/speeduino/board_same51.h +++ b/speeduino/board_same51.h @@ -189,7 +189,7 @@ *********************************************************************************************************** * CAN / Second serial */ - Uart CANSerial (&sercom3, 0, 1, SERCOM_RX_PAD_1, UART_TX_PAD_0); + Uart secondarySerial (&sercom3, 0, 1, SERCOM_RX_PAD_1, UART_TX_PAD_0); #endif //CORE_SAMD21 #endif //SAMD21_H diff --git a/speeduino/board_stm32_generic.h b/speeduino/board_stm32_generic.h index ce98e3cd..2bff993a 100644 --- a/speeduino/board_stm32_generic.h +++ b/speeduino/board_stm32_generic.h @@ -219,9 +219,9 @@ * CAN / Second serial */ #if defined(STM32GENERIC) // STM32GENERIC core - SerialUART &CANSerial = Serial2; + SerialUART &secondarySerial = Serial2; #else //libmaple core aka STM32DUINO - HardwareSerial &CANSerial = Serial2; + HardwareSerial &secondarySerial = Serial2; #endif #endif //CORE_STM32 diff --git a/speeduino/board_stm32_official.h b/speeduino/board_stm32_official.h index 47dca806..5bccb3ee 100644 --- a/speeduino/board_stm32_official.h +++ b/speeduino/board_stm32_official.h @@ -334,7 +334,6 @@ void ignitionSchedule8Interrupt(HardwareTimer*); */ #if HAL_CAN_MODULE_ENABLED #define NATIVE_CAN_AVAILABLE -//HardwareSerial CANSerial(PD6, PD5); #include //This activates CAN1 interface on STM32, but it's named as Can0, because that's how Teensy implementation is done extern STM32_CAN Can0; diff --git a/speeduino/comms.cpp b/speeduino/comms.cpp index 14221e78..358c926f 100644 --- a/speeduino/comms.cpp +++ b/speeduino/comms.cpp @@ -8,7 +8,7 @@ A full copy of the license may be found in the projects root directory */ #include "globals.h" #include "comms.h" -#include "cancomms.h" +#include "comms_secondary.h" #include "storage.h" #include "maths.h" #include "utilities.h" diff --git a/speeduino/comms_legacy.cpp b/speeduino/comms_legacy.cpp index 77315f94..707fe5fc 100644 --- a/speeduino/comms_legacy.cpp +++ b/speeduino/comms_legacy.cpp @@ -8,7 +8,7 @@ A full copy of the license may be found in the projects root directory */ #include "globals.h" #include "comms_legacy.h" -#include "cancomms.h" +#include "comms_secondary.h" #include "storage.h" #include "maths.h" #include "utilities.h" @@ -659,7 +659,7 @@ void legacySerialHandler(byte cmd, Stream &targetPort, SerialStatus &targetStatu * This will "live" information from @ref currentStatus struct. * @param offset - Start field number * @param packetLength - Length of actual message (after possible ack/confirm headers) - * @param cmd - ??? - Will be used as some kind of ack on CANSerial + * @param cmd - ??? - Will be used as some kind of ack on secondarySerial * @param targetPort - The HardwareSerial device that will be transmitted to * @param targetStatusFlag - The status flag that will be set to indicate the status of the transmission * E.g. tuning sw command 'A' (Send all values) will send data from field number 0, LOG_ENTRY_SIZE fields. @@ -667,26 +667,26 @@ void legacySerialHandler(byte cmd, Stream &targetPort, SerialStatus &targetStatu */ void sendValues(uint16_t offset, uint16_t packetLength, byte cmd, Stream &targetPort, SerialStatus &targetStatusFlag) { - #if defined(CANSerial_AVAILABLE) - if (&targetPort == &CANSerial) + #if defined(secondarySerial_AVAILABLE) + if (&targetPort == &secondarySerial) { //CAN serial if( (configPage9.secondarySerialProtocol == SECONDARY_SERIAL_PROTO_GENERIC) || (configPage9.secondarySerialProtocol == SECONDARY_SERIAL_PROTO_REALDASH)) { if (cmd == 0x30) { - CANSerial.write("r"); //confirm cmd type - CANSerial.write(cmd); + secondarySerial.write("r"); //confirm cmd type + secondarySerial.write(cmd); } else if (cmd == 0x31) { - CANSerial.write("A"); // confirm command type + secondarySerial.write("A"); // confirm command type } else if (cmd == 0x32) { - CANSerial.write("n"); // confirm command type - CANSerial.write(cmd); // send command type , 0x32 (dec50) is ascii '0' - CANSerial.write(NEW_CAN_PACKET_SIZE); // send the packet size the receiving device should expect. + secondarySerial.write("n"); // confirm command type + secondarySerial.write(cmd); // send command type , 0x32 (dec50) is ascii '0' + secondarySerial.write(NEW_CAN_PACKET_SIZE); // send the packet size the receiving device should expect. } } } diff --git a/speeduino/cancomms.h b/speeduino/comms_secondary.h similarity index 73% rename from speeduino/cancomms.h rename to speeduino/comms_secondary.h index b51443fd..bd7741de 100644 --- a/speeduino/cancomms.h +++ b/speeduino/comms_secondary.h @@ -1,5 +1,5 @@ -#ifndef CANCOMMS_H -#define CANCOMMS_H +#ifndef COMMS_SECONDARY_H +#define COMMS_SECONDARY_H #define NEW_CAN_PACKET_SIZE 123 #define CAN_PACKET_SIZE 75 @@ -10,21 +10,21 @@ #define SECONDARY_SERIAL_PROTO_REALDASH 3 #if ( defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) ) - #define CANSerial_AVAILABLE - extern HardwareSerial &CANSerial; + #define secondarySerial_AVAILABLE + extern HardwareSerial &secondarySerial; #elif defined(CORE_STM32) - #define CANSerial_AVAILABLE + #define secondarySerial_AVAILABLE #ifndef HAVE_HWSERIAL2 //Hack to get the code to compile on BlackPills #define Serial2 Serial1 #endif #if defined(STM32GENERIC) // STM32GENERIC core - extern SerialUART &CANSerial; + extern SerialUART &secondarySerial; #else //libmaple core aka STM32DUINO - extern HardwareSerial &CANSerial; + extern HardwareSerial &secondarySerial; #endif #elif defined(CORE_TEENSY) - #define CANSerial_AVAILABLE - extern HardwareSerial &CANSerial; + #define secondarySerial_AVAILABLE + extern HardwareSerial &secondarySerial; #endif void secondserial_Command(void);//This is the heart of the Command Line Interpreter. All that needed to be done was to make it human readable. @@ -33,4 +33,4 @@ void sendCancommand(uint8_t cmdtype , uint16_t canadddress, uint8_t candata1, ui void obd_response(uint8_t therequestedPID , uint8_t therequestedPIDlow, uint8_t therequestedPIDhigh); void readAuxCanBus(); -#endif // CANCOMMS_H +#endif // COMMS_SECONDARY_H diff --git a/speeduino/cancomms.ino b/speeduino/comms_secondary.ino similarity index 85% rename from speeduino/cancomms.ino rename to speeduino/comms_secondary.ino index c029936e..52ae9b95 100644 --- a/speeduino/cancomms.ino +++ b/speeduino/comms_secondary.ino @@ -16,7 +16,7 @@ sendcancommand is called when a command is to be sent either to serial3 ,to the external Can interface, or to the onboard/attached can interface */ #include "globals.h" -#include "cancomms.h" +#include "comms_secondary.h" #include "maths.h" #include "errors.h" #include "utilities.h" @@ -25,86 +25,82 @@ sendcancommand is called when a command is to be sent either to serial3 #include "page_crc.h" uint8_t currentSecondaryCommand; -uint8_t currentCanPage = 1;//Not the same as the speeduino config page numbers -uint8_t nCanretry = 0; //no of retrys -uint8_t cancmdfail = 0; //command fail yes/no -uint8_t canlisten = 0; -uint8_t Lbuffer[8]; //8 byte buffer to store incoming can data -uint8_t Gdata[9]; -uint8_t Glow, Ghigh; #if ( defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) ) - HardwareSerial &CANSerial = Serial3; + HardwareSerial &secondarySerial = Serial3; #elif defined(CORE_STM32) #ifndef HAVE_HWSERIAL2 //Hack to get the code to compile on BlackPills #define Serial2 Serial1 #endif #if defined(STM32GENERIC) // STM32GENERIC core - SerialUART &CANSerial = Serial2; + SerialUART &secondarySerial = Serial2; #else //libmaple core aka STM32DUINO - HardwareSerial &CANSerial = Serial2; + HardwareSerial &secondarySerial = Serial2; #endif #elif defined(CORE_TEENSY) - HardwareSerial &CANSerial = Serial2; + HardwareSerial &secondarySerial = Serial2; #endif void secondserial_Command(void) { - #if defined(CANSerial_AVAILABLE) - if ( serialSecondaryStatusFlag == SERIAL_INACTIVE ) { currentSecondaryCommand = CANSerial.read(); } + #if defined(secondarySerial_AVAILABLE) + if ( serialSecondaryStatusFlag == SERIAL_INACTIVE ) { currentSecondaryCommand = secondarySerial.read(); } switch (currentSecondaryCommand) { case 'A': // sends a fixed 75 bytes of data. Used by Real Dash (Among others) //sendcanValues(0, CAN_PACKET_SIZE, 0x31, 1); //send values to serial3 - sendValues(0, CAN_PACKET_SIZE, 0x31, CANSerial, serialSecondaryStatusFlag); //send values to serial3 + sendValues(0, CAN_PACKET_SIZE, 0x31, secondarySerial, serialSecondaryStatusFlag); //send values to serial3 break; case 'b': // New EEPROM burn command to only burn a single page at a time - legacySerialHandler(currentSecondaryCommand, CANSerial, serialSecondaryStatusFlag); + legacySerialHandler(currentSecondaryCommand, secondarySerial, serialSecondaryStatusFlag); break; case 'B': // AS above but for the serial compatibility mode. BIT_SET(currentStatus.status4, BIT_STATUS4_COMMS_COMPAT); //Force the compat mode - legacySerialHandler(currentSecondaryCommand, CANSerial, serialSecondaryStatusFlag); + legacySerialHandler(currentSecondaryCommand, secondarySerial, serialSecondaryStatusFlag); break; case 'd': // Send a CRC32 hash of a given page - legacySerialHandler(currentSecondaryCommand, CANSerial, serialSecondaryStatusFlag); + legacySerialHandler(currentSecondaryCommand, secondarySerial, serialSecondaryStatusFlag); break; case 'G': // this is the reply command sent by the Can interface serialSecondaryStatusFlag = SERIAL_COMMAND_INPROGRESS_LEGACY; byte destcaninchannel; - if (CANSerial.available() >= 9) + if (secondarySerial.available() >= 9) { serialSecondaryStatusFlag = SERIAL_INACTIVE; - cancmdfail = CANSerial.read(); //0 == fail, 1 == good. - destcaninchannel = CANSerial.read(); // the input channel that requested the data value - if (cancmdfail != 0) - { // read all 8 bytes of data. - for (byte Gx = 0; Gx < 8; Gx++) // first two are the can address the data is from. next two are the can address the data is for.then next 1 or two bytes of data - { - Gdata[Gx] = CANSerial.read(); - } - Glow = Gdata[(configPage9.caninput_source_start_byte[destcaninchannel]&7)]; - if ((BIT_CHECK(configPage9.caninput_source_num_bytes,destcaninchannel) > 0)) //if true then num bytes is 2 - { - if ((configPage9.caninput_source_start_byte[destcaninchannel]&7) < 8) //you can't have a 2 byte value starting at byte 7(8 on the list) - { - Ghigh = Gdata[((configPage9.caninput_source_start_byte[destcaninchannel]&7)+1)]; - } - else{Ghigh = 0;} - } - else - { - Ghigh = 0; - } + uint8_t cmdSuccessful = secondarySerial.read(); //0 == fail, 1 == good. + destcaninchannel = secondarySerial.read(); // the input channel that requested the data value + if (cmdSuccessful != 0) + { // read all 8 bytes of data. + uint8_t Gdata[9]; + uint8_t Glow, Ghigh; - currentStatus.canin[destcaninchannel] = (Ghigh<<8) | Glow; + for (byte Gx = 0; Gx < 8; Gx++) // first two are the can address the data is from. next two are the can address the data is for.then next 1 or two bytes of data + { + Gdata[Gx] = secondarySerial.read(); + } + Glow = Gdata[(configPage9.caninput_source_start_byte[destcaninchannel]&7)]; + if ((BIT_CHECK(configPage9.caninput_source_num_bytes,destcaninchannel) > 0)) //if true then num bytes is 2 + { + if ((configPage9.caninput_source_start_byte[destcaninchannel]&7) < 8) //you can't have a 2 byte value starting at byte 7(8 on the list) + { + Ghigh = Gdata[((configPage9.caninput_source_start_byte[destcaninchannel]&7)+1)]; + } + else { Ghigh = 0; } + } + else + { + Ghigh = 0; } + currentStatus.canin[destcaninchannel] = (Ghigh<<8) | Glow; + } + else{} //continue as command request failed and/or data/device was not available } @@ -115,50 +111,56 @@ void secondserial_Command(void) break; case 'L': - uint8_t Llength; - while (CANSerial.available() == 0) { } - canlisten = CANSerial.read(); + { + //uint8_t Llength; + while (secondarySerial.available() == 0) { } + uint8_t canListen = secondarySerial.read(); - if (canlisten == 0) - { - //command request failed and/or data/device was not available - break; - } - - while (CANSerial.available() == 0) { } - Llength= CANSerial.read(); // next the number of bytes expected value - - for (uint8_t Lcount = 0; Lcount SERIAL_BUFFER_THRESHOLD) ) + if ( ((mainLoopCount & 31) == 1) or (secondarySerial.available() > SERIAL_BUFFER_THRESHOLD) ) { - if (CANSerial.available() > 0) { secondserial_Command(); } + if (secondarySerial.available() > 0) { secondserial_Command(); } } } #endif @@ -311,7 +311,7 @@ void loop(void) if (configPage9.enable_secondarySerial == 1) // megas only support can via secondary serial { sendCancommand(2,0,currentStatus.current_caninchannel,0,((configPage9.caninput_source_can_address[currentStatus.current_caninchannel]&2047)+0x100)); - //send an R command for data from caninput_source_address[currentStatus.current_caninchannel] from CANSERIAL + //send an R command for data from caninput_source_address[currentStatus.current_caninchannel] from secondarySerial } } else if (((configPage9.caninput_sel[currentStatus.current_caninchannel]&12) == 4)