Need some I2C Driver help, here's my progress so far...

Runtime
Hodgy1993
2014-04-02
2014-11-18
  • Hodgy1993

    Hodgy1993 - 2014-04-02

    To the good people of the CODESYS forum,

    I've been developing a system with the new release of the Raspberry Pi platform. What's great about the GPIO access is you can have various other devices running on a "C" program and if any of you are familiar with the WiringPi libraries (found here), then you can actually "pulse a GPIO" upon a program condition, this way the codesys application can then register a change and drive other sub-systems (say a WAGO connected on the Pi's Ethernet).
    It's brilliant!

    The final sub-system I need to connect is a current sensor through I2C. I have the INA219 board, bought from here).

    I've attached my feeble attempt at writing the driver from scratch, basing it on the Adafruit_I2C_PWM library.
    The software recognizes the I2C setup, but I don't believe it "properly" connects to the device let alone control it at the current state it's been written in.

    If anyone can aid me in my struggle I would be immensely grateful.

    -Hodgy.


    INA219 Current Sensor Datasheet: http://www.adafruit.com/datasheets/ina219.pdf m

    IMG: Bild

    Adafruit CurrentSense Library.library [339.17 KiB]

    Adafruit CurrentSense.xml [2.51 KiB]

     
  • eschwellinger

    eschwellinger - 2014-04-03

    Hi Hodgy

    Unfortunately we do not have the device here to test it.
    But looking into your library, I can see that the default i²c address (that is used if you do not set the address in the device configuration (parameters)) is 0x41 (see body of Adafruit_CurrentSense, line 6). Did you verify this with i2cdetect?

    What I saw in the Adafruit example code is that the registers are 16bit long:

      uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
                        INA219_CONFIG_GAIN_8_320MV |
                        INA219_CONFIG_BADCRES_12BIT |
                        INA219_CONFIG_SADCRES_12BIT_1S_532US |
                        INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
      wireWriteRegister(INA219_REG_CONFIG, config);
    

    In your lib you used the write8 method.

    Currently, there is no ready-to-use write16 method, but it is easy to program in your own lib:

    METHOD write16 : BOOL
    VAR_INPUT
          usiRegister: USINT; (* register to write *)
          uiValue: UINT; (* value *)
    END_VAR
    VAR
          Buffer:  ARRAY[0..2] OF BYTE;
    END_VAR
    ---
    Buffer[0] := usiRegister;
    Buffer[1] := UINT_TO_USINT(SHR(uiValue, 8));
    Buffer[2] := UINT_TO_USINT(uiValue AND 16#FF);
    Write16 := DINT_TO_BOOL(Write(ADR(Buffer), 3));
    

    I guess that this should solve the problem!

    Instead of the BeforeWriteOutputs method you should use the AfterReadInputs method, as this device is an input device. You can compare to the SRF02, which is also a pure sensor which is similar to yours

    "c:\Users\<username>\CODESYS Control for Raspberry PI\1.1.0.0\I2C_SRF02.library" </username>

    BR
    Edwin

     

    Related

    Talk.ru: 1
    Talk.ru: 2

  • mbruck

    mbruck - 2014-05-26

    How to install this

    Zitat:
    Adafruit CurrentSense Library.library [339.17 KiB]
    Adafruit CurrentSense.xml [2.51 KiB]

    libraries into system to actualy get this device working ?

     
  • eschwellinger

    eschwellinger - 2014-05-26

    Hi,
    what exactly does not work, the install process to the repositories or adding the device and library and use them?
    we need more information.

    BR
    Edwin

    IMG: CurrentSense.jpg

     
  • mbruck

    mbruck - 2014-05-26

    I'd like to know when I have .library and .xml how to install it into codesys. In this case I mean about those two libraries. I had download it from forum, but did0t know what to do with it .

     
  • eschwellinger

    eschwellinger - 2014-05-27

    Hi,
    the procedure is:
    install the library by Tools-> Install Library
    this install the library to the library repository

    install the device by Tools-> Install device
    this install the device to the device repository

    BR
    Edwin

    IMG: installlibrary_and_devces.jpg

     
  • mbruck

    mbruck - 2014-05-27

    Great! That's clear.

    I'm starting to develop library based on PiFace. I'm not sure I'll succeed so easy, so how to deinstall device and library from system?

    BR,

    Mladen

     
  • eschwellinger

    eschwellinger - 2014-05-27

    Hi,

    important point is for library development you need to switch to professional mode
    under Tools-Features. (see screenshot)
    If you use CODESYS in "Standard" User mode you could not even save the library.

    best practice for library development is:
    Open two instances of CODESYS
    one instance 1 of CODESYS -> open the library which you want to develop (open for example d:\I2C.library)
    second instance 2 of CODESYS -> open th project which you could test your library. (open for example d:\Testproject.project)

    (Also debugging in the project (this is instance 1) in the library is possible... by setting brakepoint in the library fb's)

    If you change some implementation on Library use the yellow symbol to update the library in the library repository
    the second instance of CODESYS recognize the change and you need to download the changes and could retest the new changes.

    BR
    Edwin

    IMG: save_and_install_library.jpg

    IMG: Professional.jpg

     
  • mbruck

    mbruck - 2014-05-28

    Hi!

    Idea about using two IDE for library development is great. Library and device should be installed first and import device/library into new project, am I right ? In this scenario then I have to open library from "C:\ProgramData\CODESYS\Managed Libraries\VendorName...." ?

    BR,

    Mladen

     
  • mbruck

    mbruck - 2014-05-28

    I presume to remove device and library from system is enough to delete folders e.g. "0000 0003" from "C:\ProgramData\CODESYS\Devices\501" and "Raspberry SPI PiFace" from C:\ProgramData\CODESYS\Managed Libraries\3S - Smart Software Solutions GmbH\" ?

     
  • eschwellinger

    eschwellinger - 2014-05-28

    I presume to remove device and library from system is enough to delete folders e.g. "0000 0003" >from "C:\ProgramData\CODESYS\Devices\501" and "Raspberry SPI PiFace" from C:\ProgramData\CODESYS\Managed Libraries\3S >- Smart Software Solutions GmbH\" ?

    Would not recommend to open them from the repository location, you need to copy them to d:\ (somewhere on your harddisk) open it from there
    and install it later by CODESYS to the repositories (same for Devices).

    BR Edwin

     
  • mbruck

    mbruck - 2014-05-28

    Edwin,

    I asked about how to delete library and device

    Zitat:
    I presume to remove device and library from system is enough to delete folders e.g. "0000 0003" >from "C:\ProgramData\CODESYS\Devices\501" and "Raspberry SPI PiFace" from C:\ProgramData\CODESYS\Managed Libraries\3S >- Smart Software Solutions GmbH\" ?

     
  • eschwellinger

    eschwellinger - 2014-05-28

    delete devices here:
    and delete libraries here:

    Altough I do not know why you want delete them...

    BR
    Edwin

    IMG: delete_devices_from_repository.jpg

    IMG: delete_library.jpg

     
  • mbruck

    mbruck - 2014-05-28

    Zitat:
    Would not recommend to open them from the repository location, you need to copy them to d:\ (somewhere on your harddisk) open it from there
    and install it later by CODESYS to the repositories (same for Devices).

    Hmmm

    Please can you explain how to open same library in two IDE? First is developing and library is on temporaly folder, and second is testing IDE.

    BR,

    Mladen

     
  • mbruck

    mbruck - 2014-05-28

    Zitat:
    Altough I do not know why you want delete them...

    When I make some seriuos error while developing new device/library

     
  • eschwellinger

    eschwellinger - 2014-05-28

    Hi Mladen

    Please can you explain how to open same library in two IDE? First is developing and library is on temporaly folder, and second is >testing IDE.

    you mean this was an unprecise description?
    l viewtopic.php?f=21&t=5698#p10467 l

    BR
    Edwin

     
  • mbruck

    mbruck - 2014-05-28

    Dear Edwin,

    Zitat:
    you mean this was an unprecise description?
    viewtopic.php?f=21&t=5698#p10467

    Sorry for misunderstanding, that was precise description, thank you. What I mean is situation when we develop .library from scratch so there is no .project yet using that library.
    So after I create and open new library e.g. d:\mynewlib.library and keeping open it, how to actually test/debug with second instance of CodeSys.
    Hope now it's more clear. Thank you.

    Mladen

     
  • logiro-bo

    logiro-bo - 2014-11-18

    mbruck hat geschrieben:
    Dear Edwin,
    Sorry for misunderstanding, that was precise description, thank you. What I mean is situation when we develop .library from scratch so there is no .project yet using that library.
    So after I create and open new library e.g. d:\mynewlib.library and keeping open it, how to actually test/debug with second instance of CodeSys.
    Hope now it's more clear. Thank you.
    Mladen

    Hello mbruck,

    You should debug in the second instance of Codesys:
    1. open the library in the project, find the FB and/or methods in the library
    2. log into the RPi, download the application, run the application
    3. set breakpoints in the FB/methods as you would do with your program.
    4. Then you can modify the library in the first instance of Codesys accordingly.
    5. Once you make any changes in the first instance, you can update it by clicking the yellow cabinet icon.
    6. Then you log out from the RPi and relog into the RPi and download the application, which should use the new library now.

     
  • plcmax

    plcmax - 2014-11-18

    hello

    for me this FAQ section helped to understand how to debug the libs:

    l viewtopic.php?f=22&t=5641&p=10677#p10677 l

    Cheers
    plcmax

     

Log in to post a comment.