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 called FlashCenter, 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:
Call AardvarkApi.aa_i2c_write(handle, I2C_DEVICE, AardvarkI2cFlags.AA_I2C_NO_STOP, 128, offset)
...here is what the support documents define:
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 BIN file 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.
To make this even more simple, I would love to pass ANYTHING to the EEPROM. No matter what I change in this code, I simply cannot get anything to write to the EEPROM. Or maybe it does and I'm just not seeing it. When you run this code the "read" function generates the output "12345678910111213141516". Currently there is 128 bytes of dummy content filling the EEPROM, and 1-16 is NOT in there.
If ANYONE can get me on the right track, it would be hugely appreciated!!!! Thank you.