HID Keyboard Demo Problem

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

HID Keyboard Demo Problem

Post by xtrabit » Tue May 03, 2016 1:44 pm

hi guys, I'm new here.

Just bought SFC and tried to compile HID Keyboard example at here

It wont compile and i get "Identifier not defined: TXReportRAM" and "Identifier not defined: RXReportRAM" error. But these are defined in USBHID.bas file. Tried USB Library 1.4.3 but removed them.

How can i fix it?

My code:

Code: Select all

Public Structure TKeyReport
 Modifier   As Byte 
 Reserved   As Byte 
 KeyArray0  As Byte 
 KeyArray1  As Byte
 KeyArray2  As Byte
 KeyArray3  As Byte
 KeyArray4  As Byte
 KeyArray5  As Byte 
End Structure 
Public Dim KeyReport As TKeyReport Absolute TXReportRAM

Public Structure TLedsReport
 _byte      As Byte 
 NumLock    As _byte.0
 CapsLock   As _byte.1
 ScrollLock As _byte.2 
End Structure  
Public Dim LedsReport As TLedsReport Absolute RXReportRAM

Public Sub Rd()
If HID.DataAvailable() Then ReadReport Cap = LedsReport.CapsLock Scr = LedsReport.ScrollLock Num = LedsReport.NumLock EndIf
End Sub
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Wed May 04, 2016 9:40 am

Which USB library are you using... the original one that comes with the compiler or the modified version 1.4.3 from the User Modules section on the website?

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Wed May 04, 2016 12:47 pm

Tried 1.4.3 but it needed rewrite for the demo software. So i returned to original usb library from SFC. Now using usbhid.bas v1.1 10/01/2007.

If you can help me to convert this demo code to 1.4.3, i prefer to use that.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Wed May 04, 2016 3:40 pm

In V1.4.3 you can specify a "UserReports" file.
This is where you'd add the structure definitions for your reports.

ie- create a file "KeybdReports.bas" with the following:

Code: Select all

module UserReports

// TX report...
public structure TTXReport
 Modifier   As Byte 
 Reserved   As Byte 
 KeyArray0  As Byte 
 KeyArray1  As Byte
 KeyArray2  As Byte
 KeyArray3  As Byte
 KeyArray4  As Byte
 KeyArray5  As Byte 
end structure

// RX report...
public structure TRXReport
 _byte      As Byte 
 NumLock    As _byte.0
 CapsLock   As _byte.1
 ScrollLock As _byte.2 
end structure

end module
Now, using the main_hid.bas as an example, add an #option that has the filename just created

Code: Select all

  #option USER_CUSTOM_HID_REPORTS = "KeybdReports.bas"
