📄 mainloop.c
字号:
/*
//*************************************************************************
//
// BASBA P R O P R I E T A R Y
//
// COPYRIGHT (c) 2003 BY BASBA USA.
// -- ALL RIGHTS RESERVED --
//
// File Name: MAINLOOP.H
// Purpose: Checks event flags and passes to appropriate subroutine
// for further processing. It also contain the code for
// human interface such as LED and key scan.
// Author: Shuming Yu
// Created: 10 May 2003
// Modified:
// Revision: 1.0
//
//*************************************************************************
*/
#include <reg51.h> /* special function register declarations */
#include "d12hal.h"
#include "cmds.h"
#include "mainloop.h"
#include "usbStruc.h"
#include "USBProto.h"
/*
//*************************************************************************
// USB protocol function pointer arrays
//*************************************************************************
*/
code void (*StandardDeviceRequest[])(void) =
{
get_status,
clear_feature,
reserved,
set_feature,
reserved,
set_address,
get_descriptor,
reserved,
get_configuration,
set_configuration,
get_interface,
set_interface,
reserved,
reserved,
reserved,
reserved
};
code void (*ClassRequest[])(void) =
{
reserved,
get_report,
get_idle,
get_protocol,
reserved,
reserved,
reserved,
reserved,
reserved,
set_report,
set_idle,
reserved,
reserved,
reserved,
reserved,
reserved
};
code void (*VendorDeviceRequest[])(void) =
{
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved
};
/*
//*************************************************************************
// Public static data
//*************************************************************************
*/
extern unsigned long ClockTicks;
extern unsigned char idata GenEpBuf[];
EPPFLAGS bEPPflags; /* USB event flags */
CONTROL_XFER ControlData; /* Control endpoint TX/RX buffers */
code char * _NAME_USB_REQUEST_DIRECTION[] =
{
"Host_to_device",
"Device_to_host"
};
code char * _NAME_USB_REQUEST_RECIPIENT[] =
{
"Device",
"Interface",
"Endpoint(0)",
"Other"
};
code char * _NAME_USB_REQUEST_TYPE[] =
{
"Standard",
"Class",
"Vendor",
"Reserved"
};
code char * _NAME_USB_STANDARD_REQUEST[] =
{
"GET_STATUS",
"CLEAR_FEATURE",
"RESERVED",
"SET_FEATURE",
"RESERVED",
"SET_ADDRESS",
"GET_DESCRIPTOR",
"SET_DESCRIPTOR",
"GET_CONFIGURATION",
"SET_CONFIGURATION",
"GET_INTERFACE",
"SET_INTERFACE",
"SYNC_FRAME"
};
void blinkLED(unsigned char num)
{
unsigned int i;
for (i=0; i<3; i++) {
if (num==0) {
MCU_LED0 = 0;
loop_delay(100);
MCU_LED0 = 1;
loop_delay(100);
}
else {
MCU_LED1 = 0;
loop_delay(100);
MCU_LED1 = 1;
loop_delay(100);
}
}
}
/* Configure Timer 0
- Mode = 1
- Interrupt = ENABLED
- Clock Source = INTERNAL
- Enable Gating Control = DISABLED
*/
void init_timer0(void)
{
/* Configure Timer 0 as a 16 bit timer */
TMOD &= 0XF0; /* clear Timer 0, timer 1 left unchanged */
TMOD |= 0X1; /* set timer working mode 1 */
/* load the timer registers with the initial timer value */
TL0 = 0X0; /* value set by user */
TH0 = 0X0; /* value set by user */
ET0 = 1; /* Allow timer 0 interrupt */
TR0 = 1; /* Start timer 0 */
PT0 = 1; /* Set timer 0 interrupt high priority */
EA = 1;
}
void init_special_interrupts(void)
{
IT0 = 0;
EX0 = 1; // allow external INT0 interrupt
PX0 = 0; // set INT0 low interrupt priority
}
void init_port()
{
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
D12SUSPD = 0; /* Prevents D12 to go into Suspend state */
}
void main(void)
{
BOOL in_loop = TRUE;
// unsigned char key, i;
init_port();
init_timer0();
init_special_interrupts();
bEPPflags.value = 0; /* clear flags of bEPPflags */
/* Power on reset, lightup LEDs for 1 sec,
disconnect and reconnect Soft-Connect */
reconnect_USB();
/* Main program loop */
while( in_loop ) {
/* timer 0 overflow */
if (bEPPflags.bits.timer) {
DISABLE;
bEPPflags.bits.timer = 0;
ENABLE;
if(bEPPflags.bits.configuration)
check_key_LED();
}
if (bEPPflags.bits.bus_reset) {
DISABLE;
bEPPflags.bits.bus_reset = 0;
ENABLE;
//D12SUSPD = 1; /* release D12 suspend control */
} // if bus reset
if (bEPPflags.bits.suspend) {
DISABLE;
bEPPflags.bits.suspend= 0;
ENABLE;
//suspend_change();
} // if suspend change
if (bEPPflags.bits.setup_packet){
DISABLE;
bEPPflags.bits.setup_packet = 0;
ENABLE;
control_handler();
} // if setup_packet
} // Main Loop
}
/* wakeup D12, make MCU into low power mode, then suspend D12 again */
void suspend_change(void)
{
if (D12SUSPD == 1)
D12SUSPD = 0;
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
P3 = 0xFF;
D12SUSPD = 1; /* suspend D12 */
PCON |= 0x02; /* into low power mode */
while (1) ; /* hangup, only reset could wake up MCU */
}
/* Stall Enpoints */
void stall_ep0(void)
{
D12_SetEndpointStatus(0, 1);
D12_SetEndpointStatus(1, 1);
}
void disconnect_USB(void)
{
//D12_SetMode(D12_NOLAZYCLOCK|D12_CLOCKRUNNING, D12_SETTOONE | D12_CLOCK_12M);
/* Clear Soft_Connect bit, disconnect pull-up resistor */
D12_SetMode(0x00, 0x00);
}
void connect_USB(void)
{
// unsigned int i;
// reset event flags
DISABLE;
bEPPflags.value = 0; //for (i=0; i<11; i++) bEPPflags.c_value[i] = 0;
ENABLE;
// disable normal+sof interrupt
D12_SetDMA(0x00);
// Initialize D12 working mode
D12_SetMode(D12_NOLAZYCLOCK|D12_CLOCKRUNNING|D12_SOFTCONNECT, D12_SETTOONE | D12_CLOCK_12M);
//D12_SetMode(D12_SOFTCONNECT, 0x00); /* Enable softconnect */
}
void reconnect_USB(void)
{
unsigned long clk_cnt;
//Turn on LEDs
MCU_LED0 = 0;
MCU_LED1 = 0;
//D12SUSPD = 0; /* wakeup D12 */
disconnect_USB();
clk_cnt = ClockTicks;
while(ClockTicks < clk_cnt + 20)
;
connect_USB();
//Trun off LEDs
MCU_LED0 = 1;
MCU_LED1 = 1;
}
void init_unconfig(void)
{
// unsigned char i;
D12_SetEndpointEnable(0); /* Disable all endpoints but EPP0. */
}
void init_config(void)
{
D12_SetEndpointEnable(1); /* Enable generic/iso endpoints. */
}
void single_transmit(unsigned char * buf, unsigned char len)
{
if( len <= EP0_PACKET_SIZE) {
D12_WriteEndpoint(1, buf, len);
}
}
void code_transmit(unsigned char code * pRomData, unsigned short len)
{
ControlData.wCount = 0;
if(ControlData.wLength > len)
ControlData.wLength = len;
ControlData.pData = pRomData;
if( ControlData.wLength >= EP0_PACKET_SIZE) {
D12_WriteEndpoint(1, ControlData.pData, EP0_PACKET_SIZE);
ControlData.wCount += EP0_PACKET_SIZE;
DISABLE;
bEPPflags.bits.control_state = USB_TRANSMIT;
ENABLE;
}
else {
D12_WriteEndpoint(1, pRomData, ControlData.wLength);
ControlData.wCount += ControlData.wLength;
DISABLE;
bEPPflags.bits.control_state = USB_IDLE;
ENABLE;
}
}
/* LED and Key Processing subroutine */
void check_key_LED(void)
{
static unsigned char c, last_key = 0xf;
// Key processing (infor current key status to host)
c = MCU_SWM0 & MCU_SWM1; /* get two keys status */
c &= 0x0f;
if (c != last_key & bEPPflags.bits.ep1_sxdone) { /* Keys status have been changed */
bEPPflags.bits.ep1_sxdone = 0;
D12_WriteEndpoint(3, &c, 1); /* send key information to host */
}
last_key = c; /* store current key status */
// LED processing (get LED info, and set LED
if(bEPPflags.bits.ep1_rxdone) { /* Get LED control command */
DISABLE;
bEPPflags.bits.ep1_rxdone = 0; /* Clear the endpoint receiving flag */
ENABLE;
MCU_LED0 = !(GenEpBuf[3] & 0x1); /* Cnage LED status */
MCU_LED1 = !(GenEpBuf[3] & 0x2);
}
}
void control_handler()
{
unsigned char type, req;
type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK; /*0x60*/
req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK; /*0x0F*/
if (type == USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])();
// if (type == USB_CLASS_REQUEST) {
//blinkLED(0);
// (*ClassRequest[req])();
// }
else if (type == USB_VENDOR_REQUEST)
(*VendorDeviceRequest[req])();
else
stall_ep0();
}
void loop_delay(const unsigned int nDelay)
{
unsigned int x, y;
for (x=0; x<=nDelay;x++) {
for (y=0;y<=110;y++) ;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -