主题 : WINCE官方提供的串口驱动PDD层代码简单分析 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 6486
精华: 0
发帖: 27
金钱: 185 两
威望: 72 点
贡献值: 0 点
综合积分: 54 分
注册时间: 2009-06-03
最后登录: 2017-09-13
楼主  发表于: 2010-01-13 15:32

 WINCE官方提供的串口驱动PDD层代码简单分析

WINCE官方提供的串口驱动PDD层代码简单分析
作者:FLandY1982(flandy1982@sina.com)
日期:2010-1-11
版本:1.0
修改:
出处:http://blog.csdn.net/FLandY1982/archive/2010/01/11/5175162.aspx
pdf版本: wince 串口驱动 PDD层分析.rar (185 K) 下载次数:119
注意:
1.关于MDD层的代码详细分析,可以参考文章:http://blog.csdn.net/FLandY1982/archive/2009/12/24/5070059.aspx
2.文中所引用的代码, 都在目录%WINCEROOT%\PUBLIC\COMMON\OAK\DRIVERS\SERIAL\下
3.开发者在实现PDD层时, 可以直接继承CSerialPdd类, 然后对相关的接口进行实现和重载,也可以直接实现PDD层函数, 如果是直接实现PDD层函数本文第1章内容可以不用阅读。

1. PDD层代码简单分析
PDD层的主要包含了以下2个类:CSerialPDDPowerUpCallback, CSerialPDD, 下面简单的分析这2个类的作用。
1.1 CSerialPDDPowerUpCallback
CSerialPDDPowerUpCallback 类用于串口电源上电时的处理。
在调用CSerialPDD::Init()后会创建一个CSerialPDDPowerUpCallback类型的对象
在CSerialPDD::PowerOn()函数中会调用此对象的SignalCallBack()函数, 这样RunThread就开始运行, 进而通过调用CSerialPDD::NotifyPDDInterrupt()进行后续处理, 包括调用
CeEventHasOccurred (NOTIFICATION_EVENT_RS232_DETECTED, NULL) 通知系统有串口被探测到.
以及调用SerialEventHandler()进行MDD层上的处理.
更详细的细节可以参考微软的源代码:
%WINCEROOT%\PUBLIC\COMMON\OAK\DRIVERS\SERIAL\SERPDDCM\cserpdd.cpp
%WINCEROOT%\PUBLIC\COMMON\OAK\INC\cserpdd.h
2.2 CSerialPDD
CSerialPDD 是串口PDD层的关键,实现对硬件的操作,开发者开发的驱动就是继承这个类,并实现重载相关的函数来完成特定设备的PDD功能的。
这个类抽象了以下这些接口:
// Tx Function.
virtual BOOL InitXmit(BOOL bInit) = 0;
virtual void XmitInterruptHandler(PUCHAR pTxBuffer, ULONG *pBuffLen) = 0;
virtual void XmitComChar(UCHAR ComChar) = 0;
virtual BOOL EnableXmitInterrupt(BOOL bEnable)= 0;
virtual BOOL CancelXmit() = 0 ;
// Rx Function.
virtual BOOL InitReceive(BOOL bInit) = 0;
virtual ULONG ReceiveInterruptHandler(PUCHAR pRxBuffer,ULONG *pBufflen) = 0;
virtual ULONG CancelReceive() = 0;
// Modem
virtual BOOL InitModem(BOOL bInit) = 0;
virtual void ModemInterruptHandler()= 0; // This is Used to Indicate Modem Signal Changes.
virtual ULONG GetModemStatus() = 0;
virtual void SetDTR(BOOL bSet)= 0;
virtual void SetRTS(BOOL bSet)= 0;
virtual BOOL IsCTSOff() { return ((GetModemStatus() & MS_CTS_ON)==0) ; };
virtual BOOL IsDSROff() { return ((GetModemStatus() & MS_DSR_ON)==0) ; };
// Line Function
virtual BOOL InitLine(BOOL bInit) = 0;
virtual void LineInterruptHandler() = 0;
virtual void SetBreak(BOOL bSet) = 0 ;
virtual BOOL SetBaudRate(ULONG BaudRate,BOOL bIrModule) = 0;
virtual BOOL SetByteSize(ULONG ByteSize) = 0;
virtual BOOL SetParity(ULONG Parity)= 0;
virtual BOOL SetStopBits(ULONG StopBits)= 0;
以上这些接口都是纯虚函数, 在实现PDD层时, 必须实现这些接口.
当然不需要的功能你可以简单的用类似virtual BOOL func() {;} 的形式来实现。
其他的非纯虚函数的虚函数也可以被用户重载, 以实现自己特定的功能。
我们知道PDD层的函数为如下函数:
GetSerialObject
This function returns a pointer to a HWOBJ structure. The structure contains the function pointers and parameters for the hardware interface functions of the relevant lower layer.
HWClearBreak
This function clears an RS-232 line break condition.
HWClearDTR
This function clears the Data Terminal Ready (DTR) signal.
HWClearRTS
This function clears the Request to Send (RTS) signal.
HWClose
This function closes the device initialized by the HWInit function.
HWDeinit
This function is called by the upper layer to de-initialize the hardware when a device driver is unloaded.
HWDisableIR
This function disables the infrared (IR) serial interface.
HWEnableIR
This function enables the infrared (IR) serial interface.
HWGetCommProperties
This function retrieves the current properties of the communications device.
HWGetIntrType
This function returns the current interrupt type.
HWGetModemStatus
This function retrieves the modem status.
HWGetRxBufferSize
This function returns the maximum number of bytes that the hardware buffer can hold, not including the padding, stop, and start bits.
HWGetRxStart
This function returns the start of the hardware-receive buffer.
HWGetStatus
This function specifies the hardware status API.
HWInit
This function initializes a serial device.
HWIoctl
This function executes device I/O control (IOCTL) routines.
HWLineIntrHandler
This function handles line interrupts for serial port devices.
HWModemIntrHandler
This function handles the modem interrupt. In the serial port upper layer implementation available in Microsoft Windows CE 3.0 and later, this function replaces the HWOtherIntrHandler function.
HWOpen
This function is called by the upper layer to open the serial device.
HWOtherIntrHandler
In Windows CE 3.0 and later, this function has been replaced with the new function HWModemIntrHandler.
HWPostInit
This function performs necessary operations after it initializes all data structures and prepares the serial IST to begin handling interrupts. It is called by the upper layer.
HWPowerOff
This function notifies the platform-dependent driver that the hardware platform is about to enter suspend mode. It is called by the model device driver (MDD).
HWPowerOn
This function notifies the platform-dependent driver that the hardware platform is resuming from suspend mode. It is called by the MDD.
HWPurgeComm
This function purges the communications device.
HWPutBytes
This function writes bytes to hardware. The driver calls this function.
HWReset
This function resets the hardware API.
HWRxIntrHandler
This function handles serial port interrupts.
HWSetBreak
This function sets the line break condition on the transmit line.
HWSetCommTimeouts
This function sets the communications time-out events in response to a call to the SetCommTimeouts function.
HWSetDCB
This function sets the device control block.
HWSetDTR
This function sets the Data Terminal Ready (DTR) signal.
HWSetRTS
This function sets the Request to Send (RTS) signal.
HWTxIntrHandler
This function handles the transmit interrupt for serial port devices.
HWXmitComChar
This function transmits a single character.

