From e5a3b543e0d621e33ecda55bc92c886a1c07114a Mon Sep 17 00:00:00 2001 From: cfif Date: Fri, 13 Mar 2026 16:25:26 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B0=D0=BB=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inc/CanSerialPortFrame.h | 2 +- Src/CanSerialPortFrame.c | 197 +++++++++++++-------------------------- 2 files changed, 66 insertions(+), 133 deletions(-) diff --git a/Inc/CanSerialPortFrame.h b/Inc/CanSerialPortFrame.h index 17e8541..26718a7 100644 --- a/Inc/CanSerialPortFrame.h +++ b/Inc/CanSerialPortFrame.h @@ -42,7 +42,7 @@ typedef struct { osMessageQueueId_t rxDataSnifferQueue0; osMessageQueueId_t rxDataSnifferQueue1; - osMessageQueueId_t txAccessQueue; + osMessageQueueId_t txAccessQueue[3]; } tCanSerialPortFrameArtery; diff --git a/Src/CanSerialPortFrame.c b/Src/CanSerialPortFrame.c index deb16ca..6876b1f 100644 --- a/Src/CanSerialPortFrame.c +++ b/Src/CanSerialPortFrame.c @@ -5,6 +5,8 @@ #include "CanSerialPortFrame.h" #include "memory.h" #include "at32f435_437_misc.h" +#include "FreeRTOSConfig.h" + static void vCanSerialPortFrameInitNVICEnable(can_type *CANx, uint8_t irqSubPriority) { @@ -106,6 +108,10 @@ void vCanSerialPortFrameInit( env->can = CANx; + env->txAccessQueue[0] = osMutexNew(NULL); + env->txAccessQueue[1] = osMutexNew(NULL); + env->txAccessQueue[2] = osMutexNew(NULL); + if (!env->reInit) { env->reInit = true; vCanSerialPortFrameInitStructure(env, CANx, rxBufferLength0, rxSnifferLength0, rxBufferLength1, @@ -467,48 +473,55 @@ uint16_t vCanSerialPortFrameTransmit(tCanSerialPortFrameArtery *env, uint8_t *da } */ -uint16_t vCanSerialPortFrameTransmit(tCanSerialPortFrameArtery *env, uint8_t *data, uint16_t size, uint32_t timeout) { - uint16_t sent = 0; - uint32_t endMs = SystemGetMs() + timeout; - uint16_t fullSize = size / 8; - uint8_t tailSize = size % 8; +uint16_t vCanSerialPortFrameTransmit(tCanSerialPortFrameArtery *env, uint8_t *data, uint16_t size, uint32_t adr, + uint8_t canType, uint8_t mailBox, uint32_t timeout) { - uint8_t transmit_mailbox; - can_tx_message_type TxMessage; + configASSERT(mailBox < 3); + + if (osMutexAcquire(env->txAccessQueue[mailBox], 1000) == osOK) { + uint16_t sent = 0; + uint32_t endMs = SystemGetMs() + timeout; + + uint16_t fullSize = size / 8; + uint8_t tailSize = size % 8; + + uint8_t transmit_mailbox; + can_tx_message_type TxMessage; - if (env->canTypeFrame == CAN_STD_ID) { - TxMessage.standard_id = env->id; - TxMessage.extended_id = 0; - TxMessage.id_type = CAN_ID_STANDARD; - } + if (canType == CAN_STD_ID) { + TxMessage.standard_id = adr; + TxMessage.extended_id = 0; + TxMessage.id_type = CAN_ID_STANDARD; + } - if (env->canTypeFrame == CAN_EXT_ID) { - TxMessage.standard_id = 0; - TxMessage.extended_id = env->id; - TxMessage.id_type = CAN_ID_EXTENDED; - } + if (canType == CAN_EXT_ID) { + TxMessage.standard_id = 0; + TxMessage.extended_id = adr; + TxMessage.id_type = CAN_ID_EXTENDED; + } - TxMessage.frame_type = CAN_TFT_DATA; - TxMessage.dlc = 8; + TxMessage.frame_type = CAN_TFT_DATA; + TxMessage.dlc = 8; - while (size && ((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { + while (size && ((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { - uint16_t len = 0; - for (uint16_t i = 0; i < fullSize; ++i) { + uint16_t len = 0; + for (uint16_t i = 0; i < fullSize; ++i) { - memcpy(TxMessage.data, &data[len], 8); - len += 8; + memcpy(TxMessage.data, &data[len], 8); + len += 8; - transmit_mailbox = can_message_transmitExt(env->can, &TxMessage, CAN_TX_MAILBOX0); + transmit_mailbox = can_message_transmitExt(env->can, &TxMessage, mailBox); - while (can_transmit_status_get(env->can, (can_tx_mailbox_num_type) transmit_mailbox) != - CAN_TX_STATUS_SUCCESSFUL) { + while (can_transmit_status_get(env->can, (can_tx_mailbox_num_type) transmit_mailbox) != + CAN_TX_STATUS_SUCCESSFUL) { - if (!((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { - return sent; - } + if (!((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { + osMutexRelease(env->txAccessQueue[mailBox]); + return sent; + } // if ((can_flag_get(env->can, CAN_BOF_FLAG) != RESET) || // (can_flag_get(env->can, CAN_EPF_FLAG) != RESET) || @@ -516,121 +529,43 @@ uint16_t vCanSerialPortFrameTransmit(tCanSerialPortFrameArtery *env, uint8_t *da // return sent; // } - } - - sent += 8; - size -= 8; - } - - if (tailSize) { - TxMessage.dlc = tailSize; - memcpy(TxMessage.data, &data[len], tailSize); - - transmit_mailbox = can_message_transmitExt(env->can, &TxMessage, CAN_TX_MAILBOX0); - - can_transmit_status_type status = can_transmit_status_get(env->can, - (can_tx_mailbox_num_type) transmit_mailbox); - - while (status != CAN_TX_STATUS_SUCCESSFUL) { - - status = can_transmit_status_get(env->can, (can_tx_mailbox_num_type) transmit_mailbox); - - if (!((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { - return sent; } + sent += 8; + size -= 8; } - sent += tailSize; - size -= tailSize; - } + if (tailSize) { + TxMessage.dlc = tailSize; + memcpy(TxMessage.data, &data[len], tailSize); - } + transmit_mailbox = can_message_transmitExt(env->can, &TxMessage, mailBox); - return sent; -} + can_transmit_status_type status = can_transmit_status_get(env->can, + (can_tx_mailbox_num_type) transmit_mailbox); -uint16_t vCanSerialPortFrameTransmit1(tCanSerialPortFrameArtery *env, uint8_t *data, uint16_t size, uint32_t timeout) { - uint16_t sent = 0; - uint32_t endMs = SystemGetMs() + timeout; + while (status != CAN_TX_STATUS_SUCCESSFUL) { - uint16_t fullSize = size / 8; - uint8_t tailSize = size % 8; + status = can_transmit_status_get(env->can, (can_tx_mailbox_num_type) transmit_mailbox); - uint8_t transmit_mailbox; - can_tx_message_type TxMessage; + if (!((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { + osMutexRelease(env->txAccessQueue[mailBox]); + return sent; + } - - if (env->canTypeFrame == CAN_STD_ID) { - TxMessage.standard_id = env->id1; - TxMessage.extended_id = 0; - TxMessage.id_type = CAN_ID_STANDARD; - } - - if (env->canTypeFrame == CAN_EXT_ID) { - TxMessage.standard_id = 0; - TxMessage.extended_id = env->id1; - TxMessage.id_type = CAN_ID_EXTENDED; - } - - TxMessage.frame_type = CAN_TFT_DATA; - TxMessage.dlc = 8; - - while (size && ((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { - - uint16_t len = 0; - for (uint16_t i = 0; i < fullSize; ++i) { - - memcpy(TxMessage.data, &data[len], 8); - len += 8; - - transmit_mailbox = can_message_transmitExt(env->can, &TxMessage, CAN_TX_MAILBOX1); - - while (can_transmit_status_get(env->can, (can_tx_mailbox_num_type) transmit_mailbox) != - CAN_TX_STATUS_SUCCESSFUL) { - - if (!((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { - return sent; } -// if ((can_flag_get(env->can, CAN_BOF_FLAG) != RESET) || -// (can_flag_get(env->can, CAN_EPF_FLAG) != RESET) || -// (can_flag_get(env->can, CAN_EAF_FLAG) != RESET)) { -// return sent; -// } - + sent += tailSize; + size -= tailSize; } - sent += 8; - size -= 8; - } - - if (tailSize) { - TxMessage.dlc = tailSize; - memcpy(TxMessage.data, &data[len], tailSize); - - transmit_mailbox = can_message_transmitExt(env->can, &TxMessage, CAN_TX_MAILBOX1); - - can_transmit_status_type status = can_transmit_status_get(env->can, - (can_tx_mailbox_num_type) transmit_mailbox); - - while (status != CAN_TX_STATUS_SUCCESSFUL) { - - status = can_transmit_status_get(env->can, (can_tx_mailbox_num_type) transmit_mailbox); - - if (!((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { - return sent; - } - - } - - sent += tailSize; - size -= tailSize; } + osMutexRelease(env->txAccessQueue[mailBox]); + return sent; + } else { + return 0; } - - return sent; } tSerialPortFrameIO CanPortFrame_GetIo(tCanSerialPortFrameArtery *env) { @@ -638,8 +573,7 @@ tSerialPortFrameIO CanPortFrame_GetIo(tCanSerialPortFrameArtery *env) { .env = env, .receive0 = (SerialPortFrameIOTransaction) vCanSerialPortFrameReceive0, .receive1 = (SerialPortFrameIOTransaction) vCanSerialPortFrameReceive1, - .transmit = (SerialPortFrameIOTransaction) vCanSerialPortFrameTransmit, - .transmit1 = (SerialPortFrameIOTransaction) vCanSerialPortFrameTransmit1 + .transmit = (SerialPortFrameIOTransmitTransaction) vCanSerialPortFrameTransmit, }; return io; } @@ -649,8 +583,7 @@ tSerialPortFrameIO CanPort_GetSnifferIo(tCanSerialPortFrameArtery *env) { .env = env, .receive0 = (SerialPortFrameIOTransaction) vCanSerialPortFrameReceiveSniffer0, .receive1 = (SerialPortFrameIOTransaction) vCanSerialPortFrameReceiveSniffer1, - .transmit = (SerialPortFrameIOTransaction) vCanSerialPortFrameTransmit, - .transmit1 = (SerialPortFrameIOTransaction) vCanSerialPortFrameTransmit1 + .transmit = (SerialPortFrameIOTransmitTransaction) vCanSerialPortFrameTransmit, }; return io; } \ No newline at end of file