C# Programming with sam-ba.dll

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, 

I'm currently trying to use sam-ba.dll (documentation here) in one of our team's C# Windows Forms Apps for programming the bootloader and microcode. In order to simplify its usage, I've written a wrapper class (attached) to handle all of the pointers and unsafe code. Unfortunately, two functions that I keep having trouble with are AT91Boot_Scan and AT91Boot_Open. 


Problems

AT91Boot_Scan: The purpose of this function is to scan and return a list of connected devices. Whenever I call the AT91Boot_Scan function, the code in the dll throws an "A heap has been corrupted" error and crashes my program. I suspect the problem is in the way I allocated memory for the buffers, or the way I passed the pointers to the function, but I can't seem to figure out exactly what's causing this error. The following is code for how I created the array of buffers, and how I passed the pointer for said array to AT91Boot_Scan.

Code:

// Allocate an array (of size maxNumDevices ) of pointers 
byte** deviceList = stackalloc byte*[maxNumDevices * BYTES_PER_POINTER];

for (int n = 0; n < maxNumDevices; n++)
{
    // Allocate a buffer of size 100 for the device name
    byte* deviceNameBuffer = stackalloc byte[BYTES_PER_DEVICE_NAME];

    // Assign the buffer to a pointer in the deviceList
    deviceList[n] = deviceNameBuffer;
}

// Create a pointer to the deviceList. The byte is at the first byte in the array.
// Call the function. A heap has been corrupted error is thrown here.
SamBA.AT91Boot_Scan(ref *deviceList[0]);

 

AT91Boot_Open: Depending on the given device name, this function is supposed to return a communication handle with the device. However, currently the AT91Boot_Open function is always returning 0x00000000 as the communication handle. What I've found is that it will do this when it is given a name that doesn't match any of the connected devices, i.e. "foobar". However, I can't figure out how I could possibly be passing it an incorrect name. The names that I am typically passing are "\jlink\ARM0\SN:<serial number>"

Code:

ISAMBADLL SamBA = new SAMBADLL();
int deviceHandle;
try
{
    unsafe
    {
        byte[] byteName = Encoding.ASCII.GetBytes(deviceName);
        fixed (byte* byteNamePtr = byteName)
        {
            SamBA.AT91Boot_Open(ref *byteNamePtr, out deviceHandle);
        } 
    }
}
catch
{
    Console.WriteLine("Error getting Handle");
    deviceHandle = -2;  // Arbitrary error code
}
return deviceHandle;

Other Thoughts: In my installation of SAM-BA V2.16,there are code examples for C++ . Would there happen to be any for C#? 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In the last few months there were similar discussions. The poster ended up writing his own code to talk the SAM-BA protocol directly as it is quite simple. He found this easier than calling SAM-BA API functions. Try doing a search on the forum (maybe Google can search the forum better?)

 

Last Edited: Thu. Mar 9, 2017 - 10:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks Kartman, I've found the post here. Would you happen to know where I could find the documentation you mentioned for the low level protocols?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry - the project was too long ago. There seems to be plenty of references on the web.