更具体的细节可以参考:MSDN帮助 ms-help://MS.WindowsCE.500/wceddk5/html/wce50grfserialportdriverfunctions.htm
微软用一堆SerXXXXX()的函数封装了CSerialPdd相关的操作,来完成PDD功能。
2. PDD与MDD层的交互
2.1 PDD层的函数是如何能够被MDD层所调用
1.2节介绍的那一堆SerXXXXX()函数会被记录到一个函数指针表里,真是通过这个函数指针表来实现与MDD层的交互的。此函数指针表是HW_VTBL类型的, 在微软的代码里是命名为IoVTbl的函数指针表。
MDD层通过调用 GetSerialObject(DWORD DeviceArrayIndex)函数来获取一个HWOBJ结构的指针, 这个结构包含以下成员:
ULONG BindFlags; // Flags controlling MDD behaviour. Se above.
DWORD dwIntID; // Interrupt Identifier used if THREAD_AT_INIT or THREAD_AT_OPEN
PHW_VTBL pFuncTbl;
其中pFuncTbl正是指向我们之前说到的 IoVTbl 函数指针结构的地址.
这样MDD层就可以通过->pFuncTbl.DDSIFunction()来对特定的硬件做特定的操作了.
2.2 GetSerialObject函数
这个函数的原型是:PHWOBJ GetSerialObject(DWORD DeviceArrayIndex) ,它是连接MDD和PDD层的关键函数, 也是实现多端口驱动的关键, 这个函数允许一个MDD层连接多个PDD层。
在MDD层的COM_Init函数中,驱动会通过查询DeviceArrayIndex键值, 得到我们需要打开的是普通串口或者是其他种类的串口设备。
而正是这个数值决定了GetSerialObject是返回哪个串口的PHWOBJ指针, PHWOBJ包含了pFuncTbl,这个正是前一节说到的PDD层函数指针表,通过这个函数表我们就可以实现对相应的串口设备进行操作。(串口有可能是标准的串口,也有可能是红外设备)

