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
*/
/*
can_command is called when a command is received over serial3 from the Can interface
It parses the command and calls the relevant function
sendcancommand is called when a comman d is to be sent via serial3 to the Can interface
*/
2016-09-30 08:57:57 -07:00
void canCommand ( )
2016-09-26 09:44:21 -07:00
{
2017-06-02 14:09:36 -07:00
currentcanCommand = CANSerial . read ( ) ;
2017-05-10 16:08:24 -07:00
switch ( currentcanCommand )
2016-09-26 09:44:21 -07:00
{
2016-09-29 04:40:16 -07:00
case ' A ' : // sends the bytes of realtime values
2017-08-02 18:51:07 -07:00
sendValues ( 0 , packetSize , 0x30 , 3 ) ; //send values to serial3
2016-09-29 04:40:16 -07:00
break ;
2016-09-26 09:44:21 -07:00
case ' G ' : // this is the reply command sent by the Can interface
2017-08-02 18:51:07 -07:00
byte destcaninchannel ;
if ( CANSerial . available ( ) > = 10 )
{
2017-06-02 14:09:36 -07:00
cancmdfail = CANSerial . read ( ) ;
2017-08-02 18:51:07 -07:00
destcaninchannel = CANSerial . read ( ) ;
2017-04-08 16:48:39 -07:00
if ( cancmdfail ! = 0 )
2017-08-02 18:51:07 -07:00
{ // 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 [ ( configPage10 . caninput_param_start_byte [ destcaninchannel ] & 7 ) ] ;
if ( ( BIT_CHECK ( configPage10 . caninput_param_num_bytes , destcaninchannel ) ) ) //if true then num bytes is 2
{
if ( ( configPage10 . caninput_param_start_byte [ destcaninchannel ] & 7 ) < 8 ) //you cant have a 2 byte value starting at byte 7(8 on the list)
{
Ghigh = Gdata [ ( ( configPage10 . caninput_param_start_byte [ destcaninchannel ] & 7 ) + 1 ) ] ;
}
else { Ghigh = 0 ; }
}
2017-04-08 16:48:39 -07:00
else
2017-08-02 18:51:07 -07:00
{
Ghigh = 0 ;
}
2017-06-02 14:09:36 -07:00
2017-08-02 18:51:07 -07:00
currentStatus . canin [ destcaninchannel ] = ( Ghigh < < 8 ) | Glow ;
2017-01-10 23:30:46 -08:00
}
2017-06-02 14:09:36 -07:00
else { } //continue as command request failed and/or data/device was not available
2017-08-02 18:51:07 -07:00
if ( currentStatus . current_caninchannel < 15 ) // if channel is < 15 then we can do another one
2017-04-08 16:48:39 -07:00
{
currentStatus . current_caninchannel + + ; //inc to next channel
}
2017-06-02 14:09:36 -07:00
else
2017-04-08 16:48:39 -07:00
{
currentStatus . current_caninchannel = 0 ; //reset to start
2017-06-02 14:09:36 -07:00
}
2017-08-02 18:51:07 -07:00
}
2016-09-29 04:40:16 -07:00
break ;
2016-09-26 09:44:21 -07:00
2016-09-29 04:40:16 -07:00
case ' L ' :
uint8_t Llength ;
2017-06-02 14:09:36 -07:00
while ( CANSerial . available ( ) = = 0 ) { }
canlisten = CANSerial . read ( ) ;
2016-09-26 09:44:21 -07:00
if ( canlisten = = 0 )
{
//command request failed and/or data/device was not available
break ;
}
2017-06-02 14:09:36 -07:00
while ( CANSerial . available ( ) = = 0 ) { }
Llength = CANSerial . read ( ) ; // next the number of bytes expected value
2016-09-26 09:44:21 -07:00
for ( uint8_t Lcount = 0 ; Lcount < Llength ; Lcount + + )
2017-06-02 14:09:36 -07:00
{
while ( CANSerial . available ( ) = = 0 ) { }
// receive all x bytes into "Lbuffer"
Lbuffer [ Lcount ] = CANSerial . read ( ) ;
}
break ;
2017-05-08 15:15:03 -07:00
case ' r ' : //New format for the optimised OutputChannels
2017-08-02 18:51:07 -07:00
byte Cmd ;
2017-06-02 14:09:36 -07:00
if ( CANSerial . available ( ) > = 6 )
2017-05-08 15:15:03 -07:00
{
2017-06-02 14:09:36 -07:00
CANSerial . read ( ) ; //Read the $tsCanId
2017-08-02 18:51:07 -07:00
Cmd = CANSerial . read ( ) ;
2017-06-02 14:09:36 -07:00
uint16_t offset , length ;
2017-08-02 18:51:07 -07:00
if ( ( Cmd = = 0x30 ) | | ( ( Cmd > = 0x40 ) & & ( Cmd < 0x50 ) ) ) //Send output channels command 0x30 is 48dec, 0x40(64dec)-0x4F(79dec) are external can request
2017-06-02 14:09:36 -07:00
{
byte tmp ;
tmp = CANSerial . read ( ) ;
offset = word ( CANSerial . read ( ) , tmp ) ;
tmp = CANSerial . read ( ) ;
length = word ( CANSerial . read ( ) , tmp ) ;
2017-08-02 18:51:07 -07:00
sendValues ( offset , length , Cmd , 3 ) ;
//Serial.print(Cmd);
2017-06-02 14:09:36 -07:00
}
else
{
//No other r/ commands should be called
}
2017-05-08 15:15:03 -07:00
}
break ;
2017-06-02 14:09:36 -07:00
2016-09-26 09:44:21 -07:00
case ' S ' : // send code version
2017-06-02 14:09:36 -07:00
for ( unsigned int sig = 0 ; sig < sizeof ( displaySignature ) - 1 ; sig + + )
{
CANSerial . write ( displaySignature [ sig ] ) ;
2016-09-29 04:40:16 -07:00
}
//Serial3.print("speeduino 201609-dev");
break ;
2016-09-26 09:44:21 -07:00
case ' Q ' : // send code version
2017-06-02 14:09:36 -07:00
for ( unsigned int revn = 0 ; revn < sizeof ( TSfirmwareVersion ) - 1 ; revn + + )
{
CANSerial . write ( TSfirmwareVersion [ revn ] ) ;
2016-09-29 04:40:16 -07:00
}
//Serial3.print("speeduino 201609-dev");
break ;
2016-09-26 09:44:21 -07:00
2017-05-10 16:08:24 -07:00
case ' Z ' : //dev use
break ;
2017-06-02 14:09: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
}
}
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-04-08 16:48:39 -07:00
void sendCancommand ( uint8_t cmdtype , uint16_t canaddress , uint8_t candata1 , uint8_t candata2 , uint16_t paramgroup )
2016-09-26 09:44:21 -07:00
{
switch ( cmdtype )
{
case 0 :
2017-06-02 14:09:36 -07:00
CANSerial . print ( " G " ) ;
CANSerial . write ( canaddress ) ; //tscanid of speeduino device
CANSerial . write ( candata1 ) ; // table id
CANSerial . write ( candata2 ) ; //table memory offset
break ;
2017-05-10 16:08:24 -07:00
2017-06-02 14:09:36 -07:00
case 1 : //send request to listen for a can message
CANSerial . print ( " L " ) ;
CANSerial . write ( canaddress ) ; //11 bit canaddress of device to listen for
break ;
2017-04-08 16:48:39 -07:00
2017-08-02 18:51:07 -07:00
case 2 : // requests via serial3
CANSerial . print ( " R " ) ; //send "R" to request data from the parmagroup can address whos value is sent next
CANSerial . write ( candata1 ) ; //the currentStatus.current_caninchannel
CANSerial . write ( lowByte ( paramgroup ) ) ; //send lsb first
CANSerial . write ( highByte ( paramgroup ) ) ;
2017-06-02 14:09:36 -07:00
break ;
2017-05-08 15:15:03 -07:00
case 3 :
//send to truecan send routine
2017-08-02 18:51:07 -07:00
//canaddress == speeduino canid, candata1 == canin channel dest, paramgroup == can address to request from
2017-06-02 14:09:36 -07:00
break ;
2017-05-10 16:08:24 -07:00
2017-06-02 14:09:36 -07:00
default :
break ;
2017-02-07 20:40:44 -08:00
}
2016-09-26 09:44:21 -07:00
}