Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2604452
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5920
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: Windows平台

2015-06-25 13:55:42

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, &regaddress, 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;
}
阅读(776) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~