int8u ModbusSendDataSerial(void) { int16u i; int16u crc; g_modbusSendBufSerial[0] = modbusTerminalID; g_modbusSendBufNumberSerial++; crc = usMBCRC16(g_modbusSendBufSerial, g_modbusSendBufNumberSerial); for(i = 0; i < g_modbusSendBufNumberSerial; i++) { QueueWrite((void *)UART1SendBuf, g_modbusSendBufSerial[i]); } QueueWrite((void *)UART1SendBuf, crc & 0xff); QueueWrite((void *)UART1SendBuf, crc >> 8); return MODBUS_OK; }
static void write_queue(ParallelWriter *self, const void *buffer, uint32 len) { struct iovec iov[2]; AssertArg(self->conn != NULL); AssertArg(self->queue != NULL); AssertArg(len == 0 || buffer != NULL); iov[0].iov_base = &len; iov[0].iov_len = sizeof(len); iov[1].iov_base = (void *) buffer; iov[1].iov_len = len; for (;;) { if (QueueWrite(self->queue, iov, 2, DEFAULT_TIMEOUT_MSEC, false)) return; PQconsumeInput(self->conn); if (!PQisBusy(self->conn)) { ereport(ERROR, (errcode(ERRCODE_SQLCLIENT_UNABLE_TO_ESTABLISH_SQLCONNECTION), errmsg("unexpected reader termination"), errdetail("%s", finish_and_get_message(self)))); } /* retry */ } }
int32 UartDown485_Rec(void) { uint8 lu8data = 0; fd_set ReadSetFD; struct timeval stTimeVal; uint8 ret; char log[1024]; while(1) { FD_ZERO(&ReadSetFD); FD_SET(g_uiRS4852Fd, &ReadSetFD); /* 设置等待的超时时间 */ stTimeVal.tv_sec = REC_TIMEOUT_SEC; stTimeVal.tv_usec = REC_TIMEOUT_USEC; if (select(g_uiRS4852Fd+1, &ReadSetFD, NULL, NULL,&stTimeVal) > 0){ if (FD_ISSET(g_uiRS4852Fd, &ReadSetFD)){ if(read(g_uiRS4852Fd, &lu8data, 1) == 1){ ret = QueueWrite((void*)DownRecQueue_RS485, lu8data); if(ret != QUEUE_OK){ sprintf(log, "[%s][%s][%d]QueueWrite err, ret = %d QUEUE_EMPTY\n", FILE_LINE, ret); write_log_file(log, strlen(log)); } OSSemPost(DOWN_COMMU_DEV_485); //sprintf(log, "[%s][%s][%d] 0x%02X \n",FILE_LINE,lu8data); //write_log_file(log, strlen(log)); } } } } return(CONTROL_RET_FAIL); }
INT32 UartGprs_Rec(void) { uint8 lu8data = 0; fd_set ReadSetFD; struct timeval stTimeVal; uint8 ret; while(1) { FD_ZERO(&ReadSetFD); FD_SET(g_uiGprsFd, &ReadSetFD); /* 设置等待的超时时间 */ stTimeVal.tv_sec = RS485UP_REC_TIMEOUT_SEC; stTimeVal.tv_usec = RS485UP_REC_TIMEOUT_USEC; if (select(g_uiGprsFd+1, &ReadSetFD, NULL, NULL,&stTimeVal) > 0){ if (FD_ISSET(g_uiGprsFd, &ReadSetFD)){ if(read(g_uiGprsFd, &lu8data, 1) == 1){ ret = QueueWrite((void*)USART3RecQueue_At, lu8data); if(ret != QUEUE_OK){ //printf("[%s][%s][%d] ret=%d queue full\n",FILE_LINE,ret); } OSSemPost(UP_COMMU_DEV_AT); ret = QueueWrite((void*)USART3RecQueue_AtIPD, lu8data); if(ret != QUEUE_OK){ //printf("[%s][%s][%d] ret=%d queue full The Queue has data Num is %d\n",FILE_LINE,ret, QueueNData(USART3RecQueue_AtIPD)); } OSSemPost(UP_COMMU_DEV_ATIPD); //printf("%c \n",lu8data); //测试用 } } } } return(CONTROL_RET_FAIL); }
//写上行接收队列,说明: 不是所有的操作都调用此函数进行,比如UART3接收队列的写操作没有调用本函数 uint8 UpRecQueueWrite(uint8 dev, uint8* buf, uint32 n) //不可重入 { uint32 i; for (i = 0; i < n; i++) { QueueWrite((void*) pQueues[dev], buf[i]); } OSSemPost(dev); return 0; }
/********************************************************************************************************* ** 函数名称: Uart0WriteFull ** 功能描述: 数据队列写满处理程序 ** 输 入: Buf:指向数据队列 ** Data:将要入队的数据 ** Mod: 入队方式 ** 输 出:TRUE :成功 ** FALSE:失败 ** 全局变量: 无 ** 调用模块: 无 ** ** 作 者: 陈明计 ** 日 期: 2003年7月4日 **------------------------------------------------------------------------------------------------------- ** 修改人: ** 日 期: **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ uint8 Uart1WriteFull(DataQueue *Buf, QUEUE_DATA_TYPE Data, uint8 Mod) { uint16 temp; Mod = Mod; temp = QueueSize((void *)Buf); while (temp <= QueueNData((void *)Buf)) /* 等待数据队列不满 */ { OSTimeDly(2); } return QueueWrite((void *)Buf, Data); /* 数据重新入队 */ }
/********************************************************************************************************* ** 函数名称: UART0Putch ** 功能描述: 发送一个字节数据 ** 输 入: Data:发送的数据数据 ** 输 出:无 ** 全局变量: 无 ** 调用模块: 无 ** ** 作 者: 陈明计 ** 日 期: 2003年7月4日 **------------------------------------------------------------------------------------------------------- ** 修改人: 陈明计 ** 日 期: 2003年7月8日 **------------------------------------------------------------------------------------------------------- ** 修改人: 陈明计 ** 日 期: 2003年7月21日 **------------------------------------------------------------------------------------------------------ ********************************************************************************************************/ void UART1Putch(uint8 Data) { uint8 temp; OS_ENTER_CRITICAL(); QueueWrite((void *)UART1SendBuf, Data); /* 数据入队 */ if ((U1LSR & 0x00000020) != 0) { /* UART0发送保持寄存器空 */ QueueRead(&temp, UART1SendBuf); /* 发送最初入队的数据 */ U1THR = temp; U1IER = U1IER | 0x02; /* 允许发送中断 */ } OS_EXIT_CRITICAL(); }
DWORD SCALL LogFileFinalize(VOID) { // Write all pending log entries InterlockedExchange(&logStatus, LOG_STATUS_SHUTDOWN); QueueWrite(NULL); InterlockedExchange(&logStatus, LOG_STATUS_INACTIVE); // Clean-up DeleteCriticalSection(&logLock); ASSERT(logPath != NULL); Io_Free(logPath); return ERROR_SUCCESS; }
int8u ModbusReportErrorSerial(int data) { uint8_t reportData[5]; uint16_t crc; uint8_t i; reportData[0] = modbusTerminalID; reportData[1] = 0x80 + data; reportData[2] = 0x01; crc = usMBCRC16(reportData, 3); reportData[4] = crc >> 8; reportData[3] = crc & 0xff; for (i = 0; i < 5; i++) QueueWrite((void *)UART1SendBuf, reportData[i]); return MODBUS_OK; }
DWORD SCALL LogFileFinalize(VOID) { // Write all pending log entries InterlockedExchange(&logStatus, LOG_STATUS_SHUTDOWN); QueueWrite(NULL); InterlockedExchange(&logStatus, LOG_STATUS_INACTIVE); ASSERT(STAILQ_EMPTY(&logQueue)); CloseHandle(logHandle); logHandle = INVALID_HANDLE_VALUE; DeleteCriticalSection(&logLock); return ERROR_SUCCESS; }
int32 UartMbus_Rec(void) { uint8 lu8data = 0; fd_set ReadSetFD; struct timeval stTimeVal; uint8 ret; while(1) { FD_ZERO(&ReadSetFD); FD_SET(g_uiMbusFd, &ReadSetFD); /* 设置等待的超时时间 */ stTimeVal.tv_sec = REC_TIMEOUT_SEC; stTimeVal.tv_usec = REC_TIMEOUT_USEC; if (select(g_uiMbusFd+1, &ReadSetFD, NULL, NULL,&stTimeVal) > 0){ if (FD_ISSET(g_uiMbusFd, &ReadSetFD)){ if(read(g_uiMbusFd, &lu8data, 1) == 1){ ret = QueueWrite((void*)DownRecQueue_MBUS, lu8data); if(ret != QUEUE_OK){ //printf("[%s][%s][%d] ret=%d queue full\n",FILE_LINE,ret); } OSSemPost(DOWN_COMMU_DEV_MBUS); //printf("%x \n",lu8data); //测试用 } } } } return(CONTROL_RET_FAIL); }
bool Xbox360Peripheral::start(IOService *provider) { const IOUSBConfigurationDescriptor *cd; IOUSBFindInterfaceRequest intf; IOUSBFindEndpointRequest pipe; XBOX360_OUT_LED led; IOWorkLoop *workloop = NULL; /* * Xbox One controller init packets. * The Rock Candy Xbox One controller requires more than just 0x05 * Minimum required packets unknown. */ UInt8 xoneInitFirst[] = { 0x02, 0x20, 0x01, 0x1C, 0x7E, 0xED, 0x8B, 0x11, 0x0F, 0xA8, 0x00, 0x00, 0x5E, 0x04, 0xD1, 0x02, 0x01, 0x00, 0x01, 0x00, 0x17, 0x01, 0x02, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00 }; UInt8 xoneInitSecond[] = { 0x05, 0x20, 0x00, 0x09, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0x53 }; UInt8 xoneInitThird[] = { 0x05, 0x20, 0x01, 0x01, 0x00 }; UInt8 xoneInitFourth[] = { 0x0A, 0x20, 0x02, 0x03, 0x00, 0x01, 0x14 }; if (!super::start(provider)) return false; // Get device device=OSDynamicCast(IOUSBDevice,provider); if(device==NULL) { IOLog("start - invalid provider\n"); goto fail; } // Check for configurations if(device->GetNumConfigurations()<1) { device=NULL; IOLog("start - device has no configurations!\n"); goto fail; } // Set configuration cd=device->GetFullConfigurationDescriptor(0); if(cd==NULL) { device=NULL; IOLog("start - couldn't get configuration descriptor\n"); goto fail; } // Open if(!device->open(this)) { device=NULL; IOLog("start - unable to open device\n"); goto fail; } if(device->SetConfiguration(this,cd->bConfigurationValue,true)!=kIOReturnSuccess) { IOLog("start - unable to set configuration\n"); goto fail; } // Get release { UInt16 release = device->GetDeviceRelease(); switch (release) { default: IOLog("Unknown device release %.4x\n", release); // fall through case 0x0110: chatpadInit[0] = 0x01; chatpadInit[1] = 0x02; break; case 0x0114: chatpadInit[0] = 0x09; chatpadInit[1] = 0x00; break; } } // Find correct interface controllerType = Xbox360; intf.bInterfaceClass=kIOUSBFindInterfaceDontCare; intf.bInterfaceSubClass=93; intf.bInterfaceProtocol=1; intf.bAlternateSetting=kIOUSBFindInterfaceDontCare; interface=device->FindNextInterface(NULL,&intf); if(interface==NULL) { // Find correct interface, Xbox original intf.bInterfaceClass=kIOUSBFindInterfaceDontCare; intf.bInterfaceSubClass=66; intf.bInterfaceProtocol=0; intf.bAlternateSetting=kIOUSBFindInterfaceDontCare; interface=device->FindNextInterface(NULL,&intf); if(interface==NULL) { // Find correct interface, Xbox One intf.bInterfaceClass=255; intf.bInterfaceSubClass=71; intf.bInterfaceProtocol=208; intf.bAlternateSetting=kIOUSBFindInterfaceDontCare; interface=device->FindNextInterface(NULL, &intf); if(interface==NULL) { IOLog("start - unable to find the interface\n"); goto fail; } controllerType = XboxOne; goto interfacefound; } controllerType = XboxOriginal; goto interfacefound; } interfacefound: interface->open(this); // Find pipes pipe.direction=kUSBIn; pipe.interval=0; pipe.type=kUSBInterrupt; pipe.maxPacketSize=0; inPipe=interface->FindNextPipe(NULL,&pipe); if(inPipe==NULL) { IOLog("start - unable to find in pipe\n"); goto fail; } inPipe->retain(); pipe.direction=kUSBOut; outPipe=interface->FindNextPipe(NULL,&pipe); if(outPipe==NULL) { IOLog("start - unable to find out pipe\n"); goto fail; } outPipe->retain(); // Get a buffer inBuffer=IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task,0,GetMaxPacketSize(inPipe)); if(inBuffer==NULL) { IOLog("start - failed to allocate input buffer\n"); goto fail; } // Find chatpad interface intf.bInterfaceClass = kIOUSBFindInterfaceDontCare; intf.bInterfaceSubClass = 93; intf.bInterfaceProtocol = 2; intf.bAlternateSetting = kIOUSBFindInterfaceDontCare; serialIn = device->FindNextInterface(NULL, &intf); if (serialIn == NULL) { IOLog("start - unable to find chatpad interface\n"); goto nochat; } serialIn->open(this); // Find chatpad pipe pipe.direction = kUSBIn; pipe.interval = 0; pipe.type = kUSBInterrupt; pipe.maxPacketSize = 0; serialInPipe = serialIn->FindNextPipe(NULL, &pipe); if (serialInPipe == NULL) { IOLog("start - unable to find chatpad in pipe\n"); goto fail; } serialInPipe->retain(); // Get a buffer for the chatpad serialInBuffer = IOBufferMemoryDescriptor::inTaskWithOptions(kernel_task, 0, GetMaxPacketSize(serialInPipe)); if (serialInBuffer == NULL) { IOLog("start - failed to allocate input buffer for chatpad\n"); goto fail; } // Create timer for chatpad serialTimer = IOTimerEventSource::timerEventSource(this, ChatPadTimerActionWrapper); if (serialTimer == NULL) { IOLog("start - failed to create timer for chatpad\n"); goto fail; } workloop = getWorkLoop(); if ((workloop == NULL) || (workloop->addEventSource(serialTimer) != kIOReturnSuccess)) { IOLog("start - failed to connect timer for chatpad\n"); goto fail; } // Configure ChatPad // Send 'configuration' SendInit(0xa30c, 0x4423); SendInit(0x2344, 0x7f03); SendInit(0x5839, 0x6832); // Set 'switch' if ((!SendSwitch(false)) || (!SendSwitch(true)) || (!SendSwitch(false))) { // Commenting goto fail fixes the driver for the Hori Real Arcade Pro EX //goto fail; } // Begin toggle serialHeard = false; serialActive = false; serialToggle = false; serialResetCount = 0; serialTimerState = tsToggle; serialTimer->setTimeoutMS(1000); // Begin reading if (!QueueSerialRead()) goto fail; nochat: if (!QueueRead()) goto fail; if (controllerType == XboxOne) { QueueWrite(&xoneInitFirst, sizeof(xoneInitFirst)); QueueWrite(&xoneInitSecond, sizeof(xoneInitSecond)); QueueWrite(&xoneInitThird, sizeof(xoneInitThird)); QueueWrite(&xoneInitFourth, sizeof(xoneInitFourth)); } else { // Disable LED Xbox360_Prepare(led,outLed); led.pattern=ledOff; QueueWrite(&led,sizeof(led)); } // Done PadConnect(); registerService(); return true; fail: ReleaseAll(); return false; }