This commit is contained in:
cfif 2026-05-20 17:19:15 +03:00
parent df8dd7d084
commit c1d9a582fc
2 changed files with 88 additions and 13 deletions

View File

@ -21,7 +21,6 @@ typedef struct __attribute__((packed)) {
typedef struct { typedef struct {
usart_type *uart; usart_type *uart;
bool linFrameStarted; bool linFrameStarted;
uint8_t linByteCount; uint8_t linByteCount;
uint32_t linLastByteTime; uint32_t linLastByteTime;
@ -46,6 +45,6 @@ void vSerialPortLinInit(
); );
void SerialPort_IrqProcessing_UartLin(tSerialPortLinArtery *env); 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 #endif //SERIALPORT_SERIALPORTLIN_ARTERY_H

View File

@ -90,19 +90,16 @@ static uint8_t LIN_DrvMakeCheckSum(uint8_t *pBuf, uint8_t u8Size, uint8_t u8Pid)
uint8_t u8Length = 0U; uint8_t u8Length = 0U;
// For PID is 0x3C (ID 0x3C) or 0x7D (ID 0x3D) or 0xFE (ID 0x3E) or 0xBF (ID 0x3F) // 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; u8Pid = 0U;
} }
u16Checksum += u8Pid; u16Checksum += u8Pid;
for (u8Length = 0U; u8Length < u8Size; u8Length++) for (u8Length = 0U; u8Length < u8Size; u8Length++) {
{
u16Checksum += *pBuf; u16Checksum += *pBuf;
pBuf++; pBuf++;
if (u16Checksum > 0xFFU) if (u16Checksum > 0xFFU) {
{
u16Checksum -= 0xFFU; u16Checksum -= 0xFFU;
} }
} }
@ -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 txBuffer[11]; // Максимальный размер LIN кадра (без break)
uint8_t idx = 0; uint8_t idx = 0;
if ((!env) || (!pFrame)) { if (!pFrame) {
return false; return false;
} }
@ -305,3 +302,82 @@ bool vSerialPortSendLinFrame(tSerialPortLinArtery *env, const lin_frame_t *pFram
return (sent == idx); // Успех, если все байты отправлены 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;
}