A kind of CAN device driving device and method based on pci busTechnical field
The invention belongs to device drives fields, and in particular to a kind of CAN device driving device and side based on pci busMethod.
Background technique
With the progress and fast development of production domesticization information technology, domestic CPU and domestic embedded OS are increasinglyIn extensive use and industrial design.Production domesticization chip such as Godson CPU based on mips framework is with small in size, speed is fast, low in energy consumptionThe advantages that become the popular chip that domesticizes;For example domestic embedded sharp magnificent operating system ReWorks quilt of domestic embedded OSIt is widely applied, CPU environments, the real-time responses such as Godson, X86, ARM7, ARM9, PowerPC is supported to match less than 15 microseconds, minimumIt sets and is less than 20K, space protection ability and task immigration ability based on MMU.Therefore, domestic CPU and domestic embedded operation systemThe combination of system has been successfully applied to the fields such as aerospace, weapon control, naval vessel, communication, scientific research.
For the exploitation of embedded system driver, primarily solve the problems, such as it is how to carry out hardware controls, firmlyPart control is divided into two ways: one is being directly connected to for CPU and specialized chip, the driver mode of this control is not solidFixed, transplantability is poor;Another kind is the equipment being articulated in bus, and there are also a bus, such as PCI between this kind of equipment and CPUBus, this equipment is flexibly answered, scalability, adaptability are good.By taking domestic embedded OS device drives as an example, it is divided into twoClass device drives, the device drives that one kind is carried using Reworks itself, do not need user's self-developing generally, such as keyboard, mouseMark, hard disk, USB flash disk, SD card, trawl performance etc.;Another kind of is the functions of the equipments of user's extension, need to voluntarily write bottom layer driving journeySequence, such as CAN mouthfuls, serial ports, I/O driving.
However, writing for embedded device driver needs developer to understand and a large amount of kernel kernel in depthAdjustment Skill, writing for one wrong device driver or even can make system crash, and cause file system destruction andThe loss of data, simultaneously as sharp China's operating system nucleus source code is not opened to the outside world, kernel source code size of code is big and crisscross multipleIt is miscellaneous, cause the CAN device Driver Design voluntarily researched and developed exploitation difficult, it has also become urgently to be resolved in CAN device development to askTopic.
Summary of the invention
Aiming at the above defects or improvement requirements of the prior art, the present invention provides a kind of CAN devices based on pci busDriving device and method are utilized respectively drive module, switch is set by the way that corresponding module and its corresponding connection relationship is arrangedStandby module, control module and interrupt module realize the setting of the identification and initiation parameter for the treatment of driving equipment, switch operation, functionEnergy operation and interrupt operation, so that controlling CAN link module realizes the driving for treating driving equipment.
To achieve the above object, according to one aspect of the present invention, a kind of CAN device drive based on pci bus is providedDynamic device, the device include sequentially connected main control module, FPGA module and CAN link module, and Add-In write device is logicalPci bus is crossed to connect with main control module;CAN link module connects equipment to be driven for providing CAN interface;Main control module includesDrive module, switchgear module, control module and interrupt module, wherein
Drive module instructs for the externally input first stage and realizes to be driven set using first stage initialization functionStandby creation and equipment global variable to be driven initialization, execution is unsuccessful then to delete function the first rank of removing using the first stageSection initializing variable;
Drive module, which is also used to instruct according to externally input second stage, realizes note using second stage initialization functionIt records the corresponding CAN interface base address of equipment to be driven, registration interrupt service routine, enabled PCI interruption, enabled CAN interruption and beatsLocalBus interruption is opened, execution is unsuccessful to delete function removing second stage initializing variable using second stage;
Switchgear module be used for according to externally input switch order open or close CAN link module semaphore,Task sends FIFO, receives FIFO and message queue;
Control module is used to realize the command word function to CAN link module according to externally input control instruction, reads functionCan and it write feature operation;
Interrupt module is used to treat in driving equipment according to the control CAN link module realization of externally input interrupt instructionDisconnected operation.
As a further improvement of the present invention, first stage instruction further includes search pci bus lookup equipment to be driven, readingIt takes the mapping space address of equipment to be driven and the interrupt number of equipment to be driven is set.
As a further improvement of the present invention, command word function is specially to receive from external command code and relevant parameterRealize the command operation for treating driving equipment, the macro mapping respective operations for calling hardware abstraction layer to provide by different control codesThe memory address of system carries out operation realization.
As a further improvement of the present invention, writing function is specially that the CAN bus frame data sent will be needed to be put into CAN chainThe transmission FIFO and trigger data of road module send task, send task by data and carry out to the register of CAN link moduleThe transmission of write operation realization CAN bus frame data.
As a further improvement of the present invention, interrupt module passes through interrupt service routine with according to received interrupt instructionProcessing routine of having no progeny, which is realized, treats the interrupt operation of driving equipment, wherein interrupt service routine is for judging interrupt source, in shieldingDisconnected, triggering interruption and subsequent operation;Post processor is interrupted for the register data deposit of CAN link module is correspondingIt receives in FIFO, and empties the register data of CAN link module.
To achieve the above object, other side according to the invention provides a kind of CAN device based on pci busDriving method, specific steps are as follows:
S1. the wound of equipment to be driven is realized using first stage initialization function according to the instruction of externally input first stageIt builds and is initialized with equipment global variable to be driven, run succeeded, enter step S2;Otherwise, clear using first stage deletion functionExcept first stage initializing variable;
S2. it is instructed according to externally input second stage and records equipment to be driven using the realization of second stage initialization functionCorresponding CAN interface base address, registration interrupt service routine, enabled PCI is interrupted, enabled CAN is interrupted and opened in LocalBusIt is disconnected, it runs succeeded, enters step S3;Otherwise, function is deleted using second stage remove second stage initializing variable;
S3. according to externally input switch order open or close the semaphore of CAN link module, task, send FIFO,Receive FIFO and message queue;
S4. it realizes the command word function to CAN link module according to externally input control instruction, read function and writes functionOperation;
S5. the interrupt operation for treating driving equipment is realized according to externally input interrupt instruction control CAN link module.
As a further improvement of the present invention, first stage instruction further includes search pci bus lookup equipment to be driven, readingIt takes the mapping space address of equipment to be driven and the interrupt number of equipment to be driven is set.
As a further improvement of the present invention, command word function is specially to receive from external command code and relevant parameterRealize the command operation for treating driving equipment, the macro mapping respective operations for calling hardware abstraction layer to provide by different control codesThe memory address of system carries out operation realization.
As a further improvement of the present invention, writing function is specially that the CAN bus frame data sent will be needed to be put into CAN chainThe transmission FIFO and trigger data of road module send task, send task by data and carry out to the register of CAN link moduleThe transmission of write operation realization CAN bus frame data.
As a further improvement of the present invention, interrupt operation is specially according to received interrupt instruction by interrupting service journeySequence and interrupt post processor and realize and treat the interrupt operation of driving equipment, wherein interrupt service routine for judge interrupt source,Shielding is interrupted, triggering is interrupted and subsequent operation;Post processor is interrupted to be used to for the register data of CAN link module being stored inIn corresponding reception FIFO, and empty the register data of CAN link module.
In general, through the invention it is contemplated above technical scheme is compared with the prior art, have below beneficial to effectFruit:
A kind of CAN device driving device and method based on pci bus of the invention, by be arranged corresponding module andIts corresponding connection relationship is utilized respectively drive module, switchgear module, control module and interrupt module and realizes to be drivenThe identification of equipment and setting, switch operation, feature operation and the interrupt operation of initiation parameter, to control CAN link mouldBlock, which is realized, treats the driving of driving equipment, realize under the difference baud rate of the channel multichannel CAN from transmitting-receiving test, pressure test and withThe joint debugging test of the other CAN devices in periphery.
A kind of CAN device driving device and method based on pci bus of the invention, search pci bus are searched and are readDevice data to be driven is taken, drives CAN device that can shield bottom hardware structure completely by pci bus, is provided using systemFunction library searching pci bus identify device PCI present in system, configured after determining corresponding equipment, andBy the relevant register of Loongson mainboard defined in plate grade definition layer, realization peripheral operation system is associated with bottom hardware,The request of user is successfully transmitted to device to be driven, so as to reliable and stable operation in systems, fault-free, internal storage conflictThe case where occur, guarantee the driver normal operation of equipment to be driven, thus guarantee treating driving equipment driving operation justTrue property, stability and feasibility.
A kind of CAN device driving device and method based on pci bus of the invention inputs CAN by pci bus and setsStandby driver is both the interface between operating system nucleus and hardware, also can be regarded as one between application program and hardwareA intermediate software layer shields the details of hardware for application program, provides a group interface for external equipment, pass through application program tuneEquipment to be driven is created, opens, closes, is written, reads and controlled with equipment interface file manipulation function.
A kind of CAN device driving device and method based on pci bus of the invention, passes through interrupt service routineIPciCan and interruption post processor tPciCan realize the interrupt operation for treating driving equipment, can prevent the equipment from occupyingBreak period is too long to cause subsequent equipment to be unable to get response and data loss problem for a long time, meanwhile, interrupt operation spiritActivity is good, and whole efficiency is high.
Detailed description of the invention
Fig. 1 is a kind of structural schematic diagram of CAN device driving device based on pci bus of the embodiment of the present invention;
Fig. 2 is a kind of schematic diagram of CAN device driving method based on pci bus of the embodiment of the present invention.
Specific embodiment
In order to make the objectives, technical solutions, and advantages of the present invention clearer, with reference to the accompanying drawings and embodiments, rightThe present invention is further elaborated.It should be appreciated that the specific embodiments described herein are merely illustrative of the present invention, andIt is not used in the restriction present invention.
In addition, as long as technical characteristic involved in the various embodiments of the present invention described below is each other notConstituting conflict can be combined with each other.The present invention is described in more detail With reference to embodiment.
Fig. 1 is a kind of structural schematic diagram of CAN device driving device based on pci bus of the embodiment of the present invention.Such as Fig. 1Shown, which includes sequentially connected main control module, FPGA module and CAN link module, and Add-In write device passes throughPci bus is connect with main control module, and as an example, Add-In write device is the sharp magnificent operating system of Loongson mainboard;CANLink module connects equipment to be driven for providing CAN interface, and to realize the communication with equipment to be driven, main control module includes drivingDynamic model block, switchgear module, control module and interrupt module,
Drive module realizes equipment initialization to be driven for the instruction of externally input first stage and second stage instructionThe setting or closing of parameter, specifically, first stage instruction are to utilize first stage initialization function CreatePciCan realityThe creation and equipment global variable to be driven initialization of existing equipment to be driven, while PCI is searched for using function pciFindDeviceBus searches equipment to be driven, and the address mapping space BAR0, the mapping sky of equipment to be driven are read using pciConfigInLongBetween the address BAR2, while the interrupt number of equipment to be driven is set, is deleted accordingly if initialization is unsuccessful if the first stageIt removes;Second stage instruction connects to record the corresponding CAN of equipment to be driven using second stage initialization function CreatePciCan2Mouth base address, registration interrupt service routine, enabled PCI are interrupted, are enabled CAN interruption and open LocalBus to interrupt, if secondStage initialization is unsuccessful, is deleted accordingly;
Closing for first stage initiation parameter can realize the wound for deleting equipment to be driven using function RemovePciCanBuild the global variable with equipment to be driven;The closing of second stage initiation parameter is deleted using function RemovePciCan2 realizationExcept the corresponding CAN interface base address of equipment to be driven, registration interrupt service routine, closes PCI interruption, closing CAN interruption and closeClose LocalBus interruption.
Switchgear module be used for according to externally input switch order open or close CAN link module semaphore,Task sends FIFO, receives FIFO and message queue.Specifically, CAN is driven using opening equipment OpenPciCan function creationThe semaphore of link module, sends FIFO, receives FIFO, message queue task, utilizes pass hull closure ClosePciCan functionThe semaphore of CAN link module is closed, task, FIFO is sent, receives FIFO, message queue.
Control module is used to realize the command word function to CAN link module according to externally input control instruction, reads functionCan and it write feature operation;
Specifically, command word feature operation be using IocalPciCan function by receiving from external command code andRelevant parameter realizes the command operation for treating driving equipment, and the macro mapping that different control codes calls hardware abstraction layer to provide is correspondingThe memory address of operating system is operated, and as an example, IOCTL_INIT_SJA initialization function can be called to realize CANThe initialization of link module SJA controller, wherein CAN link module SJA controller initialization execute sequence are as follows: configuration resetPattern configurations, setting clock CDR, setting o controller OCR, setting baud rate BTR, setting error thresholds EWLR, removing are wrongMiscount device, the ACR and AMR being arranged under the reset mode of extension can enable interruption, the setting normal mode of demand;
Reading feature operation is the CAN bus data frame received in FIFO for reading CAN link module, using reading data letterNumber ReadPciCan reads the CAN bus data frame for receiving FIFO.
Write feature operation be will be put into from external data send FIFO and be converted to CAN bus data frame be sent toDriving equipment specifically can will need the data sent to be put into CAN link module by write data function WritePciCanFIFO is sent, and trigger data sends task tPciCan, sends task tPciCan directly to CAN link module by dataRegister carries out the transmission that write operation realizes CAN bus frame, can be effectively prevented out in such a way that creation task sends dataNow send the excessively high problem of priority.
Interrupt module is used to treat in driving equipment according to the control CAN link module realization of externally input interrupt instructionInterrupt signal is sent to external program write device, external program by control equipment by disconnected operation, specifically, equipment to be drivenWrite device sends corresponding interrupt instruction to interrupt module, and interrupt module is according to received interrupt instruction by interrupting service journeySequence iPciCan and interruption post processor tPciCan realize the interrupt operation for treating driving equipment, wherein interrupt service routineIPciCan is for judging that interrupt source (whether being that LocalBus is interrupted or Can is interrupted), shielding is interrupted, triggering is interrupted and subsequentOperation interrupts post processor tPciCan and is used to for the register data of CAN link module being stored in corresponding reception FIFO,And empty the register data of CAN link module.By interrupt service routine iPciCan and interrupt post processor tPciCanIt realizes and treats the interrupt operation of driving equipment, can prevent the equipment from occupying the break period too long when causing subsequent equipment longBetween be unable to get response and data loss problem, meanwhile, the interrupt operation flexibility is good, and whole efficiency is high.
As an example, interrupt service routine specifically:
The working method of pci bus is the C mode that address wire and data line are not multiplexed, as an example, can will be with PCITwo equipment of bus connection are divided into main equipment and from equipment, and main equipment can control bus to carry out driving address, data and controlSignal operation cannot start bus operation from equipment, rely only on master control pci controller equipment from wherein read data or toIt transmits data.It in the embodiment of the present invention, sets main control module to from equipment, is set based on the setting of external program write deviceStandby, data transmission mode setting is slave pattern between main control module and pci bus.Pci bus controller internal register is matchedConfidence breath writes in EEPROM, and when power-up can load serial EEPROM configuration information automatically, can be posted by pci bus configurationStorage read-write.
CAN link module can realize that CAN device hardware resource corresponding with equipment to be driven distributes using FPGA, i.e. bottomDriver realizes the function of resource access according to the resource that FPGA is distributed, and call driver provides in the applicationApi function realizes specific functional test.
Fig. 2 is a kind of schematic diagram of CAN device driving method based on pci bus of the embodiment of the present invention.Such as Fig. 2 instituteShow, a kind of CAN device driving method based on pci bus specifically:
S1. according to the externally input first stage instruction using first stage initialization function CreatePciCan realize toThe creation of driving equipment and equipment global variable to be driven initialization, run succeeded, enter step S2;Otherwise, the first rank is utilizedSection deletes function RemovePciCan and removes first stage initializing variable;
S2. second stage initialization function is utilized according to the instruction of externally input second stageCreatePciCanCreatePciCan2 realization records the corresponding CAN interface base address of equipment to be driven, service journey is interrupted in registrationSequence, enabled PCI are interrupted, enabled CAN interrupts and opens LocalBus and interrupts, and are run succeeded, are entered step S3;Otherwise, is utilizedTwo-stage deletes function RemovePciCan2 and removes second stage initializing variable;
S3. according to externally input switch order open or close the semaphore of CAN link module, task, send FIFO,Receive FIFO and message queue;
Specifically, semaphore, task, the hair for opening equipment OpenPciCan function creation driving CAN link module are utilizedSend FIFO, receive FIFO, message queue, using pass hull closure ClosePciCan function close CAN link module semaphore,Task sends FIFO, receives FIFO, message queue.
S4. it realizes the command word function to CAN link module according to externally input control instruction, read function and writes functionOperation;
Specifically, command word feature operation be using IocalPciCan function by receiving from external command code andRelevant parameter realizes the command operation for treating driving equipment, and the macro mapping that different control codes calls hardware abstraction layer to provide is correspondingThe memory address of operating system is operated, and as an example, IOCTL_INIT_SJA initialization function can be called to realize CANThe initialization of link module SJA controller, wherein CAN link module SJA controller initialization execute sequence are as follows: configuration resetPattern configurations, setting clock CDR, setting o controller OCR, setting baud rate BTR, setting error thresholds EWLR, removing are wrongMiscount device, the ACR and AMR being arranged under the reset mode of extension can enable interruption, the setting normal mode of demand;
Reading feature operation is the CAN bus data frame received in FIFO for reading CAN link module, using reading data letterNumber ReadPciCan reads the CAN bus data frame for receiving FIFO.
Write feature operation be will be put into from external data send FIFO and be converted to CAN bus data frame be sent toDriving equipment specifically can will need the data sent to be put into CAN link module by write data function WritePciCanFIFO is sent, and trigger data sends task tPciCan, sends task tPciCan directly to CAN link module by dataRegister carries out the transmission that write operation realizes CAN bus frame, can be effectively prevented out in such a way that creation task sends dataNow send the excessively high problem of priority.
S5. the interrupt operation for treating driving equipment is realized according to externally input interrupt instruction control CAN link module.
Specifically, interrupt signal is sent to external program write device, external journey by control equipment by equipment to be drivenSequence write device sends corresponding interrupt instruction to interrupt module, and interrupt module is according to received interrupt instruction by interrupting serviceProgram iPciCan and interruption post processor tPciCan realize the interrupt operation for treating driving equipment, wherein interrupt service journeySequence iPciCan for judge interrupt source (whether be LocalBus interrupt or Can interrupt), shielding interrupt, triggering interrupt and afterContinuous operation is interrupted post processor tPciCan and is used for for the register data of CAN link module to be stored in corresponding receptionIn FIFO, and empty the register data of CAN link module.
As an example, interrupt service routine specifically:
Externally input instruction is inputted by pci bus, wherein pci bus working method for address wire and data line notConnect with pci bus two equipment can be divided into main equipment and from equipment, main equipment as an example by the C mode of multiplexingControllable bus carries out driving address, data and control signal operation, cannot start bus operation from equipment, rely only on masterIt controls pci controller equipment and transmits data from wherein reading data or to it.In the embodiment of the present invention, by main control module be set as fromEquipment, external program write device are set as main equipment, and data transmission mode setting is from mould between main control module and pci busFormula.The configuration information of pci bus controller internal register writes in EEPROM, and when power-up can load automatically serial EEPROM and matchConfidence breath can read and write configuration register by pci bus.
CAN link module can realize that CAN device hardware resource corresponding with equipment to be driven distributes using FPGA, i.e. bottomDriver realizes the function of resource access according to the resource that FPGA is distributed, and call driver provides in the applicationApi function realizes specific functional test.
As it will be easily appreciated by one skilled in the art that the foregoing is merely illustrative of the preferred embodiments of the present invention, not toThe limitation present invention, any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should all includeWithin protection scope of the present invention.