Question about IFU(In Field Update) for USB interface of EMX

I have built a board on the basis of “FEZ Cobra Mainboard” by myself.

How can I convert my project file of visual studio to a .hex file or other type file and the .hex is updated from USB stick to EMX?

Can you give me a simple example or document?

thank you!

I look forward to your answer.

Hi, first you have to deploy your app to the mainboard and after that you create a hex file with mfdeploy by reading it back. For step by step instructions check the documentation of mfdeploy & IFU: http://www.ghielectronics.com/downloads/NETMF/Library%20Documentation%20v4.1/Index.html

For more info you can read this thread: http://www.tinyclr.com/forum/topic?id=1716

Edit:

For a code example: check pyxis2.codeplex.com and check the pyxis2updater.

@ Patrick -

I have read and tried the information of links which you gave me for a long long time, and sorry that I still don’t understand, how can I updata a application .hex file from USB stick to EMX Flash and run the application .hex file.

I only know that, I can get the .hex file of my project with “MFDeploy”. My project is make 2 leds blink.

I have read the example code of “pyxis2updater”, but I don’t know how to use it for my project. Can you give me a simple example codes.

I have found a another example codes in http://www.tinyclr.com/forum/topic?id=8521&page=1#msg85551, and the example is yours 1 month ago, question is how can I try your example? I always stop at as follow codes: (or see picture), how can I set the “bootloader mode” on “Croba” and run your example?

 if (SystemUpdate.GetMode() != SystemUpdate.SystemUpdateMode.Bootloader)
                throw new InvalidOperationException("We must be in bootloader mode!");

I have also tried to press “up”, “down” and “select” button before running your example, but it is not successful.

I repeat the codes as follow:

using System;
using System.IO;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;
using GHIElectronics.NETMF.Hardware;

namespace IFU_SD_Card
{
    public class Program
    {
        public static void Main()
        {
            if (SystemUpdate.GetMode() != SystemUpdate.SystemUpdateMode.Bootloader)
            throw new InvalidOperationException("We must be in bootloader mode!");
            
            // set up the UP button to do the system update
            InterruptPort UP_Button = new InterruptPort((Cpu.Pin)EMX.Pin.IO21, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);

            // wait for it to be pressed
            while (UP_Button.Read()) ;

            PersistentStorage sd = new PersistentStorage("SD");
            sd.MountFileSystem();

            string fileName = "\\SD\\app.hex";

            FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read);

            // start update
            SystemUpdate.ApplicationUpdate.Start();

            // read the file in chucks
            byte[] buffer = new byte[10 * 1024];
            int length;

            do
            {
                length = file.Read(buffer, 0, buffer.Length);
                SystemUpdate.ApplicationUpdate.Write(buffer, 0, length);

            } while (length == buffer.Length);

            file.Close();

            // End update
            SystemUpdate.ApplicationUpdate.End();

            // Access the application
            SystemUpdate.AccessApplication();


        }

    }
}

As fas as I can see from the code, you didn’t activate the update partition. On first run you have to split up the flash in two partitions, one for the IFU and one for the app. I’ll post some code with one or two hours on how to do that.

As a followup to my last post, try this example. It checks for an IFU partition, if present it starts your app otherwise it creates an IFU partition, reboots and start your app.

Be aware you have to deploy your app twice and you have to deploy your update software (as you posted above) to the IFU partition. I hope you get it done, it’s a really nice and useful feature.

 void Main()
        {
      
            switch (SystemUpdate.GetMode())
            {
                case SystemUpdate.SystemUpdateMode.NonFormatted: // there is no IFU partition
                    SystemUpdate.EnableBootloader(); // creates IFU partition 
                    break;
                case SystemUpdate.SystemUpdateMode.Bootloader:
                    throw new Exception("Invalid Boot Mode!");
                case SystemUpdate.SystemUpdateMode.Application:
                    //  your app startup code goes here
            }
        }

@ Patrick -

Thank for your answer, I have also refered anther post(Link http://www.tinyclr.com/forum/topic?id=2811&page=1#msg27612), CarlJ has also written the same codes as yours.
I have tried the codes and I have sever questions.

Question 1:
How can I know that the Updata File is updated from SD card or USB stick?
If I want to updata from USB stick, how can I define?
If I want to updata from SD card, how can I define?

Question 2:
Which file should I put into SD card or USB stick, before I run the codes?

Question 3:
Before running the codes I have copied the files “CLR.HEX”, “CLR2.HEX”, “Config.HEX”, “TinyBooter.GHI” and “app.hex”. The “app.hex” file comes from a project file andthe project is about making leds on IO Pins blink. I convered the project file to “app.hex” with “MFDeploy”. Am I right for the preparation?
I have tried the codes twice as you said me to be aware, and I pushed the Up button on Cobra. I can see the information in “Output” (see picture)

What is “Switching to Bootloader Mode”, I have tried pushed “down” and “select” buttons on Cobra, am I right? but I saw nothing happened on my board.

something wrong for me? How can I do it correctly?

I look forward to your answer.

The codes that I have tried is as follow:


using System;
using System.IO;
using System.Threading;

using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.System;
using GHIElectronics.NETMF.Hardware;

namespace IFU_Test
{
    public class Program
    {
        public static void Main()
        {

            // Handle Boot Modes
            switch (SystemUpdate.GetMode())
            {
                case SystemUpdate.SystemUpdateMode.NonFormatted:
                    Debug.Print("Currently in NonFormatted Mode -- Enabling Bootloader  -- Reboot Necessary");
                    SystemUpdate.EnableBootloader();  //  System Reboots
                    break;
                case SystemUpdate.SystemUpdateMode.Bootloader:
                    throw new InvalidOperationException("Currently in Bootloader Mode.  Should be in application mode!");
                case SystemUpdate.SystemUpdateMode.Application:
                    Debug.Print("Currently in Application Mode");
 
                    // set up the UP button to do the system update
                    InterruptPort UP_Button = new InterruptPort((Cpu.Pin)EMX.Pin.IO4, false, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLow);
                   UP_Button.OnInterrupt += new NativeEventHandler(upButton_OnInterrupt);
 
                    Debug.Print("Normal application workings here...");
 
                    Debug.Print("Update will be performed when Cobra UP Button is pushed...");
 
                    Thread.Sleep(Timeout.Infinite);
                    break;
            }
        }

            
        
        static void upButton_OnInterrupt(uint data1, uint data2, DateTime time)
        {
            Debug.Print("The UP Button was pressed - Switching to Bootloader Mode");
 
            // Update the system using bootloader
            SystemUpdate.AccessBootloader();
        }

        }

}