Browse Source

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..
irr_shared
John-Mark Gurney 2 years ago
parent
commit
2a35f5648e
5 changed files with 35 additions and 25 deletions
  1. +7
    -7
      main.c
  2. +1
    -1
      misc.c
  3. +1
    -1
      stm32/usb/usb_device.c
  4. +22
    -14
      stm32/usb/usbd_cdc_if.c
  5. +4
    -2
      stm32/usb/usbd_cdc_if.h

+ 7
- 7
main.c View File

@@ -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));



+ 1
- 1
misc.c View File

@@ -47,7 +47,7 @@ wait_for_vcp(void)
{

for (;;) {
if (vcp_status(&hUsbDeviceFS.request))
if (vcp_status())
break;
}
}


+ 1
- 1
stm32/usb/usb_device.c View File

@@ -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 */
}



+ 22
- 14
stm32/usb/usbd_cdc_if.c View File

@@ -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 <stdarg.h>

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 */



+ 4
- 2
stm32/usb/usbd_cdc_if.h View File

@@ -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 */

/**


Loading…
Cancel
Save