|
|
@ -4,48 +4,40 @@ |
|
|
|
// TinyFrame integration
|
|
|
|
// TinyFrame integration
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include <USB/usb_device.h> |
|
|
|
|
|
|
|
#include "platform.h" |
|
|
|
#include "platform.h" |
|
|
|
#include "task_main.h" |
|
|
|
#include "task_main.h" |
|
|
|
|
|
|
|
|
|
|
|
#include "USB/usbd_cdc_if.h" |
|
|
|
#include "USB/usbd_cdc_if.h" |
|
|
|
|
|
|
|
#include "USB/usb_device.h" |
|
|
|
#include "TinyFrame.h" |
|
|
|
#include "TinyFrame.h" |
|
|
|
|
|
|
|
|
|
|
|
extern osSemaphoreId semVcomTxReadyHandle; |
|
|
|
extern osSemaphoreId semVcomTxReadyHandle; |
|
|
|
extern osMutexId mutTinyFrameTxHandle; |
|
|
|
extern osMutexId mutTinyFrameTxHandle; |
|
|
|
|
|
|
|
|
|
|
|
static volatile bool first_tx = false; // XXX global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len) |
|
|
|
void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len) |
|
|
|
{ |
|
|
|
{ |
|
|
|
#if 1 |
|
|
|
#if 1 |
|
|
|
// if (!first_tx) {
|
|
|
|
const uint32_t real_size = len; |
|
|
|
// // wait for the last USB transmission to be finished
|
|
|
|
|
|
|
|
// int32_t mxStatus;
|
|
|
|
// Padding to a multiple of 64 bytes - this is supposed to maximize the bulk transfer speed
|
|
|
|
// mxStatus = osSemaphoreWait(semVcomTxReadyHandle, 100);
|
|
|
|
|
|
|
|
// if (mxStatus != osOK) {
|
|
|
|
|
|
|
|
// TF_Error("Tx stalled");
|
|
|
|
|
|
|
|
// return;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// first_tx = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Padding to a multiple of 64 bytes
|
|
|
|
|
|
|
|
if (len&0x3F) { |
|
|
|
if (len&0x3F) { |
|
|
|
uint32_t pad = (64 - (len&0x3F)); |
|
|
|
uint32_t pad = (64 - (len&0x3F)); |
|
|
|
memset((void *) (buff + len), 0, pad); |
|
|
|
memset((void *) (buff + len), 0, pad); |
|
|
|
len += pad; // padding to a multiple of 64 (size of the endpoint)
|
|
|
|
len += pad; // padding to a multiple of 64 (size of the endpoint)
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We bypass the USBD driver library's overhead by using the HAL function directly
|
|
|
|
assert_param(HAL_OK == HAL_PCD_EP_Transmit(hUsbDeviceFS.pData, CDC_IN_EP, (uint8_t *) buff, len)); |
|
|
|
assert_param(HAL_OK == HAL_PCD_EP_Transmit(hUsbDeviceFS.pData, CDC_IN_EP, (uint8_t *) buff, len)); |
|
|
|
|
|
|
|
|
|
|
|
// Wait for the semaphore - HAL keeps a pointer to the buffer, and it's the TinyFrame Tx buffer,
|
|
|
|
// The buffer is the TF transmit buffer, we can't leave it to work asynchronously because
|
|
|
|
// so if we let it process it in the background, it could get corrupted before the Tx is completed.
|
|
|
|
// the next call could modify it before it's been transmitted (in the case of a chunked / multi-part frame)
|
|
|
|
int32_t mxStatus; |
|
|
|
|
|
|
|
mxStatus = osSemaphoreWait(semVcomTxReadyHandle, 100); |
|
|
|
// the assumption here is that all until the last chunk use the full buffer capacity
|
|
|
|
if (mxStatus != osOK) { |
|
|
|
if (real_size == TF_SENDBUF_LEN) { |
|
|
|
TF_Error("Tx stalled"); |
|
|
|
if (pdTRUE != xSemaphoreTake(semVcomTxReadyHandle, 100)) { |
|
|
|
return; |
|
|
|
TF_Error("Tx stalled in WriteImpl"); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
#else |
|
|
|
#else |
|
|
|
(void) tf; |
|
|
|
(void) tf; |
|
|
@ -76,20 +68,18 @@ void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len) |
|
|
|
bool TF_ClaimTx(TinyFrame *tf) |
|
|
|
bool TF_ClaimTx(TinyFrame *tf) |
|
|
|
{ |
|
|
|
{ |
|
|
|
(void) tf; |
|
|
|
(void) tf; |
|
|
|
// assert_param(osThreadGetId() != tskMainHandle);
|
|
|
|
// assert_param(!inIRQ()); // useless delay
|
|
|
|
assert_param(!inIRQ()); |
|
|
|
assert_param(pdTRUE == xSemaphoreTake(mutTinyFrameTxHandle, 5000)); // trips the wd
|
|
|
|
|
|
|
|
|
|
|
|
assert_param(osOK == osMutexWait(mutTinyFrameTxHandle, 5000)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // wait for the last USB transmission to be finished
|
|
|
|
// The last chunk from some previous frame may still be being transmitted,
|
|
|
|
// int32_t mxStatus;
|
|
|
|
// wait for it to finish (the semaphore is given in the CDC tx done handler)
|
|
|
|
// mxStatus = osSemaphoreWait(semVcomTxReadyHandle, 100);
|
|
|
|
if (pdTRUE != xSemaphoreTake(semVcomTxReadyHandle, 100)) { |
|
|
|
// if (mxStatus != osOK) {
|
|
|
|
TF_Error("Tx stalled in Claim"); |
|
|
|
// TF_Error("Tx stalled");
|
|
|
|
|
|
|
|
// return false;
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
first_tx = true; |
|
|
|
// release the guarding mutex again
|
|
|
|
|
|
|
|
assert_param(pdTRUE == xSemaphoreGive(mutTinyFrameTxHandle)); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
@ -98,5 +88,7 @@ bool TF_ClaimTx(TinyFrame *tf) |
|
|
|
void TF_ReleaseTx(TinyFrame *tf) |
|
|
|
void TF_ReleaseTx(TinyFrame *tf) |
|
|
|
{ |
|
|
|
{ |
|
|
|
(void) tf; |
|
|
|
(void) tf; |
|
|
|
assert_param(osOK == osMutexRelease(mutTinyFrameTxHandle)); |
|
|
|
assert_param(pdTRUE == xSemaphoreGive(mutTinyFrameTxHandle)); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// the last payload is sent asynchronously
|
|
|
|
} |
|
|
|
} |
|
|
|