📄 usb_main.c
字号:
//-----------------------------------------------------------------------------
// F32x_USB_Main.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// This application note covers the implementation of a simple USB application
// using the interrupt transfer type. This includes support for device
// enumeration, control and interrupt transactions, and definitions of
// descriptor data. The purpose of this software is to give a simple working
// example of an interrupt transfer application; it does not include
// support for multiple configurations or other transfer types.
//
// How To Test: See Readme.txt
//
//
// FID: 32X000024
// Target: C8051F32x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Silicon Laboratories IDE version 2.6
// Command Line: See Readme.txt
// Project Name: F32x_USB_Interrupt
//
//
// Release 1.3
// -All changes by GP
// -22 NOV 2005
// -Changed revision number to match project revision
// No content changes to this file
// -Modified file to fit new formatting guidelines
// -Changed file name from USB_MAIN.c
// Release 1.1
// -All changes by DM
// -22 NOV 2002
// -Added support for switches and sample USB interrupt application.
//
// Release 1.0
// -Initial Revision (JS)
// -22 FEB 2002
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f320.h>
#include "USB_Register.h"
#include "USB_Main.h"
#include "USB_Descriptor.h"
#include "intrins.h"
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F32x
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Globals
//-----------------------------------------------------------------------------
sbit Led1 = P0^2; // LED='1' means ON
sbit RESET_IN = P0^6;
sbit RESET_OUT = P1^2;
sbit BDM_DIR = P1^4;
sbit BDM_IO = P1^6;
sbit BDM_CAP = P1^7;
idata BYTE command_buffer[128];
unsigned char * xdata pIn;
unsigned char * xdata pOut;
xdata unsigned char buffer[512];
//-----------------------------------------------------------------------------
// Main Routine
//-----------------------------------------------------------------------------
unsigned int cnt;
unsigned int sync_length;
void main(void)
{
Port_Init(); // Initialize crossbar and GPIO
PCA0MD &= ~0x40; // Disable Watchdog timer
Delay();
Delay();
Delay();
Delay();
Delay();
Sysclk_Init(); // Initialize oscillator
Usb0_Init(); // Initialize USB0
Timer_Init(); // Initialize timer2
pIn = pOut = buffer;
EIE1 |= 0x08;
ADC_Init();
while (1)
{
}
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Sysclk_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// Initialize the system clock and USB clock
//
//-----------------------------------------------------------------------------
void Sysclk_Init(void)
{
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
Delay(); // Delay for clock multiplier to begin
CLKMUL |= 0xC0; // Initialize the clock multiplier
Delay(); // Delay for clock multiplier to begin
while(!(CLKMUL & 0x20)); // Wait for multiplier to lock
CLKSEL = SYS_4X_DIV_2; // Select system clock
CLKSEL |= USB_4X_CLOCK; // Select USB clock
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// This function configures the crossbar and GPIO ports.
//
// P1.7 analog Potentiometer
// P2.2 digital push-pull LED
// P2.3 digital push-pull LED
//-----------------------------------------------------------------------------
void Port_Init(void)
{
P0SKIP = 0xFF; //0b11111111; // Port 0 pin 7 skipped by crossbar
P0MDIN = 0xFE; //0b11111110; // Port 0 pin 6 set as no analog input
P0MDOUT = 0x00; //0b00000000; // Port 0 pins 0-7 set push-pull
P1SKIP = 0x7F; //0b01111111; // Port 1 pin 7 skipped by crossbar
P1MDIN = 0xFE; //0b11111111; // Port 1 pin 6 set as no analog input
P1MDOUT = 0x14; //0b00010100; // Port 1 pins 0-7 set push-pull
P2MDOUT |= 0x0C; // P2.2 and P2.3 set to push-pull
XBR0 = 0x00;
XBR1 = 0x41; // Enable Crossbar
}
//-----------------------------------------------------------------------------
// Usb0_Init
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters : None
//
// - Initialize USB0
// - Enable USB0 interrupts
// - Enable USB0 transceiver
// - Enable USB0 with suspend detection
//-----------------------------------------------------------------------------
void Usb0_Init(void)
{
// Set initial values of In_Packet and Out_Packet to zero
// Initialized here so that WDT doesn't kick in first
POLL_WRITE_BYTE(POWER, 0x08); // Force Asynchronous USB Reset
POLL_WRITE_BYTE(IN1IE, 0x0F); // Enable Endpoint 0-2 in interrupts
POLL_WRITE_BYTE(OUT1IE, 0x0F); // Enable Endpoint 0-2 out interrupts
POLL_WRITE_BYTE(CMIE, 0x07); // Enable Reset,Resume,Suspend interrupts
USB0XCN = 0xE0; // Enable transceiver; select full speed
POLL_WRITE_BYTE(CLKREC, 0x80); // Enable clock recovery, single-step mode disabled
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
}
//-----------------------------------------------------------------------------
// 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++;
}
void Timer_Init(void) // Initialize timer3
{
TMR3RLL = 0x30;
TMR3RLH = 0xF8;
TMR3CN = 0x04; // start timer3
EIE1 |= 0x80; //Enable Timer3 Interrupt
}
void ADC_Init(void)
{
AMX0P = 0x00;
AMX0N = 0x1F;
ADC0CF = 0x44;
ADC0CN = 0x90;
}
//1ms tick
// handles general timing functions and blinks the LED
void usb_1ms_tick(void) interrupt 14
{
TMR3CN &= 0x7F;
}
void adcisr(void) interrupt 10
{
*pIn = ADC0H;
if(pIn < buffer + 512)
pIn++;
ADC0CN = 0x90;
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -