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

📄 main.c

📁 AT91SAM7A3评估版的USB测试程序
💻 C
字号:
//*--------------------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*--------------------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*--------------------------------------------------------------------------------------
//* File Name           : USB HID example
//* Object              :
//* Translator          :
//* 1.0 05/Oct/04 ODi	: CReation
//* 1.1 04/Nov/04 JPP	: Add led1 On at power supply
//* 1.2   21/Feb/05 JPP : LIB change AT91C_RTTC_RTTINC
//* 1.3   01/Mar/05 ODi : Modify to match usb.org compliancy
//* 1.4   05/Sep/05 JPP : Change PB1 management
//*--------------------------------------------------------------------------------------

#include "board.h"
#include "dbgu.h"
#include "hid_enumerate.h"

#define BUTTON_SAMPLING 1700 // Sampling frequency of buttons

#define BL_OFF(pio) ((pio) & SW2_LEFT)
#define BR_OFF(pio) ((pio) & SW3_RIGHT)
#define BU_OFF(pio) ((pio) & SW1_UP)
#define BD_OFF(pio) ((pio) & SW4_DOWN)
#define BP_OFF(pio) ((pio) & SW5_PUSH)

#define BL_ON(pio) (!BL_OFF(pio))
#define BR_ON(pio) (!BR_OFF(pio))
#define BU_ON(pio) (!BU_OFF(pio))
#define BD_ON(pio) (!BD_OFF(pio))
#define BP_ON(pio) (!BP_OFF(pio))

#define CLICKL_ON 1
#define CLICKR_ON 2

struct _AT91S_HID 	HID;

//*----------------------------------------------------------------------------
//* \fn    AT91F_USB_Open
//* \brief This function Open the USB device
//*----------------------------------------------------------------------------
void AT91F_USB_Open(void)
{
    // Set the PLL USB Divider
    AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1 ;

    // Specific Chip USB Initialisation
    // Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock
    AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
    AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);

    // Enable UDP PullUp (USB_DP_PUP) : enable & Clear of the corresponding PIO
    // Set in PIO mode and Configure in Output
    AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB1);

    // Set for set the Pul up resistor
    AT91F_PIO_ClearOutput(AT91C_BASE_PIOB,AT91C_PIO_PB1);

    // CDC Open by structure initialization
    AT91F_HID_Open(&HID, AT91C_BASE_UDP);
}

//*--------------------------------------------------------------------------------------
//* Function Name       : main
//* Object              :
//*--------------------------------------------------------------------------------------
int main ( void )
{
    char prevButton = 0, button = 0;
    int x = 0, y = 0;
    unsigned int pioStatus;

    // Init trace DBGU
    AT91F_DBGU_Init();

    // Init USB device
    AT91F_USB_Open();

    // Configure the RTT:
    *AT91C_RTTC_RTMR = BUTTON_SAMPLING;

    // Set in PIO mode and Configure in Input
    //AT91F_PIOA_CfgPMC();
    AT91F_PIOB_CfgPMC();
    AT91F_PIO_CfgInput(AT91C_BASE_PIOB, SW_MASK );
    AT91F_PIO_CfgOutput(AT91C_BASE_PIOA, LED_MASK );

    // Leds are switched off
    AT91F_PIO_SetOutput(AT91C_BASE_PIOA, LED_MASK );
    // Led1 is switched on
    AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, LED1);

    // Wait for the end of enumeration
    while (!HID.IsConfigured(&HID));

    // Leds are switched off
    AT91F_PIO_SetOutput(AT91C_BASE_PIOA, LED_MASK );

    // Start waiting some cmd
    while (!(AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY)) {
		// Check enumeration
		if (HID.IsConfigured(&HID) && ((*AT91C_RTTC_RTSR) & AT91C_RTTC_RTTINC)) {
			// Check PIO changes
			pioStatus = *AT91C_PIOB_PDSR;

			// Click on Button Left
			if (BL_ON(pioStatus) && BP_OFF(pioStatus)) {
				x = (x > 0) ? -1 : ((-127 < x) ? --x : x);
				AT91F_PIO_SetOutput(AT91C_BASE_PIOA, LED_MASK);
				AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, LED4);

			}
			// Click on Button Right
			else if (BP_OFF(pioStatus) && BR_ON(pioStatus)) {
				x = (x < 0) ? 0 : ((x < 127) ? ++x : x);
				AT91F_PIO_SetOutput(AT91C_BASE_PIOA, LED_MASK);
				AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, LED1);
			}
			// Release right/left/push button
			else {
				x = 0;
			}
			// Click on Button Up
			if (BU_ON(pioStatus) && BP_OFF(pioStatus)) {
				y = (y > 0) ? -1 : ((-127 < y) ? --y : y);
				AT91F_PIO_SetOutput(AT91C_BASE_PIOA, LED_MASK);
				AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, LED2);
			}
			// Click on Button Down
			else if (BP_OFF(pioStatus) && BD_ON(pioStatus)) {
				y = (y < 0) ? 0 : ((y < 127) ? ++y : y);
				AT91F_PIO_SetOutput(AT91C_BASE_PIOA, LED_MASK);
				AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, LED3);
			}
			else {
				y = 0;
			}
			// Click on Button Push
    		        if (BP_ON(pioStatus) && BR_OFF(pioStatus)) {
				button |= CLICKL_ON;
			}
			else {
				button &= ~(CLICKL_ON);
			}

			if (BR_ON(pioStatus) && BP_ON(pioStatus)) {
				button |= CLICKR_ON;
			}
			else {
				button &= ~(CLICKR_ON);
			}

			// Send Coordinates
			if ((prevButton != button) || x || y) {
				prevButton = button;
				HID.SendReport(&HID, button, x, y);
			}

        }
   }

   return(true);
}


⌨️ 快捷键说明

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