83 lines
2.2 KiB
C
83 lines
2.2 KiB
C
//
|
|
// Created by cfif on 13.11.2023.
|
|
//
|
|
#include "SerialPort_USB.h"
|
|
#include "SystemDelayInterface.h"
|
|
#include "hw_config.h"
|
|
#include "usb_lib.h"
|
|
|
|
static uint16_t impSerialPortReceiveQueue(
|
|
TSerialPortUsbNation *env, uint8_t *data, uint16_t size, uint32_t timeout, osMessageQueueId_t queueId
|
|
) {
|
|
uint16_t received = 0;
|
|
|
|
if (timeout) {
|
|
uint32_t endTimeout = SystemGetMs() + timeout;
|
|
uint32_t currentTimeout;
|
|
|
|
while (size && ((timeout == SystemWaitForever) || (endTimeout > SystemGetMs()))) {
|
|
currentTimeout = endTimeout - SystemGetMs();
|
|
if (osMessageQueueGet(queueId, data, NULL, currentTimeout) == 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 impSerialPortUsbTransmit(TSerialPortUsbNation *env, uint8_t *data, uint16_t size, uint32_t timeout) {
|
|
|
|
uint16_t count = size / 64;
|
|
uint16_t tail = size % 64;
|
|
|
|
uint16_t i = 0;
|
|
for (; i < count; ++i) {
|
|
CDC_Send_DATA(&data[i * 64], 64);
|
|
SystemDelayMs(5);
|
|
}
|
|
|
|
if (tail > 0) {
|
|
CDC_Send_DATA(&data[i * 64], tail);
|
|
SystemDelayMs(5);
|
|
}
|
|
|
|
return size;
|
|
}
|
|
|
|
static uint16_t impSerialPortReceive(TSerialPortUsbNation *env, uint8_t *data, uint16_t size, uint32_t timeout) {
|
|
return impSerialPortReceiveQueue(env, data, size, timeout, env->rxDataQueue);
|
|
}
|
|
|
|
void SerialPortUsb_Init(
|
|
TSerialPortUsbNation *env,
|
|
uint32_t rxBufferLength
|
|
) {
|
|
env->rxDataQueue = osMessageQueueNew(rxBufferLength, 1, NULL);
|
|
|
|
Set_System();
|
|
USB_Interrupts_Config();
|
|
Set_USBClock();
|
|
USB_Init();
|
|
}
|
|
|
|
tSerialPortIO SerialPortUsb_GetIo(TSerialPortUsbNation *env) {
|
|
tSerialPortIO io = {
|
|
.env = env,
|
|
.receive = (SerialPortIOTransaction) impSerialPortReceive,
|
|
.transmit = (SerialPortIOTransaction) impSerialPortUsbTransmit
|
|
};
|
|
return io;
|
|
} |