📄 tusb2136kbfw programmer guide.txt
字号:
TUSB2136 Keyboard Application Demo Firmware
Technical Documentation
Revision 1.00: December 15, 2000
Vault Information Services
http://www.vaultbbs.com
Email: vis@vaultbbs.com
SUMMARY
The TUSB2136 Keyboard Application Demo Firmware is a fully-functional keyboard controller application designed to operate with the Texas Instrument TUSB2136 microcontroller. The code is designed to operate within a typical 101-key keyboard with three LEDs.
This code is provided to demonstrate the use and functionality of the TUSB2136, as well as to provide the user with a starting point should it be necessary to develop code to handle an expanded keyboard.
The code has been designed to be as straight-forward as possible, maintaining readability with heavy in-line documentation, and restricting the code that will be most commonly modified to just two source files.
The code was developed in the IAR Embedded workbench, version 5.50B.
SOURCE FILE OVERVIEW
The application consists of six 'C' files, the first two of which contain the code that the user will most often need to modify. Each source file will be explained more completely in the remainder of this document.
Keyboard.c: Contains the main() function and virtually everything relating to the keyboard-specific application. Most additional keyboard functionality and keypad mapping will be handled in this source file.
Vidpid.c: Contains parameters that are utilized by the application depending on which VID/PID is selected on the S0-S3 pins. Information such as host VID/PID, function VID/PID, manufacturer and product description, and serial numbers are modified in this source file.
Descrip.c: Contains the USB descriptors used by the firmware. Note that the string descriptors are built on-the-fly based on the VID/PID selection (see vidpid.c) and the descriptors found in Descrip.C will generally only be modified if you add additional reports or descriptors to your USB application.
UsbInit.c: Contains a number of USB-related initialization routines. Initializes the integrated HUB, the USB function (the device), and enables required interrupts.
Usb.c: Contains the bulk of the USB operation code, such as USB interrupts, USB request handlers, and functions related to sending and receiving USB data packets. Most of these functions are self-contained such that they will not need to be modified to expand the keyboard application, unless additional reports or endpoints are added.
Support.c: Contains general support routines, such as strlen(), memcpy(), and memcmp(). The standard 'C' functions are rewritten here since, as implemented in the code, they produce a slightly shorter program than when the standard library functions are used.
HEADER FILE OVERVIEW
The application utilizes three header (.h) files.
Tusb2136.h: Contains definitions and declares that are specific to the TUSB2136 architecture.
Usb.h: Contains definitions and declares related to the USB and HID specification.
Types.h: Contains definitions and declares that define certain types that are used within the application. These are primarily "shorthand" for common types (such as BYTE which is defined as unsigned char, etc.).
SOURCE FILE: "KEYBOARD.C"
The keyboard.c function contains the main() function and virtually everything related to the keyboard-specific application.
Main() Function: The main function is the section of the program that is first executed at boot-up and subsequently handles the process of calling the necessary initialization routines and controls the actual cycle of polling the keyboard.
The first four lines of the function first obtain the VID/PID setting from S0-S3 and select the product configuration that corresponds to that setting. It subsequently initializes the USB Hub followed by the USB function (the keyboard) itself. Details of each of these functions may be found in the description of the source file to which they belong.
GetVidPidSetting(); From VIDPID.C
InitializeUsbHub(); From USBINIT.C
InitializeUsbFunction(); From USBINIT.C
ClearKeyBoardBuffer(); From KEYBOARD.C
The code that follows in the the main() routine is:
vidPidMask = 1 << s0123;
while(!deviceReady);
The first line creates the vidPidMask based on the value that was obtained by the GetVidPidSetting(). This mask is used in the keyboard polling routine code to determine which keys are actually enabled in a given configuration.
The while(!deviceReady) loop causes the program to hold indefinitely until the USB initialization procedure, started by the InitializeUsbFunction() routine, completes. Once it completes, the deviceReady variable is set and the program continues on to the keyboard polling loop.
P2 = 0xFF;
while(1) // Indefinite loop to handle keyboard scanning
The first instruction, assigning 0xFF to P2, is used to ensure that the P2 output latch is high so that values can actually be read back from the keypad lines. The while(1) loop is the keyboard scanning loop. Since the entire application is dedicated to the process of scanning and reporting the keyboard, this loop is infinite.
if(bSuspended)
{
EX1 = ENABLE;
bPUR0 = 0xFF;
bPUR1 = 0xFF;
bPUR3 = 0xFF;
P0 = 0x00;
P1 = 0x00;
P3.6 = 0x00;
P3.7 = 0x00;
// Set IDLE bit in PCON to bring MCU to idle state
PCON |= 0x01;
EX1 = DISABLE;
bPUR0 = 0x00;
bPUR1 = 0x00;
bPUR3 = 0x00;
// Execution will stop until we wake back up, at which point
// we then conitnue by indicating we're no longer in Suspend
// mode.
bSuspended = FALSE;
}
The above section code of code handles the situation of a "Keyboard Suspend," which normally happens when no activity is detected on the USB bus for a certain amount of time. The keyboard then goes into "suspend" mode which allows a low power-consumption mode until activity on the USB awake sthe keyboard.
The suspend mode is acomplished by enabling the EX1 interrupt, adjusting the setting of the Pull-Up resistors (PUR), and clearing the output latches of those lines involved in the keyboard polling process. The Idle bit in PCON is then set, which causes the keyboard to suspend. The keyboard then remains in suspend mode until an interrupt on EX1 causes the keyboard to awake. The subsequent lines of this segment then "undo" the adjustments that were made to the Pull-Up Resistor settings and clears the bSuspended flag.
if(!deviceReady)
continue;
The check above verifies that the device is still ready. During normal operation, it is possible that the keyboard subsequently be asked for its descriptors, etc. Such requests necessitate that the keyboard polling process be temporarily suspended. Thus, if the device is not ready, the rest of the keyboard polling loop-which actually polls the keyboard-is skipped until such time that the device returns to a "ready' mode of operation.
if(keypressBuffer[0] == 0x00)
{
The above check is a condition that verifies that the keyboard is not scanned if there is already a block of data in the keypressBuffer to be sent that hasn't yet been sent. A new keypressBuffer[] will not be polled and constructed until the previous buffer has been sent.
modifierByte = 0x00;
The modifiedByte variable is used to hold the first byte of the HID data that is sent back to the keyboard. It is initially assumed to be zero, thus we initialize it as such. During the keyboard polling process, certain keys can modify the modifierByte variable such that it assumes other values.
for(scanLine = 18; scanLine > 0; scanLine--)
{
P0 = 0xFF;
P1 = 0xFF;
P3 |= 0xC0;
switch(scanLine)
{
case 18: P1.0 = 0; break;
case 17: P1.1 = 0; break;
case 16: P1.2 = 0; break;
case 15: P1.3 = 0; break;
case 14: P1.4 = 0; break;
case 13: P1.5 = 0; break;
case 12: P1.6 = 0; break;
case 11: P1.7 = 0; break;
case 10: P3.6 = 0; break;
case 9: P0.0 = 0; break;
case 8: P0.1 = 0; break;
case 7: P0.2 = 0; break;
case 6: P0.3 = 0; break;
case 5: P0.4 = 0; break;
case 4: P0.5 = 0; break;
case 3: P0.6 = 0; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -