Example #1
0
void can_flush(void)
{
	int busy;
	time_t deadline = time_get(10); //max wait 10 ms
	do {
		busy = 0;
		busy += (CAN_TransmitStatus(CAN1, 0) == CANTXPENDING);
		busy += (CAN_TransmitStatus(CAN1, 1) == CANTXPENDING);
		busy += (CAN_TransmitStatus(CAN1, 2) == CANTXPENDING);
		if(!busy)
			break;
	} while(time_left(deadline) > 0);

	CAN_CancelTransmit(CAN1, 0);
	CAN_CancelTransmit(CAN1, 1);
	CAN_CancelTransmit(CAN1, 2);
	CAN_FIFORelease(CAN1, CAN_FIFO0);
#ifndef CONFIG_CAN_ENHANCED
	CAN_FIFORelease(CAN1, CAN_FIFO1);
#endif

#if ENABLE_CAN_INT
	circle_header = 0;
	circle_tailer = 0;
	circle_number = 0;
#endif
}
int CAN_device_tx_msg(uint8_t channel, CAN_msg * msg, unsigned int timeoutMs)
{
        CanTxMsg TxMessage;

        /* Transmit Structure preparation */
        if (msg->isExtendedAddress) {
                TxMessage.ExtId = msg->addressValue;
                TxMessage.IDE = CAN_ID_EXT;
        } else {
                TxMessage.StdId = msg->addressValue;
                TxMessage.IDE = CAN_ID_STD;
        }

        TxMessage.RTR = CAN_RTR_DATA;
        TxMessage.DLC = msg->dataLength;
        memcpy(TxMessage.Data, msg->data, msg->dataLength);

        CAN_TypeDef* chan = channel == 0 ? CAN1 : CAN2;
        const uint8_t mailbox = CAN_Transmit(chan, &TxMessage);

        /*
         * Then they don't want to wait.  Ok.  Let caller know if they
         * got a mailbox then.  If not, message was unable to be sent.
         */
        if (0 == timeoutMs)
                return mailbox != CAN_TxStatus_NoMailBox;

        /* Using ticks avoids a race-condition */
        size_t ticks = getCurrentTicks();
        const size_t trigger = ticks + msToTicks(timeoutMs);
        uint8_t status = CAN_TxStatus_Failed;

        while(ticks <= trigger) {
                status = CAN_TransmitStatus(chan, mailbox);
                if (CAN_TxStatus_Pending != status)
                        break;

                /*
                 * Not using yield here as it will cause lower priority tasks
                 * to starve.  Yield only allows tasks of equal or greater
                 * priority to run.
                 */
                delayTicks(1);

                ticks = getCurrentTicks();
        }

        if (CAN_TxStatus_Pending == status)
                CAN_CancelTransmit(chan, mailbox);

        return status == CAN_TxStatus_Ok;
}
Example #3
0
void can_flush_tx(void)
{
	CAN_CancelTransmit(CAN1, 0);
	CAN_CancelTransmit(CAN1, 1);
	CAN_CancelTransmit(CAN1, 2);
}