Patent Publication Number: US-9418038-B2

Title: Method and system for accessing data

Description:
CROSS-REFERENCE TO RELATED APPLICATION 
     This application claims the priority benefit of Taiwan application serial no. 102132604, filed on Sep. 10, 2013. The entirety of the above-mentioned patent application is hereby incorporated by reference herein and made a part of this specification. 
     BACKGROUND 
     1. Field of the Invention 
     The invention is directed to a data transmission technique and more particularly, to a method and a system for accessing data through an inter-integrated circuit (I 2 C) bus. 
     2. Description of Related Art 
     An inter integrated circuit (I 2 C) bus is a kind of serial communication bus and is common used in a motherboard, an embedded system or a mobile phone for connecting to low-speed peripheral devices. Recently, the I 2 C bus is a common data transmission interface used in an electronic product and serves for transferring some low-speed data, which includes a temperature sensor, a speed sensor, a digital gyroscope or the like. 
     Nevertheless, the I 2 C bus has a limitation on the number thereof in hardware design, and in most cases, a plurality of I 2 C devices is connected on the same I 2 C bus, which often results in a situation that the I 2 C bus is simultaneously accessed by multiple devices. At this point, in case that control software can not be performed properly, it inevitably leads to incorrect data accessing. Therefore, the system is affected to be unstable, such that the difficulty in the debugging of a driver of a single device occurs during the development process, which results in prolonged development time. 
     SUMMARY 
     The invention provides a method and a system for accessing data, in which a plurality of inter-integrated circuit (I 2 C) devices can share an I 2 C bus in common, without causing an error of simultaneous accessing. 
     The invention is directed to a method for accessing data applicable to an electronic apparatus. The method includes the following steps. A management module receives a plurality of transmission data and a plurality of identification codes respectively from a plurality of application programs and stores the plurality of transmission data to a queue according to a receiving order. The management module obtains one of the plurality of transmission data according to a delivering order of the queue. The management module transfers the obtained transmission data to a corresponding device through an inter-integrated circuit (I 2 C) driving module. The management module receives one corresponding response data from the device through the I 2 C driving module. The management module transfers the response data to one of the corresponding application programs according to one of the plurality of identification codes corresponding to the obtained transmission data. 
     In an embodiment of the invention, before the step of the management module receiving the plurality of transmission data and the plurality of identification codes respectively from the plurality of application programs, method further includes the following steps. The management module receives a register request from each of the application programs to generate one of the plurality of identification codes according to the register request and transfers the one of the plurality of identification codes corresponding to the register request to each of the application programs. 
     In an embodiment of the invention, in the method, the management module further receives a unregister request from each of the application programs to cancel one of the plurality of identification codes corresponding to the unregister request. 
     In an embodiment of the invention, a transmission format of each of the application programs includes one of the plurality of identification codes, an I 2 C address, a data length and a content of the transmission data. 
     In an embodiment of the invention, in the method, the management module further checks whether one of the plurality of transmission data exists in the queue and obtains one of the plurality of transmission data according to the delivering order when checking that the one of the plurality of transmission data exists in the queue. 
     The invention is directed to a system for accessing data including a device, an I 2 C bus coupled to the device, a processing unit coupled to the I 2 C bus and accessing the device through the I 2 C bus and a storage unit coupled to the processing unit and storing a plurality of modules to be executed by the processing unit. The modules includes a management module receiving a plurality of transmission data and a plurality of identification codes from a plurality of application programs, storing the plurality of transmission data to a queue according to a receiving order and transferring the plurality of transmission data according to a delivering order and an I 2 C driving module communicating with the plurality of application programs via the management module and accessing the device through the I 2 C bus. The management module the management module obtains one transmission data from the plurality of transmission data and transfers the obtained transmission data to the I 2 C driving module according to the delivering order, and the I 2 C driving module transfers the obtained transmission data to the device through the I 2 C bus and transfers one response data received from the device through the I 2 C bus to the management module, such that the management module transfers the response data to one of the corresponding application programs according to one of plurality of identification codes corresponding to the obtained transmission data. 
     Based on the above, the invention utilizes the management module to manage the accessing operation to the I 2 C bus, such that the I 2 C bus can be prevented from being accessed simultaneously in the scenario where a plurality of I 2 C devices shares the same I 2 C bus in common. 
     In order to make the aforementioned and other features and advantages of the invention more comprehensible, several embodiments accompanied with figures are described in detail below. 
    
    
     
       BRIEF DESCRIPTION OF THE DRAWINGS 
       The accompanying drawings are included to provide a further understanding of the invention, and are incorporated in and constitute a part of this specification. The drawings illustrate embodiments of the invention and, together with the description, serve to explain the principles of the invention. 
         FIG. 1  is a block diagram of a system for accessing data according to an embodiment of the invention. 
         FIG. 2  is a structural diagram of the system for accessing data according to an embodiment of the invention. 
         FIG. 3  is a flowchart of a method for accessing data according to an embodiment of the invention. 
         FIG. 4  is a schematic diagram of a transmission format according to an embodiment of the invention. 
         FIG. 5  is a flowchart of a method for a single application program accessing data according to an embodiment of the invention. 
         FIG. 6  is a flowchart of a method for two application programs accessing data according to an embodiment of the invention. 
     
    
    
     DESCRIPTION OF EMBODIMENTS 
     In a computer system, an inter-integrated circuit (I 2 C) bus is commonly provided to be connected with peripheral devices and driven using software or firmware to achieve the purpose of control the peripheral devices. An error of simultaneous accessing occurs easily due to a plurality of I 2 C devices sharing the same I 2 C bus in common. Thus, in order to resolve the aforementioned issue, the invention provides a method and a system for accessing data, in which the I 2 C bus is accessed in an asynchronous way so as to prevent the I 2 C bus being simultaneously accessed. 
       FIG. 1  is a structural diagram of the system for accessing data according to an embodiment of the invention. With reference to  FIG. 1 , the system of the present embodiment is an electronic apparatus, such as a mobile phone, a notebook computer (NB), a desktop computer or the like, which includes a processing unit  110 , a storage unit  120 , an I 2 C bus  130  and a device  140 . The processing unit  110  is coupled to the storage unit  120  and the I 2 C bus  130 , and accesses the device  140  via the I 2 C bus  130 . 
     The processing unit  110  is, for example, a central processing unit (CPU) or any other programmable microprocessor. The storage unit  120  is, for example, a random access memory (RAM), a read-only memory (ROM), a flash memory, a magnetic disk storage device or the like. The device  140  is, for example, a temperature sensor, a speed sensor, a digital gyroscope or the like with an I 2 C interface. 
     In the present embodiment, the storage unit  120  stores a plurality of code segments. The code segments after being installed are executed by the processing unit  110 . The code segments include a plurality of instructions, and the processing unit  110  uses the instructions to perform a plurality of steps of a method for accessing data. In the present embodiment, the system for accessing data includes only one processing unit  110 , but in other embodiments, the system for accessing data may include a plurality of processing units  110  for executing the installed code segments. 
       FIG. 2  is a structural diagram of the system for accessing data according to an embodiment of the invention. With reference to both  FIG. 1  and  FIG. 2 , a management module  230  and I 2 C driving module  240  exist in, for example, the storage unit  120  to be executed by the processing unit  110 . For descriptive convenience, two application programs  210 _ 1  and  210 _ 2  and two device  140 _ 1  and  140 _ 2  with the I 2 C interfaces are illustrated. Nevertheless, in other embodiments, the numbers of the application programs and the devices are not limited. 
     In the present embodiment, a management module  230  is installed in an operation system (OS) which is especially responsible for controlling software or firmware of the I 2 C bus  130 , and all accessing operations to the I 2 C bus  130  have to be performed through the control of the management module  230 , such that the I 2 C bus  130  can be prevented from being simultaneously accessed by a plurality of application programs. Additionally, the management module  230  is also configured with a memory space for placing I 2 C instructions and data waiting for being processed. The memory space is, for example, a first in first out (FIFO) queue. In the queue, the instructions and data of the I 2 C bus  130  to be accessed by the application programs  210 _ 1  and  210 _ 2  to may be temporarily stored. The management module  230  processes the received transmission data according to a delivering order of the queue and returns response data which is obtained after processing transmission data to an application program which request for accessing the device. The management module  230  may be general type application software or a driver. 
     The I 2 C driving module  240 , i.e., a driver of the I 2 C bus  130 , communicates with the application programs  210 _ 1  and  210 _ 2  via the management module  230  and accesses the devices  140 _ 1  and  140 _ 2  through the I 2 C bus  130 . 
     Hereinafter, a process for accessing data will be described in detail accompanying with the operation of the aforementioned system.  FIG. 3  is a flowchart of a method for accessing data according to an embodiment of the invention. In the present embodiment, taking the application programs  210 _ 1  and  210 _ 2  for example and based on an assumption that the application programs  210 _ 1  and  210 _ 2  are about to simultaneously access the devices  140 _ 1  and  140 _ 2  through the I 2 C bus  130 . Namely, the application program  210 _ 1  is about to access the device  140 _ 1  through the I 2 C bus  130 , while the application program  210 _ 2  is about to access the device  140 _ 2  through the I 2 C bus  130 . 
     With reference to both  FIG. 2  and  FIG. 3 , in step S 305 , the management module  230  receives a plurality of transmission data and a plurality of identification codes respectively from a plurality of application programs and stores the transmission data to a queue according to a receiving order. In this case, an application program, for example, packages each of the transmission data and each of the identification codes as a packet complying with a transmission format and then, transfers the packet to the management module  230 . For instance,  FIG. 4  is a schematic diagram of a transmission format according to an embodiment of the invention. With reference to  FIG. 4 , the transmission format includes four fields  410  through  440 . The field  410  records an identification code, the field  420  records an I 2 C address to be accessed by a device, the field  430  records a data length, and the field  440  records data content (including transmission data and/or an instruction). The identification code is used to identify an identity of an application program. 
     The application programs  210 _ 1  and  210 _ 2  first respectively package identification codes and transmission data corresponding thereto as packets complying with the transmission format illustrated in  FIG. 4  and then transfers the packets to the management module  230 . After receiving the packets, the management module  230  stores the packets to the queue according to the order of receiving the packets so as to process the packets one by one sequentially. 
     Afterward, the management module  230  checks whether any transmission data exists in the queue so as to obtain one of the transmission data according to a delivering order when checking that the transmission data exists in the queue. That is, in step S 310 , the management module  230  obtains transmission data with a first delivering order from the queue according to the delivering order of the queue. The aforementioned queue is, for example, a FIFO queue, and transmission data that first enters the queue will be processed first, and another transmission data entering the queue afterward has to wait. For descriptive convenience, description with respect to an example where the transmission data of the application program  210 _ 1  is the first data written into the queue will be set forth below. Namely, in this case, the management module  230  obtains the packet with the first delivering order which is transferred by the application program  210 _ 1  from the queue and disassembles the packet to obtain the content, the I 2 C address to be transferred to and the data length of the transmission data. 
     Then, in step S 315 , the management module  230  transfers the obtained transmission data to the corresponding device  140 _ 1  using the I 2 C driving module  240  through the I 2 C bus  130 . For instance, the management module  230  re-packages the content, the I 2 C address to be transferred to and the data length of the transmission data and transfers the re-packaged packet to the corresponding device  140 _ 1  according to the recorded I 2 C address. 
     Thereafter, in step S 320 , the management module  230  receives corresponding response data from the device  140 _ 1  using the I 2 C driving module  240 . Meanwhile, in step S 325 , the management module  230  transfers the response data to the corresponding application program  210 _ 1  according to the corresponding identification code. For example, the management module  230  re-packages the response data and the identification code a the packet in compliance with the transmission format illustrated in  FIG. 4  and then returns the re-packaged packet to the application program  210 _ 1 . 
     An embodiment of a timing sequence of devices accessed by a single application program via the management module  230  and an embodiment of a timing sequence of devices accessed by multiple application programs via the management module  230  will be illustrated an examples below. 
       FIG. 5  is a flowchart of a method for a single application program accessing data according to an embodiment of the invention. In  FIG. 5 , a process of the application program  210 _ 1  assessing the devices  140 _ 1  and  140 _ 2  via the management module  230  is illustrated as an example. 
     First, in step S 501 , the application program  210 _ 1  sends a register request to the management module  230 . When receiving the register request, the management module  230  generates (or looks up for) an identification code ID 1  according to the register request. Then, in step S 503 , the management module  230  returns the identification code ID  1  to the application program  210 _ 1 . 
     In step S 505 , the application program  210  transfers transmission data A 1  to the management module  230 . For instance, the application program  210 _ 1  packages the identification code ID 1 , an I 2 C address to be accessed by the device  140 _ 1 , a data length and the content of the transmission data A 1  in a packet and then transfers the packet to the management module  230 . After receiving the packet, the management module  230  stores the packet to the queue. 
     Thereafter, in step S 507 , the management module  230  obtains the first packet from the queue, disassembles the packet to receive the transmission data A 1 , and transforms the transmission data A 1  into a packet in compliance with the I 2 C format so as to transfer the transmission data A 1  to the I 2 C driving module  240  according to the I 2 C address. Besides, in step S 509 , the transmission data A 1  is transferred to the device  140 _ 1  through the I 2 C bus  130 . 
     Afterward, in step S 511 , the device  140 _ 1  transfers response data B 1  to the I 2 C driving module  240  through the I 2 C bus  130 . Further, in step S 513 , the I 2 C driving module  240  transfers the response data B 1  to the management module  230 . In step S 515 , the management module  230  packages the response data B 1  of the device  140 _ 1  in a packet complying with the transmission format (as illustrated in  FIG. 4 ) and then returns the packet to the application program  210 _ 1 . 
     Then, in step S 517 , the application program  210 _ 1  transfers another transmission data A 2  to the device  140 _ 2 . The application program  210 _ 1  packages the transmission data A 2  and the identification code ID  1  in a packet complying with the transmission format (including the identification code ID 1  corresponding to the application program  210 _ 1 , an I 2 C address to be accessed by the device  140 _ 2 , a data length and the content of the transmission data A 2 ) and then transfers the packet to the management module  230 . After receiving the packet from the application program  210 _ 1 , the management module  230  places the packet to the queue. 
     After that, in step S 519  (in the same way of step S 507 ), the management module  230  obtains the packet from the queue, disassembles the packet to receive the transmission data A 2  and transfers the packet in the I 2 C format to the I 2 C driving module  240 . Further, in step S 521 , the I 2 C driving module  240  transfers the transmission data A 2  to the device  140 _ 2  through the I 2 C bus  130 . 
     Then, in step S 523 , the device  140 _ 2  transfers response data B 2  to I 2 C driving module  240  through the I 2 C bus  130 . In step S 525 , the I 2 C driving module  240  returns the response data B 2  of the device  140 _ 2  to the management module  230 . Further, in step S 527 , the management module  230  returns the response data B 2  in the transmission format to the application program  210 _ 1 . 
     When no longer performing data transmission with the devices  140 _ 1  and  140 _ 2 , or being ended, the application program  210 _ 1  may cancel the registration step via the management module  230 , such that the management module  230  may release the resources to other application programs. In step S 529 , the application program  210 _ 1  sends an unregister request to the management module  230  so as to cancel the register request. Further, in step S 531 , the management module  230  notifies the application program  210 _ 1  about the success of the unregistration, for example, by sending a specific value “−1” to the application program  210 _ 1  to indicate that the identification code ID  1  is successfully unregistered. 
     In steps S 501  through S 531  above, the data transmission between the application program  210 _ 1  and the devices  140 _ 1  and  140 _ 2  having the I 2 C interfaces is started with the step of first sending the register request to the management module  230  (step S 501 ) and then returning the corresponding identification code to the application program  210 _ 1  from the management module  230 . The application program  210 _ 1  sends the register request to the management module  230  to obtain the corresponding identification code and then is allowed to communicate with the devices  140 _ 1  and  140 _ 2 . The identification code is used in the transmission format (e.g., the field  410  illustrated in  FIG. 4 ) when the application program  210 _ 1  accesses the devices  140 _ 1  and  140 _ 2  via the management module  230 . 
     In addition, a process according to a timing sequence of devices accessed by multiple application programs via the management module  230  will be described in detail below.  FIG. 6  is a flowchart of a method for two application programs accessing data according to an embodiment of the invention. In  FIG. 6 , a process of the application program  210 _ 1  accessing the device  140 _ 1  via the management module  230  while the application program  210 _ 2  also accessing the device  140 _ 2  via the management module  230  is illustrated as an example. 
     In step S 601 , the application program  210 _ 1  sends a register request to the management module  230  for registration. Then, in step S 603 , the management module  230  returns an identification code ID 1  to the application program  210 _ 1 . 
     Meanwhile, in step S 605 , the application program  210 _ 2  simultaneously sends a register request to the management module  230  for registration. Then, in step S 607 , the management module  230  returns an identification code ID 2  to the application programs  210 _ 2 . 
     Thereafter, in step S 609 , the application program  210 _ 1  transfers transmission data A 3  in a transmission format to the management module  230 . For instance, the application program  210 _ 1  packages the identification code ID 1 , an I 2 C address to be accessed by the device  140 _ 1 , a data length and the content transmission data A 3  in a packet and then transfers the packet to the management module  230 . 
     Then, in step S 611 , the application program  210 _ 2  transfers transmission data A 4  in a transmission format to the management module  230 . For instance, the application program  210 _ 2  packages the identification code ID 2 , an I 2 C address to be accessed by the device  140 _ 2 , a data length and the content of the transmission data A 4  in a packet and transfers the packet to the management module  230 . 
     In this case, the application program  210 _ 1  and the application program  210 _ 2  simultaneously transfer the transmission data A 3  and the transmission data A 4 . At this time, the application program  210 _ 1  does not yet receive response data returned by the management module  230 , but the application program  210 _ 2  already transfers the transmission data A 4 , and thus, if the data transmission is not well controlled using, a data error or other unexpected results will occur due to the I 2 C bus  130  being simultaneously accessed by both the transmission data A 3  and the transmission data A 4 . In the present embodiment, the management module  230  is configured between the application programs  210 _ 1  and  210 _ 2  and the I 2 C bus  130  to manage the data transmission therebetween, such that the transmission data A 3  and the transmission data A 4  to be transferred are first placed in a queue (e.g., a FIFO queue) for the management module  230  to then process the two transmission data one by one according to a delivering order of the queue. 
     Afterward, the management module  230  obtains the first data from the queue, which is the data having the first delivering order. Here, it is assumed that the first data is the transmission data A 3 . For instance, after obtaining a first packet from the queue, disassembling the packet to receive the transmission data A 3 , the management module  230  transforms the packet into a packet complying with the I 2 C format (including the I 2 C address to be accessed by the device  140 _ 1 , the data length and the content of the transmission data A 3 ) and then transfers the transmission data A 3  to the I 2 C driving module  240  according to the I 2 C address, as shown in step S 613 . Further, in step S 615 , the I 2 C driving module  240  transfers the transmission data A 3  to the device  140 _ 1  through the I 2 C bus  130 . 
     Then, in step S 617 , the device  140 _ 1  transfers response data B 3  to the I 2 C driving module  240  through the I 2 C bus  130 , and in step S 619 , the I 2 C driving module  240  returns the response data B 3  to the management module  230 . Further, in step S 621 , the management module  230  returns the response data B 3  of the device  140 _ 1  in the transmission format (e.g., the transmission format illustrated in  FIG. 4 ) to the application program  210 _ 1 . Until now, application program  210 _ 1  receives the response data (i.e., the response data B 3 ). Then, the management module  230  further obtains the next data (i.e., the transmission data A 4 ) from the queue for processing. 
     The management module  230  obtains the next packet (i.e., the first packet at this moment due to the prior packet is already processed) from the queue and disassembles the packet to receive the transmission data A 4 . In step S 623 , the management module  230  transfers the transmission data A 4  in the I 2 C format to the I 2 C driving module  240 . In step S 625 , the I 2 C driving module  240  transfers the transmission data A 4  to the device  140 _ 2  through the I 2 C bus  130 . 
     After that, in step S 627 , the device  140 _ 2  returns response data B 4  to the I 2 C driving module  240 . In step S 629 , the I 2 C driving module  240  returns the response data B 4  of the device  140 _ 2  to the management module  230 . Then, in step S 631 , the management module  230  returns the response data B 4  in the transmission format to the application program  210 _ 2 . At this time, the application program  210 _ 2  also receives the response data B 4  returned from the device  140 _ 2 . 
     Moreover, when no longer performing data transmission with the devices  140 _ 1  and  140 _ 2 , or being ended, the application programs  210 _ 1  and  210 _ 2  may cancel the registration step via the management module  230 , such that the management module  230  may release the resources to other application programs. 
     Referring to step S 633 , the application program  210 _ 1  sends an unregister request to the management module  230 . In step S 635 , the management module  230  notifies the application program  210 _ 1  about the success of the unregistration about the success of the unregistration, for example, by sending a specific value “−1” to the application program  210 _ 1  to indicate that the identification code ID 1  is successfully unregistered. 
     Likewise, in step S 637 , the application program  210 _ 2  sends an unregister request to the management module  230 . In step S 639 , the management module  230  notifies the application program  210 _ 2  about the success of the unregistration, for example, by sending a specific value “−1” to the application program  210 _ 2  to indicate that the identification code ID 2  is successfully unregistered. 
     In the embodiment above, the application program  210 _ 2  does not receive the response data B 4  returned by the device  140 _ 2  right away after the transmission data A 4  is transferred, but waiting until the management module  230  processes the transmission data A 4  transferred by the application program  210 _ 2 , the application program  210 _ 2  can obtain the response data B 4  returned by the device  140 _ 2 . Accordingly, with the management of the management module  230 , the I 2 C bus  130  is controlled to process the transmission and receiving of only one data in one time, that is, process only one I 2 C instruction in one time, without causing data error or any other unexpected results due to the randomly alternating transmission and receiving operations of the data. 
     To sum up, in the embodiments described above, a management module is configured between the application programs and the I 2 C bus, such that accessing operations to the I 2 C bus have to be performed via the management module. Thereby, the I 2 C bus can be prevented from being simultaneously accessed by multiple application programs, and thus, errors in data accessing can be avoided, which results the system in being more stable, easier for debugging and more time-saving for development. Moreover, program development for each I 2 C device can be simpler, and system integration and design can be more efficient. 
     Although the invention has been described with reference to the above embodiments, it will be apparent to one of the ordinary skill in the art that modifications to the described embodiment may be made without departing from the spirit of the invention. Accordingly, the scope of the invention will be defined by the attached claims not by the above detailed descriptions.