implemented fallback to physical usart and basis for wireless support in the future

This commit is contained in:
2018-03-23 18:54:25 +01:00
parent 1797f179b4
commit 102d5f5a4c
17 changed files with 513 additions and 49 deletions
+36 -5
View File
@@ -6,6 +6,9 @@
#include "platform.h"
#include "task_main.h"
#include "comm/messages.h"
#include "comm/interfaces.h"
#include "framework/system_settings.h"
#include "USB/usbd_cdc_if.h"
#include "USB/usb_device.h"
@@ -14,13 +17,20 @@
extern osSemaphoreId semVcomTxReadyHandle;
extern osMutexId mutTinyFrameTxHandle;
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
/**
* USB transmit implementation
*
* @param tf - TF
* @param buff - buffer to send (can be longer than the buffers)
* @param len - buffer size
*/
static inline void _USB_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
{
#if 1
const uint32_t real_size = len;
// Padding to a multiple of 64 bytes - this is supposed to maximize the bulk transfer speed
if (len&0x3F) {
if ((len&0x3F) && !SystemSettings.visible_vcom) { // this corrupts VCOM on Linux for some reason
uint32_t pad = (64 - (len&0x3F));
memset((void *) (buff + len), 0, pad);
len += pad; // padding to a multiple of 64 (size of the endpoint)
@@ -32,8 +42,9 @@ void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
// The buffer is the TF transmit buffer, we can't leave it to work asynchronously because
// the next call could modify it before it's been transmitted (in the case of a chunked / multi-part frame)
// the assumption here is that all until the last chunk use the full buffer capacity
// If this is not the last chunk (assuming all but the last use full 512 bytes of the TF buffer), wait now for completion
if (real_size == TF_SENDBUF_LEN) {
// TODO this seems wrong - investigate
if (pdTRUE != xSemaphoreTake(semVcomTxReadyHandle, 100)) {
TF_Error("Tx stalled in WriteImpl");
return;
@@ -41,7 +52,7 @@ void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
}
#else
(void) tf;
#define CHUNK 64 // same as TF_SENDBUF_LEN, so we should always have only one run of the loop
#define CHUNK 64 // size of the USB packet
int32_t total = (int32_t) len;
while (total > 0) {
const int32_t mxStatus = osSemaphoreWait(semVcomTxReadyHandle, 100);
@@ -64,11 +75,31 @@ void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
#endif
}
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len)
{
if (gActiveComport == COMPORT_USB) {
_USB_WriteImpl(tf, buff, len);
}
else if (gActiveComport == COMPORT_USART) {
// TODO rewrite this to use DMA, then wait for the DMA
for(uint32_t i=0;i<len;i++) {
while(!LL_USART_IsActiveFlag_TXE(USART2));
LL_USART_TransmitData8(USART2, buff[i]);
}
xSemaphoreGive(semVcomTxReadyHandle); // act as if we just finished it and this is perhaps the DMA irq
}
else {
// TODO other transports
trap("not implemented.");
}
}
/** Claim the TX interface before composing and sending a frame */
bool TF_ClaimTx(TinyFrame *tf)
{
(void) tf;
// assert_param(!inIRQ()); // useless delay
assert_param(!inIRQ());
assert_param(pdTRUE == xSemaphoreTake(mutTinyFrameTxHandle, 5000)); // trips the wd
// The last chunk from some previous frame may still be being transmitted,