VOID EvtIoWrite( IN WDFQUEUE Queue, IN WDFREQUEST Request, IN size_t Length ) { WDFMEMORY memory; NTSTATUS status; PMY_SERIAL_DEVICE_EXTENSION pContext; PUCHAR buffer; size_t buf_size; size_t i = 0; KdPrint((DRIVER_NAME "-->EvtIoWrite\n")); pContext = MySerialGetDeviceExtension(WdfIoQueueGetDevice(Queue)); status = WdfRequestRetrieveInputMemory(Request, &memory); if(!NT_SUCCESS(status)) { KdPrint((DRIVER_NAME "EvtIoWrite Could not get request memory buffer 0x%x\n", status)); WdfRequestComplete(Request, status); KdPrint((DRIVER_NAME "<-- EvtDeviceIoWrite\n")); return; } buffer = WdfMemoryGetBuffer(memory, &buf_size); KdPrint((DRIVER_NAME "Sending a buffer of %d bytes\n", buf_size)); while (i < buf_size) { while (UartGetBit(pContext, LSR, LSR_ETHR) == 0 ) {} UartWriteByte(pContext, buffer[i]); i++; } WdfRequestCompleteWithInformation(Request, status, i); KdPrint((DRIVER_NAME "<--EvtIoWrite\n")); }
/*---------------------------------------------------------------------------------------------------------------------- Waits for a byte to appear in the Rx buffer. The BufferParser is always moved through all new characters placing them into the command buffer until it hits a CR or there are no new characters to read. If there is no CR in this iteration, nothing else occurs. Backspace: Echo the backspace and a space character to clear the character on screen; move Debug_pu8BufferCurrentChar back. CR: Advance states to process the command. Any other character: Echo it to the UART Tx and place a copy in Debug_au8CommandBuffer. */ void DebugSM_Idle(void) { bool bCommandFound = FALSE; u8 u8CurrentByte; static u8 au8BackspaceSequence[] = {ASCII_BACKSPACE, ' ', ASCII_BACKSPACE}; static u8 au8CommandOverflow[] = "\r\n*** Command too long ***\r\n\n"; /* Parse any new characters that have come in until no more chars or a command is found */ while( (Debug_pu8RxBufferParser != Debug_pu8RxBufferNextChar) && (bCommandFound == FALSE) ) { /* Grab a copy of the current byte and echo it back */ u8CurrentByte = *Debug_pu8RxBufferParser; /* If the LED test is active, toggle LEDs based on characters */ if(Debug_bLedTestActive == TRUE) { DebugLedTestCharacter(u8CurrentByte); } /* Process the character */ switch (u8CurrentByte) { /* Backspace: update command buffer pointer and send sequence to delete the char on the terminal */ case(ASCII_BACKSPACE): { if(Debug_pu8CmdBufferNextChar != &Debug_au8CommandBuffer[0]) { Debug_pu8CmdBufferNextChar--; Debug_u16CommandSize--; } DebugPrintf(au8BackspaceSequence); break; } /* Carriage return: change states to process new command and fall through to echo character */ case(ASCII_CARRIAGE_RETURN): { bCommandFound = TRUE; Debug_pfnStateMachine = DebugSM_CheckCmd; /* Fall through to default */ } /* Add to command buffer and echo */ default: { /* Echo the character and place it in the command buffer */ UartWriteByte(Debug_Uart, u8CurrentByte); *Debug_pu8CmdBufferNextChar = u8CurrentByte; Debug_pu8CmdBufferNextChar++; Debug_u16CommandSize++; /* If the command buffer is now full but the last character was not ASCII_CARRIAGE_RETURN, throw out the whole buffer and report an error message */ if( (Debug_pu8CmdBufferNextChar >= &Debug_au8CommandBuffer[DEBUG_CMD_BUFFER_SIZE]) && (u8CurrentByte != ASCII_CARRIAGE_RETURN) ) { Debug_pu8CmdBufferNextChar = &Debug_au8CommandBuffer[0]; Debug_u16CommandSize = 0; Debug_u32CurrentMessageToken = DebugPrintf(au8CommandOverflow); } break; } } /* end switch (u8RxChar) */ /* In all cases, advance the RxBufferParser pointer safely */ Debug_pu8RxBufferParser++; if(Debug_pu8RxBufferParser >= &Debug_au8RxBuffer[DEBUG_RX_BUFFER_SIZE]) { Debug_pu8RxBufferParser = &Debug_au8RxBuffer[0]; } } /* end while */ /* Clear out any completed messages */ if(Debug_u32CurrentMessageToken != 0) { QueryMessageStatus(Debug_u32CurrentMessageToken); } } /* end DebugSM_Idle() */