📄 main.c
字号:
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "44b.h"
//#include "../inc/shell.h"
#include "def.h"
#include "D12Def.h"
//#include "utils.h"
#define ESC_KEY 0x1b
extern U8 g_Command; //上位机发来的命令
extern U8 g_DataLen ; //处理的数据长度
extern unsigned long g_Address;
extern unsigned long g_Count;
void D12Suspend( U8 flag )
{
if( !flag )
{//使能
rPDATB &= 0x7ef;
}
else
{//挂起
rPDATB |= 0x10;
}
}
U8 D12RdEpStat(U8 Ep)
{
D12Cmd = Ep|0x80;
return D12Dat;
}
U8 D12RdLastTrStat(U8 Ep)
{
D12Cmd = Ep|0x40;
return D12Dat;
}
//设置端点状态
void D12SetEpStat(U8 Ep,U8 Stat)
{
D12Cmd = Ep+0x40;
D12Dat = Stat;
}
//设置有效地址
void D12SetAddr(U8 Addr)
{
D12Cmd = 0xd0;
D12Dat = Addr;
}
//设置同步端点是否有效.
void D12SetEp(U8 Val)
{
D12Cmd = 0xd8;
D12Dat = Val;
}
//设置DMA
void D12SetDma(U8 Dma)
{
D12Cmd = 0xfb;
D12Dat = Dma;
}
//设置工作模式
void D12SetMode(U8 Val1, U8 Val2)
{
D12Cmd = 0xf3;
D12Dat = Val1;
D12Dat = Val2;
}
void D12SendResume()
{
D12Cmd = 0xf6;
}
void D12AckEp(U8 Ep)
{
D12Cmd = Ep; //选择endpoint
D12Cmd = 0xf1; //acknowledge setup
if(!Ep)
D12Cmd = 0xf2; //clear buffer
}
//读取芯片ID
U32 D12RdChipId()
{
U32 tmp;
//rPCOND = 0x1551;
//rPDATD |= 2;
D12Cmd = 0xfd;
//Delay(0);
//return tmp;
tmp = D12Dat;
//Delay(0);
tmp = tmp<<8|D12Dat;
//Delay(0);
return tmp;
}
//获取DMA信息
U8 D12GetDma()
{
D12Cmd = 0xfb;
//return D12Cmd;
return D12Dat;
}
//从endpoint读取数据
U8 D12RdEp(U8 Ep,U8 buf[],U8 cnt)
{
U8 tmp;
D12SelEp(Ep);
//if(D12Cmd&1)
if( D12Dat & 1 ) //modified by yangkai
{
D12Cmd = 0xf0;
//tmp = D12Cmd;
//tmp = D12Cmd;
tmp = D12Dat; //modified by yangkai
tmp = D12Dat;
if(tmp<cnt)
cnt = tmp;
tmp = cnt;
while(cnt)
{
//*buf++ = D12Cmd;
*buf++ = D12Dat; //modified by yangkai
cnt--;
}
D12Cmd = 0xf2;
return tmp;
}
else
return 0;
}
//写数据
void D12WrEp(U8 Ep, U8 buf[], U8 cnt)
{
while(1)
{
D12SelEp(Ep);
//if(!(D12Cmd&1))
if(!(D12Dat&1)) //modified by yangkai
break;
}
D12Cmd = 0xf0;
D12Dat = 0;
D12Dat = cnt;
while(cnt)
{
D12Dat = *buf++;
cnt--;
}
D12Cmd = 0xfa;
}
//读取中断寄存器的值
U8 D12RdInt()
{
//D12Cmd = 0xf4;
//return D12Cmd;
U8 bl;
unsigned int j;
D12Cmd = 0xf4;
bl = D12Dat;
j = D12Dat;
j <<= 8;
j += bl;
return j;
}
int main(void)
{
U8 i;
U8 Buffer[64];
//unsigned char test[]="成功";
//static unsigned long test_off_side = 0xc400000;//测试数据偏移
//while(1) *(volatile unsigned int *)0xc400000 = UsbRdChipId();
//printf( "\nPDIUSBD12 TEST \n" ) ;
/*
D12Cmd = 0xf3;
D12Dat = 0xde;
D12Dat = 0xb2;
*/
//rPDATD |= (0x02);
UsbInit();
if(UsbRdChipId()==0x1210)
{
//puts("Pdiusbd12 Found, chip id = 0x1210\n");
//printf( "Please connect USB Line from PC to the Board \n" ) ;
//printf( "run LEDDEMO.EXE in the PC and Test \n" ) ;
g_Command = 0; //上位机发来的命令
g_DataLen = 0; //处理的数据长度
g_Address = 0;
g_Count = 0;
UsbInit();
i = D12RdInt();
while( 1 ) //getkey()!=ESC_KEY )
{
i = UsbQueryInt();
UsbIntProc(i);
if( g_Command == 0x01 )
{//烧写flash命令
Buffer[0] = 0x5A;
Buffer[1] = 0xA5;
Buffer[2] = 0x02; //烧写flash的响应.
Buffer[3] = g_DataLen;
*(unsigned long*)(Buffer+4) = g_Address;
Buffer[8] = 0;
Buffer[9] = 0;
D12WrEp( 5 , Buffer , 10 );
g_Command = 0;
}
else if( g_Command == 0x03 )
{
Buffer[0] = 0x5A;
Buffer[1] = 0xA5;
Buffer[2] = 0x04; //验证flash的响应.
Buffer[3] = g_DataLen;
*(unsigned long*)(Buffer+4) = g_Address;
memcpy( Buffer+10 , (U8*)g_Address , g_DataLen );
Buffer[8] = 0;
Buffer[9] = 0;
D12WrEp( 5 , Buffer , 10+g_DataLen );
g_Command = 0;
}
}//while( 1 )
D12SetMode(MyD12EpCfgOff, D12Pll24M); //pull-up off
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -