Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions inc/slcan.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,41 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len);
#define SLCAN_STD_ID_LEN 3
#define SLCAN_EXT_ID_LEN 8

/**
* Serial CAN message types
*
* The SLCAN protocol was specified by Lawicel AB, Sweden,
* for their CAN232 and CANUSB adapters.
* Several products exist on the market,
* which use the protocol and add their own extensions,
* including CANtact, USBTIN and Mictronics USB-CAN.
*
* @attention
* The following specification collisions exist:
* 'A': between Lawicel CAN232 and Mictronics CAN-USB
* 'm': between Lawicel CAN232 and CANtact
* 'M': between Lawicel CAN232 and CANtact
*/
enum slcan_message_type {
SLCAN_OPEN_CHANNEL = 'O',
SLCAN_CLOSE_CHANNEL = 'C',

SLCAN_GET_HARDWARE_VERSION = 'V',

SLCAN_SET_BITRATE_CANONICAL = 'S',
SLCAN_SET_BITRATE_ARBITRARY = 's',

SLCAN_TRANSMIT_STANDARD = 't',
SLCAN_TRANSMIT_EXTENDED = 'T',
SLCAN_TRANSMIT_REQUEST_STANDARD = 'r',
SLCAN_TRANSMIT_REQUEST_EXTENDED = 'R',

SLCAN_FIFO_POLL_ALL = 'A',

CANTACT_SET_MODE1 = 'm',
CANTACT_SET_MODE2 = 'M',

MICTRONICS_GET_ERROR = 'E',
};

#endif // _SLCAN_H
26 changes: 12 additions & 14 deletions src/slcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
// Process command
switch(buf[0])
{
case 'O':
// Open channel command
case SLCAN_OPEN_CHANNEL:
can_enable();
return 0;

case 'C':
// Close channel command
case SLCAN_CLOSE_CHANNEL:
can_disable();
return 0;

case 'S':
case SLCAN_SET_BITRATE_CANONICAL:
// Set bitrate command

// Check for valid bitrate
Expand All @@ -131,8 +129,8 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
can_set_bitrate(buf[1]);
return 0;

case 'm':
case 'M':
case CANTACT_SET_MODE1:
case CANTACT_SET_MODE2:
// Set mode command
if (buf[1] == 1)
{
Expand All @@ -145,7 +143,7 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
return 0;

case 'a':
case 'A':
case SLCAN_FIFO_POLL_ALL:
// Set autoretry command
if (buf[1] == 1)
{
Expand All @@ -157,7 +155,7 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
}
return 0;

case 'V':
case SLCAN_GET_HARDWARE_VERSION:
{
// Report firmware version and remote
char* fw_id = GIT_VERSION " " GIT_REMOTE "\r";
Expand All @@ -166,7 +164,7 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
}

// Nonstandard!
case 'E':
case MICTRONICS_GET_ERROR:
{
// Report error register
char errstr[64] = {0};
Expand All @@ -175,16 +173,16 @@ int8_t slcan_parse_str(uint8_t *buf, uint8_t len)
return 0;
}

case 'T':
case SLCAN_TRANSMIT_EXTENDED:
frame_header.IDE = CAN_ID_EXT;
case 't':
case SLCAN_TRANSMIT_STANDARD:
// Transmit data frame command
frame_header.RTR = CAN_RTR_DATA;
break;

case 'R':
case SLCAN_TRANSMIT_REQUEST_EXTENDED:
frame_header.IDE = CAN_ID_EXT;
case 'r':
case SLCAN_TRANSMIT_REQUEST_STANDARD:
// Transmit remote frame command
frame_header.RTR = CAN_RTR_REMOTE;
break;
Expand Down