Check initialization of CAN module (untested)
This commit is contained in:
parent
5b63d93df3
commit
124e0bb43a
3 changed files with 32 additions and 17 deletions
|
@ -76,11 +76,24 @@ uint8_t payload_type(uint8_t module_type, uint8_t module_id, uint8_t msg_type) {
|
|||
}
|
||||
|
||||
|
||||
void init() {
|
||||
bool init() {
|
||||
is_init = true;
|
||||
mcp2515.reset();
|
||||
mcp2515.setBitrate(CAN_50KBPS);
|
||||
mcp2515.setNormalMode();
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -91,8 +91,10 @@ uint8_t payload_type(uint8_t module_type, uint8_t message_type);
|
|||
|
||||
/**
|
||||
* Initialize the CAN controller for OBUS messaging
|
||||
*
|
||||
* @return true if initialization and self-test succeeded, false otherwise
|
||||
*/
|
||||
void init();
|
||||
bool init();
|
||||
/**
|
||||
* Receive a message
|
||||
*
|
||||
|
|
|
@ -76,6 +76,16 @@ void _setLedBlink(struct color color, uint16_t delay) {
|
|||
_ledLoop();
|
||||
}
|
||||
|
||||
void blink_error(String message) {
|
||||
bool blink = false;
|
||||
while (true) {
|
||||
digitalWrite(PIN_LED_RED, blink);
|
||||
digitalWrite(PIN_LED_GREEN, blink);
|
||||
blink = !blink;
|
||||
delay(blink ? BLINK_DELAY_SLOW : BLINK_DELAY_FAST);
|
||||
Serial.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
void _resetState() {
|
||||
strike_count = 0;
|
||||
|
@ -93,10 +103,11 @@ void _resetState() {
|
|||
void setup(uint8_t type, uint8_t id) {
|
||||
this_module.type = type;
|
||||
this_module.id = id;
|
||||
|
||||
obus_can::init();
|
||||
|
||||
_resetState();
|
||||
|
||||
if (!obus_can::init()) {
|
||||
blink_error(F("CAN init failed"));
|
||||
}
|
||||
}
|
||||
|
||||
void empty_callback_info(uint8_t info_id, uint8_t infomessage[7]) {
|
||||
|
@ -113,17 +124,6 @@ void empty_callback_state(uint32_t time_left, uint8_t strikes, uint8_t max_strik
|
|||
(void)puzzle_modules_left;
|
||||
}
|
||||
|
||||
void blink_error(String message) {
|
||||
bool blink = false;
|
||||
while (true) {
|
||||
digitalWrite(PIN_LED_RED, blink);
|
||||
digitalWrite(PIN_LED_GREEN, blink);
|
||||
blink = !blink;
|
||||
delay(blink ? BLINK_DELAY_SLOW : BLINK_DELAY_FAST);
|
||||
Serial.println(message);
|
||||
}
|
||||
}
|
||||
|
||||
bool loopPuzzle(obus_can::message* message, void (*callback_game_start)(uint8_t puzzle_modules), void (*callback_game_stop)(), void (*callback_info)(uint8_t info_id, uint8_t infomessage[7]), void (*callback_state)(uint32_t time_left, uint8_t strikes, uint8_t max_strikes, uint8_t puzzle_modules_left)) {
|
||||
// TODO this can be more efficient by only enabling error interrupts and
|
||||
// reacting to the interrupt instead of checking if the flag is set in a loop
|
||||
|
|
Loading…
Reference in a new issue