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

📄 main.c

📁 CyPress的C8051F32X系列底层驱动(C语言)及上位机demo(vc环境)
💻 C
字号:
/*
   Copyright 2005 Silicon Laboratories Inc.

   File:    main.c
   Author:  JS & CS original, modified by JM
   Created: OCT 03

   Target Device: C8051F320

   Source file for USB firmware. Includes main routine and
   all hardware initialization routines.

   This is the main routine file for the USBProgramGuide.wsp project,
   which includes the following source files:

   main.c
   usb_desc.c
   usb_isr.c
   usb_stdreq.c
   usb_utils.c

   This firmware is intended to work with the Silabs USB Bulk File Transfer example,
   implementing two Bulk pipes with 64-byte Maximum transfers. The endpoints
   used are as follows:

   Endpoint1 IN - BULK IN
   Endpoint2 OUT - BULK OUT

******************************************/

#include <c8051f320.h>                    // SFR declarations
#include "usb_regs.h"
#include "usb_structs.h"
#include "usb_main.h"
#include "usb_desc.h"

//	Bit Definitions
sbit Led1 = P2^2; // LED='1' means ON
sbit Led2 = P2^3; // These blink to indicate data transmission

//------------------------------------------------------------------------
// Local Function Prototypes
//------------------------------------------------------------------------

void USB0_Init (void);
void USB0_Enable (void);
void PORT_Init (void);
void SYSCLK_Init (void);
void VDD_MON_Init(void);			//	Turn on VDD Monitor, for Flash Write protection

//------------------------------------------------------------------------
// Global Variables
//------------------------------------------------------------------------

DEVICE_STATUS gDeviceStatus;
EP_STATUS gEp0Status;
EP_STATUS gEp1InStatus;
EP_STATUS gEp2OutStatus;
EP0_COMMAND gEp0Command;

//------------------------------------------------------------------------
// Main Routine
//------------------------------------------------------------------------

void main (void)
{
   PCA0MD &= ~0x40;                       // Disable Watchdog timer
   PORT_Init ();                          // Initialize Crossbar and GPIO
   SYSCLK_Init ();                        // Initialize oscillator
   USB0_Init ();                          // Initialize USB0
   VDD_MON_Init();                        // Turn on VDD Monitor


   EA = 1;                                // Enable global interrupts

   USB0_Enable ();                        // Enable USB0

   while (1);
}

//------------------------------------------------------------------------
// Initialization Routines
//------------------------------------------------------------------------

//---------------------------
// SYSCLK_Init
//---------------------------
// SYSLCK Initialization
// - Initialize the system clock and USB clock
//
void SYSCLK_Init (void)
{
   unsigned char delay = 100;

   OSCICN |= 0x03;                        // Configure internal oscillator for
                                          // its maximum frequency

   CLKMUL = 0x00;                         // Select internal oscillator as
                                          // input to clock multiplier

   CLKMUL |= 0x80;                        // Enable clock multiplier
   while (delay--);                       // Delay for >5us
   CLKMUL |= 0xC0;                        // Initialize the clock multiplier

   while(!(CLKMUL & 0x20));                 // Wait for multiplier to lock

   CLKSEL |= USB_4X_CLOCK;                // Select USB clock
   CLKSEL |= SYS_4X_DIV_2;                 // Select system clock as Clock Multiplier over 2
}

//---------------------------
// VDD_MON_Init
//---------------------------
// VDD Monitor Initialization
// - Enable VDD Monitor
//
void VDD_MON_Init(void)                    // Enable VDD Monitor
{
   RSTSRC |= 0x02;
}

//---------------------------
// USB0_Init
//---------------------------
// USB Initialization
// - Initialize USB0
// - Enable USB0 interrupts
// - Enable USB0 transceiver
// - USB0 left disabled
//
void USB0_Init (void)
{
   UWRITE_BYTE(POWER, 0x08);              // Asynch. reset

   UWRITE_BYTE(IN1IE, 0x0F);              // Enable Endpoint0 Interrupt
   UWRITE_BYTE(OUT1IE, 0x0F);
   UWRITE_BYTE(CMIE, 0x04);               // Enable Reset interrupt

   USB0XCN = 0xC0;                        // Enable transceiver
   USB0XCN |= FULL_SPEED;                 // Select device speed

   UWRITE_BYTE(CLKREC, 0x80);             // Enable clock recovery,
                                          // single-step mode disabled

   EIE1 |= 0x02;                          // Enable USB0 Interrupts
}

//---------------------------
// USB0_Enable
//---------------------------
// USB Enable
// - Enable USB0
//
void USB0_Enable (void)
{

   UWRITE_BYTE(POWER, 0x00);              // Enable USB0 by clearing the
                                          // USB Inhibit bit
                                          // Suspend mode disabled
}

//---------------------------
// PORT_Init
//---------------------------
// Port Initialization
// - Configure the Crossbar and GPIO ports.
//
void PORT_Init(void)
{  
   P2MDOUT	|=	0x0C;					//	Port 2 pins 0,1 set high impedence
   Led1 = 0;							//	Start with both Leds off
   Led2 = 0;
   XBR0 = 0x00;						    
   XBR1 = 0x40;						//	Enable Crossbar
}

⌨️ 快捷键说明

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