📄 usb_main.c
字号:
/*
* File: main.c
* Purpose: Main process
*
*/
#include "src/common/common.h"
#include "usb.h"
#include "usb_main.h"
uint32 mem_address;
/* extern Globals */
extern char Desc_RAM[];
extern unsigned char String0Desc;
extern unsigned char String1Desc;
extern unsigned char String2Desc;
extern DESCRIPTOR_STRUCT Descriptors;
extern CFG_BUFF_STRUCT cfg_data;
extern uint32 TotalData;
/********************************************************************/
/* The command table entry data structure */
typedef const struct
{
char * cmd; /* command name user types, ie. GO */
int min_args; /* min num of args command accepts */
int max_args; /* max num of args command accepts */
void (*func)(int, char **); /* actual function to call */
char * description; /* brief description of command */
char * syntax; /* syntax of command */
} CMD;
/********************************************************************/
static char input[MAX_LINE];
static const char PROMPT[] = "dUSB> ";
static const char BANNER[] = "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n. .\n. ~dUSB~ .\n. .\n. USB Test program for M5275EVB .\n. .\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
static const char HELPMSG[] = "Enter 'help' for help.\n\n";
static const char SYNTAX[] = "Error: Invalid syntax for: %s\n";
static const char INVCMD[] = "Error: No such command: %s\n";
static const char INVALUE[] = "Error: Invalid value: %s\n";
static const char HELPFORMAT[] = " %-10s %-22s %s %s\n";
CMD CMDTAB[] =
{
{"help", 0, 0, help, "Display this message", ""},
{"isrdb", 0, 0, isrdb, "Toggle ISR debug prints",""},
{"ddr", 2, 2, ddram, "Dump 5275 Desc RAM","<hAddr> <hLength>"},
{"flush", 0, 1, flush, "Flush FIFOs","<ep FIFO>"},
{"stat", 0, 0, stat, "USB module status", ""},
{"reset", 0, 0, reset, "Reset USB module", ""},
{"quit", 0, 0, quit, "Exit", " "}
};
const int NUM_CMD = sizeof(CMDTAB)/sizeof(CMD);
/*******************************************************************/
void main (void)
{
char ch;
int i;
/* Set DTOUT0,1,2,3 to act as GPIO */
MCF_GPIO_PAR_TIMER = 0x0000;
/* Write 1 to turn LEDs off */
MCF_GPIO_PODR_TIMERH = 0x0F;
MCF_GPIO_PODR_TIMERL = 0x0F;
/* Set Data Direction of DTOUT0,1,2,3 to Output */
MCF_GPIO_PDDR_TIMERH = 0x0F;
MCF_GPIO_PDDR_TIMERL = 0x0F;
/* USB init */
usb_init();
printf("Initialization Complete\n");
printf("CIR is 0x%04X\n",MCF_CCM_CIR);
/* Enable interrupts */
mcf5xxx_wr_sr(0x2000);
/*
* Determine cause(s) of Reset
*/
printf("\n\n");
if (MCF_RCM_RSR & MCF_RCM_RSR_EXT)
printf("RSR: External Reset\n");
if (MCF_RCM_RSR & MCF_RCM_RSR_WDR)
printf("RSR: Watchdog Timer Reset\n");
if (MCF_RCM_RSR & MCF_RCM_RSR_POR)
printf("RSR: Power On Reset\n");
if (MCF_RCM_RSR & MCF_RCM_RSR_SOFT)
printf("RSR: Software Reset\n");
if (MCF_RCM_RSR & MCF_RCM_RSR_LOL)
printf("RSR: PLL Loss of Lock Reset\n");
if (MCF_RCM_RSR & MCF_RCM_RSR_LOC)
printf("RSR: PLL Loss of Clock Reset\n");
/*
* Clear the Reset Status Register
*/
MCF_RCM_RSR = (MCF_RCM_RSR_EXT
| MCF_RCM_RSR_WDR
| MCF_RCM_RSR_POR
| MCF_RCM_RSR_SOFT
| MCF_RCM_RSR_LOL
| MCF_RCM_RSR_LOC);
/*
* Allow interrupts from IRQ7 (black button)
*/
/* Set IRQ7 to be rising edge triggered */
MCF_EPORT_EPPAR = MCF_EPORT_EPPAR_EPPA7(MCF_EPORT_EPPAR_EPPAx_RISING);
/* Enable EPORT interrupt 7 requests */
MCF_EPORT_EPIER = MCF_EPORT_EPIER_EPIE7;
/* Enable IRQ7 in the IMR */
MCF_INTC0_IMRL &= ~(MCF_INTC0_INTFRCL_INT7 | MCF_INTC0_IMRL_MASKALL);
/* Display banner */
printf(BANNER);
printf(HELPMSG);
/* Start command loop */
while (1)
{
printf(PROMPT);
run_cmd();
}
}
/********************************************************************/
/********************************************************************/
void
isrdb (int argc, char **argv)
{
(void)argc;
(void)argv;
if(usb_isr_debug_print == 0)
usb_isr_debug_print = 1;
else
usb_isr_debug_print = 0;
}
/********************************************************************/
void
reset (int argc, char **argv)
{
(void)argc;
(void)argv;
MCF_USB_CR &= ~(MCF_USB_CR_USBENA);
MCF_USB_CR |= MCF_USB_CR_USBRST;
/* re-init USB */
usb_init();
}
/********************************************************************/
void
flush (int argc, char **argv)
{
uint32 epfifo =0;
uint32 hLength =0;
int success,i;
uint8 dat;
if(argc > 1)
{
/* get arg1 value hAddr */
epfifo = get_value(argv[1],&success,16);
if (!success)
{
printf(INVALUE,argv[1]);
return;
}
}
else
{
/* Flush all FIFOs */
MCF_USB_EP0SR |= MCF_USB_EPnSR_FL;
MCF_USB_EP1SR |= MCF_USB_EPnSR_FL;
MCF_USB_EP2SR |= MCF_USB_EPnSR_FL;
MCF_USB_EP3SR |= MCF_USB_EPnSR_FL;
}
if(epfifo == 0)
MCF_USB_EP0SR |= MCF_USB_EPnSR_FL;
else if(epfifo == 1)
MCF_USB_EP1SR |= MCF_USB_EPnSR_FL;
else if(epfifo == 2)
MCF_USB_EP2SR |= MCF_USB_EPnSR_FL;
else if(epfifo == 3)
MCF_USB_EP3SR |= MCF_USB_EPnSR_FL;
else
printf("Error: Bad FIFO #\n");
}
/********************************************************************/
void
ddram (int argc, char **argv)
{
uint32 hAddr=0;
uint32 hLength=0;
int success,i;
uint8 dat;
/* get arg1 value hAddr */
hAddr = get_value(argv[1],&success,16);
if (!success)
{
printf(INVALUE,argv[1]);
return;
}
/* get arg2 value hLength */
hLength = get_value(argv[2],&success,16);
if (!success)
{
printf(INVALUE,argv[2]);
return;
}
/* Disable USB device */
MCF_USB_CR &= ~MCF_USB_CR_USBENA;
printf("USB_DAR = 0x%08X\n",MCF_USB_DAR);
/* Point the MCF_USB_DAR to hAddr */
MCF_USB_DAR =hAddr;
printf("USB_DAR = 0x%08X\n",MCF_USB_DAR);
for(i=0;i<hLength;i++)
{
dat = (uint8)MCF_USB_DDR;
printf("0x%04X = 0x%02X - %c\n",hAddr, dat, dat);
hAddr++;
}
/* Enable USB device */
MCF_USB_CR |= MCF_USB_CR_USBENA;
}
/********************************************************************/
void
stat (int argc, char **argv)
{
(void)argc;
(void)argv;
printf("------------------------\n");
printf("SR = 0x%08X\n",MCF_USB_SR);
printf("CR = 0x%08X\n",MCF_USB_CR);
printf("ISR = 0x%08X\n",MCF_USB_ISR);
printf("IMR = 0x%08X\n",MCF_USB_IMR);
printf("MCR = 0x%08X\n",MCF_USB_MCR);
printf("FRAME = 0x%08X\n",MCF_USB_FRAME);
printf("------------------------\n");
printf("EP0SR = 0x%08X\n",MCF_USB_EP0SR);
printf("EP1SR = 0x%08X\n",MCF_USB_EP1SR);
printf("EP2SR = 0x%08X\n",MCF_USB_EP2SR);
printf("EP3SR = 0x%08X\n",MCF_USB_EP3SR);
printf("------------------------\n");
printf("EP0ISR = 0x%08X\n",MCF_USB_EP0ISR);
printf("EP1ISR = 0x%08X\n",MCF_USB_EP1ISR);
printf("EP2ISR = 0x%08X\n",MCF_USB_EP2ISR);
printf("EP3ISR = 0x%08X\n",MCF_USB_EP3ISR);
printf("------------------------\n");
printf("EP0FSR = 0x%08X\n",MCF_USB_EP0FSR);
printf("EP1FSR = 0x%08X\n",MCF_USB_EP1FSR);
printf("EP2FSR = 0x%08X\n",MCF_USB_EP2FSR);
printf("EP3FSR = 0x%08X\n",MCF_USB_EP3FSR);
printf("------------------------\n");
printf("EP0IMR = 0x%08X\n",MCF_USB_EP0IMR);
printf("EP1IMR = 0x%08X\n",MCF_USB_EP1IMR);
printf("EP2IMR = 0x%08X\n",MCF_USB_EP2IMR);
printf("EP3IMR = 0x%08X\n",MCF_USB_EP3IMR);
printf("------------------------\n");
printf("TotalData = 0x%08X\n",TotalData);
printf("------------------------\n");
}
/********************************************************************/
/********************************************************************/
/********************************************************************/
/********************************************************************/
static void
get_user_input (char *userline)
{
char line[MAX_LINE];
int pos, ch;
pos = 0;
ch = (int)in_char();
while ( (ch != 0x0D /* CR */) &&
(ch != 0x0A /* LF/NL */) &&
(pos < MAX_LINE))
{
switch (ch)
{
case 0x08: /* Backspace */
case 0x7F: /* Delete */
if (pos > 0)
{
pos -= 1;
out_char(0x08); /* backspace */
out_char(' ');
out_char(0x08); /* backspace */
}
break;
default:
if ((pos+1) < MAX_LINE)
{
/* only printable characters */
if ((ch > 0x1f) && (ch < 0x80))
{
line[pos++] = (char)ch;
out_char((char)ch);
}
}
break;
}
ch = (int)in_char();
}
out_char(0x0D); /* CR */
out_char(0x0A); /* LF */
if (!pos && (strncasecmp(userline,"md",2) == 0))
{
/* Allow 'md' command to be repeated */
}
else
{
line[pos] = '\0';
strcpy(userline,line);
}
}
/********************************************************************/
static int
make_argv (char *cmdline, char *argv[])
{
int argc, i, in_text;
/* break cmdline into strings and argv */
/* it is permissible for argv to be NULL, in which case */
/* the purpose of this routine becomes to count args */
argc = 0;
i = 0;
in_text = FALSE;
/* getline() must place 0x00 on end */
while (cmdline[i] != '\0')
{
if (((cmdline[i] == ' ') ||
(cmdline[i] == '\t')) )
{
if (in_text)
{
/* end of command line argument */
cmdline[i] = '\0';
in_text = FALSE;
}
else
{
/* still looking for next argument */
}
}
else
{
/* got non-whitespace character */
if (in_text)
{
}
else
{
/* start of an argument */
in_text = TRUE;
if (argc < MAX_ARGS)
{
if (argv != NULL)
argv[argc] = &cmdline[i];
argc++;
}
else
/*return argc;*/
break;
}
}
i++; /* proceed to next character */
}
if (argv != NULL)
argv[argc] = NULL;
return argc;
}
/********************************************************************/
static uint32
get_value (char *s, int *success, int base)
{
uint32 value;
char *p;
value = strtoul(s,&p,base);
if ((value == 0) && (p == s))
{
*success = FALSE;
return 0;
}
else
{
*success = TRUE;
return value;
}
}
/********************************************************************/
void
run_cmd (void)
{
int argc;
char *argv[MAX_ARGS + 1]; /* One extra for NULL terminator */
get_user_input(input);
argc = make_argv(input,argv);
if (argc)
{
int i;
for (i = 0; i < NUM_CMD; i++)
{
if (strcasecmp(CMDTAB[i].cmd,argv[0]) == 0)
{
if (((argc-1) >= CMDTAB[i].min_args) &&
((argc-1) <= CMDTAB[i].max_args))
{
CMDTAB[i].func(argc,argv);
return;
}
else
{
printf(SYNTAX,argv[0]);
return;
}
}
}
printf(INVCMD,argv[0]);
printf(HELPMSG);
}
}
/********************************************************************/
void
help (int argc, char **argv)
{
int index;
(void)argc;
(void)argv;
printf("\n");
for (index = 0; index < NUM_CMD; index++)
{
printf(HELPFORMAT,
CMDTAB[index].cmd,
CMDTAB[index].description,
CMDTAB[index].cmd,
CMDTAB[index].syntax);
}
printf("\n");
}
/********************************************************************/
void
quit (int argc, char **argv)
{
(void)argc;
(void)argv;
printf("Exiting...\n");
while (1)
;
}
/********************************************************************/
void
toggle_leds (void)
{
uint32 led;
uint32 datalength;
datalength = MCF_USB_EP3FDR;
led = MCF_USB_EP3FDR;
if(led&0x80) //D22
MCF_GPIO_PODR_TIMERL &= ~(0x02);
else
MCF_GPIO_PODR_TIMERL |= 0x02;
if(led&0x40) //D24
MCF_GPIO_PODR_TIMERL &= ~(0x08);
else
MCF_GPIO_PODR_TIMERL |= 0x08;
if(led&0x20) //D26
MCF_GPIO_PODR_TIMERH &= ~(0x02);
else
MCF_GPIO_PODR_TIMERH |= 0x02;
if(led&0x10) //D28
MCF_GPIO_PODR_TIMERH &= ~(0x08);
else
MCF_GPIO_PODR_TIMERH |= 0x08;
if(led&0x08) //D21
MCF_GPIO_PODR_TIMERL &= ~(0x01);
else
MCF_GPIO_PODR_TIMERL |= 0x01;
if(led&0x04) //D23
MCF_GPIO_PODR_TIMERL &= ~(0x04);
else
MCF_GPIO_PODR_TIMERL |= 0x04;
if(led&0x02) //D25
MCF_GPIO_PODR_TIMERH &= ~(0x01);
else
MCF_GPIO_PODR_TIMERH |= 0x01;
if(led&0x01) //D27
MCF_GPIO_PODR_TIMERH &= ~(0x04);
else
MCF_GPIO_PODR_TIMERH |= 0x04;
}
/********************************************************************/
void
quicktest (void)
{
unsigned int i =0;
MCF_USB_EP0SR |= MCF_USB_EPnSR_FL;
printf("\n");
/* wait */
for(i=0; i<0xFFFF ; i++);
i=0;
/* Fill FIFO till FIFO Full */
while(!(MCF_USB_EP0ISR&MCF_USB_EPnISR_FH))
{
/* i is the byte count */
printf("EP0ISR 0x%08X\n",MCF_USB_EP0ISR);
/* Write Byte to FIFO */
MCF_USB_EP0FDR_B3 = i;
i++;
}
printf("i = %d\n",i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -