HeapCrash / Challenges / CAN node goes silent for good

CAN node goes silent for good

A controller sends an 8-byte status frame every 10 ms on a 500 kbit/s CAN bus (40 MHz CAN clock, 16 time quanta per bit). During a service visit a technician hot-plugged a node with the wrong bit timing, and the bus was full of errors for about a second. Afterwards our controller never transmitted again, its control task froze until a power cycle, and the service log showed no CAN errors at all.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

void MX_CAN1_Init(void)
{
    hcan1.Instance = CAN1;
    hcan1.Init.Prescaler = 5;
    hcan1.Init.Mode = CAN_MODE_NORMAL;
    hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;
    hcan1.Init.TimeSeg1 = CAN_BS1_13TQ;
    hcan1.Init.TimeSeg2 = CAN_BS2_2TQ;
    hcan1.Init.AutoBusOff = DISABLE;
    hcan1.Init.AutoRetransmission = ENABLE;
    HAL_CAN_Init(&hcan1);
    HAL_CAN_Start(&hcan1);
}

bool can_send_status(const uint8_t d[8])
{
    CAN_TxHeaderTypeDef h = { .StdId = 0x321, .IDE = CAN_ID_STD, .RTR = CAN_RTR_DATA, .DLC = 8 };
    uint32_t mb;
    while (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) == 0) { }
    HAL_CAN_AddTxMessage(&hcan1, &h, d, &mb);
    return true;
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs