From 2a35f5648edeee8b829885c9e596ea4758cf4e95 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Tue, 6 Jul 2021 17:34:37 -0700 Subject: [PATCH] use funopen/stdio to buffer output, "fix" for FreeBSD... convert to using stdio to line buffer usb output... This reduces usb traffic like when doing hexdump... The delay works, but could be a problem on slower systems... It's been reported, so hopefully a fix will happen.. --- main.c | 14 +++++++------- misc.c | 2 +- stm32/usb/usb_device.c | 2 +- stm32/usb/usbd_cdc_if.c | 36 ++++++++++++++++++++++-------------- stm32/usb/usbd_cdc_if.h | 6 ++++-- 5 files changed, 35 insertions(+), 25 deletions(-) diff --git a/main.c b/main.c index b1bbdfc..a2c829c 100644 --- a/main.c +++ b/main.c @@ -184,6 +184,7 @@ process_line(char *start, char *end) return; } usb_printf("line: %.*s", end - start, start); + fflush(vcp_usb); } int @@ -199,16 +200,15 @@ main(void) /* turn on LED */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET); + setlinebuf(vcp_usb); + #if 1 wait_for_vcp(); /* - * This is required to use w/ FreeBSD. Not sure why but if this - * delay isn't here, the code will hang waiting for the output to - * be sent, and it never does get sent. I believe this is because - * the packet to enable sending the data hasn't arrived yet, and - * this code fails to handle that case, and drops the data on the - * floor. + * This is required to use w/ FreeBSD. This is an issue w/ the + * STM32 Core USB library: + * https://github.com/STMicroelectronics/STM32CubeL1/issues/10 */ DelayMs(50); usb_printf("starting...\r\n"); @@ -227,7 +227,7 @@ main(void) Radio.SetChannel(914350 * 1000); v = Radio.Random(); - usb_printf("rr: %#x\r\n", v); + usb_printf("rr: %#lx\r\n", v); usb_printf("rssi: %#hx\r\n", Radio.Rssi(MODEM_LORA)); diff --git a/misc.c b/misc.c index 0e9c847..7f8d6df 100644 --- a/misc.c +++ b/misc.c @@ -47,7 +47,7 @@ wait_for_vcp(void) { for (;;) { - if (vcp_status(&hUsbDeviceFS.request)) + if (vcp_status()) break; } } diff --git a/stm32/usb/usb_device.c b/stm32/usb/usb_device.c index 7acc5c0..6ba479d 100755 --- a/stm32/usb/usb_device.c +++ b/stm32/usb/usb_device.c @@ -87,7 +87,7 @@ void MX_USB_DEVICE_Init(void) } /* USER CODE BEGIN USB_DEVICE_Init_PostTreatment */ - + vcp_usb = funopen(&vcp_usb, NULL, usb_write, NULL, NULL); /* USER CODE END USB_DEVICE_Init_PostTreatment */ } diff --git a/stm32/usb/usbd_cdc_if.c b/stm32/usb/usbd_cdc_if.c index 35fd4d5..bb1daa9 100755 --- a/stm32/usb/usbd_cdc_if.c +++ b/stm32/usb/usbd_cdc_if.c @@ -25,6 +25,15 @@ /* USER CODE BEGIN INCLUDE */ uint8_t* CDC_RX_BUFFER = NULL ; uint16_t CDC_RX_LEN = 0; + +/* + * PSTN v1.20, ยง 6.3.12 SetControlLineState + * + * wValue: + * D0 DTR + * D1 RTS + */ +static uint16_t line_status; /* USER CODE END INCLUDE */ /* Private typedef -----------------------------------------------------------*/ @@ -184,6 +193,10 @@ static int8_t CDC_DeInit_FS(void) static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) { /* USER CODE BEGIN 5 */ + USBD_SetupReqTypedef *req; + + req = (USBD_SetupReqTypedef *)pbuf; + switch(cmd) { case CDC_SEND_ENCAPSULATED_COMMAND: @@ -232,7 +245,7 @@ static int8_t CDC_Control_FS(uint8_t cmd, uint8_t* pbuf, uint16_t length) break; case CDC_SET_CONTROL_LINE_STATE: - + line_status = req->wValue; break; case CDC_SEND_BREAK: @@ -300,23 +313,16 @@ uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len) /* USER CODE BEGIN PRIVATE_FUNCTIONS_IMPLEMENTATION */ #include -uint16_t vcp_status( USBD_SetupReqTypedef *req){ - if(req->bRequest ==CDC_SET_CONTROL_LINE_STATE){ - if(req->wValue){ - return 1; - } - } - return 0; +uint16_t vcp_status(){ + return line_status & 0x1; } -void usb_printf(const char *format, ...) +int +usb_write(void *cookie, const char *buf, int length) { - va_list args; - uint32_t length; - va_start(args, format); - length = vsnprintf((char *)UserTxBufferFS, APP_TX_DATA_SIZE, (char *)format, args); - va_end(args); + length = MIN(length, APP_TX_DATA_SIZE); + memcpy((char *)UserTxBufferFS, buf, length); for (;;) { if (CDC_Transmit_FS(UserTxBufferFS, length) == USBD_BUSY) { HAL_Delay(1); @@ -324,6 +330,8 @@ void usb_printf(const char *format, ...) } break; } + + return length; } /* USER CODE END PRIVATE_FUNCTIONS_IMPLEMENTATION */ diff --git a/stm32/usb/usbd_cdc_if.h b/stm32/usb/usbd_cdc_if.h index 87da7a5..b46f64e 100755 --- a/stm32/usb/usbd_cdc_if.h +++ b/stm32/usb/usbd_cdc_if.h @@ -116,8 +116,10 @@ extern uint16_t CDC_RX_LEN; uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len); /* USER CODE BEGIN EXPORTED_FUNCTIONS */ -void usb_printf(const char *format, ...); -uint16_t vcp_status( USBD_SetupReqTypedef *req); +FILE *vcp_usb; +int usb_write(void *cookie, const char *buf, int len); +#define usb_printf(...) fprintf(vcp_usb, __VA_ARGS__) +uint16_t vcp_status(void); /* USER CODE END EXPORTED_FUNCTIONS */ /**