📄 usb_main.#2
字号:
//-----------------------------------------------------------------------------
// USB_MAIN.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: JS
// DATE: 22 FEB 02
//
//
// Target: C8051F32x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
//
// REVISIONS: 11/22/02 - DM: Added support for switches and sample USB
// interrupt application.
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f320.h>
#include "USB_REGISTER.h"
#include "USB_MAIN.h"
#include "USB_DESCRIPTOR.h"
#undef c_cUSR
#include "USB_USER.h"
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F32x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xca; // Timer2 reload value
sfr16 TMR2 = 0xcc; // Timer2 counter
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
sbit Led1 = P2^2; // LED='1' means ON
sbit Led2 = P2^3;
#define Sw1 0x01 // These are the port2 bits for Sw1
#define Sw2 0x02 // and Sw2 on the development board
BYTE Switch1State = 0; // Indicate status of switch
BYTE Switch2State = 0; // starting at 0 == off
BYTE Toggle1 = 0; // Variable to make sure each button
BYTE Toggle2 = 0; // press and release toggles switch
BYTE Potentiometer = 0x00; // Last read potentiometer value
BYTE Temperature = 0x00; // Last read temperature sensor value
BYTE Out_Packet[8] = {0,0,0,0,0,0,0,0}; // Last packet received from host
BYTE In_Packet[8] = {0,0,0,0,0,0,0,0}; // Next packet to sent to host
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
void main(void)
{
PCA0MD &= ~0x40; // Disable Watchdog timer
Sysclk_Init(); // Initialize oscillator
Port_Init(); // Initialize crossbar and GPIO
Usb0_Init(); // Initialize USB0
Timer_Init(); // Initialize timer2
Adc_Init(); // Initialize ADC
Fun_Pro_Main();
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-------------------------
// Sysclk_Init
//-------------------------
// SYSLCK Initialization
// - Initialize the system clock and USB clock
//
void Sysclk_Init(void)
{
#ifdef _USB_LOW_SPEED_
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency and enable
// missing clock detector
CLKSEL = SYS_INT_OSC; // Select System clock
CLKSEL |= USB_INT_OSC_DIV_2; // Select USB clock
#else
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency and enable
// missing clock detector
CLKMUL = 0x00; // Select internal oscillator as
// input to clock multiplier
CLKMUL |= 0x80; // Enable clock multiplier
CLKMUL |= 0xC0; // Initialize the clock multiplier
Delay(); // Delay for clock multiplier to begin
while(!CLKMUL & 0x20); // Wait for multiplier to lock
CLKSEL = SYS_INT_OSC; // Select system clock
CLKSEL |= USB_4X_CLOCK; // Select USB clock
#endif /* _USB_LOW_SPEED_ */
}
//-------------------------
// Port_Init
//-------------------------
// Port Initialization
// - Configure the Crossbar and GPIO ports.
//
void Port_Init(void)
{
//PORT0 init
P0MDIN = 0xFf;
P0MDOUT = 0x80; // Port P0^7 out Port P0^0-P0^6 in
P0SKIP = 0xff; // Port 0 pin 0, 1skipped by crossbar iic
P0 = 0xff; //
//port1 init
P1MDIN = 0xFf; // Port 1 pins 0,1,2,3 set as analog input others as GPIO
P1MDOUT = 0xFF; // Port 1 pins 0-3 set high impedence
P1SKIP = 0xFF; // Port 1 pin 7 skipped by crossbar
//port2 init
P2MDIN = 0xFF; // Port 2 pins as GPIO
P2MDOUT = 0xff; // Port 2 pins 0,1 set high impedence
P2SKIP = 0xFF; // Port 1 pin 7 skipped by crossbar
P2 = 0xFF;
//port3
P3MDIN = 0xFF; // Port 1 pins as GPIO
//crossbar init
XBR0 = 0x00; //none route iic and uart
XBR1 = 0x00; // disenable Crossbar
// All P0 pins open-drain output
Delay();
}
//-------------------------
// Usb0_Init
//-------------------------
// USB Initialization
// - Initialize USB0
// - Enable USB0 interrupts
// - Enable USB0 transceiver
// - Enable USB0 with suspend detection
//
void Usb0_Init(void)
{
POLL_WRITE_BYTE(POWER, 0x08); // Force Asynchronous USB Reset
POLL_WRITE_BYTE(IN1IE, 0x07); // Enable Endpoint 0-2 in interrupts
POLL_WRITE_BYTE(OUT1IE, 0x07); // Enable Endpoint 0-2 out interrupts
POLL_WRITE_BYTE(CMIE, 0x07); // Enable Reset, Resume, and Suspend interrupts
#ifdef _USB_LOW_SPEED_
USB0XCN = 0xC0; // Enable transceiver; select low speed
POLL_WRITE_BYTE(CLKREC, 0xA0); // Enable clock recovery; single-step mode
// disabled; low speed mode enabled
#else
USB0XCN = 0xE0; // Enable transceiver; select full speed
POLL_WRITE_BYTE(CLKREC, 0x80); // Enable clock recovery, single-step mode
// disabled
#endif /* _USB_LOW_SPEED_ */
EIE1 |= 0x02; // Enable USB0 Interrupts
EA = 1; // Global Interrupt enable
// Enable USB0 by clearing the USB Inhibit bit
POLL_WRITE_BYTE(POWER, 0x01); // and enable suspend detection
}
//-------------------------
// Timer_Init
//-------------------------
// Timer initialization
// - 1 mhz timer 2 reload, used to check if switch pressed on overflow and
// used for ADC continuous conversion
//
void Timer_Init(void)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
CKCON &= ~0xF0; // Timer2 clocked based on T2XCLK;
TMR2RL = -(SYSCLK / 12); // Initialize reload value
TMR2 = 0xffff; // Set to reload immediately
ET2 = 1; // Enable Timer2 interrupts
TR2 = 1; // Start Timer2
}
//-------------------------
// Adc_Init
//-------------------------
// ADC initialization
// - Configures ADC for single ended continuous conversion or Timer2
//
void Adc_Init(void)
{
REF0CN = 0x0E; // Enable voltage reference VREF
AMX0P = 0x1E; // Positive input starts as temp sensor
AMX0N = 0x1F; // Single ended mode(negative input = gnd)
ADC0CF = 0xFC; // SAR Period 0x1F, Left adjusted output
ADC0CN = 0xC2; // Continuous converion on timer 2 overflow
// with low power tracking mode on
EIE1 |= 0x08; // Enable conversion complete interrupt
}
//-------------------------
// Timer2_ISR
//-------------------------
// Called when timer 2 overflows, check to see if switch is pressed,
// then watch for release.
//
void Timer2_ISR(void) interrupt 5
{
if (!(P2 & Sw1)) // Check for switch #1 pressed
{
if (Toggle1 == 0) // Toggle is used to debounce switch
{ // so that one press and release will
Switch1State = ~Switch1State; // toggle the state of the switch sent
Toggle1 = 1; // to the host
}
}
else Toggle1 = 0; // Reset toggle variable
if (!(P2 & Sw2)) // This is the same as above, but for Switch2
{
if (Toggle2 == 0)
{
Switch2State = ~Switch2State;
Toggle2 = 1;
}
}
else Toggle2 = 0;
TF2H = 0; // Clear Timer2 interrupt flag
}
//-------------------------
// Adc_ConvComplete_ISR
//-------------------------
// Called after a conversion of the ADC has finished
// - Updates the appropriate variable for either potentiometer or temperature sensor
// - Switches the Adc multiplexor value to switch between the potentiometer and temp sensor
//
void Adc_ConvComplete_ISR(void) interrupt 10
{
if (AMX0P == 0x1E) // This switches the AMUX between
{ // the temperature sensor and the
Temperature = ADC0H; // potentiometer pin after each
AMX0P = 0x07; // conversion
}
else
{
Potentiometer = ADC0H;
AMX0P = 0x1E;
}
AD0INT = 0;
}
//-------------------------
// Delay
//-------------------------
// Used for a small pause, approximately 80 us in Full Speed,
// and 1 ms when clock is configured for Low Speed
//
void Delay(void)
{
int x;
for(x = 0;x < 500;x)
x++;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -