From 3c51c0633fa1f448884b53b45a712825a054fbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Thu, 8 Feb 2018 21:31:06 +0100 Subject: [PATCH] Made "usb tx done" notification bypass the usual bounce via the main thread, made higher samplerate achievable --- FreeRTOSConfig.h | 9 +++++---- .../Class/MSC_CDC/usbd_msc_cdc.c | 10 +++++++++- USB/usbd_cdc_if.c | 8 ++++++-- freertos.c | 4 ++-- platform/plat_compat.h | 11 +++++++++-- tasks/task_main.c | 6 +++--- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/FreeRTOSConfig.h b/FreeRTOSConfig.h index 4b9bfd9..97f7401 100644 --- a/FreeRTOSConfig.h +++ b/FreeRTOSConfig.h @@ -89,8 +89,9 @@ /* Ensure stdint is only used by the compiler, and not the assembler. */ #if defined(__ICCARM__) || defined(__CC_ARM) || defined(__GNUC__) #include - #include "main.h" - extern uint32_t SystemCoreClock; + #include "main.h" + #include "plat_compat.h" +extern uint32_t SystemCoreClock; #endif #define configUSE_PREEMPTION 1 @@ -110,11 +111,11 @@ #define configENABLE_BACKWARD_COMPATIBILITY 0 #define configUSE_TIMERS 1 -#define configTIMER_TASK_PRIORITY 4 // above normal +#define configTIMER_TASK_PRIORITY TSK_TIMERS_PRIO // above normal #define configTIMER_TASK_STACK_DEPTH TSK_STACK_TIMERS //128 #define configTIMER_QUEUE_LENGTH 4 -#define configTOTAL_HEAP_SIZE 4096 +#define configTOTAL_HEAP_SIZE PLAT_HEAP_SIZE /* Co-routine definitions. */ #define configUSE_CO_ROUTINES 0 diff --git a/USB/STM32_USB_Device_Library/Class/MSC_CDC/usbd_msc_cdc.c b/USB/STM32_USB_Device_Library/Class/MSC_CDC/usbd_msc_cdc.c index dca206d..6b76552 100644 --- a/USB/STM32_USB_Device_Library/Class/MSC_CDC/usbd_msc_cdc.c +++ b/USB/STM32_USB_Device_Library/Class/MSC_CDC/usbd_msc_cdc.c @@ -9,6 +9,7 @@ #include "usbd_msc.h" #include "usbd_cdc.h" #include "usbd_msc_cdc.h" +#include "USB/usb_device.h" #define USBD_MSC_CDC_CONFIG_DESC_SIZ 98 /* USB Mass storage device Configuration Descriptor */ @@ -112,7 +113,7 @@ __ALIGN_BEGIN uint8_t USBD_MSC_CDC_CfgFSDesc[USBD_MSC_CDC_CONFIG_DESC_SIZ] __AL 0x03, /* bmAttributes: Interrupt */ LOBYTE(CDC_CMD_PACKET_SIZE), /* wMaxPacketSize: TODO: 2?*/ HIBYTE(CDC_CMD_PACKET_SIZE), - 0x10, //0xFF, /* bInterval: TODO was 0x10?*/ + 0xFF, /* bInterval: TODO was 0x10?*/ /********** CDC Data Class Interface Descriptor ***********/ /*75*/ 0x09, /* bLength: Endpoint Descriptor size */ @@ -232,6 +233,13 @@ static uint8_t USBD_MSC_CDC_EP0_RxReady(struct _USBD_HandleTypeDef *pdev) static uint8_t USBD_MSC_CDC_DataIn(struct _USBD_HandleTypeDef *pdev, uint8_t epnum) { BaseType_t xHigherPriorityTaskWoken = pdFALSE; + + // This is a notification about data been Tx'd - avoid the bounce via main thread. + if (epnum == (CDC_IN_EP&0x7)) { + USBD_CDC_DataIn(&hUsbDeviceFS, CDC_IN_EP); + return USBD_OK; + } + xTaskNotifyFromISR(tskMainHandle, USBEVT_FLAG_EPx_IN(epnum), eSetBits, &xHigherPriorityTaskWoken); portYIELD_FROM_ISR(xHigherPriorityTaskWoken); // if (epnum == MSC_EPIN_ADDR||epnum==MSC_EPOUT_ADDR) USBD_MSC_DataIn(pdev, epnum); diff --git a/USB/usbd_cdc_if.c b/USB/usbd_cdc_if.c index f0457a9..53ca180 100644 --- a/USB/usbd_cdc_if.c +++ b/USB/usbd_cdc_if.c @@ -306,11 +306,15 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ void USBD_CDC_TransmitDone(USBD_HandleTypeDef *pdev) { - assert_param(xTaskGetCurrentTaskHandle() == tskMainHandle); +// assert_param(xTaskGetCurrentTaskHandle() == tskMainHandle); // Notify the semaphore that we're ready to transmit more assert_param(semVcomTxReadyHandle != NULL); - xSemaphoreGive(semVcomTxReadyHandle); + assert_param(inIRQ()); + + portBASE_TYPE taskWoken = pdFALSE; + assert_param(xSemaphoreGiveFromISR(semVcomTxReadyHandle, &taskWoken) == pdTRUE); + portYIELD_FROM_ISR(taskWoken); } /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ diff --git a/freertos.c b/freertos.c index da8accd..7ac46bc 100644 --- a/freertos.c +++ b/freertos.c @@ -178,11 +178,11 @@ void MX_FREERTOS_Init(void) { /* Create the thread(s) */ /* definition and creation of tskMain */ - osThreadStaticDef(tskMain, TaskMain, osPriorityHigh, 0, TSK_STACK_MAIN, mainTaskStack, &mainTaskControlBlock); + osThreadStaticDef(tskMain, TaskMain, TSK_MAIN_PRIO, 0, TSK_STACK_MAIN, mainTaskStack, &mainTaskControlBlock); tskMainHandle = osThreadCreate(osThread(tskMain), NULL); /* definition and creation of TaskMessaging */ - osThreadStaticDef(tskMsg, TaskMsgJob, osPriorityNormal, 0, TSK_STACK_MSG, msgJobQueTaskStack, &msgJobQueTaskControlBlock); + osThreadStaticDef(tskMsg, TaskMsgJob, TSK_JOBS_PRIO, 0, TSK_STACK_MSG, msgJobQueTaskStack, &msgJobQueTaskControlBlock); tskMsgJobHandle = osThreadCreate(osThread(tskMsg), NULL); /* USER CODE BEGIN RTOS_THREADS */ diff --git a/platform/plat_compat.h b/platform/plat_compat.h index 03b1a1f..076a531 100644 --- a/platform/plat_compat.h +++ b/platform/plat_compat.h @@ -7,6 +7,11 @@ #define VFS_DRIVE_NAME "GEX" +// -------- Priorities ------------- +#define TSK_MAIN_PRIO osPriorityNormal +#define TSK_JOBS_PRIO osPriorityHigh +#define TSK_TIMERS_PRIO 4 // this must be in the 0-7 range + // -------- Static buffers --------- // USB / VFS task stack size #if DISABLE_MSC @@ -17,10 +22,12 @@ // 180 is normally enough if not doing extensive debug logging #define TSK_STACK_MSG 200 // TF message handler task stack size (all unit commands run on this thread) - #define TSK_STACK_IDLE 64 //configMINIMAL_STACK_SIZE #define TSK_STACK_TIMERS 64 //configTIMER_TASK_STACK_DEPTH +#define PLAT_HEAP_SIZE 4096 + + #define BULK_READ_BUF_LEN 256 // Buffer for TF bulk reads #define UNIT_TMP_LEN 512 // Buffer for internal unit operations @@ -28,7 +35,7 @@ #define FLASH_SAVE_BUF_LEN 128 // Malloc'd buffer for saving to flash #define MSG_QUE_SLOT_SIZE 64 // FIXME this should be possible to lower, but there's some bug with bulk transfer / INI parser -#define RX_QUE_CAPACITY 36 // TinyFrame rx queue size (64 bytes each) +#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) diff --git a/tasks/task_main.c b/tasks/task_main.c index 51d5f75..17e9757 100644 --- a/tasks/task_main.c +++ b/tasks/task_main.c @@ -81,9 +81,9 @@ void TaskMain(void const * argument) #endif // CDC - config packets and data in/out - if (msg & (USBEVT_FLAG_EPx_IN(CDC_IN_EP))) { - USBD_CDC_DataIn(&hUsbDeviceFS, CDC_IN_EP); - } +// if (msg & (USBEVT_FLAG_EPx_IN(CDC_IN_EP))) { +// USBD_CDC_DataIn(&hUsbDeviceFS, CDC_IN_EP); +// } if (msg & (USBEVT_FLAG_EPx_IN(CDC_CMD_EP))) { USBD_CDC_DataIn(&hUsbDeviceFS, CDC_CMD_EP); }