⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sample.c

📁 Texas Instruments的TUSB3410芯片接键盘输入的固件程序源码和驱动程序源码。
💻 C
字号:
// (c)2002 by Texas Instruments Incorporated, All Rights Reserved.
/*----------------------------------------------------------------------------+
|                                                                             |
|                              Texas Instruments                              |
|                                                                             |
|                                 TUSB3410                                    |
|                                  Sample                                     |
|                                                                             |
+-----------------------------------------------------------------------------+
|  Source: Sample.c, v01.01 2002/07/25                                        |
|  Author: Ching-Hua Jim Chen Jim_Chen@ti.com                                 |
|                                                                             |
|  For more information, please contact                                       |
|                                                                             |
|  Jim Chen                                                                   |
|  Texas Instruments                                                          |
|  12500 TI Blvd, MS 8761                                                     |
|  Dallas, TX 75243                                                           |
|  USA                                                                        |
|                                                                             |
|  Tel 214-480-4656                                                           |
|  Fax 972-761-6043                                                           |
|                                                                             |
|  Release Notes:                                                             |
|                                                                             |
|  Logs:                                                                      |
|                                                                             |
|  WHO       WHEN         WHAT                                                |
|  ---       ----------   --------------------------------------------------  |
|  CJH       07/25/2002   Version 1.00                                        |
|  CJH       09/09/2002   Version 1.01 Modified the code to mute all warning  |
|                         messages in IAR and Keil C compiler                 |
+----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------+
| Include files                                                               |
+----------------------------------------------------------------------------*/
#ifdef KEIL
#include <reg51.h>      // for KEIL compiler 8051 sfr definition
sbit    BUTTON = P3 ^ 3;
sbit    LED    = P3 ^ 4;
#else
#include <io51.h>       // for IAR compiler 8051 sfr definition
#define BUTTON   P3.3
#define LED      P3.4
#endif


#include "types.h"      // Basic Type declarations
#include "usb.h"        // USB-specific Data Structures
#include "tusb3410.h"
#include "watchdog.h"
#include "sample.h"
#include "rs232dbg.h"

/*----------------------------------------------------------------------------+
| External Function Prototype                                                 |
+----------------------------------------------------------------------------*/
extern VOID UsbDataInitialization(VOID);
/*----------------------------------------------------------------------------+
| External Variables                                                          |
+----------------------------------------------------------------------------*/
extern idata BYTE bFunctionSuspended;
extern BYTE gbqSec; // quarter second, decreased by timer interrupt 0
extern BYTE gbmSec; // mini second, decreased by timer interrupt 0

/*----------------------------------------------------------------------------+
| Internal Type Definition & Macro                                            |
+----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------+
| Internal Constant Definition                                                |
+----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------+
| Internal Variables (must be declared by static)                             |
+----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------+
| Global Variables                                                            |
+----------------------------------------------------------------------------*/
BYTE gbClearEndpointFeature;
idata BYTE gbPreviousIEP1packet[8];
idata BYTE gbKeypressBuffer[KEYPRESS_BUFFER_SIZE + 1];
BYTE bModifierByte;
BYTE bLED;
/*----------------------------------------------------------------------------+
| Hardware Related Structure Definition                                       |
+----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------+
| System Initialization Routines                                              |
+----------------------------------------------------------------------------*/
//----------------------------------------------------------------------------
VOID CopyDefaultSettings(VOID)
{
    // disable global interrupt
    EA = FALSE;

    // initial usb endpoint and related registers
    UsbDataInitialization();

    // set i2c speed
//    i2cSetBusSpeed(I2C_400KHZ);


    // initial timer 0 interrupt
    TMOD &= 0xf0; // zero low nibble 
    TMOD |= 0x01; // Set Timer 0 to 16-bit timer
    
    // setup 1 mini second interval for timer 0, based on 24MHz clock
    TH0 = DELAY_1MS_12X_24MHz_H; // (65536-2000)>>8,   2000 means 1 mSec
    TL0 = DELAY_1MS_12X_24MHz_L; // (65536-2000)&0xff, 2000 means 1 mSec  
    
    TF0 = 0; // clear timer 0 overflow flag
    TR0 = 1; // enable timer 0
    
    ET0 = 1; // enable timer 0 interrupt
}

/*----------------------------------------------------------------------------+
| General Subroutines                                                         |
+----------------------------------------------------------------------------*/
BYTE bMemcmp(char *string1, char *string2, BYTE count)
{
	BYTE str;
	for(str = 0; str < count; str++)
		if( string1[str] != string2[str] )
			return(1);
	return(0);
}

/***************************************************************************
 * Function:   ClearKeyBoardBuffer                                         *
 * Description: Clears the keyboard's internal keypress buffer.  The       *
 *    keypress buffer is 6 bytes which represent a maximum of 6 keypresses *
 *    that can be returned via USB.  Once the keypresses are sent via USB, *
 *    this routine may be called to clear the buffer.                      * 
 ***************************************************************************/
