Files
BC5D-POS-firmware/app/by_can.c
2024-06-10 18:04:45 +08:00

40 lines
1.5 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "by_can.h"
#include <string.h>
#include <assert.h>
#include "by_utils.h"
#include "by_debug.h"
by_error_status by_can_send_stdd(uint32_t id, const uint8_t *data, uint8_t len, uint16_t timeout)
{
assert(id < 0x7FF);
if (len > 8) {
len = 8;
}
uint8_t transmit_mailbox;
can_tx_message_type tx_message_struct;
tx_message_struct.standard_id = id; /* 设置发送数据帧的 ID=0x400 */
tx_message_struct.extended_id = 0; /* 不设置 */
tx_message_struct.id_type = CAN_ID_STANDARD; /* 发送数据帧类型(标准/扩展):标准数据帧 */
tx_message_struct.frame_type = CAN_TFT_DATA; /* 发送帧类型(远程/数据):数据帧 */
tx_message_struct.dlc = len; /* 发送数据长度0~88 */
memcpy(tx_message_struct.data, data, len); /* 复制发送数据 */
transmit_mailbox = can_message_transmit(CAN1, &tx_message_struct); /* 将以上待发送报文写入发送邮箱并请求发送 */
/* 等待该邮箱发送成功—对应邮箱发送成功标志置起 */
while (can_transmit_status_get(CAN1, (can_tx_mailbox_num_type)transmit_mailbox) != CAN_TX_STATUS_SUCCESSFUL) {
// LOGD("CAN#SEND: timeout=%d", timeout);
if (0 == timeout--) {
LOGW("CAN#TIMEOUT: ID=0x%x", id);
return BY_ERROR;
}
delay_us(10);
}
return BY_SUCCESS;
}