PDA

View Full Version : USB CDC : how can the firmware determine what baud rate etc. is being emulated



towlerg
29th August 2017, 22:16
Playing with the idea of remote serial via USB.

tumbleweed
30th August 2017, 09:35
The USB CDC SET_LINE_CODING request message (0x20) is used to configure the baud rate, stop-bits, parity, and number of data bits.

towlerg
30th August 2017, 13:58
Hi Tumbleweed, oh that sounds good. Any hints as to how to use this function appreciated.

towlerg
30th August 2017, 23:00
I did a little reading on the subject, I think I understand the principle but not the application. I think send a particular sequence of bytes to a particular end point and ger back the baud rate etc. I compiled the CDC example and found this code snippet. Can I just all this code rather than for the buffer and tx?


USBCHECKCDCREQUEST3
movlw cSET_LINE_CODING
cpfseq USB_aSetupPkt + cUSB_bRequest
bra __USB_CHECKCDCREQUEST7
movlw cLINE_CODING_LENGTH
movwf USB_wOutCount
clrf USB_wOutCountH
movlw (USB_aLineCoding & 0XFF)
movwf USB_wDst
movlw ((USB_aLineCoding >> 8) & 0XFF)
movwf USB_wDstH
bsf USB_bOutFo,cUSB_Busy
return

Seems a little odd that all the other similar labels were prebended with a double underscore.

tumbleweed
30th August 2017, 23:53
George,

I'm not 100% up on the inner workings of the stack, but it looks like that little snippet is where the driver recognizes the SET_LINE_CODING request and gets ready to copy the data from the packet into the 7-byte array defined in USB_MemAlloc.inc


Dim USB_aLineCoding[cLINE_CODING_LENGTH] As Byte At USB_aCtrlTrfData + cUSB_EP0_BUFF_SIZE

'
' USB_aLineCoding Byte and Bit Aliases
'
$define cUSB_dwDTERate 0 ' Complex data structure
$define cUSB_bCharFormat 4
$define cUSB_bParityType 5
$define cUSB_bDataBits 6



After a SET_LINE_CODING command that array should contain the baudrate (4 bytes at index 0), stop bits (1 byte at index 4), parity (1 byte at index 5), and data bits (1 byte at index 6) settings. You should be able to get the data from there.

towlerg
31st August 2017, 15:24
Now why didn't I think of that.

Edit. actually my problem wasn't understanding the data returned it was with the byte sequence and the endpoint (which I believe is 0) to tx to.

I tried adding this to an otherwise working CDC program but it hangs


' hangs if this is encluded
' Clear Array1
' Array1 = cGET_LINE_CODING , 0, cLINE_CODING_LENGTH, 0, 0, 0, 0, 0 ' Fill the string with Null terminated characters

' Repeat
' USBOut 0, Array1, Auto
' Until Carry_Flag = 0 ' Keep trying if the microcontroller does not have control over the buffer