Windows CE串口驱动简析(4)-PDD层实现:CPdd2410Serial1和CPdd2410Serial2(基于WinCE5.0 SMDK2410 BSP的Serial驱动)

最后就需要实现针对特定串口的CPdd2410Uart的继承类CPdd2410Serial1和CPdd2410Serial2.分别对应于串口UART0和UART2.
1.CPdd2410Serial1
CPdd2410Serial1主要进行了端口初始化,针对UART0进行IO Port的设置,同时还实现了Modem的一些函数.
(1)构造函数CPdd2410Serial1和析构函数~CPdd2410Serial1
CPdd2410Serial1初始化m_pIOPregs(IO控制寄存器虚拟地址)和fIsDSRSet(DSR属性).
~CPdd2410Serial1释放 m_pIOPregs.
CPdd2410Serial1(LPTSTR lpActivePath, PVOID pMdd, PHWOBJ pHwObj) : CPdd2410Uart(lpActivePath, pMdd, pHwObj) { m_pIOPregs = NULL; m_fIsDSRSet = FALSE; } ~CPdd2410Serial1() { if (m_pIOPregs!=NULL) MmUnmapIoSpace((PVOID)m_pIOPregs,0); }
2.Init
获取UART0的串口寄存器基址,映射IO地址空间,获取逻辑中断号,获取DSR,DTR地址,DSR/DTR端口号,最后调用基类Init函数.
virtual BOOL Init() { PHYSICAL_ADDRESS ioPhysicalBase = { S3C2410X_BASE_REG_PA_IOPORT, 0}; ULONG inIoSpace = 0; if (TranslateBusAddr(m_hParent,Internal,0, ioPhysicalBase,&inIoSpace,&ioPhysicalBase)) { // Map it if it is Memeory Mapped IO. m_pIOPregs = (S3C2410X_IOPORT_REG *)MmMapIoSpace(ioPhysicalBase, sizeof(S3C2410X_IOPORT_REG),FALSE); } if (m_pIOPregs) { DDKISRINFO ddi; if (GetIsrInfo(&ddi)== ERROR_SUCCESS && KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &ddi.dwIrq, sizeof(UINT32), &ddi.dwSysintr, sizeof(UINT32), NULL)) { RegSetValueEx(DEVLOAD_SYSINTR_VALNAME,REG_DWORD,(PBYTE)&ddi.dwSysintr, sizeof(UINT32)); } else return FALSE; m_pDTRPort = (volatile ULONG *)&(m_pIOPregs->GPHDAT); m_pDSRPort = (volatile ULONG *)&(m_pIOPregs->GPHDAT); m_dwDTRPortNum = 6; m_dwDSRPortNum = 7; return CPdd2410Uart::Init(); } return FALSE; };
3.SetDefaultConfiguration
SetDefaultConfiguration用来配置PORT H,设置引脚功能为UART0的RXD0,TXD0,GPH6为DTR,GPH7为DSR,使能RTS0,CTS0等.
virtual void SetDefaultConfiguration() { m_pIOPregs->GPHCON &= ~(0x3<<0 | 0x3<<2 | 0x3<<4 | 0x3<<6 | 0x3<<12 | 0x3<<14); // clear uart 0 - rx, tx m_pIOPregs->GPHCON |= (0x2<<4 | 0x2<<6 | 0x1<<12 | 0x0<<14); m_pIOPregs->GPHCON |= (0x2<<0 | 0x2<<2 ); m_pIOPregs->GPHUP |= 0xc3; CPdd2410Uart::SetDefaultConfiguration(); }
4.InitModem
InitModem直接调用基类InitModem函数.
virtual BOOL InitModem(BOOL bInit) { SetDTR(bInit); return CPdd2410Uart::InitModem(bInit); }
5.GetModemStatus
GetModemStatus获取Modem状态,首先调用基类GetModemStatus,然后读取DSR端口状态获取相应状态调用事件回调函数.
virtual ULONG GetModemStatus() { ULONG ulReturn = CPdd2410Uart::GetModemStatus(); ULONG ulEvent = 0; m_HardwareLock.Lock(); BOOL fIsDSRSet = (((*m_pDSRPort) & (1<<m_dwDSRPortNum))==0); if (fIsDSRSet != m_fIsDSRSet) { ulEvent |= EV_DSR | EV_RLSD; } ulReturn |= (fIsDSRSet?(MS_DSR_ON|MS_RLSD_ON):0); m_fIsDSRSet = fIsDSRSet; m_HardwareLock.Unlock(); if (ulEvent!=0) EventCallback(ulEvent,ulReturn); return ulReturn; }
6.SetDTR
SetDTR用来设置DTR引脚.
virtual void SetDTR(BOOL bSet) { if (bSet) *m_pDTRPort &= ~(1<<m_dwDTRPortNum); else *m_pDTRPort |= (1<<m_dwDTRPortNum); };
2.CPdd2410Serial2
CPdd2410Serial2与CPdd2410Serial1类似,由于DTR,DSR使用GPH6,GPH7(UART2的Rx & Tx),所以DTR,DSR使能的时候不能使用UART2.
除了DTR,DSR部分,其余实现与CPdd2410Serial1类似.
// CPdd2410Serial2 is only use for UART 2 which // RTS CTS is use GPH0 GPH1 // DTR & DSR is USE GPH6 GPH7 which is UART2's Rx & Tx. You can not enable UART2 when this object is running. class CPdd2410Serial2 : public CPdd2410Uart { public: CPdd2410Serial2(LPTSTR lpActivePath, PVOID pMdd, PHWOBJ pHwObj) : CPdd2410Uart(lpActivePath, pMdd, pHwObj) { m_pIOPregs = NULL; } ~CPdd2410Serial2() { if (m_pIOPregs!=NULL) MmUnmapIoSpace((PVOID)m_pIOPregs,0); } virtual BOOL Init() { PHYSICAL_ADDRESS ioPhysicalBase = { S3C2410X_BASE_REG_PA_IOPORT, 0}; ULONG inIoSpace = 0; if (TranslateBusAddr(m_hParent,Internal,0, ioPhysicalBase,&inIoSpace,&ioPhysicalBase)) { // Map it if it is Memeory Mapped IO. m_pIOPregs =(S3C2410X_IOPORT_REG *) MmMapIoSpace(ioPhysicalBase, sizeof(S3C2410X_IOPORT_REG),FALSE); } if (m_pIOPregs) { DDKISRINFO ddi; if (GetIsrInfo(&ddi)== ERROR_SUCCESS && KernelIoControl(IOCTL_HAL_REQUEST_SYSINTR, &ddi.dwIrq, sizeof(UINT32), &ddi.dwSysintr, sizeof(UINT32), NULL)) { RegSetValueEx(DEVLOAD_SYSINTR_VALNAME,REG_DWORD,(PBYTE)&ddi.dwSysintr, sizeof(UINT32)); } else return FALSE; return CPdd2410Uart::Init(); } return FALSE; }; virtual void SetDefaultConfiguration() { m_pIOPregs->GPHCON &= ~(0x3<<12 | 0x3<<14); // clear uart 2 - rx, tx m_pIOPregs->GPHCON |= (0x2<<12 | 0x2<<14); m_pIOPregs->GPHUP |= 0xc0; CPdd2410Uart::SetDefaultConfiguration(); } virtual ULONG GetModemStatus() { return (CPdd2410Uart::GetModemStatus() | MS_CTS_ON); } volatile S3C2410X_IOPORT_REG * m_pIOPregs; };
3.CSerialPDD * CreateSerialObject
CreateSerialObject创建了UART0或UART2的实例(UART1被用作调试串口)返回给MDD层调用.调用方式请见之前的文章:
http://blog.csdn.net/shevsten/archive/2010/06/08/5655749.aspx
CSerialPDD * CreateSerialObject(LPTSTR lpActivePath, PVOID pMdd,PHWOBJ pHwObj, DWORD DeviceArrayIndex) { CSerialPDD * pSerialPDD = NULL; switch (DeviceArrayIndex) { case 0: pSerialPDD = new CPdd2410Serial1(lpActivePath,pMdd, pHwObj); break; case 1: pSerialPDD = new CPdd2410Serial2(lpActivePath,pMdd, pHwObj); break; } if (pSerialPDD && !pSerialPDD->Init()) { delete pSerialPDD; pSerialPDD = NULL; } return pSerialPDD; }
4.DeleteSerialObject
对应的还有一个删除串口实例的函数DeleteSerialObject.
void DeleteSerialObject(CSerialPDD * pSerialPDD) { if (pSerialPDD) delete pSerialPDD; }
到这里串口驱动就简单分析到这里,简单介绍了驱动架构和详细介绍了用户需要实现的CSerialPDD的继承类CPdd2410Uart和CPdd2410Serial1/2. 关于MDD和PDD COMMON层的具体实现没有过多涉及,如有兴趣请参考相应代码.

你可能感兴趣的:(windows,IO,object,null,delete,WinCE)