Skip to content

Commit

Permalink
Update mcp_can.cpp
Browse files Browse the repository at this point in the history
Maintain the deterministic timeout for sendMsg().
  • Loading branch information
coryjfowler authored Nov 24, 2022
1 parent 2eca0ac commit c4b6fc0
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions mcp_can.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1108,29 +1108,32 @@ INT8U MCP_CAN::clearMsg()
INT8U MCP_CAN::sendMsg()
{
INT8U res, res1, txbuf_n;
uint16_t uiTimeOut = 0;

uint32_t uiTimeOut, temp;

temp = micros();
// 24 * 4 microseconds typical
do {
res = mcp2515_getNextFreeTXBuf(&txbuf_n); /* info = addr. */
uiTimeOut++;
uiTimeOut = micros() - temp;
} while (res == MCP_ALLTXBUSY && (uiTimeOut < TIMEOUTVALUE));

if(uiTimeOut == TIMEOUTVALUE)
if(uiTimeOut >= TIMEOUTVALUE)
{
return CAN_GETTXBFTIMEOUT; /* get tx buff time out */
}
uiTimeOut = 0;
uiTimeOut = micros;
mcp2515_write_canMsg( txbuf_n);
mcp2515_modifyRegister( txbuf_n-1 , MCP_TXB_TXREQ_M, MCP_TXB_TXREQ_M );

temp = micros();
do
{
uiTimeOut++;
{
res1 = mcp2515_readRegister(txbuf_n-1); /* read send buff ctrl reg */
res1 = res1 & 0x08;
res1 = res1 & 0x08;
uiTimeOut = micros() - temp;
} while (res1 && (uiTimeOut < TIMEOUTVALUE));

if(uiTimeOut == TIMEOUTVALUE) /* send msg timeout */
if(uiTimeOut >= TIMEOUTVALUE) /* send msg timeout */
return CAN_SENDMSGTIMEOUT;

return CAN_OK;
Expand Down

0 comments on commit c4b6fc0

Please sign in to comment.