📄 testusbbin.c
字号:
/*
**************************************************************************************************************
* uC/OS-II
* The Real-Time Kernel
*
*
* AVR Sample code
* File : TEST.C
* By : Ole Saether
* Version : V1.01
*
* AVR-GCC port version : 1.0 2001-04-02 modified/ported to avr-gcc by Jesper Hansen (jesperh@telia.com)
*
* 2003-06-27 Modification to gcc v. 3.x and uC/OS-II v 2.52 by Julius Luukko (Julius.Luukko@lut.fi).
* See the file README for details of the changes.
*
*
* Description :
*
* This file contains a simple example program showing how to use the AVR port of uC/OS-II. It is
* based on Example #1 from Jean Labrosse's book "MicroC/OS-II, The Real Time Kernel." The main
* difference is that this example does not display the time of day and the uC/OS-II version number.
* You must have the AVR UART connected to a VT102 compatible terminal (HyperTerminal in Windows is OK)
* to get the most out of this example.
*
* The support routines at the end of this file are included only to make this example run; they should
* not be used in production code without careful testing.
**************************************************************************************************************
*/
#include "includes.h"
#include "USB.h"
/*
**************************************************************************************************************
* CONSTANTS
**************************************************************************************************************
*/
#define TASK_STK_SIZE OS_TASK_DEF_STK_SIZE /* Size of each task's stacks (# of bytes) */
/* #define UART_TX_BUF_SIZE 512 */
#define UART_TX_BUF_SIZE 256
#define MSG_QUEUE_SIZE 20
/*
**************************************************************************************************************
* VARIABLES
**************************************************************************************************************
*/
OS_EVENT *DispStrSem;
INT8U UartTxBuf[UART_TX_BUF_SIZE]; /* UART transmit buffer */
INT16U UartTxRdPtr; /* UART transmit buffer read pointer */
INT16U UartTxWrPtr; /* UART transmit buffer write pointer */
INT16U UartTxCount; /* Number of characters to send */
OS_EVENT *UartTxSem;
/*
typedef struct {
char name[10];
INT8U message;
}OSMSG,*POSMSG; Message Type */
/*
**************************************************************************************************************
* TASK DEFINE
**************************************************************************************************************
*/
//MAIN TASK
void MainTask(void *data); /* Function prototypes of Startup task */
OS_STK MainTaskStk[TASK_STK_SIZE];
#define Main_Task_Prio 0;
//LCD TASK
void LCDTask(void *data); /* Function prototypes of Additional task */
OS_STK LCDTaskStk[TASK_STK_SIZE];
#define LCD_Task_Prio 12;
/*
**************************************************************************************************************
* SPECIAL FUNCTION
**************************************************************************************************************
*/
void PutChar(char c); /* Write a character to the AVR UART */
void AvrInit(void); /* Initialize AVR */
void PutString(const char *s); /* Write a null-terminated string to the AVR UART */
void SPrintDec(char *, INT16U, INT8U); /* Output an INT16U to a string (right adjust) */
void PutDec (INT8U x); /* Display an INT8U without leading zeros */
void VT102Attribute (INT8U fgcolor, INT8U bgcolor); /* Set attributes on VT102 terminal */
void VT102DispClrScr(void); /* Clear VT102 terminal */
void VT102DispChar(INT8U, INT8U, char, INT8U, INT8U); /* Display a character on VT102 terminal */
void VT102DispStr(INT8U, INT8U, char *, INT8U, INT8U); /* Display a string on VT102 terminal */
void VT102DispNumH (INT8U x, INT8U y, INT16U n3, INT8U fgcolor, INT8U bgcolor);
INT8U convertH(INT8U n);
void VT102DispNum (INT8U x, INT8U y, INT16U x4, INT8U fgcolor, INT8U bgcolor);
char GetKey(void);
char GetKeyPro(void);
int testsram(void);
void USB(void);
void init_port(void);
void init_special_interrupts(void);
/*
**************************************************************************************************************
* USB varieties
**************************************************************************************************************
*/
void bus_reset(void);
void ep0_txdone(void);
void ep0_rxdone(void);
void ep1_txdone(void);
void ep1_rxdone(void);
void main_txdone(void);
void main_rxdone(void);
void dma_eot(void);
void outportb(unsigned int Addr,unsigned char Data);
unsigned char inportb(unsigned int Addr);
void fn_usb_isr(void);
unsigned char D12_GetDMA(void);
/*
*********************
Public static data
*********************
*/
EPPFLAGS bEPPflags;
/* Control endpoint TX/RX buffers */
CONTROL_XFER ControlData;
/* ISR static vars */
unsigned char GenEpBuf[EP1_PACKET_SIZE];
unsigned char EpBuf[EP2_PACKET_SIZE];
IO_REQUEST ioRequest;
unsigned char ioSize, ioCount;
unsigned long ClockTicks = 0;
unsigned char MainEpBuf[256];
BOOL bNoRAM;
/*
*************************************
USB protocol function pointer arrays
*************************************
*/
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
};
void (*VendorDeviceRequest[])(void) =
{
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
reserved,
read_write_register,
reserved,
reserved,
reserved
};
/*
********************
Public static data
********************
*/
char * _NAME_USB_REQUEST_DIRECTION[] =
{
"Host_to_device",
"Device_to_host"
};
char * _NAME_USB_REQUEST_RECIPIENT[] =
{
"Device",
"Interface",
"Endpoint(0)",
"Other"
};
char * _NAME_USB_REQUEST_TYPE[] =
{
"Standard",
"Class",
"Vendor",
"Reserved"
};
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"
};
//#include "protodma.h"
//CONTROL_XFER ControlData;
//extern IO_REQUEST ioRequest;
//EPPFLAGS bEPPflags;
//bool bNoRAM;
/*
**************************************************************************************************************
* MAIN
**************************************************************************************************************
*/
int main (void)
{
AvrInit(); /* Initialize the AVR UART and Timer */
OSInit();
DispStrSem = OSSemCreate(1); /* Display string semaphore */
UartTxSem = OSSemCreate(UART_TX_BUF_SIZE); /* Initialize Uart transmit buffer semaphore */
OSTaskCreate(MainTask, (void *)0, (void *)&MainTaskStk[TASK_STK_SIZE - 1], 0);
OSStart(); /* Start multitasking */
return 0;
}
/*
**************************************************************************************************************
* MAIN TASK
**************************************************************************************************************
*/
void MainTask (void *data)
{
unsigned char key, i;
data = data; /* Prevent compiler warning */
/*
* Enabling of timer interrupt is moved from AvrInit() to here
*/
OS_ENTER_CRITICAL();
TCCR0=0x07; /* Set TIMER0 prescaler to CLK/1024 */
TIMSK=_BV(TOIE0); /* Enable TIMER0 overflow interrupt */
TCNT0=256-(CPU_CLOCK_HZ/OS_TICKS_PER_SEC/1024); /* Set the counter initial value */
OS_EXIT_CRITICAL();
VT102DispClrScr(); /* Clear the screen */
VT102DispStr(26, 1, "The Embeded System Based on AVR MCU", COLOR_WHITE, COLOR_RED);
VT102DispStr(29, 2, "Micro Control Unit: ATmega 128 ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(16, 3, "The RTOS based on: uC/OS-II, The Real-Time Kernel", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(10, 4, "Main Function: UART USB Ethernet", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(60, 4, "Date: April 25th,2004", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 23, "Determining CPU's Memory ...", COLOR_WHITE, COLOR_BLACK);
OSStatInit(); /* Initialize uC/OS-II's statistics */
// OSTaskCreate(KeyTask, (void *)0, (void *)&KeyTaskStk[TASK_STK_SIZE - 1], 1);
OSTaskCreate(LCDTask, (void *)0, (void *)&LCDTaskStk[128 - 1], 2);
OSTimeDlyHMSM(0,0,1,0);
VT102DispStr(1, 5, "Determining CPU's Capacity ...", COLOR_WHITE, COLOR_BLACK);
init_port();
// init_serial();
// init_timer0();
init_special_interrupts();
// MCU_D12CS = 0x1;
for(i = 0; i < 16; i ++)
MainEpBuf[i] = i;
for(i = 0; i < 16; i ++) {
if(MainEpBuf[i] != i)
break;
}
if(i < 16)
bNoRAM = TRUE;
else
bNoRAM = FALSE;
// MCU_D12CS = 0x0;
// if(MCU_SWM0 == 0 && MCU_SWM1 == 0) {
// MCU_D12RST = 0;
// MCU_D12RST = 1;
// D12_SetDMA(0x0);
// }
if((i = D12_GetDMA()) == 0xC3) {
D12_SendResume();
VT102DispStr(51, 19, "DMA is ", COLOR_WHITE, COLOR_BLACK);
VT102DispNumH(60, 19, i, COLOR_WHITE, COLOR_BLACK);
}
else {
bEPPflags.value = 0;
/* Power on reset, lightup LEDs for 1 sec,
disconnect and reconnect Soft-Connect */
// printf("\nPDIUSBD12 SMART evaluation board firmware V3.0.\n");
VT102DispStr(1, 6, "PDIUSBD12 SEAPORT board firmware V3.0.", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 7, "input number 1 to see DMA status. ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 8, "input number 2 to reconect.", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 9, "input number 3 to see the state of verbose. ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 10, "input symble # : esc of USB", COLOR_WHITE, COLOR_BLACK);
outportb(D12_DATA,'b');
key=inportb(D12_DATA);
VT102DispChar (2, 20, key, COLOR_WHITE, COLOR_BLACK);
VT102DispStr(3, 20, " ", COLOR_WHITE, COLOR_BLACK);
reconnect_USB();
}
key=inportb(D12_COMMAND);
VT102DispNumH(3, 20, key, COLOR_WHITE, COLOR_BLACK);
/* Main program loop */
while(key!='#')
{
key=GetKeyPro();
VT102DispStr(1, 19, "you have entered:", COLOR_WHITE, COLOR_BLACK);
VT102DispChar (30, 19, key, COLOR_WHITE, COLOR_BLACK);
VT102DispStr(31, 19, " ", COLOR_WHITE, COLOR_BLACK);
// VT102DispStr(51, 19, "PORTD is ", COLOR_WHITE, COLOR_BLACK);
// VT102DispNumH(60, 19, PORTD, COLOR_WHITE, COLOR_BLACK);
switch(key) {
case '1':
if(bEPPflags.bits.control_state == USB_IDLE)
// printf("Control state = USB_IDLE.");
VT102DispStr(1, 11, "Control state = USB_IDLE.", COLOR_WHITE, COLOR_BLACK);
else if(bEPPflags.bits.control_state == USB_RECEIVE)
// printf("Control state = USB_RECEIVE.");
VT102DispStr(1, 11, "Control state = USB_RECEIVE.", COLOR_WHITE, COLOR_BLACK);
else if(bEPPflags.bits.control_state == USB_TRANSMIT)
// printf("Control state = USB_TRANSMIT.");
VT102DispStr(1, 11, "Control state = USB_TRANSMIT.", COLOR_WHITE, COLOR_BLACK);
// printf(" ControlData.wCount = %x.\n", ControlData.wCount);
VT102DispStr(1, 12, "ControlData.wCount =", COLOR_WHITE, COLOR_BLACK);
VT102DispNum(23, 12, ControlData.wCount, COLOR_WHITE, COLOR_BLACK);
// printf("Endpoint 4 (Bulk Out) Status = %bx, Endpoint 5 (Bulk In) Status = %bx.\n",
// D12_ReadEndpointStatus(4), D12_ReadEndpointStatus(5));
VT102DispNum(32, 13, D12_ReadEndpointStatus(4), COLOR_WHITE, COLOR_BLACK);
VT102DispNum(70, 13, D12_ReadEndpointStatus(5), COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 13, "Endpoint 4 (Bulk Out) Status =", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(39, 13, "Endpoint 5 (Bulk In) Status =", COLOR_WHITE, COLOR_BLACK);
if(bEPPflags.bits.dma_state == DMA_IDLE)
VT102DispStr(1, 11, "DMA_State = DMA_IDLE.", COLOR_WHITE, COLOR_BLACK);
// printf("DMA_State = DMA_IDLE.\n");
else if(bEPPflags.bits.dma_state == DMA_RUNNING)
VT102DispStr(1, 11, "DMA_State = DMA_IDLE.", COLOR_WHITE, COLOR_BLACK);
// printf("DMA_State = DMA_RUNNING.\n");
else if(bEPPflags.bits.dma_state == DMA_PENDING)
VT102DispStr(1, 11, "DMA_State = DMA_IDLE.", COLOR_WHITE, COLOR_BLACK);
// printf("DMA_State = DMA_PENDING.\n");
// printf("Last Device Request: bmRequestType = 0x%bx, bRequest= 0x%bx, wValue = 0x%x, wLength = 0x%x, wIndex = 0x%x.\n",
// ControlData.DeviceRequest.bmRequestType,
// ControlData.DeviceRequest.bRequest,
// ControlData.DeviceRequest.wValue,
// ControlData.DeviceRequest.wLength,
// ControlData.DeviceRequest.wIndex);
// printf("Data: ");
// for(i = 0; i < ControlData.DeviceRequest.wLength; i ++)
// printf("0x%bx, ", *((ControlData.dataBuffer)+i));
// printf("\n");
break;
case '2':
VT102DispStr(1, 13, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 12, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 11, " ", COLOR_WHITE, COLOR_BLACK);
reconnect_USB();
break;
case '3':
VT102DispStr(1, 13, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 12, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 11, " ", COLOR_WHITE, COLOR_BLACK);
if(bEPPflags.bits.verbose == 0) {
// printf("Verbose Mode = ON.\n");
VT102DispStr(1, 11, "Verbose Mode = ON.", COLOR_WHITE, COLOR_BLACK);
DISABLE;
bEPPflags.bits.verbose = 1;
ENABLE;
}
else {
// printf("Verbose Mode = OFF.\n");
VT102DispStr(1, 11, "Verbose Mode = OFF.", COLOR_WHITE, COLOR_BLACK);
DISABLE;
bEPPflags.bits.verbose = 0;
ENABLE;
}
break;
case '#':
VT102DispStr(1, 13, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 12, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 11, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 19, " ", COLOR_WHITE, COLOR_BLACK);
VT102DispStr(1, 20, " ", COLOR_WHITE, COLOR_BLACK);
break;
default:
break;
}
if (bEPPflags.bits.bus_reset) {
DISABLE;
bEPPflags.bits.bus_reset = 0;
ENABLE;
// Release D12's SUSPEND pin after bus reset
// Enable 74HCT123 pulse generation before disconnect
// D12SUSPD = 1;
PORTD|=1<<4;
} // if bus reset
if (bEPPflags.bits.suspend) {
DISABLE;
bEPPflags.bits.suspend= 0;
ENABLE;
// if(D12SUSPD == 1) {
if((PIND&0x80) == 0x80) {
// D12SUSPD = 0;
PORTD|=0<<4;
PORTD|=1<<5;
D12_SetDMA(0xC3);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -