1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Trying to understand USB protocol

Discussion in 'Oshonsoft' started by graemeian, Aug 25, 2015.

  1. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    I purchased the Oshnsoft PIC16 software for USB.
    The sample is as follows:
    Define CLOCK_FREQUENCY = 16
    Define CONFIG1 = 0x0f84
    Define CONFIG2 = 0x1def
    OSCCON = 0x3c 'intosc=16mhz

    UsbSetVendorId 0x1234
    UsbSetProductId 0x1234
    UsbSetVersionNumber 0x1122
    UsbSetManufacturerString "OshonSoft.com"
    UsbSetProductString "Generic USB HID Device"
    UsbSetSerialNumberString "1111111111"
    UsbOnIoInGosub usbonioin
    UsbOnIoOutGosub usbonioout
    UsbOnFtInGosub usbonftin
    UsbOnFtOutGosub usbonftout

    AllDigital
    ANSELA.4 = 1
    TRISC = 0
    PORTC = 0xff
    UsbStart
    PORTC = 0

    Dim an3 As Byte

    loop:
    Adcin 3, an3
    If an3 < 50 Then
    PORTC = 0
    UsbStop
    While an3 < 100
    Adcin 3, an3
    Wend
    PORTC = 0xff
    UsbStart
    PORTC = 0
    Endif
    UsbService
    Goto loop
    End

    usbonftout:
    Toggle PORTC.0
    Return

    usbonftin:
    UsbFtBuffer(0) = UsbFtBuffer(0) - 1
    UsbFtBuffer(1) = UsbFtBuffer(1) - 1
    UsbFtBuffer(2) = UsbFtBuffer(2) - 1
    UsbFtBuffer(3) = UsbFtBuffer(3) - 1
    UsbFtBuffer(4) = UsbFtBuffer(4) - 1
    UsbFtBuffer(5) = UsbFtBuffer(5) - 1
    UsbFtBuffer(6) = UsbFtBuffer(6) - 1
    UsbFtBuffer(7) = UsbFtBuffer(7) - 1
    Return

    usbonioout:
    Toggle PORTC.1
    Return

    usbonioin:
    UsbIoBuffer(0) = UsbIoBuffer(0) + 1
    UsbIoBuffer(1) = UsbIoBuffer(1) + 1
    UsbIoBuffer(2) = UsbIoBuffer(2) + 1
    UsbIoBuffer(3) = UsbIoBuffer(3) + 1
    UsbIoBuffer(4) = UsbIoBuffer(4) + 1
    UsbIoBuffer(5) = UsbIoBuffer(5) + 1
    UsbIoBuffer(6) = UsbIoBuffer(6) + 1
    UsbIoBuffer(7) = UsbIoBuffer(7) + 1
    Return
    I am having some trouble with the logic.
    When the program runs, it cycles through the loop statement.

    I do not understand how to get data to and from the PC.
    It looks like these statements get used only once at the beginning
    UsbOnIoInGosub usbonioin
    UsbOnIoOutGosub usbonioout
    UsbOnFtInGosub usbonftin
    UsbOnFtOutGosub usbonftout
    I see that USB start and service are in the main loop. However, I do not seem to understand the logic.
    It looks like
    UsbOnIoOutGosub usbonioout
    will call the usbonioout subroutine and what is ever in the subroutine will pass out of the USB port?
    If that is the case, can I pass more than a bit through the USB port?
    I am using PIC16F1459.

    Would someone please provide an explanation of the logic?

    Thank you.
     
  2. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK
    This does all as necessary..

    If you load the feature buffer and the io buffer full 'o' bytes... The PC program receives said bytes you can then send the two buffers back to the pic!!!

    The functions
    are just function declarations....

    Just call usbonioout and usbonftout after you filled them!!

    usbonftin and usbonioin read the PC -> pic transitions...
     
  3. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    Thank you Ian. You got me started but now I am still confused. I wrote the following code:
    I expect to get an3 incremented by one but instead the incremental amount is 5.
    I am using Jan's vb.net program.
    Help.

    UsbOnIoInGosub input_report_before_sending
    UsbOnIoOutGosub output_report_received
    UsbOnFtInGosub feature_report_before_sending
    UsbOnFtOutGosub feature_report_received

    AllDigital
    TRISC = 32 '100000

    Dim an3 As Byte
    UsbStart
    loop:

    an3 = an3 + 1
    WaitMs 100


    UsbIoBuffer(3) = an3
    UsbIoBuffer(4) = an3

    Gosub input_report_before_sending
    Gosub output_report_received
    Gosub feature_report_before_sending
    Gosub feature_report_received


    UsbService

    Goto loop
    End

    feature_report_received:
    Toggle PORTC.0
    WaitMs 100
    Return

    feature_report_before_sending:
    UsbFtBuffer(0) = UsbFtBuffer(0) - 1
    UsbFtBuffer(1) = UsbFtBuffer(1) - 1
    UsbFtBuffer(2) = UsbFtBuffer(2) - 1
    UsbFtBuffer(3) = UsbFtBuffer(3) - 1
    UsbFtBuffer(4) = UsbFtBuffer(4) - 1
    UsbFtBuffer(5) = UsbFtBuffer(5) - 1
    UsbFtBuffer(6) = UsbFtBuffer(6) - 1
    UsbFtBuffer(7) = UsbFtBuffer(7) - 1
    Return

    output_report_received:
    Toggle PORTC.1
    WaitMs 100
    Return

    input_report_before_sending:
    UsbIoBuffer(0) = UsbIoBuffer(0) + 1
    UsbIoBuffer(1) = UsbIoBuffer(1) + 1
    UsbIoBuffer(2) = UsbIoBuffer(2) + 1
    UsbIoBuffer(3) = UsbIoBuffer(3) + 1
    UsbIoBuffer(4) = UsbIoBuffer(4) + 1
    UsbIoBuffer(5) = UsbIoBuffer(5) + 1
    UsbIoBuffer(6) = UsbIoBuffer(6) + 1
    UsbIoBuffer(7) = UsbIoBuffer(7) + 1
    Return
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,160
    Likes:
    909
    Location:
    Rochdale UK

    you need a "ping pong " type of communication.... Use the PC to "Request" the packets..
    If your transfer is slower than the pic ( 5 times slower it would seem ) then your result is understandable...

    Increase the AN3 value inside the feature received function... It should work as expected!!

    Code (basic):

    feature_report_received:
    Toggle PORTC.0
    AN3 = AN3 +1
    WaitMs 100
    Return
     
     
  6. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    Thank you very much. I can now get the desired result.
     
  7. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
  8. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    Thank you for the demo. I can now send and receive data to and from the PC using USB but I cannot do much else as the USB protocol locks out other activity.
    Here is an example: I am trying to receive data through a serial port and when the PC prompts, the received bytes will pass through the USB port to the PC.
    The PC should receive 0 to 8 bytes.
    If I put UsbStart in the loop, PORTC.1 never toggles.
    Without UsbStart in the loop, I cannot connect to PC using Eric's VB program or Jan's.
    What is incorrect with this software/function protocol?

    Dim i as Byte
    Dim x(8) as Byte
    i = 0
    UsbStart
    loop:
    'This program needs to wait for some bytes to be received through the serial port .
    'The PIC16F1459 will respond to a PC call to send the data through the USB port.
    'set up PIC chip to receive data through serial port
    'Wait for receive register to get a byte from serial port (portc.5)
    If PIR1.RCIF = 1 Then ' Is this correct? Otherwise, how do I use these?
    Define SERIN_TIMEOUT_REG = INTCON
    Define SERIN_TIMEOUT_BIT = T0IF

    Serin PORTC.5, 9600, x(i)
    WaitMs 10
    i = i + 1
    If i = 8 then i = 0 '
    Endif
    UsbService
    Toggle PORTC.1 '
    WaitMs 50
    Toggle PORTC. 1' I have an LED attached and I want to see it blink to confirm the loop
    WaitMs 50
    Goto loop


    Thanks in advance..

    Regards
     
    Last edited: Oct 6, 2015
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi gra'
    If you post your full program listing [ as a post attachment] I will run it in my system, see if I can locate the problem.

    E

    EDIT:
    Please confirm the PIC type, the IDE says a 16F1459 does not have USB support.??
     

    Attached Files:

    Last edited: Oct 7, 2015
  10. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    Eric,
    Thank you very much. Attached is the code.
    I saw that also but I thought it was a typo as it lists all of the chips with an asterisk. I am using PIC16 Simulator with basic and a few other options. On the web, the creator is listing a sample USB code for the 16f1459 chip.
    I have attached a sample code that does send bytes to the PC via USB. However, I cannot get it to receive bytes through PORTC.5( the RX port) . I am using the PIC low count USB board where LEDs are connected to ports C 0-3.
    Please review my code and see why I cannot read the serial port. I am using a square wave output from my o-scope in the hopes that I would get some random byte values as the PIC does not appear to have parity check. I had good luck with Serout as I output a string of "U"s to get sections of square waves.
    Regards.
    G
     

    Attached Files:

  11. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi.
    Downloaded your code OK.
    I will try to reply in next 24 hrs. [its late evening here]

    E
     
  12. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi,
    At what time interval are you receiving data on the RXD input line.?
    If the calling UsbService interval is longer than one second you will have a problem.
    Have you considered the UsbStop command.?

    Extract from the OSH Manual.
    UsbService statement should be executed as often as possible,
    because several calls to UsbService routine are necessary to process each of the USB events.
    The firmware will work correctly even if you execute UsbService only once per second.


    E

    EDIT:
    Why are you using a software UART on RC5 rather than the hardware UART on RB5.?

    Clip from your Code:
    'set up PIC chip to receive data through serial port
    'Wait for receive register to get a byte from serial port (portc.5)
    INTCON.GIE = 1 'this enables global interrupts
    If PIR1.RCIF = 1 Then 'you are waiting for a hardware RXD intr.??
    'but you have not Enabled the RCIE for the hardware Uart.??
    'you are not using the hardware Uart
    Serin PORTC.5, 9600, x(i)
    WaitMs 10
    i = i + 1
    If i = 5 Then i = 0
    Endif
     
    Last edited: Oct 8, 2015
  13. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi gra'
    I see why you have been forced to use Serin, its because Oshonsoft does not support the hardware UART.

    I have come to the conclusion that the supposed upgrades to the Oshonsoft PIC list are a load of hooey.!:mad:
    Over the past 12 months I have run into problem after problem with other later PIC's that do not have support for this or that feature.:banghead:

    All my debug reports regarding the shortcomings of this IDE, except for one, have been ignored, I was advised to fix a compiler problem by adding more decoupling caps.!!

    Eric

    How do you want to proceed.?
     
  14. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    E,
    Thanks for your help. The RCIE was an oversight as I am mixing Serin and RCREG . The final design will remove the waitMS for the portc3 Toggle.
    Here is another version. I have a sister PIC piping data to portc.5 of this PIC16f1459. I am not seeing data change on the HID bytes.
    Do you see any blatant errors in this new version?

    D
     

    Attached Files:

  15. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
    hi D.
    Downloaded your code OK
    At what time interval are you receiving data on the RXD input line, then transmitting to the PC using USB..?

    eg: if its a greater period than 1 second so that the UsbService call interval is greater than 1 second, you will have a problem.

    E
     
  16. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,181
    Likes:
    644
    Location:
    Ex Yorks' Hants UK
  17. graemeian

    graemeian New Member

    Joined:
    Jul 22, 2008
    Messages:
    17
    Likes:
    0
    The PIC16f1459 will need to wait as much as a day for the serial data. This PIC will input serial and output USB when an activity occurs. Eight bytes of serial data will be be received through RX. Software from a PC will be polling the USB port looking for data from the PIC. The polling interval will be about every 5 seconds.
     

Share This Page