CODESYS - the IEC 61131-3 automation software
https://forum.codesys.com/

Need some I2C Driver help, here's my progress so far...
https://forum.codesys.com/viewtopic.php?f=21&t=5698
Page 1 of 2

Author:  Hodgy1993 [ Wed Apr 02, 2014 12:41 pm ]
Post subject:  Need some I2C Driver help, here's my progress so far...

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.


Image

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

Author:  Edwin Schwellinger [ Thu Apr 03, 2014 8:43 am ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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:

Code:
  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:
Code:
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"

BR
Edwin

Author:  mbruck [ Mon May 26, 2014 4:46 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

How to install this

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


libraries into system to actualy get this device working ?

Author:  Edwin Schwellinger [ Mon May 26, 2014 5:50 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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

Author:  mbruck [ Mon May 26, 2014 9:56 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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 :(.

Author:  Edwin Schwellinger [ Tue May 27, 2014 4:33 am ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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

Author:  mbruck [ Tue May 27, 2014 8:41 am ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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

Author:  Edwin Schwellinger [ Tue May 27, 2014 2:58 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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

Author:  mbruck [ Wed May 28, 2014 11:07 am ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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

Author:  mbruck [ Wed May 28, 2014 11:08 am ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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\" ?

Author:  Edwin Schwellinger [ Wed May 28, 2014 2:13 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

>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

Author:  mbruck [ Wed May 28, 2014 2:29 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

Edwin,

I asked about how to delete library and device :)
Quote:
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\" ?

Author:  Edwin Schwellinger [ Wed May 28, 2014 2:36 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

delete devices here:
and delete libraries here:

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

BR
Edwin

Author:  mbruck [ Wed May 28, 2014 2:41 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

Quote:
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

Author:  mbruck [ Wed May 28, 2014 2:42 pm ]
Post subject:  Re: Need some I2C Driver help, here's my progress so far...

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


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

Page 1 of 2 All times are UTC+01:00
Powered by phpBB® Forum Software © phpBB Limited
https://www.phpbb.com/