From fdfa36a496eafbbe49d0ccc3c253c5ed6ab73de8 Mon Sep 17 00:00:00 2001 From: cfif Date: Wed, 29 Oct 2025 11:17:05 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=B1=D0=BD=D0=BE=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Inc/SerialPortCan.h | 27 +++++++++++++++++ Src/SerialPortCan.c | 73 +++++++++++++++++++++++++++++++++++++++++++++ modular.json | 10 +++++++ 3 files changed, 110 insertions(+) create mode 100644 Inc/SerialPortCan.h create mode 100644 Src/SerialPortCan.c create mode 100644 modular.json diff --git a/Inc/SerialPortCan.h b/Inc/SerialPortCan.h new file mode 100644 index 0000000..83b58cb --- /dev/null +++ b/Inc/SerialPortCan.h @@ -0,0 +1,27 @@ +// +// Created by cfif on 12.11.2024. +// + +#ifndef HVAC_SERIALPORTCAN_H +#define HVAC_SERIALPORTCAN_H + +#include "cmsis_os2.h" +#include "SerialPortIO.h" +#include "SerialPortFrameIO.h" + +typedef struct { + tSerialPortFrameIO *CanIO; + osMessageQueueId_t queue; + uint32_t adr; +} tSerialPortCan; + +void vSerialPortCanInit( + tSerialPortCan *env, + tSerialPortFrameIO *CanIO, + uint32_t adr, + uint32_t rxBufferLength +); + +tSerialPortIO vSerialPortCanGetIo(tSerialPortCan *env); + +#endif //HVAC_SERIALPORTCAN_H diff --git a/Src/SerialPortCan.c b/Src/SerialPortCan.c new file mode 100644 index 0000000..de93ff8 --- /dev/null +++ b/Src/SerialPortCan.c @@ -0,0 +1,73 @@ +// +// Created by cfif on 12.11.2024. +// + +#include "SerialPortCan.h" +#include "SystemDelayInterface.h" +#include "CanSerialPortFrame.h" + +void vSerialPortCanInit( + tSerialPortCan *env, + tSerialPortFrameIO *CanIO, + uint32_t adr, + uint32_t rxBufferLength +) { + env->CanIO = CanIO; + env->queue = osMessageQueueNew(rxBufferLength, 1, NULL); + env->adr = adr; +} + +static uint16_t vSerialPortReceiveQueue(tSerialPortCan *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 uint16_t vSerialPortReceive(tSerialPortCan *env, uint8_t *data, uint16_t size, uint32_t timeout) { + return vSerialPortReceiveQueue(env, data, size, timeout, env->queue); +} + +static uint16_t vSerialPortTransmit(tSerialPortCan *env, uint8_t *data, uint16_t size, uint32_t timeout) { + + CanSerialPortFrameSetId(env->CanIO->env, env->adr); + uint16_t sent = env->CanIO->transmit(env->CanIO->env, data, size, timeout); + + return sent; +} + +tSerialPortIO vSerialPortCanGetIo(tSerialPortCan *env) { + tSerialPortIO io = { + .env = env, + .receive = (SerialPortIOTransaction) vSerialPortReceive, + .transmit = (SerialPortIOTransaction) vSerialPortTransmit + }; + return io; +} \ No newline at end of file diff --git a/modular.json b/modular.json new file mode 100644 index 0000000..71971cd --- /dev/null +++ b/modular.json @@ -0,0 +1,10 @@ +{ + "cmake": { + "inc_dirs": [ + "Inc" + ], + "srcs": [ + "Src/**.c" + ] + } +} \ No newline at end of file