NTSTATUS
I2CSensorReadRegister(
_In_ PDEVICE_CONTEXT pDevice,
_In_ ULONG index,
_In_ BYTE regaddress, //寄存器地址
_In_ size_t regByteLength, //寄存器字节长度
_Out_ BYTE * pOutputBuffer,
_Out_ size_t * ReadLength
)
/*++
Routine Description:
This routine writes Data to the Register.
Arguments:
pDevice - a pointer to the device context
index - the chip num
regaddress - Register address
regByteLength - Register BYTE length
pOutputBuffer - output buffer length
RegLength - the length of the Read data
Return Value:
NTSTATUS
--*/
{
PUCHAR Buffer;
ULONG BufferLength = 0;
ULONG_PTR BytesWritten = 0;
ULONG_PTR BytesRead;
WDF_MEMORY_DESCRIPTOR MemoryDescriptor;
PUCHAR ReadBuffer;
WDF_MEMORY_DESCRIPTOR ReadMemoryDescriptor;
WDFMEMORY MemoryHandle = NULL;
// ULONG_PTR bytesRead = NULL;
size_t MY_BUFFER_SIZE;
WDFMEMORY MemoryWrite;
NTSTATUS Status;
MemoryWrite = NULL;
UNREFERENCED_PARAMETER(regaddress);
UNREFERENCED_PARAMETER(pOutputBuffer);
UNREFERENCED_PARAMETER(ReadLength);
UNREFERENCED_PARAMETER(regByteLength);
BufferLength = 1;
Status = WdfMemoryCreate(WDF_NO_OBJECT_ATTRIBUTES,
NonPagedPool,
0,
BufferLength,
&MemoryWrite,
(PVOID*)&Buffer);
MY_BUFFER_SIZE = regByteLength;
Status = WdfMemoryCreate(
NULL,
NonPagedPool,
0,
MY_BUFFER_SIZE,
&MemoryHandle,
(PVOID*)&ReadBuffer
);
WDF_MEMORY_DESCRIPTOR_INIT_HANDLE(
&ReadMemoryDescriptor,
MemoryHandle,
NULL
);
if (!NT_SUCCESS(Status)) {
Trace(
TRACE_LEVEL_ERROR,
TRACE_FLAG_SPBAPI,
"%s: WdfMemoryCreate failed allocating memory buffer for write!"
"Status:%#x\n",
__FUNCTION__,
Status);
goto SpbWriteEnd;
}
// Setup the write buffer. The buffer should contain address followed by
// data.
RtlCopyMemory(Buffer, ®address, DATABYTELENGYH);
// Send the request synchronously.
WDF_MEMORY_DESCRIPTOR_INIT_HANDLE(&MemoryDescriptor, MemoryWrite, NULL);
Status = WdfIoTargetSendWriteSynchronously(
pDevice->SpbController[index],
NULL,
&MemoryDescriptor,
NULL,
NULL,
&BytesWritten);
Status = WdfIoTargetSendReadSynchronously(
pDevice->SpbController[index],
NULL,
&ReadMemoryDescriptor,
NULL,
NULL,
&BytesRead);
if (NT_SUCCESS(Status))
{
Trace(
TRACE_LEVEL_ERROR,
TRACE_FLAG_SPBAPI,
"SpbPeripheralRead lengther is : %x",
BytesRead);
*ReadLength = BytesRead;
for (ULONG i = 0; i < BytesRead; i++)
{
pOutputBuffer[i] = ReadBuffer[i];
Trace(
TRACE_LEVEL_ERROR,
TRACE_FLAG_SPBAPI,
"SpbPeripheralRead Data is : %x",
ReadBuffer[i]);
}
}
if (BytesWritten > 0)
{
if (NT_SUCCESS(Status))
{
Trace(
TRACE_LEVEL_ERROR,
TRACE_FLAG_SPBAPI,
"SpbPeripheralWrite lengther is : %x",
BytesWritten);
}
}
if (!NT_SUCCESS(Status)) {
Trace(
TRACE_LEVEL_ERROR,
TRACE_FLAG_SPBAPI,
"%s: WdfIoTargetSendWriteorReadSynchronously failed! Status = %#x\n",
__FUNCTION__,
Status);
goto SpbWriteEnd;
}
SpbWriteEnd:
if (MemoryWrite != NULL) {
WdfObjectDelete(MemoryWrite);
}
return Status;
}
阅读(856) | 评论(0) | 转发(0) |