From 5687196bdd2d9122086fb9749e31824c5779040a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 5 Mar 2018 10:21:23 +0100 Subject: [PATCH] small speed up using larger buffer and multipart Tx --- TinyFrame/TF_Integration.c | 44 ++++++++++++++++++++++++++++++++++++++ freertos.c | 2 +- platform/plat_compat.h | 4 ++-- units/adc/_adc_core.c | 12 ++++++----- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/TinyFrame/TF_Integration.c b/TinyFrame/TF_Integration.c index 6aa1b18..22e0b9f 100644 --- a/TinyFrame/TF_Integration.c +++ b/TinyFrame/TF_Integration.c @@ -14,8 +14,40 @@ extern osSemaphoreId semVcomTxReadyHandle; extern osMutexId mutTinyFrameTxHandle; +static volatile bool first_tx = false; // XXX global + void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len) { +#if 1 +// if (!first_tx) { +// // wait for the last USB transmission to be finished +// int32_t mxStatus; +// 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) { + uint32_t pad = (64 - (len&0x3F)); + memset((void *) (buff + len), 0, pad); + len += pad; // padding to a multiple of 64 (size of the endpoint) + } + + 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, + // so if we let it process it in the background, it could get corrupted before the Tx is completed. + int32_t mxStatus; + mxStatus = osSemaphoreWait(semVcomTxReadyHandle, 100); + if (mxStatus != osOK) { + TF_Error("Tx stalled"); + return; + } +#else (void) tf; #define CHUNK 64 // same as TF_SENDBUF_LEN, so we should always have only one run of the loop int32_t total = (int32_t) len; @@ -37,6 +69,7 @@ void TF_WriteImpl(TinyFrame *tf, const uint8_t *buff, uint32_t len) buff += chunksize; total -= chunksize; } +#endif } /** Claim the TX interface before composing and sending a frame */ @@ -47,6 +80,17 @@ bool TF_ClaimTx(TinyFrame *tf) assert_param(!inIRQ()); assert_param(osOK == osMutexWait(mutTinyFrameTxHandle, 5000)); + +// // wait for the last USB transmission to be finished +// int32_t mxStatus; +// mxStatus = osSemaphoreWait(semVcomTxReadyHandle, 100); +// if (mxStatus != osOK) { +// TF_Error("Tx stalled"); +// return false; +// } + + first_tx = true; + return true; } diff --git a/freertos.c b/freertos.c index 6213905..10cebcf 100644 --- a/freertos.c +++ b/freertos.c @@ -169,7 +169,7 @@ void MX_FREERTOS_Init(void) { /* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ - xSemaphoreGive(semVcomTxReadyHandle); +// xSemaphoreGive(semVcomTxReadyHandle); /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ diff --git a/platform/plat_compat.h b/platform/plat_compat.h index 74f1025..585aeb3 100644 --- a/platform/plat_compat.h +++ b/platform/plat_compat.h @@ -30,7 +30,7 @@ #define BULK_READ_BUF_LEN 256 // Buffer for TF bulk reads -#define UNIT_TMP_LEN 512 // Buffer for internal unit operations +#define UNIT_TMP_LEN 256 // Buffer for internal unit operations #define FLASH_SAVE_BUF_LEN 128 // Malloc'd buffer for saving to flash @@ -38,7 +38,7 @@ #define RX_QUE_CAPACITY 16 // TinyFrame rx queue size (64 bytes each) #define TF_MAX_PAYLOAD_RX 512 // TF max Rx payload -#define TF_SENDBUF_LEN 64 // TF transmit buffer (can be less than a full frame) +#define TF_SENDBUF_LEN 512 // TF transmit buffer (can be less than a full frame) #define TF_MAX_ID_LST 4 // Frame ID listener count #define TF_MAX_TYPE_LST 6 // Frame Type listener count diff --git a/units/adc/_adc_core.c b/units/adc/_adc_core.c index 8a59d8e..023de49 100644 --- a/units/adc/_adc_core.c +++ b/units/adc/_adc_core.c @@ -171,7 +171,7 @@ static void handle_httc(Unit *unit, bool tc) const bool m_fixcpt = priv->opmode == ADC_OPMODE_BLCAP; if (ht) { - end = (priv->buf_itemcount / 2); + end = (priv->buf_itemcount >> 1); // div2 } else { end = priv->buf_itemcount; @@ -234,7 +234,7 @@ static void handle_httc(Unit *unit, bool tc) priv->stream_startpos = 0; } else { - priv->stream_startpos = priv->buf_itemcount / 2; + priv->stream_startpos = priv->buf_itemcount >> 1; // div2 } } @@ -289,7 +289,7 @@ void UADC_DMA_Handler(void *arg) const bool m_stream = priv->opmode == ADC_OPMODE_STREAM; const bool m_fixcpt = priv->opmode == ADC_OPMODE_BLCAP; if (m_trigd || m_stream || m_fixcpt) { - const uint32_t half = (uint32_t) (priv->buf_itemcount / 2); + const uint32_t half = (uint32_t) (priv->buf_itemcount >> 1); // div2 if (ht && tc) { // dual event interrupt - may happen if we missed both and they were pending after // interrupts became enabled again (this can happen due to the EOS or other higher prio irq's) @@ -336,8 +336,10 @@ void UADC_ADC_EOS_Handler(void *arg) if (priv->opmode == ADC_OPMODE_UNINIT) return; // Wait for the DMA to complete copying the last sample - uint32_t dmapos; - hw_wait_while((dmapos = DMA_POS(priv)) % priv->nb_channels != 0, 100); // XXX this could be changed to reading it from the DR instead + uint32_t dmapos = DMA_POS(priv); + if ((DMA_POS(priv) % priv->nb_channels) != 0) { + hw_wait_while((dmapos = DMA_POS(priv)) % priv->nb_channels != 0, 100); // XXX this could be changed to reading it from the DR instead + } uint32_t sample_pos; if (dmapos == 0) {