Adaptation/GUID-2E42E7EA-FED8-522C-8A5F-F65D799476C9.dita
changeset 15 307f4279f433
equal deleted inserted replaced
14:578be2adaf3e 15:307f4279f433
       
     1 <?xml version="1.0" encoding="utf-8"?>
       
     2 <!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
       
     3 <!-- This component and the accompanying materials are made available under the terms of the License 
       
     4 "Eclipse Public License v1.0" which accompanies this distribution, 
       
     5 and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
       
     6 <!-- Initial Contributors:
       
     7     Nokia Corporation - initial contribution.
       
     8 Contributors: 
       
     9 -->
       
    10 <!DOCTYPE concept
       
    11   PUBLIC "-//OASIS//DTD DITA Concept//EN" "concept.dtd">
       
    12 <concept id="GUID-2E42E7EA-FED8-522C-8A5F-F65D799476C9" xml:lang="en"><title>Keyboard Driver Implementation Tutorial</title><shortdesc>This topic describes how to implement an interrupt driven
       
    13 keyboard driver. </shortdesc><prolog><metadata><keywords/></metadata></prolog><conbody>
       
    14 <p>The steps are: </p>
       
    15 <ul>
       
    16 <li id="GUID-BC3A7259-A750-5D5F-8363-6F8BBC03E8EB"><p>Implement the
       
    17 driver entry point function and initialisation code. This function
       
    18 is called when the extension is loaded. </p> <p>The initialisation
       
    19 code binds the hardware interrupt to the Interrupt Service Routine
       
    20 (ISR) and enables the interrupt. </p> </li>
       
    21 <li id="GUID-3A050F21-FE10-5C0F-8BFE-C44692C0BDE2"><p>Implement an
       
    22 ISR to handle key events. The ISR queues a keyboard DFC. </p> </li>
       
    23 <li id="GUID-6580FC61-3D9E-5370-88AD-50CC1FB28E5F"><p>Implement a
       
    24 keyboard DFC. This function interrogates the keyboard, converts the
       
    25 scancode to a keypress event, and places it onto the kernel's event
       
    26 queue. </p> </li>
       
    27 </ul>
       
    28 <section id="GUID-B4B6A6C6-39D0-49FC-A630-1979368056B0"><title>Set
       
    29 Up</title> <p>In the template reference board port, the <filepath>.mmp</filepath> file for the keyboard driver is <filepath>...\template_variant\exkey_inttemplate.mmp</filepath>. This is one of the PRJ_MMPFILES referenced in the template variant's <filepath>bld.inf</filepath> file in the <filepath>...\template_variant\...</filepath> directory, and means that the keyboard driver is built as part of
       
    30 the Variant. </p> <p>The source for the driver is contained entirely
       
    31 within <filepath>...\template_variant\specific\keyboard_interrupt.cpp</filepath>. </p> <p>The driver is defined as a kernel extension and is loaded
       
    32 early in the boot sequence. </p> </section>
       
    33 <section id="GUID-547A2791-82D8-4C86-86E6-B4B9CEF04E3B"><title>Entry
       
    34 point implementation</title> <p>The driver functionality is encapsulated
       
    35 by the <codeph>DKeyboardTemplate</codeph> class, and an instance of
       
    36 this is created when the extension is loaded. </p> <p>As the driver
       
    37 is a kernel extension, it must have a <codeph>DECLARE_STANDARD_EXTENSION()</codeph> statement. In the template port, this is implemented: </p> <codeblock id="GUID-69F707CF-642D-5D03-AC78-4914549C4413" xml:space="preserve">DECLARE_STANDARD_EXTENSION()
       
    38     {
       
    39     __KTRACE_OPT(KEXTENSION,Kern::Printf("Starting keyboard driver"));
       
    40 
       
    41     // create keyboard driver
       
    42     TInt r=KErrNoMemory;
       
    43     DKeyboardTemplate* pK=new DKeyboardTemplate;
       
    44     if (pK)
       
    45         r=pK-&gt;Create();
       
    46 
       
    47     __KTRACE_OPT(KEXTENSION,Kern::Printf("Returns %d",r));
       
    48     return r;
       
    49     }
       
    50 
       
    51 </codeblock> <p>It simply creates an instance of the <codeph>DKeyboardTemplate</codeph> class and then performs a second-phase initialisation, which is
       
    52 a common pattern in Symbian platform and third party applications. </p> <p><b>Initialisation on construction</b> </p> <p>The constructor
       
    53 of the <codeph>DKeyboardTemplate</codeph> class has the following
       
    54 signature: </p> <codeblock id="GUID-5D247843-FF8C-5157-8BE5-53B5883F00B2" xml:space="preserve">DKeyboardTemplate::DKeyboardTemplate()
       
    55     :    DPowerHandler(KLitKeyboard), 
       
    56         iMsgQ(rxMsg,this,NULL,1),
       
    57         iPowerUpDfc(PowerUpDfcFn,this,6),
       
    58         iPowerDownDfc(PowerDownDfcFn,this,7),
       
    59         iEventDfc(EventDfcFn,this,1)
       
    60     {
       
    61     }
       
    62 
       
    63 </codeblock> <p>See also <xref href="GUID-3C34724F-B476-5329-B0B1-6D5A34294979.dita">Interrupt Dispatcher
       
    64 Tutorial</xref>. </p> </section>
       
    65 <section id="GUID-F11265AA-D280-4B4E-92DA-38F12E7D5E30"><title> Interrupt
       
    66 Service Routine (ISR) implementation</title> <p>The ISR (Interrupt
       
    67 Service Routine) just schedules the DFC that handles the keypress.
       
    68 It can also optionally contribute the timing of key interrupts as
       
    69 a source of random data for the Random Number Generator (see <xref href="GUID-8290AAF0-577C-51D2-8AC1-0D37A10F45CB.dita">CSPRNG Implementation
       
    70 in Kernel</xref>). On the template reference board, this is implemented
       
    71 as: </p> <codeblock id="GUID-866CEA65-78AC-5A2C-AF08-29DF8FE53DD5" xml:space="preserve">void DKeyboardTemplate::Isr(TAny* aPtr)
       
    72 	{
       
    73 	Interrupt::Disable(KIntIdKeyboard);
       
    74 	
       
    75 	// Add the timing of key interrupts as entropy data for the RNG
       
    76 	Interrupt::AddTimingEntropy();
       
    77 	
       
    78 	k.iEventDfc.Add();
       
    79 	}
       
    80 </codeblock> <p>The ISR disables the keyboard interrupt, as repeated
       
    81 ISRs are not allowed to queue further DFC routines. </p> </section>
       
    82 <section id="GUID-E53E0FE1-3100-4B8B-BE90-250C1BDDE15C"><title>DFC
       
    83 function implementation</title> <p>The DFC is the function <codeph>DKeyboardTemplate::EventDfcFn</codeph> which is implemented as a
       
    84 call to <codeph>DKeyboardTemplate::EventDfc()</codeph>. </p> <codeblock id="GUID-2A1B334A-441E-57C3-B40C-603D992F934F" xml:space="preserve">void DKeyboardTemplate::EventDfcFn(TAny* aPtr)
       
    85     {
       
    86     ((DKeyboardTemplate*)aPtr)-&gt;EventDfc();
       
    87     }
       
    88 
       
    89 void DKeyboardTemplate::EventDfc()
       
    90     {
       
    91     __KTRACE_OPT(KHARDWARE,Kern::Printf("DKeyboardTemplate::EventDfc"));
       
    92 
       
    93     TInt irq=NKern::DisableAllInterrupts();
       
    94     while (IsKeyReady())                        // while there are keys in the controller's output buffer
       
    95         {
       
    96         NKern::RestoreInterrupts(irq);
       
    97         TRawEvent e;
       
    98         TUint keyCode=GetKeyCode();                // Read keycodes from controller
       
    99         __KTRACE_OPT(KHARDWARE,Kern::Printf("#%02x",keyCode));
       
   100 
       
   101         //
       
   102         // TO DO: (mandatory)
       
   103         //
       
   104         // Convert from hardware scancode to EPOC scancode and send the scancode as an event (key pressed or released)
       
   105         // as per below EXAMPLE ONLY:
       
   106         //
       
   107         TUint bareCode=keyCode&amp;~KFlagKeyPressed;
       
   108         TUint8 stdKey=convertCode[bareCode];
       
   109         if (keyCode&amp;KFlagKeyPressed)
       
   110             e.Set(TRawEvent::EKeyUp,stdKey,0);
       
   111         else
       
   112             e.Set(TRawEvent::EKeyDown,stdKey,0);
       
   113         Kern::AddEvent(e);
       
   114         NKern::Sleep(1);                        // pause before reading more keycodes
       
   115         irq=NKern::DisableAllInterrupts();
       
   116         }
       
   117     Interrupt::Enable(KIntIdKeyboard);
       
   118     NKern::RestoreInterrupts(irq);
       
   119     }</codeblock> <p>This: </p> <ul>
       
   120 <li id="GUID-8A202894-34F8-5E7B-B791-20D4B6C8B914"><p>reads the scan
       
   121 code data by calling <codeph>DKeyboardTemplate::GetKeyCode()</codeph>  </p> </li>
       
   122 <li id="GUID-D553B662-F60C-575F-893D-347A9344F9B9"><p>re-enables the
       
   123 keyboard interrupt, now that the DFC has freed up the input data register </p> </li>
       
   124 <li id="GUID-BFC75E95-A9AE-584E-8A17-CCAF9C2F5913"><p>translates the
       
   125 keypress event into a Symbian scancode </p> </li>
       
   126 <li id="GUID-20BC22AF-C58C-56DC-B777-B5C8681668D6"><p>puts the translated
       
   127 event on to the event queue. </p> </li>
       
   128 </ul> </section>
       
   129 <section id="GUID-437735D9-916C-4308-913C-3839CC7F0002"><title>See
       
   130 also</title> <p> <xref href="GUID-EB76FAF8-CD4B-5CB6-9F23-C852ED91866F.dita">Concepts</xref>  </p> </section>
       
   131 </conbody></concept>