📄 mainloop.c
字号:
#include <stdio.h>
#include <string.h>
#include "epphal.h"
#include "d12ci.h"
#include "mainloop.h"
#include "usb100.h"
#include "chap_9.h"
#include "protodma.h"
#include "c6211dsk.h"
//*************************************************************************
// USB protocol function pointer arrays
//*************************************************************************
cregister unsigned int IER,CSR,IFR,ICR,ISTP;
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 (*VendorDeviceRequest[])(void) =
{
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
read_write_register,
reserved,
reserved,
reserved
};
//*************************************************************************
// Public static data
//*************************************************************************
extern EPPFLAGS bEPPflags;
extern unsigned long ClockTicks;
extern unsigned char idata GenEpBuf[];
extern IO_REQUEST idata ioRequest;
CONTROL_XFER ControlData;
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 help_devreq(unsigned char typ, unsigned char req)
{
typ >>= 5;
if(typ == USB_STANDARD_REQUEST) {
printf("Request Type = %s, Request = %s.\n", _NAME_USB_REQUEST_TYPE[typ],
_NAME_USB_STANDARD_REQUEST[req]);
}
else {
#ifdef __C51__
if(bEPPflags.bits.verbose)
printf("Request Type = %s, bRequest = 0x%bx.\n", _NAME_USB_REQUEST_TYPE[typ],
req);
#else
if(bEPPflags.bits.verbose)
printf("Request Type = %s, bRequest = 0x%x.\n", _NAME_USB_REQUEST_TYPE[typ],
req);
#endif
}
}
/* Configure Timer 0
- Mode = 1
- Interrupt = ENABLED
- Clock Source = INTERNAL
- Enable Gating Control = DISABLED
*/
#ifndef __C51__
//extern void interrupt (*OldTimerIsr)();
//extern void interrupt (*OldUsbIsr)();
extern unsigned long ClockIsr;
unsigned char far *privateBuffer;
unsigned long dmaBuffer;
unsigned long ioBuffer; // V2.1
unsigned short ioSize, ioCount; // V2.1
#endif
void init_timer0(void)
{
#ifdef __C51__
TMOD &= 0XF0; /* clear Timer 0 */
TMOD |= 0X1;
TL0 = 0X0; /* value set by user */
TH0 = 0X0; /* value set by user */
ET0 = 1; /* IE.1*/
TR0 = 1; /* TCON.4 start timer */
PT0 = 1;
EA = 1;
#else
disable();
// OldTimerIsr = getvect(0x8);
// setvect(0x8, timer_isr);
// enable();
#endif
}
void init_special_interrupts(void)
{
CSR=CSR&0xfffffffe;
ISTP=0;
EIP=0x1;
*(int *)IMH=0;
*(int *)IML=0x18a4;
ICR=0xff;
IER=0x13;
CSR=CSR|1;
}
void init_port()
{
}
void init_serial(void)
{
}
void on_exit(void)
{
}
void main(void)
{
BOOL in_loop = TRUE;
unsigned char key, i;
#ifndef __C51__
unsigned long ClockSec, last_clk;
#endif
init_port();
init_serial();
init_timer0();
init_special_interrupts();
printf("\nPDIUSBD12 evaluation board firmware V2.2.\n");
printf("Copyright (c) Philips Semiconductors, 1998.\n");
/* Power on reset, lightup LEDs for 1 sec,
disconnect and reconnect Soft-Connect */
printf("Re-connect PDIUSBD12 evaluation board to USB.\n");
reconnect_USB();
/* Main program loop */
while( in_loop ){
i=*(int *)0x9020201a&0x80000000;
if(i == 0)
fn_usb_isr();
if (bEPPflags.bits.bus_reset) {
DISABLE;
bEPPflags.bits.bus_reset = 0;
ENABLE;
printf("Bus reset!\n");
} // if bus reset
if (bEPPflags.bits.suspend) {
DISABLE;
bEPPflags.bits.suspend= 0;
ENABLE;
suspend_change();
printf("Suspend change!\n");
} // if suspend change
if (bEPPflags.bits.setup_packet){
DISABLE;
bEPPflags.bits.setup_packet = 0;
ENABLE;
control_handler();
} // if setup_packet
if(bEPPflags.bits.setup_dma) {
DISABLE;
bEPPflags.bits.setup_dma = 0;
ENABLE;
setup_dma();
} // if setup_dma
} // Main Loop
on_exit();
}
void suspend_change(void)
{
}
void stall_ep0(void)
{
D12_SetEndpointStatus(0, 1);
D12_SetEndpointStatus(1, 1);
}
void disconnect_USB(void)
{
// Initialize D12 configuration
D12_SetMode(D12_NOLAZYCLOCK|D12_CLOCKRUNNING, D12_SETTOONE | D12_CLOCK_12M);
}
void connect_USB(void)
{
// reset event flags
DISABLE;
bEPPflags.value = 0;
ENABLE;
// V2.1 enable normal+sof interrupt
D12_SetDMA(D12_ENDP4INTENABLE | D12_ENDP5INTENABLE);
// Initialize D12 configuration
D12_SetMode(D12_NOLAZYCLOCK|D12_CLOCKRUNNING|D12_SOFTCONNECT, D12_SETTOONE | D12_CLOCK_12M);
}
void reconnect_USB(void)
{
unsigned long clk_cnt;
disconnect_USB();
printf("Wait for 1 second ...\n");
clk_cnt = ClockTicks;
//while(ClockTicks < clk_cnt + 20)
// ;
connect_USB();
}
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;
}
}
void setup_dma()
{
#ifndef __C51__
if(!(ioRequest.bCommand & 0x80)) {
bEPPflags.bits.dma_disable = 1; // V2.1, x86 only
D12_SetDMA(D12_ENDP4INTENABLE | D12_ENDP5INTENABLE);
setup_io();
return;
}
#endif
bEPPflags.bits.dma_disable = 0; // V2.1
dma_start(&ioRequest);
DISABLE;
bEPPflags.bits.dma_state = DMA_RUNNING;
ENABLE;
single_transmit(0, 0);
if(ioRequest.bCommand & 0x1)
D12_SetDMA(D12_BURST_16 | D12_DMAENABLE | D12_DMA_INTOKEN);
else
D12_SetDMA(D12_BURST_16 | D12_DMAENABLE);
}
#ifndef __C51__
// setup_io() setup buffer address for I/O mode Main endpoints transfer,
// it only works on PC evaluation kit.
void setup_io()
{
unsigned long offset;
unsigned char far *fp;
unsigned short seg, off;
offset = (((unsigned long)ioRequest.bAddressH)<<16)
+ ioRequest.uAddressL;
ioBuffer = dmaBuffer + offset;
ioSize = ioRequest.uSize;
ioCount = 0;
seg = (ioBuffer + ioCount)>>4;
off = (ioBuffer + ioCount)&0xf;
// fp = MK_FP(seg, off);
DISABLE;
bEPPflags.bits.dma_state = DMA_RUNNING;
ENABLE;
single_transmit(0, 0);
if(ioRequest.bCommand & 0x1) {
if(ioSize > 64) {
D12_WriteEndpoint(5, fp, 64);
ioCount += 64;
}
else {
D12_WriteEndpoint(5, fp, ioSize);
ioCount += ioSize;
}
}
}
#endif
void control_handler()
{
unsigned char type, req;
type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK;
req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK;
help_devreq(type, req); // print out device request
if (type == USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])();
else if (type == USB_VENDOR_REQUEST)
(*VendorDeviceRequest[req])();
else
stall_ep0();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -