Over the past year many individuals have offered assistance with an ongoing project, and things have been taking monumental leaps forward. However, with progress comes more complex needs. As such, I am again stuck. You all have helped me to expand my limited knowledge of VB programming, and I must again ask for help.
Without going deep into the background of the project, we have created a BIN file from some standard text fields, through the use of some very creative coding. No issues there - it has worked perfectly. We then write this BIN file to an EEPROM chip using a programmer called an Aardvark. It has its own program to do this, but also offers an API. Because programming this chip has been a hurdle for some of the employees, we would like to include this functionality into our BIN file generator (i.e. - the BIN file is generated, stored, and immediately written to EEPROM).
The only problem is that this is incredibly advanced for my limited knowledge and cannot figure out how to pass the BIN file data to the EEPROM using the VB code that the Aardvark API has provided. I hope you don't mind if I provide some sample code, and hopefully someone can help me figure this out.
This is the sample VB code provided in the Aardvark API:
Code:
'==========================================================================
' (c) 2004-2009 Total Phase, Inc.
'--------------------------------------------------------------------------
' Project : Aardvark Sample Code
' File : aai2c_eeprom.bas
'--------------------------------------------------------------------------
' Perform simple read and write operations to an I2C EEPROM device.
'--------------------------------------------------------------------------
' Redistribution and use of this file in source and binary forms, with
' or without modification, are permitted.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
' FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
' COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
' INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
' BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
' CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
' LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
' ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
' POSSIBILITY OF SUCH DAMAGE.
'==========================================================================
Imports TotalPhase
Module aai2c_eeprom
'==========================================================================
' CONSTANTS
'==========================================================================
Const I2C_BITRATE As Integer = 100 'kHz
Const I2C_BUS_TIMEOUT As UShort = 150 'ms
Const I2C_DEVICE As UShort = &H54
'==========================================================================
' MAIN PROGRAM
'==========================================================================
Sub aai2c_eeprom_run(ByRef sampletext As Windows.Forms.TextBox)
Dim handle As Long
handle = AardvarkApi.aa_open(0)
If (handle <= 0) Then
sampletext.Text &= "Unable to open Aardvark device on port 0" & vbCrLf
sampletext.Text &= "Error code = " & handle & vbCrLf
Exit Sub
End If
' Ensure that the I2C subsystem is enabled
Call AardvarkApi.aa_configure(handle, AardvarkConfig.AA_CONFIG_SPI_I2C)
' Enable the I2C bus pullup resistors (2.2k resistors).
' This command is only effective on v2.0 hardware or greater.
' The pullup resistors on the v1.02 hardware are enabled by default.
Call AardvarkApi.aa_i2c_pullup(handle, AardvarkApi.AA_I2C_PULLUP_BOTH)
' Power the board using the Aardvark adapter's power supply.
' This command is only effective on v2.0 hardware or greater.
' The power pins on the v1.02 hardware are not enabled by default.
Call AardvarkApi.aa_target_power(handle, AardvarkApi.AA_TARGET_POWER_BOTH)
' Set the bitrate
Dim bitrate As Long
bitrate = AardvarkApi.aa_i2c_bitrate(handle, I2C_BITRATE)
sampletext.Text &= "Bitrate set to " & bitrate & " kHz" & vbCrLf
' Set the bus lock timeout
Dim bus_timeout As Long
bus_timeout = AardvarkApi.aa_i2c_bus_timeout(handle, I2C_BUS_TIMEOUT)
sampletext.Text &= "Bus lock timeout set to " & bus_timeout & " ms" & vbCrLf
' Write the offset and read the data
'Dim offset(15) As Byte
Dim offset() As Byte = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1} 'THIS IS JUST A TEST STRING, but doesn't seem to ever get written
Dim data(128) As Byte
Dim result As Long
Dim k As String = String.Empty
offset(0) = 1
Call AardvarkApi.aa_i2c_write(handle, I2C_DEVICE, AardvarkI2cFlags.AA_I2C_NO_STOP, 128, offset)
result = AardvarkApi.aa_i2c_read(handle, I2C_DEVICE, AardvarkI2cFlags.AA_I2C_NO_FLAGS, 128, data)
If result <= 0 Then
sampletext.Text &= "i2c read error" & vbCrLf
Else
Dim i As Integer
sampletext.Text &= "Read data bytes:"
For i = 0 To 127
k &= System.Convert.ToChar(data(i)).ToString
Next
sampletext.Text &= k
sampletext.Text &= vbCrLf
End If
' Close the device and exit
AardvarkApi.aa_close(handle)
End Sub
End Module
And specifically dealing with the "write" function:
Master Write (aa_i2c_write)
int aa_i2c_write (Aardvark aardvark,
aa_u16 slave_addr,
AardvarkI2cFlags flags,
aa_u16 num_bytes,
const aa_u08 * data_out);
Write a stream of bytes to the I2C slave device.
Arguments
aardvark handle of an Aardvark adapter
slave_addr the slave from which to read
flags special operations as described in "Notes" section
num_bytes the number of bytes to write (maximum 65535)
data_out pointer to data
Return Value
Number of bytes written.
Specific Error Codes
AA_I2C_WRITE_ERROR There was an error reading the acknowledgment from the Aardvark adapter. This is most likely a result of a communication error.
Details
For ordinary 7-bit addressing, the lower 7 bits of slave_addr should correspond to the slave address. The topmost bits are ignored. The Aardvark I2C subsystem will assemble the address along with the R/W bit after grabbing the bus. For 10-bit addressing, the lower 10 bits of addr should correspond to the slave address. The Aardvark adapter will then assemble the address into the proper format as described in the Philips specification. There is a limitation that a maximum of only 65534 bytes can be written in a single transaction if the 10-bit addressing mode is used.
The slave_addr 0x00 has been reserved in the I2C protocol specification for general call addressing. I2C slaves that are enabled to respond to a general call will acknowledge this address. The general call is not treated specially in the Aardvark I2C master. The user of this API can manually assemble the first data byte if the hardware address programming feature with general call is required.
It is actually possible to write 0 bytes to the slave. The slave will be addressed and then the stop condition will be immediately transmitted by the Aardvark adapter. No bytes are sent to the slave, so the data argument is ignored (i.e., it can be 0 or point to invalid memory).
If the number of bytes written is zero, the following conditions are possible.
The requested slave was not found.
The requested slave is on the bus but refuses to acknowledge its address.
The Aardvark adapter was unable to seize the bus due to the presence of another I2C master. Here, the arbitration was lost during the slave addressing phase results can be unpredictable.
The slave was addressed and no bytes were written to it because num_bytes was set to 0.
The number of bytes written can be less than the requested number of bytes in the transaction due to the following possibilities.
The Aardvark adapter loses the bus during the data transmission due to the presence of another I2C master.
The slave refuses the reception of any more bytes.
So clearly I have to pass a byte array, but I'm not entirely sure how to encode the attached file (a sample BIN file in a zip archive) so that it can be passed. There's a lot of conversions at this low level that I'm cloudy about... converting things to strings, or to byte, or to char, etc. I have played around with them enough to know that in some cases you end up with ASCII, or hex, etc... but I'm not sure what is needed in this case to make it work.
If ANYONE can get me on the right track, it would be hugely appreciated!!!! Thank you.
What it appears to me like what you have to do is convert your string which in this case is some text from a BIN file to a byte array. MSDN shows how to do that here.
I tried your suggestion by implementing the following:
Code:
Dim offset() As Byte = System.Text.Encoding.Unicode.GetBytes("HELLO")
Unfortunately it does not appear that the write function is actually writing anything, and I guess that's where my biggest hurdle lies. I'm wondering if I'm deciphering the information correctly within the write function:
The BIN files are all 128 bytes, so did I set 128 in the correct place according to the documentation? Likewise, is their variable "offset" really the variable that contains the data (or, the contents of my BIN file in this case)?
The attached images shows what the EEPROM looks like after the write procedure.... which is the same way it looked BEFORE the write procedure. I previously filled up the EEPROM using the Aardvark application just to see if I can impart a change. So if it is actually writing, I'm not sure how or where, which leads me to believe I may be confused about the syntax/formatting of this write command.
I'm familiar with the Aardvark and the Beagle too, great little gadgets. Unfortunately I don't have any source files to give you but I was sure the means to do this was on the web. I can't find it at TotalPhase but I did stumble across this link which does the same thing.
Bulldog, thanks for the response. The link you provided is a c# module from their API... which is hidden pretty well on their website. :-)
The code I provided above in my original message is the actual VB code from their API, the exact same module as your c# link, and while it does run it doesn't seem to actually write anything into the EEPROM.
That's the issue - no matter what I modify in the example, I can't seem to impart any change to the contents of the EEPROM. I am checking the EEPROM using FlashCenter by TotalPhase, so it is definitely reading the correct areas, but there is nothing there that I am supposedly writing through this code.
I was hoping that the problem is my fundamental misunderstanding of some part of the code. For example, how many bytes should be written, if the offset is actually a memory offset or if that is the data payload, etc. To me it seems very ambiguous.
I wish I had an aardvark to power up, I did a ton of work with them. If you implement the read_memory function in the link I posted (you'll have to convert it or install VS C#), can you read using that?
From what I remember, there is a security bit, so I'm guessing if you can read the comms are fine, but if you cannot write, the security bit may be set.
Can you hang a scope on the I2C pins and see traffic? If you trigger it correctly you should see your actual message being sent.
Last edited by Bulldog; Mar 5th, 2015 at 04:07 PM.
Well, we don't support c# in our organization, but I have the exact same code in VB format, and when I run it it does seem to query memory. Problem is I can't tell what is actually being written and what is being returned. I know what is currently IN the memory (FlashCenter screenshot above), but it returns "12345678910111213141516". I have tried modifying how many bytes get written (0-128), I have changed what is in the offset variable, I have changed how many bytes get read.... nothing is consistent nor can anything be traced. It seems like the writes are random and the reads are random. I just can't wrap my head around this to get it working!! Ugh.
Just a guess, but are you sure that the variables other than the byte array and it's length are correct/pointing to the right things?
I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it. My war with a browser-redirect trojan
Hi prosper50,
I'll say straight away that I don't know anything about Aardvarks, Beagles etc., but there is one thing that strikes me about the code you posted.
Code:
Dim Handle As Long
is typical of legacy VB, where Long is a 32 bit type. But this is a VB.Net forum, so I assume you are running the code in .Net where Long is 64-bit. If so, you must change it to Dim Handle As IntPtr (or Dim Handle As Int32). The same may apply to all the other numeric data types in the code. If you have the Csharp code available you could check the types against that because there ain't no legacy Csharp.
Dolot and Bops..... thank you both for your inquiries. Dolot, the code I provided is straight from TotalPhase (who makes the Aardvark and the related API).... so I would assume that what they provided is correct to achieve minimum functionality/testing with the API and the device. The project compiles with no errors and does some processing, but I believe the issue is my comprehension of what is going on with the read/write functions rather than their actual functionality.
Bops, I went through and changed the variables you mentioned to Int32. Unfortunately, that didn't seem to do anything different than what the program is already doing. Rather, there is no VISIBLE difference in functionality between the Long and the Int32 in terms of the read/write functionality.
I regret that I cannot help further right now, but I'd suggest you ping a mail to TotalPhase and see if they can help. This subject is discussed in some of their blogs like http://www.totalphase.com/blog/2015/...d-binary-data/ but try opening a ticket and see what they say.
I'll have a look around and see if I can find an Aardvark and if so I'll try it.
Thanks anyway, Bulldog - I appreciate your time. Unfortunately I already exhausted the help of the TotalPhase support team before I came to this forum. They only provide support for out-of-the-box functionality of their products and the API. As soon as you start changing or customizing things, they end support. It's a massive frustration!