2.3 CreateSerialObject 函数
如果开发者选择直接继承微软CSerialPdd类来实现串口的PDD层,还要实现CreateSerialObject及DeleteSerialObject函数。
在MDD层的COM_Init函数被调用时,驱动会通过查询注册表来得到相关参数并调用PDD层的HWInit函数,HWInit函数又会调用CreateSerialObject来初始化串口设备,在关闭串口以后DeleteSerialObject函数会被调用。
3. 其他
3.1 COM_Init函数如何被调用
此函数通常是由Device.exe进程调用的, 而此函数的参数Identifier 是一个指向注册表HKEY_LOCAL_MACHINE\Drivers\Active.下的一个键值,这也是决定驱动如何操作硬件的关键。
对于使用ActivateDeviceEx函数调用的情况,请参照MSND, http://msdn.microsoft.com/en-us/library/aa447677.aspx
3.2 相关代码段:
复制代码
  1. HANDLE
  2. COM_Init(
  3. ULONG Identifier
  4. )
  5. {
  6. PVOID pHWHead = NULL;
  7. PHW_INDEP_INFO pSerialHead = NULL;
  8. ULONG HWBufferSize;
  9. DWORD DevIndex;
  10. HKEY hKey;
  11. ULONG kreserved = 0, kvaluetype;
  12. ULONG datasize = sizeof(ULONG);
  13. /*
  14. * INTERNAL: this routine initializes the hardware abstraction interface
  15. * via HWInit(). It allocates a data structure representing this
  16. * instantiation of the device. It also creates an event and initializes
  17. * a critical section for receiving as well as registering the logical
  18. * interrupt dwIntID with NK via InterruptInitialize. This call
  19. * requires that the hardware dependent portion export apis that return
  20. * the physical address of the receive buffer and the size of that buffer.
  21. * Finally, it creates a buffer to act as an intermediate
  22. * buffer when receiving.
  23. */
  24. DEBUGMSG (ZONE_INIT | ZONE_FUNCTION, (TEXT("+COM_Init\r\n")));
  25. // Allocate our control structure.
  26. pSerialHead = (PHW_INDEP_INFO)LocalAlloc(LPTR, sizeof(HW_INDEP_INFO));
  27. // Check that LocalAlloc did stuff ok too.
  28. if ( !pSerialHead ) {
  29. DEBUGMSG(ZONE_INIT | ZONE_ERROR,
  30. (TEXT("Error allocating memory for pSerialHead, COM_Init failed\n\r")));
  31. return(NULL);
  32. }
  33. // Initially, open list is empty.
  34. InitializeListHead( &pSerialHead->OpenList );
  35. InitializeCriticalSection(&(pSerialHead->OpenCS));
  36. pSerialHead->pAccessOwner = NULL;
  37. pSerialHead->fEventMask = 0;
  38. // Init CommTimeouts.
  39. pSerialHead->CommTimeouts.ReadIntervalTimeout = READ_TIMEOUT;
  40. pSerialHead->CommTimeouts.ReadTotalTimeoutMultiplier =
  41. READ_TIMEOUT_MULTIPLIER;
  42. pSerialHead->CommTimeouts.ReadTotalTimeoutConstant =
  43. READ_TIMEOUT_CONSTANT;
  44. pSerialHead->CommTimeouts.WriteTotalTimeoutMultiplier= 0;
  45. pSerialHead->CommTimeouts.WriteTotalTimeoutConstant = 0;
  46. /* Create tx and rx events and stash in global struct field. Check return.
  47. */
  48. pSerialHead->hSerialEvent = CreateEvent(0,FALSE,FALSE,NULL);
  49. pSerialHead->hKillDispatchThread = CreateEvent(0, FALSE, FALSE, NULL);
  50. pSerialHead->hTransmitEvent = CreateEvent(0, FALSE, FALSE, NULL);
  51. pSerialHead->hReadEvent = CreateEvent(0, FALSE, FALSE, NULL);
  52. if ( !pSerialHead->hSerialEvent || !pSerialHead->hKillDispatchThread ||
  53. !pSerialHead->hTransmitEvent || !pSerialHead->hReadEvent ) {
  54. DEBUGMSG(ZONE_ERROR | ZONE_INIT,
  55. (TEXT("Error creating event, COM_Init failed\n\r")));
  56. LocalFree(pSerialHead);
  57. return(NULL);
  58. }
  59. /* Initialize the critical sections that will guard the parts of
  60. * the receive and transmit buffers.
  61. */
  62. InitializeCriticalSection(&(pSerialHead->ReceiveCritSec1));
  63. InitializeCriticalSection(&(pSerialHead->TransmitCritSec1));
  64. /* Want to use the Identifier to do RegOpenKey and RegQueryValue (?)
  65. * to get the index to be passed to GetHWObj.
  66. * The HWObj will also have a flag denoting whether to start the
  67. * listening thread or provide the callback.
  68. */
  69. DEBUGMSG (ZONE_INIT,(TEXT("Try to open %s\r\n"), (LPCTSTR)Identifier));
  70. hKey = OpenDeviceKey((LPCTSTR)Identifier);
  71. // 通过调用OpenDeviceKey, 驱动可以通过Identifier找到驱动的注册表键的所在.
  72. if ( !hKey ) {
  73. DEBUGMSG (ZONE_INIT | ZONE_ERROR,
  74. (TEXT("Failed to open devkeypath, COM_Init failed\r\n")));
  75. LocalFree(pSerialHead);
  76. return(NULL);
  77. }
  78. datasize = sizeof(DWORD);
  79. // 通过查询DeviceArrayIndex键值, 我们可以知道需要打开的是普通串口或者是其他类型的串口设备.
  80. if ( RegQueryValueEx(hKey, L"DeviceArrayIndex", NULL, &kvaluetype,
  81. (LPBYTE)&DevIndex, &datasize) ) {
  82. DEBUGMSG (ZONE_INIT | ZONE_ERROR,
  83. (TEXT("Failed to get DeviceArrayIndex value, COM_Init failed\r\n")));
  84. RegCloseKey (hKey);
  85. LocalFree(pSerialHead);
  86. return(NULL);
  87. }
  88. datasize = sizeof(DWORD);
  89. // 通过查询Priority256键值, 我们可以知道IST的优先级, 在后面会通过这个值来改变IST的优先级.
  90. if ( RegQueryValueEx(hKey, L"Priority256", NULL, &kvaluetype,
  91. (LPBYTE)&pSerialHead->Priority256, &datasize) ) {
  92. pSerialHead->Priority256 = DEFAULT_CE_THREAD_PRIORITY;
  93. DEBUGMSG (ZONE_INIT | ZONE_WARN,
  94. (TEXT("Failed to get Priority256 value, defaulting to %d\r\n"), pSerialHead->Priority256));
  95. }
  96. RegCloseKey (hKey);
  97. DEBUGMSG (ZONE_INIT,
  98. (TEXT("DevIndex %X\r\n"), DevIndex));
  99. // Initialize hardware dependent data.
  100. // GetSerialObject是连接MDD和PDD层的关键函数, 在这个函数里面决定了DevIndex是对什么设备进行操作.
  101. // 可能是标准的串口, 也可能是红外接口
  102. pSerialHead->pHWObj = GetSerialObject( DevIndex );
  103. if ( !pSerialHead->pHWObj ) {
  104. DEBUGMSG(ZONE_ERROR | ZONE_INIT,
  105. (TEXT("Error in GetSerialObject, COM_Init failed\n\r")));
  106. LocalFree(pSerialHead);
  107. return(NULL);
  108. }
  109. DEBUGMSG (ZONE_INIT, (TEXT("About to call HWInit(%s,0x%X)\r\n"),
  110. Identifier, pSerialHead));
  111. // 这里调用了PDD层的HWInit
  112. pHWHead = pSerialHead->pHWObj->pFuncTbl->HWInit(Identifier, pSerialHead, pSerialHead->pHWObj);
  113. pSerialHead->pHWHead = pHWHead;
  114. /* Check that HWInit did stuff ok. From here on out, call Deinit function
  115. * when things fail.
  116. */
  117. if ( !pHWHead ) {
  118. DEBUGMSG (ZONE_INIT | ZONE_ERROR,
  119. (TEXT("Hardware doesn't init correctly, COM_Init failed\r\n")));
  120. COM_Deinit(pSerialHead);
  121. return(NULL);
  122. }
  123. DEBUGMSG (ZONE_INIT,
  124. (TEXT("Back from hardware init\r\n")));
  125. // Allocate at least twice the hardware buffer size so we have headroom
  126. HWBufferSize = 2 * pSerialHead->pHWObj->pFuncTbl->HWGetRxBufferSize(pHWHead);
  127. // Init rx buffer and buffer length here.
  128. pSerialHead->RxBufferInfo.Length =
  129. HWBufferSize > RX_BUFFER_SIZE ? HWBufferSize:RX_BUFFER_SIZE;
  130. pSerialHead->RxBufferInfo.RxCharBuffer =
  131. LocalAlloc(LPTR, pSerialHead->RxBufferInfo.Length);
  132. if ( !pSerialHead->RxBufferInfo.RxCharBuffer ) {
  133. DEBUGMSG(ZONE_INIT|ZONE_ERROR,
  134. (TEXT("Error allocating receive buffer, COM_Init failed\n\r")));
  135. COM_Deinit(pSerialHead);
  136. return(NULL);
  137. }
  138. DEBUGMSG (ZONE_INIT, (TEXT("RxHead init'ed\r\n")));
  139. RxResetFifo(pSerialHead);
  140. InitializeCriticalSection(&(pSerialHead->RxBufferInfo.CS));
  141. InitializeCriticalSection(&(pSerialHead->TxBufferInfo.CS));
  142. DEBUGMSG (ZONE_INIT, (TEXT("RxBuffer init'ed with start at %x\r\n"),
  143. pSerialHead->RxBufferInfo.RxCharBuffer));
  144. if ( pSerialHead->pHWObj->BindFlags & THREAD_AT_INIT ) {
  145. // Hook the interrupt and start the associated thread.
  146. if ( ! StartDispatchThread( pSerialHead ) ) {
  147. // Failed on InterruptInitialize or CreateThread. Bail.
  148. COM_Deinit(pSerialHead);
  149. return(NULL);
  150. }
  151. }
  152. // OK, now that everything is ready on our end, give the PDD
  153. // one last chance to init interrupts, etc.
  154. (void) pSerialHead->pHWObj->pFuncTbl->HWPostInit( pHWHead );
  155. DEBUGMSG (ZONE_INIT | ZONE_FUNCTION, (TEXT("-COM_Init\r\n")));
  156. return(pSerialHead);
  157. }


2012,我要玩安卓4,有木有,有木有,有木有,有木有
级别: 侠客
UID: 4925
精华: 0
发帖: 84
金钱: 600 两
威望: 177 点
贡献值: 0 点
综合积分: 168 分
注册时间: 2009-04-03
最后登录: 2015-01-14
1楼  发表于: 2010-01-13 16:02
支持~
级别: 侠客
UID: 14802
精华: 0
发帖: 50
金钱: 255 两
威望: 51 点
贡献值: 0 点
综合积分: 100 分
注册时间: 2010-02-23
最后登录: 2017-09-13
2楼  发表于: 2010-02-23 11:08
不错学习了