CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

usb driver & speed problem-Solved!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

usb driver & speed problem-Solved!
PostPosted: Fri May 24, 2019 12:01 pm     Reply with quote

Hello All,

I face strange problem using CCS example "ex_usb_keyboard.c". I using it to send data from rfid reader to PC like keyboard send keys!
It's work OK, except when data have same characters in sequence!
For example : "12345AA678". Send only one 'A' caracter. Any idea what could be the problem?

Some part of code:
Code:

void usb_keyboard_task(void)
{
   unsigned int8 tx_msg[7];
   unsigned int8 leds;
   unsigned int16 scancode;
   
   if(usb_tbe(1))
   {
      memset(tx_msg, 0x00, sizeof(tx_msg));   
     
      if(FlagNewCard){
     
         scancode = ToHidKeyboardScancode(CardIdBuff[CardIdIndex++]);
     
         if (bit_test(scancode, 8))
            tx_msg[0] = KB_MODIFIER_LEFT_SHIFT;
     
         tx_msg[2] = scancode;
         
         if(CardIdIndex > 10)
            FlagNewCard = FALSE;
      }
     
      usb_put_packet(1, tx_msg, sizeof(tx_msg), USB_DTS_TOGGLE);
   }

   //receive NUM LOCK, CAPS LOCK, etc LED status from PC.a
   //we won't do anything with it.
   if (usb_kbhit(1))
   {
      usb_get_packet(1, &leds, 1);
   }
}


CCS ver.5.076
PIC18F24K50

Best Regards!


Last edited by kmp84 on Wed Jun 12, 2019 6:49 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Fri May 24, 2019 12:46 pm     Reply with quote

A search here would have found the answer, Look at this thread:

<http://www.ccsinfo.com/forum/viewtopic.php?t=43330&highlight=usb+keyboard>

Look my reply (one from the end).
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Sat May 25, 2019 2:43 am     Reply with quote

Hello,

Thanks mr.Ttelmah! I didn't search enough! Problem solved!
I also want to ask you is it possible to speed-up incoming data to PC or send data as one packet not byte by byte ?


My edited function code:
Code:

void usb_keyboard_task(void)
{
   unsigned int8 tx_msg[7];
   unsigned int8 leds;
   unsigned int16 scancode;
   
   static unsigned int16 last_scancode;
   
   if(usb_tbe(1))
   {
      memset(tx_msg, 0x00, sizeof(tx_msg));   
     
      if(FlagNewCard){
     
         scancode = ToHidKeyboardScancode(CardIdBuff[CardIdIndex++]);
         
         if(scancode == last_scancode){
           
            scancode = 0;
            CardIdIndex--;
         }
         
         if (bit_test(scancode, 8))
            tx_msg[0] = KB_MODIFIER_LEFT_SHIFT;
     
         tx_msg[2] = scancode;
         
         if(CardIdIndex > 10)
            FlagNewCard = FALSE;
           
         last_scancode = scancode;
      }
     
      //printf("\r\ntx_mes size:%u, scancode=%LX", sizeof(tx_msg), scancode);
     
      usb_put_packet(1, tx_msg, sizeof(tx_msg), USB_DTS_TOGGLE);
   }

   //receive NUM LOCK, CAPS LOCK, etc LED status from PC.a
   //we won't do anything with it.
   if (usb_kbhit(1))
   {
      usb_get_packet(1, &leds, 1);
   }
}


Best Regards!
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Sat May 25, 2019 11:53 pm     Reply with quote

USB itself never sends 'byte by byte'. The communications will be a packet.
8, 16, 32 or 64 bytes. There is a maximum of one packet every mSec for an
HID device. However the Windows keyboard driver does itself not call
the HID device terribly quickly. Remember the keyboard interface is designed
to handle the original PS/2 'traffic', which was limited to a maximum of
16.7Kbaud. It is this that the USB interface handling replicates.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Mon May 27, 2019 2:34 am     Reply with quote

Hello,

I'm not familiar with USB and KBD protocol, but have USB barcode scanner, which also is recognize as my USB reader (HID Keyboard Device) and data are send to PC (Notepad) 3-4 times faster!

Best Regards!
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon May 27, 2019 5:07 am     Reply with quote

In Windows 'Accessibility options'. Turn off 'filter keys'. This otherwise adds
a short delay to the handling of quick key sequences.
Also the key will generally be handled quicker by Windows if you always
send a key released message. Otherwise the keyboard response is limited
by the key repeat timing in Windows.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Mon May 27, 2019 6:14 am     Reply with quote

Hi Mr.Ttelmah,

What is structure of " key released message"? I tried to insert '0x00' between my data bytes, but the speed is slower!

Thanks,
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon May 27, 2019 7:57 am     Reply with quote

Have you tried turning off filter keys?. This makes a huge difference...

You can speed things by up to a factor of 6, by sending multiple keys per
packet.

Currently you are sending one scancode in tx_msg[2]
However the keyboard message will accept up to six scancodes
in tx_msg[2] to tx_msg[7]
So six keys per message, where you are currently sending one....

The '0' message is 'all released', so works reliably. The key released code
is the key scancode with bit 7 set.

So if you put your scancode into tx_msg[2], then the scancode|0x80
into tx_msg[3], then the next scancode into tx_msg[4], and this again
or 0x80 in tx_msg[5], another code into tx_msg[6] and this again or'ed
with 0x80 in tx_msg[7], you will send three complete key pressed and
released sequences in one packet. However _caveat_, 'filter keys', will
reject short scancodes. It is designed to prevent people accidentally
typing a value, by momentary touches on things like touch screens.

Settings
Ease of Access
Keyboard
Use filter keys

Turn this off to allow short key presses to work.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

speed problem Solved!
PostPosted: Wed Jun 12, 2019 6:48 am     Reply with quote

Hello,

Excuse me for the late answer!
The problem with speed of incomming data was task, which serve EM4095 basestation driver. Now if I have new card I just stop EM4095 task and data are send to PC very fast!

Some part of code:

Code:

   for(;;){

      CurTick = get_ticks();

      // service low level USB operations.
      usb_task();
       
      if(usb_enumerated()){
         
         usb_keyboard_task();
         
         if(!FlagNewCard){
           
            EM4095_Task();
         }
      }


Best Regards!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group