: Xu Ping Jiang Jian Zhang Hong Source: Single Chip Microcomputer and Embedded System Application Time: December 29, 2004 0:35 Read 203 Summary: Mainly discussed in Wince Design and Development CAN Card Communication Procedure; Detail CAN Card The design and implementation of the underlying drive function, and the drive is packaged, and the use of dynamic library provides the user's CAN cartoon credit driver. Using the check can be easily called in its own program, implement the CAN card communication under WinCE. Keywords: Wince.Net CAN Driver In recent years, in order to quickly deploy substations in order to quickly deploy substations, the power industry has adopted the method of building an overall substation: installing the internal equipment of the substation, completing the internal equipment of the substation, leaving only the interface with the outside world. Entering and simply debugging after the substation is located. Its internal equipment communicates through the CAN bus, and the original monitoring software is based on the DOS system, and it is more difficult to maintain commissioning, so you want to seek more convenient and friendly system support. After comparison, the windows CE .NET in the windsting of the embedded operating system market has finally selected. Microsoft's latest product Windows CE.NET provides end-to-end development, debugging means, which can be commissioned and maintained on Windows in Windows Celete without disassembling the device. The system itself is redesigned for embedded markets, including creating one. Everything you need to customize the custom device for Windowsce. This needs to be transplanted under WindowsCE.net, but each hardware vendor has not provided the CAN communication card under Windows CE.NET, so the CAN card drive under Windows CE.NET has developed into project implementation. The key one ring. This paper mainly analyzes the two-port CAN card PCM3680 of Advana, introduces the method of underlying the underlying device driver development under the WindowsCe.nt system and provides examples of CAN communication. 1 CAN bus communication protocol and CAN communication card Introduction to the CAN bus is a serial data communication protocol that Germany BOSCH has been in the early 1980s in the early 1980s in the early 1980s. It is a multi-master bus that abolizes traditional station address coding, which is entered to encode the communication data block. This method makes the number of nodes in the network unrestricted, and 29-bit identification code in the extended format can define 2 29 different data blocks. In this project, the PCM3680 of Advantech is used. This is a double-port CAN bus communication card for embedded PC104; the CAN controller uses Philips's independent CAN controller SJA1000 chip; the CAN transceiver uses Philips's P82C250, which can operate two simultaneously A CAN network provides up to 1MB / s transmission speed. The PCM3680 supports a wide range of interrupts: interrupt 3, 4, 5, 6, 7, 9, 10, 11, 12, 15, while 1000V photoelectric isolation provides high reliability. In CAN card communication, many registers in the CAN controller, the meaning and function of each register can refer to the instructions for the control chip. Figure 1 lists the most important register structure in the driver design. 2 CAN card drive underlayer design This solution design CAN drive is a real drive in the kernel of the Windows CE operating system, a real drive located in the OEM Adaptation Layer (OAL), not serial operation in the main program. The device manager in the Windows CE can see two ports of CAN1 and CAN2, and can view the normal or not of their work and configure it. Such as: interrupt number and I / O address. 2.1 The communication of the CAN card register read and write function CAN card is performed by operating the CAN card on the CAN card. There are many registers in the CAN controller, such as control registers, command registers, status registers, interrupt registers, etc., by reading and writing of command status characters in these registers, can detect and control the CAN card behavior.
Under Windows CE.NET, the physical address assigned by the CAN card is mapped to the logical address by calling the API function haltrandrateslatebusaddress in DOK. Such various registers correspond to the offset address of the CAN card base address, so the read and write to the register is transformed into read and write to the memory address. Here is the read and write function of the CAN card register: * Read a byte data in the address of the offset INLINE BYTE CANR (LPCAN_HW_OPEN_INFO HCAN, DWORD OFF) {Return HcAN-> lpcanhwinfo-> lpcanobj-> lpmappedbaseaddr [OFF ]; * Write one byte data to an offset of OFF in the address of OFF in INLINE VOID CANW (LPCAN_HW_OPEN_INFO HCAN, DWORD OFF, BYTE VAL) {hcan-> lpcanhwinfo-> lpcanobj-> lpMappedBaseAddr [OFF] = VAL;} parameter LPCAN_HW_OPEN_INFO defines the data structure of the CAN card, where member LPMAPPEBaseAddr [0] indicates that the mapping rear base address, LPMAPPEDBaseAddr [1] is the address of the base address 1, the corresponding CAN card register is a command register. All registers on the CAN card can be operated by the above two functions. 2.2 The CAN card initializes the controller of the CAN card is more complicated. You must confirm the correctness of hardware information before communication, initialize each register. The basic flow of the initialization function is shown in Figure 3. The first step, check the correctness of the port number and hardware information, mainly whether the CAN card break is valid. A second card, the card is provided CAN default parameters: CanCardConfigInfo CAN_DEFAULT_SETTING = {0X00,0XFF, 0X03,0X1C}; / * set the default baud rate is 125Kbps * / DWORD dwThreadID = 0; PHYSICAL_ADDRESS phyAddr = {hwInfo-> dwIOBaseAddr * 16, 0}; third card, use the WinCE API function localalloc to allocate buffers for the data structure used in the CAN card drive; mapping the I / O address via HaltranslateBusAddress and MMMapiospace function, providing a virtual address of the direct access device: if (! HaltranslateBusAddress) Isa, 0, phyAddr, 0, & phyAddr)) goto _ExitInit; hCan-> lpCanHWInfo-> lpCanObj-> lpMappedBaseAddr = (LPBYTE) MmMapIoSpace (phyAddr, CANCARDADDRLEN, FALSE); if (hCan-> lpCanHWInfo-> lpCanObj-> lpMappedBaseAddr! ) goto _exitinit; If the memory or mapping logical address fails, exit the initializer, the CAN card is initialization failed. The fourth step, initialization read and write properties, sharing mode, reading timeout, and the base address of the second CAN port. Step 5, create CAN card events and data receiving events: hcan-> lpcanhwinfo-> hcanevent = CreateEvent (NULL, FALSE, FALSE, NULL); hcan-> lpcanhwinfo-> hRECVMSGEvent = CreateEvent (null, false, false, null) Step 6, initialize the interrupt, if the CAN card has a reset request to exit the initializer.
After setting up the interrupt, start the data receiving thread, set the thread priority to continue the thread processing; finally configure the CAN card parameters, enter the normal operation. 2.3 The information transmission of the CAN card information Send the CAN card is divided into two steps. After checking the basic information of the CAN card, first set the ID number of the send buffer. The ID number of the CAN standard mode is 11 bits. The offset address 10 is stored in the 22nd digit of the ID number. The high 3 bits of the offset address 11 are stored in the Low 3 digits of the ID number, and the remaining 5 digits are RTR. Bit (remote transfer request bit) and data length. After writing the processing data to the corresponding offset address, the corresponding address data is set, and the data acquisition of the offset address 12 to 19 is returned to the array by looping. Then, the transmission request of the CAN card is set to allow and constantly detect changes in the status register. When the transfer buffer flag or transmission end flag is 1, the program is completed, completes a data acquisition. The register of the transfer buffer is listed in Table 1. Table 1ID No. 10ID.10ID.9ID.8ID.7ID.6ID.5ID.4ID.3RTR, Data Length Code 11ID.2ID.1ID.0RTRDLC.3DLC.2DLC.1DLC.0 Data 1 ~ 812 ~ 19 Data Data Data Data Data Data Data Data Table 2
ID number 20ID.10ID.9ID.8ID.7ID.6ID.5ID.4ID.3RTR, data length code 21ID.2ID.1ID.0rtrdlc.3dlc.2dlc.1dlc.0 data 1 ~ 822 ~ 29 data data data data data data Data Data CAN Message Send Function is implemented as follows: BOOL CAN_SENDMESSAGE (LPCAN_HW_Open_Info HcAN, LPCANCARDMESSAGEBUFLPMSG) {BOOL BSUC = false; assert (hcan && lpmsg && lpmsg-> dwMessagelen <= 8); / * anti-wrong processing * / if (0 = = (hcan-> dwaccesscode & generic_write)) Return False ;:: EntercriticalSection (& HcAN-> lpcanhwinfo-> transmitcritsec); / * Enter the critical zone * / byte byv = static_cast
The implementation of the CAN message reception function is as follows: BOOL CAN_RECVRECVMESSAGE (lpcan_hw_open_info hcan, out lpcancardMessageBuflpmsg) {... IF (CANR (HCAN, 2) & 1) {/ * Judging whether the receiving buffer is full * / for (uint i = 0; i <10; i) Recvbuf [I] = CANR (HCAN, 20 I); / * Specify data to temporary buffer * / can (hcan, 1, 4); / * Release the receiving buffer * / LPMSG-> dwmsgid = recvbuf [0] << 3; / * Remove the ID of the ID * / byte byv = recvbuf [1]; lpmsg-> dwmsgid = byv >> 5; / * Remove the ID low 3, then And high 8-bit merge * / lpmsg-> BRTR = BYV & 0X10? True: / * Return RTR status * / lpmsg-> dwMessagelen = BYV & 0xF; / * Return data length * / ...} else { hcan-> lpcanhwinfo -> dwerrormsgcount;} / * does not receive data, error count plus 1 * / :: Leavecriticalsection (& hcan-> lpcanhwinfo-> receivecritsec); / * Leave the critical area * / return bsuC;} 2.5 CAN card event handling CAN card event The processing function is a very important part of the CAN card driver. The driver design requires a function of message notification, which is timely captured events and performs message processing when an event occurs. Here is the event handler: staric DWORD WINAPI CAN_EventHanle (LPVOID lpParam) {ASSERT (lpParam); LPCAN_HW_OPEN_INFO hCan = (LPCAN_HW_OPEN_INFO) lpParam; CanCardMessageBuf bufMsg; while (TEUE) {/ * loop waiting CAN card message generating and processing * / :: WaitForsingleObject (hcan-> lpcanhwinfo-> hcanevent, 0xffffff); if (hcan-> lpcanhwinfo-> bkillcanthread) Break; / * If the CAN thread is closed, the interrupt * / if (can_recvmessage (hufmsg)) { / * After receiving the data correctly, * / can_recvbuff;} / * Press the data into buffer * / byte byv = canR (HcAN, 3); / * Read the No. 3 register and then write * / canw (HCAN, 3, BYV); / * can get interrupts * / interruptdone (hcan-> lpcanhwinfo-> lpcanobj-> dwsysirqt);} / * This interrupt ends, waiting for the next interrupt * / return 0;} 2.6 Other functions In order to provide more features and more easily using the CAN card, some functions are designed in the CAN card driver, such as CAN_Config, CAN_RECVBUFPOP is used to process the receiving buffer, can_reset is used to reset the CAN Card, Checkhwinfo is used for hardware information, etc. These functions provide functions such as setting, checking, etc., which are not detailed here.