in place of the following code (You'll have to comment out the "#option DEMO_EXAMPLE = 1") or just remove all the demo stuff

Code: Select all

//
//-----------------------------------------------------------------------------
// optional custom HID report structures for ReadReport()/WriteReport()
//-----------------------------------------------------------------------------
// if you want to use custom report structures see the instructions
// in USBHID.bas for creating your own report definitions file, 
// and set '#option USER_CUSTOM_HID_REPORTS' to the name of your 
// custom report file. otherwise, leave the option undefined.
// example files can be found in the UserHIDReports directory
//
// here we change the filename based on the selected demo example
#if (DEMO_EXAMPLE = 1)
  #option USER_CUSTOM_HID_REPORTS = "UserReportsExample1.bas"
#elseif (DEMO_EXAMPLE = 2)
  #option USER_CUSTOM_HID_REPORTS = "UserReportsExample2.bas"
#else
    // no custom reports file req'd
#endif
In your main module add

Code: Select all

Public Dim KeyReport As HID.TXReport
Public Dim LedsReport As HID.RXReport

Public Sub Rd()
	If HID.DataAvailable() Then 
		HID.ReadReport()
		Cap = LedsReport.CapsLock 
		Scr = LedsReport.ScrollLock 
		Num = LedsReport.NumLock 
	EndIf
End Sub
See if that works. I'm a bit tied up at the moment to actually try it...

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Thu May 05, 2016 10:20 am

Thanks for help.

Returned to 1.4.3, but code can't get Caps Lock status from PC. My SendKey() code is not working too.

Here is my code:

Code: Select all

Device = 18F2550
Clock = 48

Config
PLLDIV = 5,CPUDIV = OSC1_PLL2,USBDIV = 2,FOSC = HSPLL_HS,VREGEN = ON,
PWRT = ON,BOR = ON, MCLRE = OFF

#option HID_BUFFER_SIZE = 8
#option USB_DESCRIPTOR = "Key_Desc.bas"
#option WDT = true
#option USER_CUSTOM_HID_REPORTS = "HIDReports.bas"

Include "usbhid.bas"  
Include "Timer.bas"

Public Dim
Pdl As PORTA.0,PDL2 As PORTA.1,PDL3 As PORTA.2,
LED3 As PORTB.3, LED2 As PORTB.2, LED1 As PORTB.1, LED0 As PORTB.0,
Num As Bit,Cap As Bit,Scr As Bit

Public Dim KeyReport As HID.TXReport
Public Dim LedsReport As HID.RXReport

Public Sub Rd()
   If HID.DataAvailable() Then
      HID.ReadReport()
      Cap = LedsReport.CapsLock
      Scr = LedsReport.ScrollLock
      Num = LedsReport.NumLock
   EndIf
End Sub

Public Sub SendKey(PKey As Byte, pModifier As Byte = $00) 
 KeyReport.Modifier  = pModifier
 KeyReport.KeyArray0 = PKey
 HID.WriteReport()
End Sub

Sub Main()
Repeat Until Attached
Clear(KeyReport)
rd

While true
If Cap = 1 Then Low(LED1) High(LED2) Else Low(LED2) High(LED1) EndIf
SendKey(34)
Rd
Wend
End Sub Low(LED3) ADCON1 = 15 Main
I always get Cap = 0.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Fri May 06, 2016 11:01 am

I'll give it a try and see what I find.

Did you have to modify any of the other files from the demo? If so, could you post them as well?

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Fri May 06, 2016 11:15 am

Hi, my files are at the attachment. Thanks. Cant send or receive data. replaced SFC original usb library files with 1.4.3 files.
Attachments
Test2.zip
(3.53 KiB) Downloaded 183 times
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Fri May 06, 2016 2:29 pm

I can't seem to get it to work either, and I've tried adapting the original demo example.

The other HID code works ok and it seems to enumerate, but I can't get it to send/rcv reports.

There must be something going on with the buffers or descriptors. I'll try to look at it some more over the weekend.

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Fri May 06, 2016 3:38 pm

Thanks for help. Waiting for your work.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Fri May 06, 2016 10:19 pm

I can't even get the USBKeybdDemo example to work using the original USB library... seems to be having issues sending the HID report descriptor (Public Const hid_rpt01(63) As Byte), so it's not getting installed properly on the PC.

I'm pretty sure this used to work, so maybe something has changed. Anyone else try this lately?

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Sat May 07, 2016 5:48 am

It gives absurd warnings (Inline procedure called from within context save block) with original library, when converted to 1.4.3, compiles but don't work. USB Keyboard Demo code works well with SFC 2.1.0.0 from 2008, but i want to use licensed and up to date program, library and the compiler.
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Sat May 07, 2016 10:40 am

It gives absurd warnings (Inline procedure called from within context save block) with original library
Those warnings aren't really absurd... they're to be expected.

At one point (perhaps version 2.2.1.5??) the compiler was changed so that if you use an inline procedure in an interrupt handler the compiler would ignore the 'inline' and just use a regular call/return. The warning lets you know that the compiler has detected this. The details escape me now, but that was to fix some abnormal behavior.

You can usually ignore those warnings but it is something you might want to know about.
USB Keyboard Demo code works well with SFC 2.1.0.0 from 2008
And I'm pretty sure it's worked after that too... I just can't get it working now for some reason. This is going to take a little digging around...

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Sat May 07, 2016 2:31 pm

Not sure I understand why, but for some reason you must have a serial number defined.

Here's a version of the original USB HIDKeyboard demo adapted for V1.4.3. I incorporated it into the 'main_hid.bas' example file as '#option DEMO_EXAMPLE = 4'

See if that works for you...
Attachments
usb_hid_kbd.zip
(7.34 KiB) Downloaded 192 times

User avatar
xtrabit
Posts: 23
Joined: Tue May 03, 2016 10:04 am

Re: HID Keyboard Demo Problem

Post by xtrabit » Mon May 09, 2016 7:28 am

Thanks. It now works with 1.4.3. But i cant use ISRTimer with this code. I think USB Service must be disabled to use ISRTimer. How can get it work?
"See God, then brake" -Kevin Schwantz

Jerry Messina
Swordfish Developer
Posts: 1470
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Re: HID Keyboard Demo Problem

Post by Jerry Messina » Mon May 09, 2016 9:31 am

Leave the USB using the high-priority interrupt and set ISRtimer to use the low-priority interrupt...

Code: Select all

#option TIMER_PRIORITY = ipLow
include "isrtimer.bas"

Post Reply