2016-09-26 09:44:21 -07:00
/*
Speeduino - Simple engine management for the Arduino Mega 2560 platform
Copyright ( C ) Josh Stewart
A full copy of the license may be found in the projects root directory
can_comms was originally contributed by Darren Siepka
*/
/*
2020-02-27 15:18:37 -08:00
secondserial_command is called when a command is received from the secondary serial port
It parses the command and calls the relevant function .
2022-04-10 17:49:58 -07:00
can_command is called when a command is received by the onboard / attached canbus module
2020-02-27 15:18:37 -08:00
It parses the command and calls the relevant function .
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
2016-09-26 09:44:21 -07:00
*/
2018-09-23 16:33:49 -07:00
# include "globals.h"
2023-10-05 17:48:05 -07:00
# include "comms_secondary.h"
2023-11-02 21:37:29 -07:00
# include "comms_CAN.h"
2018-09-23 16:33:49 -07:00
# include "maths.h"
# include "errors.h"
2020-08-22 03:53:42 -07:00
# include "utilities.h"
2023-10-04 18:54:22 -07:00
# include "comms_legacy.h"
# include "logger.h"
2023-10-05 04:24:13 -07:00
# include "page_crc.h"
2023-11-20 21:47:24 -08:00
# include BOARD_H
2016-09-26 09:44:21 -07:00
2023-10-05 04:24:13 -07:00
uint8_t currentSecondaryCommand ;
2023-11-20 21:47:24 -08:00
SECONDARY_SERIAL_T * pSecondarySerial ;
2021-04-20 21:36:27 -07:00
2022-11-05 15:43:29 -07:00
void secondserial_Command ( void )
2016-09-26 09:44:21 -07:00
{
2023-10-05 17:48:05 -07:00
# if defined(secondarySerial_AVAILABLE)
if ( serialSecondaryStatusFlag = = SERIAL_INACTIVE ) { currentSecondaryCommand = secondarySerial . read ( ) ; }
2017-05-10 16:08:24 -07:00
2023-10-05 04:24:13 -07:00
switch ( currentSecondaryCommand )
2016-09-26 09:44:21 -07:00
{
2023-10-04 18:54:22 -07:00
case ' A ' :
2023-10-05 04:24:13 -07:00
// sends a fixed 75 bytes of data. Used by Real Dash (Among others)
2023-10-09 21:49:36 -07:00
if ( configPage9 . secondarySerialProtocol = = SECONDARY_SERIAL_PROTO_GENERIC_FIXED ) { sendValues ( 0 , CAN_PACKET_SIZE , 0x31 , secondarySerial , serialSecondaryStatusFlag , & getLegacySecondarySerialLogEntry ) ; } // Send values using the legacy fixed byte order
else { sendValues ( 0 , CAN_PACKET_SIZE , 0x31 , secondarySerial , serialSecondaryStatusFlag ) ; } //send values to serial3 using the order in the ini file
2023-10-05 04:24:13 -07:00
break ;
case ' b ' : // New EEPROM burn command to only burn a single page at a time
2023-10-05 17:48:05 -07:00
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
2023-10-05 04:24:13 -07:00
break ;
case ' B ' : // AS above but for the serial compatibility mode.
BIT_SET ( currentStatus . status4 , BIT_STATUS4_COMMS_COMPAT ) ; //Force the compat mode
2023-10-05 17:48:05 -07:00
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
2023-10-05 04:24:13 -07:00
break ;
case ' d ' : // Send a CRC32 hash of a given page
2023-10-05 17:48:05 -07:00
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
2023-10-05 04:24:13 -07:00
break ;
2016-09-26 09:44:21 -07:00
case ' G ' : // this is the reply command sent by the Can interface
2023-10-05 04:24:13 -07:00
serialSecondaryStatusFlag = SERIAL_COMMAND_INPROGRESS_LEGACY ;
byte destcaninchannel ;
2023-10-05 17:48:05 -07:00
if ( secondarySerial . available ( ) > = 9 )
2017-06-19 18:29:55 -07:00
{
2023-10-05 04:24:13 -07:00
serialSecondaryStatusFlag = SERIAL_INACTIVE ;
2023-10-05 17:48:05 -07:00
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 ;
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 ;
2017-01-10 23:30:46 -08:00
}
2017-05-31 01:57:36 -07:00
2023-10-05 17:48:05 -07:00
currentStatus . canin [ destcaninchannel ] = ( Ghigh < < 8 ) | Glow ;
}
2017-05-31 01:57:36 -07:00
else { } //continue as command request failed and/or data/device was not available
2017-06-19 18:29:55 -07:00
}
2023-10-05 04:24:13 -07:00
break ;
2016-09-26 09:44:21 -07:00
2019-08-10 05:32:05 -07:00
case ' k ' : //placeholder for new can interface (toucan etc) commands
break ;
2016-09-29 04:40:16 -07:00
case ' L ' :
2023-10-05 17:48:05 -07:00
{
//uint8_t Llength;
while ( secondarySerial . available ( ) = = 0 ) { }
uint8_t canListen = secondarySerial . read ( ) ;
2017-05-31 01:57:36 -07:00
2023-10-05 17:48:05 -07:00
if ( canListen = = 0 )
{
//command request failed and/or data/device was not available
2020-05-20 22:42:02 -07:00
break ;
2023-10-05 17:48:05 -07:00
}
while ( secondarySerial . available ( ) = = 0 ) { }
/*
Unclear what the below is trying to achieve . Commenting out for now to avoid compiler warnings for unused variables
Llength = secondarySerial . read ( ) ; // next the number of bytes expected value
uint8_t Lbuffer [ 8 ] ; //8 byte buffer to store incoming can data
2017-05-31 01:57:36 -07:00
2023-10-05 17:48:05 -07:00
for ( uint8_t Lcount = 0 ; Lcount < Llength ; Lcount + + )
{
while ( secondarySerial . available ( ) = = 0 ) { }
// receive all x bytes into "Lbuffer"
Lbuffer [ Lcount ] = secondarySerial . read ( ) ;
}
*/
break ;
}
2023-10-23 18:18:00 -07:00
case ' M ' :
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
break ;
2023-10-05 17:48:05 -07:00
2019-08-10 05:32:05 -07:00
case ' n ' : // sends the bytes of realtime values from the NEW CAN list
2023-10-09 21:49:36 -07:00
//sendValues(0, NEW_CAN_PACKET_SIZE, 0x32, secondarySerial, serialSecondaryStatusFlag); //send values to serial3
if ( configPage9 . secondarySerialProtocol = = SECONDARY_SERIAL_PROTO_GENERIC_FIXED ) { sendValues ( 0 , NEW_CAN_PACKET_SIZE , 0x32 , secondarySerial , serialSecondaryStatusFlag , & getLegacySecondarySerialLogEntry ) ; } // Send values using the legacy fixed byte order
else { sendValues ( 0 , NEW_CAN_PACKET_SIZE , 0x32 , secondarySerial , serialSecondaryStatusFlag ) ; } //send values to serial3 using the order in the ini file
2023-10-05 04:24:13 -07:00
break ;
2019-08-10 05:32:05 -07:00
2023-10-05 04:24:13 -07:00
case ' p ' :
2023-10-05 17:48:05 -07:00
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
2023-10-05 04:24:13 -07:00
break ;
2017-05-31 01:57:36 -07:00
2023-10-05 04:24:13 -07:00
case ' Q ' : // send code version
2023-10-05 17:48:05 -07:00
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
2023-10-05 04:24:13 -07:00
break ;
case ' r ' : //New format for the optimised OutputChannels over CAN
2023-10-05 17:48:05 -07:00
legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ;
2017-05-08 15:15:03 -07:00
break ;
2017-05-31 01:57:36 -07:00
2018-09-23 14:50:15 -07:00
case ' s ' : // send the "a" stream code version
2023-10-05 17:48:05 -07:00
secondarySerial . print ( F ( " Speeduino csx02019.8 " ) ) ;
2020-05-20 22:42:02 -07:00
break ;
2016-09-26 09:44:21 -07:00
2018-09-23 14:50:15 -07:00
case ' S ' : // send code version
2023-10-05 17:48:05 -07:00
if ( configPage9 . secondarySerialProtocol = = SECONDARY_SERIAL_PROTO_MSDROID ) { legacySerialHandler ( ' Q ' , secondarySerial , serialSecondaryStatusFlag ) ; } //Note 'Q', this is a workaround for msDroid
else { legacySerialHandler ( currentSecondaryCommand , secondarySerial , serialSecondaryStatusFlag ) ; }
2018-09-23 14:50:15 -07:00
2023-10-05 04:24:13 -07:00
break ;
2016-09-26 09:44:21 -07:00
2017-05-10 16:08:24 -07:00
case ' Z ' : //dev use
break ;
2017-05-31 01:57:36 -07:00
2016-09-26 09:44:21 -07:00
default :
2016-09-29 04:40:16 -07:00
break ;
2016-09-26 09:44:21 -07:00
}
2020-05-20 22:42:02 -07:00
# endif
2023-10-12 18:25:05 -07:00
}
2020-02-27 15:18:37 -08:00
2017-05-08 15:15:03 -07:00
// this routine sends a request(either "0" for a "G" , "1" for a "L" , "2" for a "R" to the Can interface or "3" sends the request via the actual local canbus
2017-11-01 13:17:47 -07:00
void sendCancommand ( uint8_t cmdtype , uint16_t canaddress , uint8_t candata1 , uint8_t candata2 , uint16_t sourcecanAddress )
2016-09-26 09:44:21 -07:00
{
2023-10-05 17:48:05 -07:00
# if defined(secondarySerial_AVAILABLE)
2016-09-26 09:44:21 -07:00
switch ( cmdtype )
{
case 0 :
2023-10-05 17:48:05 -07:00
secondarySerial . print ( " G " ) ;
secondarySerial . write ( canaddress ) ; //tscanid of speeduino device
secondarySerial . write ( candata1 ) ; // table id
secondarySerial . write ( candata2 ) ; //table memory offset
2017-05-31 01:57:36 -07:00
break ;
2017-05-10 16:08:24 -07:00
2017-05-31 01:57:36 -07:00
case 1 : //send request to listen for a can message
2023-10-05 17:48:05 -07:00
secondarySerial . print ( " L " ) ;
secondarySerial . write ( canaddress ) ; //11 bit canaddress of device to listen for
2017-05-31 01:57:36 -07:00
break ;
2017-04-08 16:48:39 -07:00
2017-06-19 18:29:55 -07:00
case 2 : // requests via serial3
2023-10-05 17:48:05 -07:00
secondarySerial . print ( " R " ) ; //send "R" to request data from the sourcecanAddress whose value is sent next
secondarySerial . write ( candata1 ) ; //the currentStatus.current_caninchannel
secondarySerial . write ( lowByte ( sourcecanAddress ) ) ; //send lsb first
secondarySerial . write ( highByte ( sourcecanAddress ) ) ;
2017-05-31 01:57:36 -07:00
break ;
2017-05-08 15:15:03 -07:00
case 3 :
//send to truecan send routine
2017-06-19 18:29:55 -07:00
//canaddress == speeduino canid, candata1 == canin channel dest, paramgroup == can address to request from
2020-02-27 15:18:37 -08:00
//This section is to be moved to the correct can output routine later
2021-12-22 18:46:13 -08:00
# if defined(NATIVE_CAN_AVAILABLE)
2020-01-24 04:10:25 -08:00
outMsg . id = ( canaddress ) ;
outMsg . len = 8 ;
outMsg . buf [ 0 ] = 0x0B ; //11;
2020-02-27 15:18:37 -08:00
outMsg . buf [ 1 ] = 0x15 ;
2020-01-24 04:10:25 -08:00
outMsg . buf [ 2 ] = candata1 ;
outMsg . buf [ 3 ] = 0x24 ;
outMsg . buf [ 4 ] = 0x7F ;
outMsg . buf [ 5 ] = 0x70 ;
outMsg . buf [ 6 ] = 0x9E ;
outMsg . buf [ 7 ] = 0x4D ;
2023-12-20 18:40:36 -08:00
CAN_write ( ) ;
2020-01-24 05:10:21 -08:00
# endif
2017-05-31 01:57:36 -07:00
break ;
2017-05-10 16:08:24 -07:00
2017-05-31 01:57:36 -07:00
default :
break ;
2017-02-07 20:40:44 -08:00
}
2021-04-20 23:33:19 -07:00
# else
UNUSED ( cmdtype ) ;
UNUSED ( canaddress ) ;
UNUSED ( candata1 ) ;
UNUSED ( candata2 ) ;
UNUSED ( sourcecanAddress ) ;
# endif
2016-09-26 09:44:21 -07:00
}