2020-08-20 17:47:32 +02:00
|
|
|
#include <mcp2515.h>
|
|
|
|
|
2020-09-11 21:32:23 +02:00
|
|
|
#include <obus_util.h>
|
2020-08-20 21:51:33 +02:00
|
|
|
#include "obus_can.h"
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-26 23:35:13 +02:00
|
|
|
// Chip select for the CAN module
|
2021-02-01 19:18:28 +01:00
|
|
|
#define PIN_MCP_CS 8
|
2020-08-26 23:35:13 +02:00
|
|
|
|
2020-08-26 21:13:16 +02:00
|
|
|
namespace obus_can {
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2021-02-01 19:18:28 +01:00
|
|
|
MCP2515 mcp2515(PIN_MCP_CS);
|
2020-08-26 21:13:16 +02:00
|
|
|
bool is_init = false;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
uint16_t _encode_can_id(struct module mod, bool priority) {
|
|
|
|
assert(mod.type <= 0b11);
|
|
|
|
|
|
|
|
/* b bb bbbbbbbb
|
|
|
|
* ↓ type module ID
|
|
|
|
* priority bit
|
|
|
|
*/
|
|
|
|
return \
|
|
|
|
((uint16_t) (priority ? CAN_DOMINANT : CAN_RECESSIVE) << 10) | \
|
|
|
|
((uint16_t) mod.type << 8) | \
|
|
|
|
(uint16_t) mod.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void _decode_can_id(uint16_t can_id, struct module *mod, bool *priority) {
|
|
|
|
*priority = ((can_id >> 10) & 1) == CAN_DOMINANT;
|
|
|
|
mod->type = (can_id >> 8) & 0b11;
|
|
|
|
mod->id = can_id & 0b11111111;
|
|
|
|
|
|
|
|
assert(mod->type <= 0b11);
|
|
|
|
}
|
|
|
|
|
2020-09-09 18:18:55 +02:00
|
|
|
uint8_t payload_type(uint8_t module_type, uint8_t module_id, uint8_t msg_type) {
|
2020-09-09 20:41:48 +02:00
|
|
|
if (module_type == OBUS_TYPE_CONTROLLER && module_id == OBUS_CONTROLLER_ID) {
|
2020-08-20 21:13:19 +02:00
|
|
|
switch (msg_type) {
|
|
|
|
case OBUS_MSGTYPE_C_ACK:
|
2020-09-11 21:10:47 +02:00
|
|
|
return OBUS_PAYLDTYPE_MODULEADDR;
|
2020-08-20 21:13:19 +02:00
|
|
|
case OBUS_MSGTYPE_C_HELLO:
|
|
|
|
return OBUS_PAYLDTYPE_EMPTY;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
case OBUS_MSGTYPE_C_GAMESTART:
|
|
|
|
case OBUS_MSGTYPE_C_STATE:
|
|
|
|
case OBUS_MSGTYPE_C_SOLVED:
|
|
|
|
case OBUS_MSGTYPE_C_TIMEOUT:
|
|
|
|
case OBUS_MSGTYPE_C_STRIKEOUT:
|
|
|
|
return OBUS_PAYLDTYPE_GAMESTATUS;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2021-02-03 01:25:03 +01:00
|
|
|
case OBUS_MSGTYPE_C_INFOSTART:
|
|
|
|
return OBUS_PAYLDTYPE_INFOSTART;
|
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
break;
|
|
|
|
}
|
2020-09-09 20:41:48 +02:00
|
|
|
} else if (module_type == OBUS_TYPE_INFO) {
|
2020-09-09 18:18:55 +02:00
|
|
|
// Info modules can only send 7 bytes of data
|
|
|
|
return OBUS_PAYLDTYPE_INFO;
|
2020-08-20 21:13:19 +02:00
|
|
|
// Module messages
|
|
|
|
} else {
|
|
|
|
switch (msg_type) {
|
|
|
|
case OBUS_MSGTYPE_M_STRIKE:
|
2020-08-25 21:47:58 +02:00
|
|
|
return OBUS_PAYLDTYPE_COUNT;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
case OBUS_MSGTYPE_M_HELLO:
|
|
|
|
case OBUS_MSGTYPE_M_SOLVED:
|
|
|
|
return OBUS_PAYLDTYPE_EMPTY;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-20 17:47:32 +02:00
|
|
|
}
|
2020-08-20 21:13:19 +02:00
|
|
|
}
|
2020-08-20 17:47:32 +02:00
|
|
|
|
|
|
|
|
2021-07-31 22:21:48 +02:00
|
|
|
bool init() {
|
2020-08-26 21:13:16 +02:00
|
|
|
is_init = true;
|
2020-08-20 21:13:19 +02:00
|
|
|
mcp2515.reset();
|
|
|
|
mcp2515.setBitrate(CAN_50KBPS);
|
2021-07-31 22:21:48 +02:00
|
|
|
mcp2515.setLoopbackMode();
|
|
|
|
struct can_frame test_msg;
|
|
|
|
test_msg.can_id = 0x01;
|
|
|
|
test_msg.can_dlc = 1;
|
|
|
|
test_msg.data[0] = 0x23;
|
|
|
|
mcp2515.sendMessage(&test_msg);
|
|
|
|
struct can_frame recv_msg;
|
|
|
|
delay(50);
|
|
|
|
if (mcp2515.readMessage(&recv_msg) == MCP2515::ERROR_OK && recv_msg.can_id == 0x01 && recv_msg.can_dlc == 1 && recv_msg.data[0] == 0x23) {
|
|
|
|
mcp2515.setNormalMode();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-20 17:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-26 21:13:16 +02:00
|
|
|
bool receive(struct message *msg) {
|
|
|
|
if (!is_init) {
|
|
|
|
Serial.println(F("E Call init first"));
|
2020-08-20 17:47:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct can_frame receive_frame;
|
|
|
|
|
|
|
|
memset(&receive_frame.data, 0, CAN_MAX_DLEN);
|
|
|
|
|
|
|
|
MCP2515::ERROR status = mcp2515.readMessage(&receive_frame);
|
|
|
|
if (status != MCP2515::ERROR_OK) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always at least OBUS message type required
|
|
|
|
if (receive_frame.can_dlc < 1) {
|
|
|
|
Serial.println(F("W Received illegal msg: payload <1"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t msg_type = receive_frame.data[0];
|
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
struct module from;
|
|
|
|
bool priority;
|
|
|
|
_decode_can_id(receive_frame.can_id, &from, &priority);
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
// Controller messages
|
2020-09-09 18:18:55 +02:00
|
|
|
switch (payload_type(from.type, from.id, msg_type)) {
|
2020-08-20 17:47:32 +02:00
|
|
|
case OBUS_PAYLDTYPE_EMPTY:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBUS_PAYLDTYPE_GAMESTATUS:
|
2021-02-01 15:59:46 +01:00
|
|
|
if (receive_frame.can_dlc < 8) {
|
|
|
|
Serial.println(F("W Received illegal gamestatus msg: payload <8"));
|
2020-08-20 17:47:32 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-01 18:51:02 +01:00
|
|
|
msg->gamestatus.time_left = unpack_4b_into_u32(&(receive_frame.data[1]));
|
|
|
|
msg->gamestatus.strikes = receive_frame.data[5];
|
|
|
|
msg->gamestatus.max_strikes = receive_frame.data[6];
|
|
|
|
msg->gamestatus.puzzle_modules_left = receive_frame.data[7];
|
2020-08-20 17:47:32 +02:00
|
|
|
break;
|
|
|
|
|
2020-08-25 21:47:58 +02:00
|
|
|
case OBUS_PAYLDTYPE_COUNT:
|
2020-09-07 19:05:54 +02:00
|
|
|
if (receive_frame.can_dlc < 2) {
|
|
|
|
Serial.println(F("W Received illegal count msg: payload <2"));
|
|
|
|
return false;
|
|
|
|
}
|
2020-08-25 21:47:58 +02:00
|
|
|
msg->count = receive_frame.data[1];
|
2020-08-20 17:47:32 +02:00
|
|
|
break;
|
|
|
|
|
2020-09-09 18:18:55 +02:00
|
|
|
case OBUS_PAYLDTYPE_INFO:
|
2020-09-09 22:00:59 +02:00
|
|
|
{
|
|
|
|
uint8_t data_len = receive_frame.can_dlc - 1;
|
|
|
|
memcpy(msg->infomessage.data, &(receive_frame.data[1]), data_len);
|
|
|
|
msg->infomessage.len = data_len;
|
2020-09-09 18:18:55 +02:00
|
|
|
}
|
|
|
|
break;
|
2020-09-11 21:10:47 +02:00
|
|
|
case OBUS_PAYLDTYPE_MODULEADDR:
|
|
|
|
{
|
|
|
|
if (receive_frame.can_dlc < 3) {
|
2021-02-03 01:25:03 +01:00
|
|
|
Serial.println(F("W Received illegal moduleaddr msg: payload <3"));
|
2020-09-11 21:10:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
msg->payload_address.type = receive_frame.data[1];
|
|
|
|
msg->payload_address.id = receive_frame.data[2];
|
|
|
|
}
|
|
|
|
break;
|
2021-02-03 01:25:03 +01:00
|
|
|
case OBUS_PAYLDTYPE_INFOSTART:
|
|
|
|
{
|
|
|
|
if (receive_frame.can_dlc < 5) {
|
|
|
|
Serial.println(F("W Received illegal infostart msg: payload <5"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
msg->infostart.seed = unpack_4b_into_u32(&(receive_frame.data[1]));
|
|
|
|
}
|
|
|
|
break;
|
2020-08-20 17:47:32 +02:00
|
|
|
default:
|
2020-08-20 21:13:19 +02:00
|
|
|
Serial.println(F("W Couldn't determine payload type"));
|
|
|
|
return false;
|
2020-08-20 17:47:32 +02:00
|
|
|
}
|
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
msg->from = from;
|
|
|
|
msg->priority = priority;
|
2020-08-20 17:47:32 +02:00
|
|
|
msg->msg_type = msg_type;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-27 03:29:16 +02:00
|
|
|
bool is_error_condition() {
|
|
|
|
return mcp2515.getInterrupts() & MCP2515::CANINTF_ERRIF;
|
|
|
|
}
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-26 21:13:16 +02:00
|
|
|
void send(struct message *msg) {
|
|
|
|
if (!is_init) {
|
|
|
|
Serial.println(F("E Call init first"));
|
2020-08-20 21:13:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
struct can_frame send_frame;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
memset(&send_frame.data, 0, CAN_MAX_DLEN);
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
uint8_t length = 1;
|
|
|
|
send_frame.data[0] = msg->msg_type;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-09-09 18:18:55 +02:00
|
|
|
uint8_t pyld_type = payload_type(msg->from.type, msg->from.id, msg->msg_type);
|
2020-09-07 19:05:54 +02:00
|
|
|
switch (pyld_type) {
|
2020-08-20 21:13:19 +02:00
|
|
|
case OBUS_PAYLDTYPE_EMPTY:
|
|
|
|
break;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
case OBUS_PAYLDTYPE_GAMESTATUS:
|
2020-09-11 21:32:23 +02:00
|
|
|
pack_u32_into_4b(&(send_frame.data[1]), msg->gamestatus.time_left);
|
2020-08-20 21:13:19 +02:00
|
|
|
send_frame.data[5] = msg->gamestatus.strikes;
|
|
|
|
send_frame.data[6] = msg->gamestatus.max_strikes;
|
2021-02-01 18:51:02 +01:00
|
|
|
send_frame.data[7] = msg->gamestatus.puzzle_modules_left;
|
2021-02-01 15:59:46 +01:00
|
|
|
length = 8;
|
2020-08-20 21:13:19 +02:00
|
|
|
break;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-25 21:47:58 +02:00
|
|
|
case OBUS_PAYLDTYPE_COUNT:
|
|
|
|
send_frame.data[1] = msg->count;
|
|
|
|
length = 2;
|
|
|
|
break;
|
|
|
|
|
2020-09-09 18:18:55 +02:00
|
|
|
case OBUS_PAYLDTYPE_INFO:
|
2020-09-09 22:00:59 +02:00
|
|
|
memcpy(&(send_frame.data[1]), msg->infomessage.data, msg->infomessage.len);
|
|
|
|
length = msg->infomessage.len + 1;
|
2020-09-09 18:18:55 +02:00
|
|
|
break;
|
2020-09-09 22:00:59 +02:00
|
|
|
|
2020-09-11 21:10:47 +02:00
|
|
|
case OBUS_PAYLDTYPE_MODULEADDR:
|
|
|
|
send_frame.data[1] = msg->payload_address.type;
|
|
|
|
send_frame.data[2] = msg->payload_address.id;
|
|
|
|
break;
|
|
|
|
|
2021-02-03 01:25:03 +01:00
|
|
|
case OBUS_PAYLDTYPE_INFOSTART:
|
|
|
|
pack_u32_into_4b(&(send_frame.data[1]), msg->gamestatus.time_left);
|
|
|
|
length = 5;
|
|
|
|
break;
|
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
default:
|
2020-09-07 19:05:54 +02:00
|
|
|
Serial.print(F("E Unknown payload type "));
|
|
|
|
Serial.println(pyld_type);
|
2020-08-20 21:13:19 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
send_frame.can_id = _encode_can_id(msg->from, msg->priority);
|
|
|
|
send_frame.can_dlc = length;
|
2020-08-20 17:47:32 +02:00
|
|
|
|
2020-08-20 21:13:19 +02:00
|
|
|
mcp2515.sendMessage(&send_frame);
|
2020-08-20 17:47:32 +02:00
|
|
|
}
|
2020-08-26 21:13:16 +02:00
|
|
|
|
|
|
|
}
|