From c1d9a582fcd7c1ac6854137c2b33fc481753d0fc Mon Sep 17 00:00:00 2001 From: cfif Date: Wed, 20 May 2026 17:19:15 +0300 Subject: [PATCH] Init --- Inc/SerialPortLinArtery.h | 3 +- Src/SerialPortLinArtery.c | 98 ++++++++++++++++++++++++++++++++++----- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/Inc/SerialPortLinArtery.h b/Inc/SerialPortLinArtery.h index c8231b5..db30db8 100644 --- a/Inc/SerialPortLinArtery.h +++ b/Inc/SerialPortLinArtery.h @@ -21,7 +21,6 @@ typedef struct __attribute__((packed)) { typedef struct { usart_type *uart; - bool linFrameStarted; uint8_t linByteCount; uint32_t linLastByteTime; @@ -46,6 +45,6 @@ void vSerialPortLinInit( ); void SerialPort_IrqProcessing_UartLin(tSerialPortLinArtery *env); -bool vSerialPortSendLinFrame(tSerialPortLinArtery *env, const lin_frame_t *pFrame, uint32_t timeout); +tSerialPortLinIO vSerialPorLinGetIo(tSerialPortLinArtery *env); #endif //SERIALPORT_SERIALPORTLIN_ARTERY_H diff --git a/Src/SerialPortLinArtery.c b/Src/SerialPortLinArtery.c index 12cd3b7..717a27b 100644 --- a/Src/SerialPortLinArtery.c +++ b/Src/SerialPortLinArtery.c @@ -86,28 +86,25 @@ vSerialPortLinTransmitOverCore(tSerialPortLinArtery *env, uint8_t *data, uint16_ } static uint8_t LIN_DrvMakeCheckSum(uint8_t *pBuf, uint8_t u8Size, uint8_t u8Pid) { - uint16_t u16Checksum = 0U; - uint8_t u8Length = 0U; + uint16_t u16Checksum = 0U; + uint8_t u8Length = 0U; // For PID is 0x3C (ID 0x3C) or 0x7D (ID 0x3D) or 0xFE (ID 0x3E) or 0xBF (ID 0x3F) - if ((0x3CU == u8Pid) || (0x7DU == u8Pid) || (0xFEU == u8Pid) || (0xBFU == u8Pid)) - { + if ((0x3CU == u8Pid) || (0x7DU == u8Pid) || (0xFEU == u8Pid) || (0xBFU == u8Pid)) { u8Pid = 0U; } u16Checksum += u8Pid; - for (u8Length = 0U; u8Length < u8Size; u8Length++) - { + for (u8Length = 0U; u8Length < u8Size; u8Length++) { u16Checksum += *pBuf; pBuf++; - if (u16Checksum > 0xFFU) - { + if (u16Checksum > 0xFFU) { u16Checksum -= 0xFFU; } } - return ~(uint8_t)(u16Checksum); + return ~(uint8_t) (u16Checksum); } static uint8_t LIN_CalcParity(uint8_t id) { @@ -260,11 +257,11 @@ void LIN_CheckTimeout(tSerialPortLinArtery *env) { } -bool vSerialPortSendLinFrame(tSerialPortLinArtery *env, const lin_frame_t *pFrame, uint32_t timeout) { +static bool vSerialPortSendLinFrame(tSerialPortLinArtery *env, const lin_frame_t *pFrame, uint32_t timeout) { uint8_t txBuffer[11]; // Максимальный размер LIN кадра (без break) uint8_t idx = 0; - if ((!env) || (!pFrame)) { + if (!pFrame) { return false; } @@ -305,3 +302,82 @@ bool vSerialPortSendLinFrame(tSerialPortLinArtery *env, const lin_frame_t *pFram return (sent == idx); // Успех, если все байты отправлены } + + +static uint16_t vSerialPortLinReceiveQueue(tSerialPortLinArtery *env, uint8_t *data, uint16_t size, uint32_t timeout, + osMessageQueueId_t queueId) { + uint16_t received = 0; + + + if (timeout) { + uint32_t endMs = SystemGetMs() + timeout; + uint32_t leftMs; + + while (size && ((timeout == SystemWaitForever) || (endMs > SystemGetMs()))) { + leftMs = endMs - SystemGetMs(); + if (osMessageQueueGet(queueId, data, NULL, leftMs) == osOK) { + --size; + ++received; + ++data; + } + } + + } else { + while (size) { + if (osMessageQueueGet(queueId, data, NULL, 0) == osOK) { + --size; + ++received; + ++data; + } else { + return received; + } + } + } + + return received; + +} + +static uint8_t vLinTransmitCommand(tSerialPortLinArtery *env, tLinData *linData, uint32_t timeout) { + + lin_frame_t txFrame; + + txFrame.dataLen = linData->g_aTxBufferLen; + for (uint8_t i = 0; i < linData->g_aTxBufferLen; ++i) { + txFrame.data[i] = linData->g_aTxBuffer[i]; + } + + bool result = vSerialPortSendLinFrame(env, &txFrame, timeout); + + if (result) { + return LIN_RX_COMPLETED; + } + + return LIN_TIMEOUT; +} + +static uint8_t vLinReceivedCommand(tSerialPortLinArtery *env, tLinData *linData, uint32_t timeout) { + lin_frame_t rxFrame; + + uint16_t len = vSerialPortLinReceiveQueue(env, (void *) &rxFrame, sizeof(lin_frame_t), osWaitForever, env->rxDataQueue); + + if (len == 0) { + return LIN_NO_EVENT; + } + + linData->g_aRxBufferLen = rxFrame.dataLen; + for (uint8_t i = 0; i < rxFrame.dataLen; ++i) { + linData->g_aRxBuffer[i] = rxFrame.data[i]; + } + + return rxFrame.event; +} + +tSerialPortLinIO vSerialPorLinGetIo(tSerialPortLinArtery *env) { + tSerialPortLinIO io = { + .env = env, + .transmitCommand = (LinIOTransaction) vLinTransmitCommand, + .receivedCommand = (LinIOTransaction) vLinReceivedCommand + }; + return io; +} \ No newline at end of file