DMP_INLINE(void) uart_Free(SerialPort *port) { if (port == NULL) { err_print((char*)"%s: port is null.\n", __FUNCTION__); return; } DestoryQueue(port->rcvd); DestoryQueue(port->xmit); ker_Mfree((void*)port); }
DMPAPI(void) usb_Close(void *vusb) { USB_Device *usb = (USB_Device *)vusb; if (usb == NULL) { err_print((char*)"%s: USB device is null.\n", __FUNCTION__); return; } if (usb->InUse != 0) { io_DisableINT(); { io_outpb(usb->CFR, io_inpb(usb->CFR) & 0xFE); irq_UninstallISR(usb->nIRQ, (void *)usb); } io_RestoreINT(); irq_Close(); io_outpb(usb->CFR, 0x02); // Soft reset while (io_inpb(usb->CFR) & 0x02); #if defined DMP_DOS_DJGPP if (dma_Free(dma_handle) == false) err_print((char*)"%s: Free DMA buffer fail!!\n", __FUNCTION__); #endif ker_Mfree(usb->EP[0].SetupBuf); ker_Mfree(usb->EP[0].InBuf); ker_Mfree(usb->EP[0].OutBuf); ker_Mfree(usb->EP[1].InBuf); ker_Mfree(usb->EP[2].InBuf); ker_Mfree(usb->EP[2].OutBuf); if (io_Close() == false) err_print((char*)"%s: Close IO lib error!!\n", __FUNCTION__); usb->state = USB_DEV_POWERED; usb->InUse = 0; USB_Disconnect(); } DestoryQueue(usb->rcvd); DestoryQueue(usb->xmit); ker_Mfree((void *)usb); }
int main(int argc, char** argv) { int i,e,n; LinkQueue Q; printf("------------------------------------------------------\n"); printf("1.构造队列 2.插入元素\n"); printf("3.get队头元素 4.删除队头元素\n"); printf("5.清空队列 6.销毁队列\n"); printf("7.判断是否为空 8.退出程序\n"); printf("------------------------------------------------------\n"); while(1==1){ printf("请选择:"); scanf("%d",&i); switch(i){ case 1:InitQueue(Q); break; case 2:printf("请输入个数:"); scanf("%d",&n); printf("请输入数据:\n"); for(i=0;i<n;i++){ scanf("%d",&e); EnQueue(Q,e); } break; case 3:GetHead(Q,e); printf("队头元素是:%d\n",e); break; case 4:DeQueue(Q,e); printf("被删除的队头元素是:%d\n",e); break; case 5:ClearQueue(Q); break; case 6:DestoryQueue(Q); break; case 7:printf("若为空,则为1,否则为0\nresult=%d\n",QueueEmpty(Q)); break; case 8:exit(0); } } return 0; }
void main() { Queue *queue = NULL; Element item; queue = CreateQueue(); item.key = 1; printf("Add ist element into the queue.\n"); AddQueue(queue, item); PrintQueue(queue); printf("Add 2st element into the queue.\n"); item.key = 2; AddQueue(queue, item); PrintQueue(queue); printf("Add 2st element into the queue.\n"); item.key = 3; AddQueue(queue, item); PrintQueue(queue); printf("Add 2st element into the queue.\n"); item.key = 4; AddQueue(queue, item); PrintQueue(queue); printf("Delete the top element from the queue.\n"); item = DeleteQueue(queue); printf("Delete %d\n", item.key); PrintQueue(queue); printf("Destroy the queue.\n"); DestoryQueue(queue); PrintQueue(queue); }
DMPAPI(void *) CreateUART(int com) { SerialPort *port; if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); return NULL; } if ((port = (SerialPort *)ker_Malloc(sizeof(SerialPort))) == NULL) { io_Close(); return NULL; } port->com = com; port->addr = vx86_uart_GetBaseAddr(com); port->nIRQ = vx86_uart_GetIRQ(com); if (port->addr == 0x00 || port->nIRQ == 0) { err_print((char*)"%s: COM%d is null. [Base address: 0x%04x, IRQ: %d]\n", __FUNCTION__, com+1, port->addr, port->nIRQ); ker_Mfree((void*)port); io_Close(); return NULL; } port->nBuad = 115200L; port->nData = BYTESIZE8; port->nStop = STOPBIT1; port->nParity = NOPARITY; port->control = NO_CONTROL; port->InUse = 0; port->INT_InUse = 0; port->rts = 0; port->cts = CTS_OFF; port->xonxoff_rcvd = XOFF_RCVD; port->xonxoff_xmit = XOFF_XMIT; port->xon = 0; port->xoff = 0; port->old_lsb = port->lsb = 0; port->old_msb = port->msb = 0; port->old_ier = port->ier = 0; port->old_lcr = port->lcr = 0; port->old_mcr = port->mcr = 0; port->old_TimeOut = port->TimeOut = UART_NO_TIMEOUT; port->fcr = 0; port->RFIFO_Size = 0; port->WFIFO_Size = 0; if ((port->rcvd = CreateQueue(RX_QUEUE_SIZE)) == NULL) goto CREATE_RX_QUEUE_FAIL; if ((port->xmit = CreateQueue(TX_QUEUE_SIZE)) == NULL) goto CREATE_TX_QUEUE_FAIL; port->msr_handler = NULL; port->lsr_handler = NULL; port->TXDB = port->addr + 0; port->RXDB = port->addr + 0; port->DLLSB = port->addr + 0; port->DLMSB = port->addr + 1; port->IER = port->addr + 1; port->IIR = port->addr + 2; port->FCR = port->addr + 2; port->LCR = port->addr + 3; port->MCR = port->addr + 4; port->LSR = port->addr + 5; port->MSR = port->addr + 6; port->SCR = port->addr + 7; return (void *)port; CREATE_TX_QUEUE_FAIL: DestoryQueue(port->rcvd); CREATE_RX_QUEUE_FAIL: ker_Mfree((void*)port); io_Close(); return NULL; }
DMPAPI(void *) CreateUSBDevice(void) { USB_Device *usb = NULL; if ((usb = (USB_Device *)ker_Malloc(sizeof(USB_Device))) == NULL) return NULL; if (io_Init() == false) { err_print((char*)"%s: Init IO lib error!!\n", __FUNCTION__); ker_Mfree((void *)usb); return NULL; } usb->addr = vx86_GetUSBDevAddr(); usb->nIRQ = vx86_GetUSBDevIRQ(); if (usb->addr == 0x0000 || usb->nIRQ == 0) { io_Close(); ker_Mfree((void *)usb); return NULL; } usb->DevAddr = 0x00; usb->ReadySetAddr = false; usb->state = USB_DEV_NOT_ATTACHED; usb->stall = false; usb->InUse = 0; usb->IsSet = 0; usb->setup_in_handled = false; usb->setup_out_handled = false; usb->bulk_in_transmitting = false; usb->TimeOut = USB_NO_TIMEOUT; usb->InDataPtr = NULL; usb->OutDataPtr = NULL; usb->InDataSize = 0; usb->OutDataSize = 0; if ((usb->rcvd = CreateQueue(RX_QUEUE_SIZE)) == NULL) goto CREATE_RX_QUEUE_FAIL; if ((usb->xmit = CreateQueue(TX_QUEUE_SIZE)) == NULL) goto CREATE_TX_QUEUE_FAIL; usb->Setup.bmRequestType = 0; usb->Setup.bRequest = 0; usb->Setup.wValue.Value = 0; usb->Setup.wIndex.Value = 0; usb->Setup.wLength = 0; usb->ling_coding.dwDTERate = 0; usb->ling_coding.bCharFormat = 0; usb->ling_coding.bParityType = 0; usb->ling_coding.bDataBits = 0; usb->control_line_state = 0; usb->serial_state = 0; usb->DAR = usb->addr + 0x00; usb->CFR = usb->addr + 0x02; usb->FNR = usb->addr + 0x06; usb->IER = usb->addr + 0x08; usb->ISR = usb->addr + 0x0C; usb->TMR = usb->addr + 0x68; memset((Endpoint *)usb->EP, 0, sizeof(usb->EP)); usb->EP[0].CtrlTR = usb->addr + 0x10; usb->EP[1].OutTR = usb->addr + 0x12; usb->EP[1].InTR = usb->addr + 0x14; usb->EP[2].OutTR = usb->addr + 0x16; usb->EP[2].InTR = usb->addr + 0x18; usb->EP[3].OutTR = usb->addr + 0x1A; usb->EP[3].InTR = usb->addr + 0x1C; usb->EP[0].SetupDLR = usb->addr + 0x20; usb->EP[0].OutDLR = usb->addr + 0x24; usb->EP[0].InDLR = usb->addr + 0x28; usb->EP[1].OutDLR = usb->addr + 0x2C; usb->EP[1].InDLR = usb->addr + 0x30; usb->EP[2].OutDLR = usb->addr + 0x34; usb->EP[2].InDLR = usb->addr + 0x38; usb->EP[3].OutDLR = usb->addr + 0x3C; usb->EP[3].InDLR = usb->addr + 0x40; usb->EP[0].SetupDSR = usb->addr + 0x44; usb->EP[0].OutDSR = usb->addr + 0x48; usb->EP[0].InDSR = usb->addr + 0x4C; usb->EP[1].OutDSR = usb->addr + 0x50; usb->EP[1].InDSR = usb->addr + 0x54; usb->EP[2].OutDSR = usb->addr + 0x58; usb->EP[2].InDSR = usb->addr + 0x5C; usb->EP[3].OutDSR = usb->addr + 0x60; usb->EP[3].InDSR = usb->addr + 0x64; #ifdef DMP_86DUINO_MODE set_gpio_config_addr(GPIO_CONFIG_ADDR); // for 86duino set_tx_led(7, 2); set_rx_led(7, 3); TX_LED_OFF(); RX_LED_OFF(); #endif return (void *)usb; CREATE_TX_QUEUE_FAIL: DestoryQueue(usb->rcvd); CREATE_RX_QUEUE_FAIL: io_Close(); ker_Mfree((void *)usb); return NULL; }