shell/chprintf use with usb-raw (OTG) driver

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
Noctámbulo
Posts: 3
Joined: Sat Jul 04, 2020 7:36 am

shell/chprintf use with usb-raw (OTG) driver

Post by Noctámbulo »

Hi, my first post here.

First of all, a very big thank you for such a powerful and beauty piece of code, marvelous!

On a stm32f4-discovery I have been able to successfully run (separately and with the respective modifications) both the shell demo and the usb demo. Then, naively, I tried to "merge" the two demos by simply passing as the shell thread argument the ShellConfig structure of the main thread with the reference USBD2 (USBD1 in the case of stm32f4-discovery) as the first field and NULL as the second field and, of course, it didn't work. The question is, what would be the proper approach to overcome the problem, act on the shell and/or chprintf implementation? I don't even dare to consider the usb side. Finally, does any of what I'm saying make sense? Is it worth making the attempt? What would be the advantages/disadvantages regarding the usb-serial implementation?

Thanks in advance, Diego.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: shell/chprintf use with usb-raw (OTG) driver

Post by Giovanni »

Hi,

USB raw is not meant to work with streams and shell. If you look for the normal USB demo (non-raw) you will find an example with shell working.

Giovanni
Noctámbulo
Posts: 3
Joined: Sat Jul 04, 2020 7:36 am

Re: shell/chprintf use with usb-raw (OTG) driver

Post by Noctámbulo »

Hi, and thanks for the quick reply.

I meant something like this (with the appropriate inclusions and guards):

Code: Select all

uint8_t txbuf[BUF_SIZE];

size_t size = chsnprintf((char *)txbuf, BUF_SIZE, "ChibiOS/RT Shell\r\n");
msg_t msg = usbTransmit(&USBD1, USBD1_DATA_REQUEST_EP, txbuf, size);

/* instead of */

chprintf(chp, "ChibiOS/RT Shell" SHELL_NEWLINE_STR);


or it simply has no benefit over the "traditional" method?

Diego.
mikeprotts
Posts: 166
Joined: Wed Jan 09, 2019 12:37 pm
Has thanked: 19 times
Been thanked: 31 times

Re: shell/chprintf use with usb-raw (OTG) driver

Post by mikeprotts »

I don't think what you are trying to achieve is clear. Are you trying to communicate with a USB peripheral with the discovery board as host or accessing the discovery board by a program running on a PC?

Mike
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: shell/chprintf use with usb-raw (OTG) driver

Post by Giovanni »

Hi,

The difference between the USB demo and the USB-RAW demo is that the former uses a buffering layer offering a stream interface, it is the stream interface that chprintf() and the shell need in order to work.

If you remove the buffering then you don't have a stream, just those transmit and receive functions, you need to work with your own memory buffers in order to exchange data.

Giovanni
Noctámbulo
Posts: 3
Joined: Sat Jul 04, 2020 7:36 am

Re: shell/chprintf use with usb-raw (OTG) driver

Post by Noctámbulo »

Hi,

@mikeprotts
No, just the regular CLI, more details below.

@Giovanni
I understand, or at least I think so, that's why "txbuf" is provided in the code snippet shown above. Surely the best place to declare it would be at shell module level with a default buffer size or set by the user code (e.g. in shellconf.h), thus:

Code: Select all

/**
 * @file    shell.c
 * @brief   Simple CLI shell code.
 */

...;
#if HAL_USE_SERIAL_USB != TRUE
static uint8_t txbuf[TXBUF_SIZE];
#endif
...;

/**
 * @brief   Shell thread function.
 */
THD_FUNCTION(shellThread, p) {
  ...;
  /* this one */
  shprintf(chp, "ChibiOS/RT Shell" SHELL_NEWLINE_STR);
    /* instead of */
  chprintf(chp, "ChibiOS/RT Shell" SHELL_NEWLINE_STR);
  ...;
}

/**
 * @brief   The new shprintf() would be:
 */
int shprintf(void *chp, const char *fmt, ...) {
  int retval;

  va_list ap;
  va_start(ap, fmt);
#if HAL_USE_SERIAL_USB == TRUE
  retval = chvprintf((BaseSequentialStream *)chp, fmt, ap);
#else
  size_t size = chvsnprintf((char *)txbuf, TXBUF_SIZE, fmt, ap);
  retval = usbTransmit((USBDriver *)chp, USBD1_DATA_REQUEST_EP, txbuf, size);
#endif
  va_end(ap);

  return retval;
}


Before going any further, it's just that, based on this and all the benefits it brings, I thought it would be a good opportunity to improve the shell module.

Anyway, if you consider all of this pointless, just let me know :)

Diego.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: shell/chprintf use with usb-raw (OTG) driver

Post by Giovanni »

If it is useful for you then it is not pointless.

Giovanni
Post Reply