📄 ad4360.c
字号:
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, EP0_PACKET_SIZE, ControlData.pData) ;
ControlData.wCount += EP0_PACKET_SIZE ;
DISABLE ;
bEPPflags_control_state = USB_TRANSMIT ;
ENABLE ;
}
else // 发送代码小于包长度
{
D12_WriteEndpoint(1, ControlData.wLength, pRomData);
ControlData.wCount += ControlData.wLength;
DISABLE;
bEPPflags_control_state = USB_IDLE;
ENABLE;
}
}
// 设置DMA
void setup_dma()
{
ioSize = ioRequest.uSize;
ioCount = 0;
DISABLE;
bEPPflags_dma_state = DMA_RUNNING;
ENABLE;
single_transmit(0, 0);
if(ioRequest.bCommand & 0x1)
{
if(ioSize > 64 || ioSize == 0)
{
D12_WriteEndpoint(5, 64, EpBuf);
ioCount += 64;
}
else
{
D12_WriteEndpoint(5, ioSize, EpBuf);
ioCount += ioSize;
}
}
}
// 处理标准USB设备请求和厂商请求
void control_handler()
{
unsigned char type, req;
type = ControlData.DeviceRequest.bmRequestType & USB_REQUEST_TYPE_MASK; //0x60
req = ControlData.DeviceRequest.bRequest & USB_REQUEST_MASK; //0x0F
//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();
}
void main ( )
{
unsigned char i , j ;
P1_2 = False ;
P1_3 = False ;
P3_3 = False ;
// OutFreq ( 0x00 ) ;
RecivedHead = False ;
coma_count_rxd = coma_count_txd = coma_max_count_txd = 0x00 ;
RxdHead = RxdEnd = 0x00 ;
//DISABLE;
init_port();
P1_2 = False ;
P1_3 = False ;
P3_3 = False ;
init_serial();
init_timer0();
MainRxDone = False ;
MainTxDone = False ;
MainRxFlag = False ;
EA = False ;
AUXR_89C516 = 0x01 ; // Use Internal SRAM 1KB,禁止ALE输出
//for test
IE0=0;
IT0=0; // 必须设置成电平触发中断
EX0=1;
//--------------------
// MCU_D12RST = 0;
// MCU_D12RST = 1;
//--------------------
/* Test WatchDog
RunLed = 0 ;
WDT_CONTR = 0x36 ; // 2.3 s
while ( 1 )
{
WDT_CONTR |= 0x10 ;
}
*/
MCU_D12CS = True ;
bNoRAM = TRUE;
//--------------------------
MCU_D12CS = False;
i=D12_ReadInterruptRegister();
i=0;
//i=D12_ReadEndpointStatus(0);
i=D12_ReadLastTransactionStatus(0);
i=D12_ReadLastTransactionStatus(1);
i=D12_ReadLastTransactionStatus(2);
i=D12_ReadLastTransactionStatus(3);
i=D12_ReadLastTransactionStatus(4);
i=D12_ReadLastTransactionStatus(5);
ENABLE;
reconnect_USB();
ENABLE;
// 在此其间,为了不影响主机枚举所以还要执行一部分程序
// 如果只延时,那么可能使主机枚举USB的过程延长
TimerForICcard = 0x00 ;
while ( TimerForICcard < 200 ) // delay 0.5 s
{
if (bEPPflags_bus_reset) // USB总线复位
{
DISABLE;
bEPPflags_bus_reset = False ;
ENABLE;
// Release D12's SUSPEND pin after bus reset
// Enable 74HCT123 pulse generation before disconnect
// D12SUSPD = 1;
D12SUSPD=0;
}
if (bEPPflags_suspend) // USB总线状态改变
{
DISABLE;
bEPPflags_suspend= 0;
ENABLE;
if(D12SUSPD == 1) // USB总线挂起
{
D12SUSPD = 0;
P0 = 0xFF;
P1 = 0x00;
P2 = 0xFF;
P3 = 0xF7;
D12_SetDMA(0xC3);
D12SUSPD = 1;
PCON |= 0x02;
while (1) ; // loop forever
}
}
if (bEPPflags_setup_packet)// 建立包
{
DISABLE;
bEPPflags_setup_packet = 0;
ENABLE;
control_handler();
//D12SUSPD = 1;
D12SUSPD=0;
}
if(bEPPflags_setup_dma != 0)// 建立DMA
{
DISABLE;
bEPPflags_setup_dma --;
ENABLE;
setup_dma();
}
}
// 主循环程序
counter = 0x00 ;
freqno = 100 ;
counter = 0x00 ;
P3 = 0xf7 ;
while ( counter < 1000 ) _nop_ ( ) ;
OutFreq ( freqno ) ; // 依据频点代码输出控制频率数据值
while( True ) // Main program loop
{
if ( counter > 1000 )
{
OutFreq ( freqno ) ; // 依据频点代码输出控制频率数据值
counter = 0x00 ;
// freqno +=5 ;
// if ( freqno > 125 ) freqno = 0x00 ;
}
//*/
if (bEPPflags_bus_reset) // USB总线复位
{
DISABLE;
bEPPflags_bus_reset = 0;
ENABLE;
// Release D12's SUSPEND pin after bus reset
// Enable 74HCT123 pulse generation before disconnect
// D12SUSPD = 1;
D12SUSPD=0;
}
if (bEPPflags_suspend) // USB总线状态改变
{
DISABLE;
bEPPflags_suspend= 0;
ENABLE;
if(D12SUSPD == 1) // USB总线挂起
{
D12SUSPD = 0;
P0 = 0xFF;
P1 = 0x00;
P2 = 0xFF;
P3 = 0xF7;
D12_SetDMA(0xC3);
D12SUSPD = 1;
PCON |= 0x02;
while (1) ; // loop forever
}
}
if (bEPPflags_setup_packet)// 建立包
{
DISABLE;
bEPPflags_setup_packet = 0;
ENABLE;
control_handler();
//D12SUSPD = 1;
D12SUSPD=0;
}
if(bEPPflags_setup_dma != 0)// 建立DMA
{
DISABLE;
bEPPflags_setup_dma --;
ENABLE;
setup_dma();
}
///////
if (MainRxDone)
{
MainRxDone = False ;
MainRxFlag = True ;
// MainTxLength = 100 ;
// MainTxIndex = 0x31 ;
// MainTxDone = True ;
}
if (MainTxDone)
{
if ( MainTxLength > 0x00 )
{
ioSize = ioCount = 64 ;
for ( i = 23 ; i < 64 ; i ++ ) EpBuf [i] = 0x21 + i ;
for ( i = 0 ; i < 8 ; i ++ ) EpBuf [i] = MyDateStr[i] ;
for ( i = 0 ; i < 8 ; i ++ ) EpBuf [i+10] = MyTimeStr[i] ;
EpBuf [8] = 0x20 ;
EpBuf [9] = 0x20 ;
EpBuf [18] = 0x20 ;
EpBuf [19] = 0x20 ;
EpBuf [20] = 0x20 ; //MainTxIndex ;
EpBuf [21] = 0x20 ;
EpBuf [22] = 0x20 ;
if ( MainTxLength > 64 ) D12_WriteEndpoint(5, 64, EpBuf );
else D12_WriteEndpoint(5, MainTxLength, EpBuf );
if ( MainTxLength > 64 ) MainTxLength -= 64 ;
else MainTxLength = 0x00 ;
// MainTxIndex ++ ;
}
MainTxDone = False ;
}
// 命令格式:"!123x!",!=标识,123=命令号三个ASCII字符(数字),X=三个ASC字符的XOR校验
if(bEPPflags_ep1_rxdone)
{
DISABLE;
bEPPflags_ep1_rxdone = False;
ENABLE;
CommandFlag = True ;
if ( ( GenEpBuf[0] != '!' ) || ( GenEpBuf[5] != '!' ) ) CommandFlag = False ;
i = GenEpBuf[1] ^ GenEpBuf[2] ;
i ^= GenEpBuf[3] ;
if ( i != GenEpBuf[4] ) CommandFlag = False ;
i = ( GenEpBuf[1] - 0x30 ) * 100 ;
i += ( GenEpBuf[2] - 0x30 ) * 10 ;
i += ( GenEpBuf[3] - 0x30 ) ;
if ( i >= MaxCmdNo ) CommandFlag = False ;
if ( CommandFlag )
{
for ( j = 0 ; j < 6 ; j ++ ) GenEpBufToHost[j]=GenEpBuf[j] ;
switch (i)
{
case 0 :
default :
OutFreq ( GenEpBuf[6] ) ; // 依据频点代码输出控制频率数据值
for ( i = 0 ; i < 16 ; i ++ ) GenEpBufToHost[i]=GenEpBuf[i] ;
break ;
}
}
else for ( i = 0 ; i < 16 ; i ++ ) GenEpBufToHost[i] = ErrorCheck [i] ;
D12_WriteEndpoint(3, 16, GenEpBufToHost);
}
/*
if ( RxdEnd != RxdHead )
{
coma_rxd_buff [ RxdEnd ++ ] ;
RxdEnd &= 0x7f ;
}
*/
} // Main Loop
//=======================================
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -