diff --git a/zcanreceiver/zcanreceiver.cpp b/zcanreceiver/zcanreceiver.cpp index 115c6a0..427a9d5 100644 --- a/zcanreceiver/zcanreceiver.cpp +++ b/zcanreceiver/zcanreceiver.cpp @@ -20,6 +20,8 @@ static zcanbus_on_connected_t m_connectedlistener; static canrxbuffer_t m_rxbufcache; static uint16_t reportIndex; +static uint16_t eventId; +static uint16_t rxConfirmEventId; static uint8_t rxdata[200]; static uint8_t txbuff[200]; @@ -52,7 +54,7 @@ void HAL_CAN_ErrorCallback(CAN_HandleTypeDef *hcan) { ZLOGI(TAG, "HAL_CAN_ErrorC } /*********************************************************************************************************************** - * ??消息处理 * + * �?�?消息处理 * ***********************************************************************************************************************/ static void _oncanpacket(CAN_HandleTypeDef *hcan) { @@ -66,7 +68,7 @@ static void _oncanpacket(CAN_HandleTypeDef *hcan) { static bool _getRxMessage(CAN_RxHeaderTypeDef *pHeader, uint8_t aData[] /*8byte table*/) { /** - * @brief 读取当前FIFO?缓存了?少帧的数据 + * @brief 读取当前FIFO�?缓存了�?�少帧的数据 */ uint32_t level = HAL_CAN_GetRxFifoFillLevel(CANHANDLER, CAN_FIFO_NUM); if (level == 0) { @@ -99,7 +101,7 @@ static void _processOneCanPacket(CAN_RxHeaderTypeDef *pHeader, uint8_t *aData) { return; } - // ?接收主机消息 + // �?接收主机消息 if (from != 1) { return; } @@ -108,7 +110,7 @@ static void _processOneCanPacket(CAN_RxHeaderTypeDef *pHeader, uint8_t *aData) { return; } - // 上?接收到的消?还没有来的急?理 + // 上�?�接收到的消�?还没有来的急�?�理 if (m_rxbufcache.dataIsReady) { ZLOGI(TAG, "discard rx packet ,last packet not processed"); return; @@ -194,7 +196,7 @@ HAL_StatusTypeDef initializeFilter() { sFilterConfig.SlaveStartFilterBank = CAN_MAX_FILTER_NUM; // slave filter start index /******************************************************************************* - * 接收所有消? * + * 接收所有消�? * *******************************************************************************/ filterId = (0); // mask = (0); // @@ -218,7 +220,7 @@ void zcanbus_init(uint8_t deviceId) { HAL_StatusTypeDef hal_status; m_rxbufcache.dataIsReady = false; - m_rxbufcache.id = 0; // ?接收来自主机的消? + m_rxbufcache.id = 0; // �?接收来自主机的消�? m_rxbufcache.canPacketNum = 0; hal_status = initializeFilter(); @@ -261,7 +263,7 @@ static char *hex2str(uint8_t *data, size_t len) { } bool zcanbus_send_packet(uint8_t to, uint8_t *packet, size_t len, int overtimems) { - ZLOGI(TAG, "sendPacket to:%d, %s(%d)", to, hex2str(packet, len), len); + // ZLOGI(TAG, "sendPacket to:%d, %s(%d)", to, hex2str(packet, len), len); int npacket = len / 8 + (len % 8 == 0 ? 0 : 1); if (npacket > 255) { ZLOGE(TAG, "sendPacket fail, len:%d", len); @@ -328,6 +330,7 @@ bool zcanbus_send_sub_packet(uint8_t to, int npacket, int packetIndex, uint8_t * } zos_delay_ms(1); } + zos_delay_ms(1); return true; } @@ -370,6 +373,29 @@ bool zcanbus_send_report(uint16_t function_id, uint8_t *param, size_t len, int32 return zcanbus_send_packet(1 /*mainboard*/, (uint8_t *)&txpacket[0], sizeof(zcanbus_packet_t) + len, overtime); } +bool zcanbus_send_event(uint16_t function_id, uint8_t *param, size_t len, int32_t overtime) { + zlock_guard l(m_lock); + m_priority = kpriority_emergency_report; + + eventId++; + rxConfirmEventId = 0; + + for (size_t i = 0; i < 5; i++) { + zcanbus_packet_t *txpacket = (zcanbus_packet_t *)txbuff; + txpacket->index = eventId; + txpacket->function_id = function_id; + txpacket->ptype = kevent; + memcpy(txpacket->params, param, len); + zcanbus_send_packet(1 /*mainboard*/, (uint8_t *)&txpacket[0], sizeof(zcanbus_packet_t) + len, 10); + + for (size_t i = 0; i < 100; i++) { + if (rxConfirmEventId == eventId) break; + zos_delay_ms(1); + } + } + return true; +} + bool zcanbus_send_emergency_report(uint16_t function_id, uint8_t *param, size_t len, int32_t overtime) { zlock_guard l(m_lock); m_priority = kpriority_emergency_report; @@ -396,6 +422,10 @@ static void process_rx_packet(canrxbuffer_t *canrxbuf, uint8_t *rx, size_t len) m_rxlistener[i](canrxbuf->from, canrxbuf->to, rxdata, len); } } + + if (packet->ptype == kevent_confirm) { + rxConfirmEventId = packet->index; + } } void zcanbus_schedule() { diff --git a/zcanreceiver/zcanreceiver.hpp b/zcanreceiver/zcanreceiver.hpp index 3a2987b..0a43c97 100644 --- a/zcanreceiver/zcanreceiver.hpp +++ b/zcanreceiver/zcanreceiver.hpp @@ -42,4 +42,5 @@ bool zcanbus_send_ack(zcanbus_packet_t *rxpacket, int32_t val); bool zcanbus_send_ack(zcanbus_packet_t *rxpacket, bool _val); bool zcanbus_send_errorack(zcanbus_packet_t *rxpacket, int32_t errcode); bool zcanbus_send_report(uint16_t function_id, uint8_t *param, size_t len, int32_t overtime); +bool zcanbus_send_event(uint16_t function_id, uint8_t *param, size_t len, int32_t overtime); bool zcanbus_send_emergency_report(uint16_t function_id, uint8_t *param, size_t len, int32_t overtime);