void ClearKeyBoardBuffer(void)
{
	BYTE bTemp;

	for(bTemp = 0; bTemp < KEYPRESS_BUFFER_SIZE; bTemp++)
		gbKeypressBuffer[bTemp] = 0x00;
}
/***************************************************************************
 * Function:   UpdateIEP1WithKeypress                                      *
 * Description: Is called by the main() program in order to send the       *
 *    contents of the keypressBuffer[] to the host via USB.  The function  *
 *    does the following:                                                  *
 *                                                                         * 
 *    1. Makes sure the current IEP1 Byte Counter is "NAK", which means    * 
 *       there is nothing pending to send.  Any other value means the last * 
 *       packet sent to the host hasn't been sent yet, so we don't send    * 
 *       anything else until the previous packet is sent.                  * 
 *    2. If nothing is pending to send, the routine constructs the output  * 
 *       data packet in the IEP1 Output buffer, copying the modifier byte, * 
 *       the constant 0x00 byte, and the 6 bytes of keypressBufferp[] data.*   
 *    3. Compares to see if the current packet that is ready to send is    * 
 *       the same as the last packet that was sent.  If it is, it exits    * 
 *       since only *changes* in the keyboard state need to be reported.   *   
 *    4. If all of the above tests are successful, the routine sets the    * 
 *       Byte Counter to 8 which will cause the 8-byte packet to be        * 
 *       sent when the next IEP1 poll is received from the host.           *   
 ***************************************************************************/
void UpdateIEP1WithKeypress(void)
{
	BYTE bTemp;

	// We first check to see if the character count is NAK.  If it's not
	// NAK that means we still have some output pending, so we leave it
	// alone for now.
	
	if(bIEPDCTX1 != (signed char)EPBCT_NAK) // different from IAR
		{
//        PUTS("bIEPDCTX1\r\n");
//		  PUTHEX(bIEPDCTX1);
//		  PUTS("\r\n");
		ClearKeyBoardBuffer();
		return;
		}

	// Start by initialization the HID Keyboard report header
	pbIEPX1[0] = bModifierByte; // Holds shift, alt, etc.
	pbIEPX1[1] = 0x00; // Second byte is always 0x00
	
	// We now copy the keypress buffer into the rest of the iep1Buffer.
	for(bTemp = 0; bTemp < 6; bTemp++)
		pbIEPX1[2 + bTemp] = gbKeypressBuffer[bTemp];

	// We now compare the currently prepared IEP1 output buffer with the last
	// packet that was sent, which is held in the variable previousIEP1packet.
	// If the current packet is the same as the previous packet sent, we 
	// don't need to do anything since the host will assume the previous state
	// is still current until we report a different state.
	if( bMemcmp(pbIEPX1, gbPreviousIEP1packet, 8) == 0)
		{
//        PUTS("ClrKeyBuf 2\r\n");
		ClearKeyBoardBuffer();
		return;
		}

	// We finally the set the CTX_1 to the number of bytes in the IEP1 output
	// buffer (8).  Thus, the next time the host issues an IN1 poll to the
	// TUSB2136, the data will be sent out immediately.  We will know that
	// the transfer has taken place when the IEP1InterruptHandler() interrupt
	// handler is called.
//    PUTS("Update\r\n");
	bIEPDCTX1 = 8;
}

/*----------------------------------------------------------------------------+
| Interrupt Sub-routines                                                      |
+----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------+
| Interrupt Service Routines                                                  |
+----------------------------------------------------------------------------*/
VOID IEP1InterruptHandler(VOID)
{
BYTE bTemp;
	
	// This interrupt is triggered when we've finished sending a block of data
	// to the host.  At that point we copy the current IEP1 buffer to the
	// "previousIEP1packet" buffer.  This holds a copy of the last data that
	// was sent to the host.  This is used later to see if a subsequent packet
	// is the same as a new packet.  If it is, we won't have to resend it.
	BYTE str;

//	PUTS("IEP1\r\n");

	for(str = 0; str < 8; str++)
		gbPreviousIEP1packet[str] = pbIEPX1[str];
	
	// We clear the keypressBuffer.  The keyboard scan process will only begin
	// if the keypressBuffer is clear.
//	ClearKeyBoardBuffer();
	for(bTemp = 0; bTemp < KEYPRESS_BUFFER_SIZE; bTemp++)
		gbKeypressBuffer[bTemp] = 0x00;
}
/*----------------------------------------------------------------------------+
| Main Routine                                                                |
+----------------------------------------------------------------------------*/
VOID main(VOID)
{
//    RESET_WATCHDOG;
    
    DISABLE_WATCHDOG;
    
    CopyDefaultSettings();
        
    RS232INIT();
    PUTSTR("Sample\r\n");

    EA          = ENABLE;           // Enable global interrupt
    EX0         = ENABLE;
    bUSBCTL    |= USBCTL_CONT;      // connect to upstream port
    // timer 0 interrupt is working after connect to upstream port

    while(1) {

	  if(gbKeypressBuffer[0] == 0x00) {
//	   	BYTE scanLine, keypress;
		BYTE keyDefOffset = 0;
		bModifierByte = 0x00; // We initialize the modifier byte to zero initially

        if (BUTTON == 0) {

          LED = 0;
          // remember, one packet can't generate same data
		  // becasue no one can type same character within 10ms
		  bModifierByte = 0x02; // left shift
		  gbKeypressBuffer[0] = usbUsageT;
		  gbKeypressBuffer[1] = usbUsageI;
		  gbKeypressBuffer[2] = usbUsageU ;
		  gbKeypressBuffer[3] = usbUsageS;
		  gbKeypressBuffer[4] = usbUsageB;
		  gbKeypressBuffer[5] = usbUsageSpacebar;

          PUTSTR("TIUSB ");
	    }
        else {
          LED = 1;
        }

        UpdateIEP1WithKeypress();
	  }
	
      if (bFunctionSuspended == TRUE) {
        RESET_WATCHDOG;
        PUTSTR("SUSPEND");
        PCON |= PCON_IDL;
        RESET_WATCHDOG;
      }
    }

}
/*----------------------------------------------------------------------------+
| End of source file                                                          |
+----------------------------------------------------------------------------*/
/*------------------------ Nothing Below This Line --------------------------*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -