Skip to content

Commit

Permalink
CDC get and set line coding, some machines will refuse to connect to …
Browse files Browse the repository at this point in the history
…the COM port without this.
  • Loading branch information
Ant1882 committed Oct 24, 2017
1 parent 9d223fb commit 4cf6591
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions 429-DISCO-Tracealyzer/Src/usbd_cdc_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ uint32_t baudrate;
static int8_t CDC_Control_HS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
{
/* USER CODE BEGIN 10 */
uint8_t tempbuf[7] = {0,0,0,0,0,0,0};
switch (cmd)
{
case CDC_SEND_ENCAPSULATED_COMMAND:
Expand Down Expand Up @@ -237,20 +238,24 @@ static int8_t CDC_Control_HS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
case CDC_SET_LINE_CODING:

break;

case CDC_GET_LINE_CODING:
//I was missing this part
baudrate = 115200;
pbuf[0] = (uint8_t)(baudrate);
pbuf[1] = (uint8_t)(baudrate >> 8);
pbuf[2] = (uint8_t)(baudrate >> 16);
pbuf[3] = (uint8_t)(baudrate >> 24);
pbuf[4] = 0;
pbuf[5] = 0;
pbuf[6] = 8;
break;
tempbuf[0] = pbuf[0];
tempbuf[1] = pbuf[1];
tempbuf[2] = pbuf[2];
tempbuf[3] = pbuf[3];
tempbuf[4] = pbuf[4];
tempbuf[5] = pbuf[5];
tempbuf[6] = pbuf[6];
break;

case CDC_GET_LINE_CODING:
pbuf[0] = tempbuf[0];
pbuf[1] = tempbuf[1];
pbuf[2] = tempbuf[2];
pbuf[3] = tempbuf[3];
pbuf[4] = tempbuf[4];
pbuf[5] = tempbuf[5];
pbuf[6] = tempbuf[6];
break;

case CDC_SET_CONTROL_LINE_STATE:

Expand Down

12 comments on commit 4cf6591

@RomanovSergey
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you use tempbuf[] ?
it is local (automatic) variable in stack, and not save data between calls this function/

@Ant1882
Copy link
Owner Author

@Ant1882 Ant1882 commented on 4cf6591 Mar 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are correct, it turns out that it is enough for the data to be just all 0 anyway... Thanks for pointing out the mistake, I didn't spot it as it worked and I didn't revisit :-)

In later versions of Tracealyzer this may not be required anyway.

@un7pcs
Copy link

@un7pcs un7pcs commented on 4cf6591 May 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! It is work!
I use "static uint8_t tempbuf[7] = {0,0,0,0,0,0,0};"

@nsaow
Copy link

@nsaow nsaow commented on 4cf6591 May 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, help me a lot.

@ShawnHymel
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also helped me out. Thank you!

@gspelectrical
Copy link

@gspelectrical gspelectrical commented on 4cf6591 Feb 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep getting unexpected or unqualified-id before 'switch' .

My original issue is that I could not upload a simple 'blink' file to confirm operation of an STM32 blue pill. Stating that it could not read port -s, which is the serial port. I am assuming that comments above are related to the serial issue.

@Ant1882
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep getting unexpected or unqualified-id before 'switch' .

My original issue is that I could not upload a simple 'blink' file to confirm operation of an STM32 blue pill. Stating that it could not read port -s, which is the serial port. I am assuming that comments above are related to the serial issue.

Not sure what the issue is there, a quick search suggests a number of things... the switch statement being outside of a function for example, It depends on your project really.

@gspelectrical
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Ant,

Thanks for getting in touch!

Yeh the switch statement was out, so that caused the initial issue, the error was on me.

Now its purely down to an error of not being able to read port -s. Which I believe is the serial port, but I have had a search around with no real answer as to how this is caused or how to solve it,

@Ant1882
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the board enumerating on USB? Check device manager if using windows, it should appear under ports shortly after plugging in.

I'm not familiar with the board you are using, I'd suggest looking through some of ST's USB examples and documentation... it could be one of many things, as an example you will have to make sure you are utilising the external crystal as the internal clocking arrangement of most STM32 is not sufficiently accurate for USB to function properly.

Maybe this will explain a little:

https://www.youtube.com/watch?v=YZjnCOun1wU&vl=en

@gspelectrical
Copy link

@gspelectrical gspelectrical commented on 4cf6591 Mar 22, 2020 via email

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MasoudShah
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. It really helped.

@jamiehankins
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I know this is a long time ago, and anyone here has moved on already, but if you're reading this looking for a solution to your Windows SetCommState error, please don't do what's suggested above.

Do this instead.

Declare your line encoding. If you're only using HS or FS, then you only need one.

  static USBD_CDC_LineCodingTypeDef LineCodingHS = {
    115200,                       /* baud rate */
    0x00,                         /* stop bits-1 */
    0x00,                         /* parity - none */
    0x08                          /* nb. of bits 8 */
  };

  static USBD_CDC_LineCodingTypeDef LineCodingFS = {
    115200,                       /* baud rate */
    0x00,                         /* stop bits-1 */
    0x00,                         /* parity - none */
    0x08                          /* nb. of bits 8 */
  };

Then, handle the GET and SET appropriately in the CDC_Control_HS/FS functions like this:

    case CDC_SET_LINE_CODING:
        LineCodingHS.bitrate = (uint32_t) (pbuf[0] | (pbuf[1] << 8) |
        (pbuf[2] << 16) | (pbuf[3] << 24));
        LineCodingHS.format = pbuf[4];
        LineCodingHS.paritytype = pbuf[5];
        LineCodingHS.datatype = pbuf[6];
        break;

    case CDC_GET_LINE_CODING:
        pbuf[0] = (uint8_t) (LineCodingHS.bitrate);
        pbuf[1] = (uint8_t) (LineCodingHS.bitrate >> 8);
        pbuf[2] = (uint8_t) (LineCodingHS.bitrate >> 16);
        pbuf[3] = (uint8_t) (LineCodingHS.bitrate >> 24);
        pbuf[4] = LineCodingHS.format;
        pbuf[5] = LineCodingHS.paritytype;
        pbuf[6] = LineCodingHS.datatype;
        break;

Please sign in to comment.