Thursday, November 15, 2012

blinky and printf on netduino using YAGARTO

Aim

To run simple C programs on netduino using YAGARTO+Eclipse on Windows.

Software Development Tools

  1. A Windows PC with 3 free USB ports
    I used Windows 7 Home Premium 64 bit SP1
  2. Eclipse IDE for Automotive Software Developers (32 bit for Windows)
    download link | I used juno SR1 | This version includes CDT
  3. Atmel SAM-BA for Windows
    download link | I used 2.12 (sam-ba_2.12.exe)
  4. YAGARTO GNU ARM toolchain (for Windows)
    download link | I used 13.10.2012 (yagarto-bu-2.22_gcc-4.7.2-c-c++_nl-1.20.0_gdb-7.5_eabi_20121013.exe)
  5. YAGARTO Tools
    download link | I used 18.10.2012 (yagarto-tools-20121018-setup.exe)

Hardware

  1. One no. netduino
    buy link | I used the plain one (not plus or go)
  2. Two nos. FTDI Basic Breakout - 3.3V
  3. Four nos. LEDs
    buy link | I used 3mm ones
  4. Four nos. 1Kilo-ohm resistors (current limiting for the above 4 LEDs)
    I used 1/4 watt through hole ones
  5. Two nos. Potentiometers
    buy link | I used 10 kilohom linear ones 
  6. One no. Tactile Switch


Circuit used for developing simple programs for netduino using YAGARTO+Eclipse
The tactile switch is used to apply 3.3V to the ERASE pad of netduino when required
(FZZ and PDF source for the above diagram is available from the github repository)

Instructions


  1. Install FT232RL Driver if you haven't already have them installed. (get those from here)
  2. Install SAM-BA
  3. Install YAGARTO GNU ARM toolchain
  4. Install YAGARTO Tools
    (run "arm-none-eabi-gcc -v" in a command window to ensure that gcc for ARM is installed correctly)
  5. Install Eclipse by extracting it to a folder on your hard disk (usually C:\eclipse) and create a shortcut to eclipse.exe on your desktop.
  6. Create a new folder for maintaining the Eclipse Workspace.
    (I created mine here: C:\Users\Anurag Chugh\Documents\Eclipse_yagarto)
  7. Start Eclipse and point to this folder as the workspace and click OK and go to the workbench.
  8. Switch to "C/C++ Projects" view (Window > Show View)
  9. Right click in "Project Explorer" and select New > C Project.
  10. Enter the project name as "blinky" and select "Empty Project" under "Makefile Project". Select toolchain as Cross GCC and click Finish.
  11. Use Windows Explorer and create the following structure in your "blinky" project directory populating it with the files from the archive here.

  12. Go back to eclipse and click on "blinky" and press the F5 key. The folder structure will appear there. You can double click on the various source files and look at the source code contained in them.
  13. Right click on "blinky" and select "Build Project" and .bin file will be generated which you can now program into netduino's program flash memory and watch the LED Blink.

Flashing the netduino:

AT91SAM7X512 's memory is programmed using Atmel's SAM-BA software. This software can we be used with Atmel's SAM-ICE JTAG Emulator or Segger's J-Link JTAG Emulator - but these hardware tools require special connector to be present on the target board. In our case, such a connector is not present and so we must resort to one of the two remaining ways of programming the AT91SAM7X512's memory: over USB or over Debug UART:

Method 1 - Programming over USB

  1. Power-up and erase the microcontroller - Connect the netduino board to your computer to power it up via its microUSB connector. Apply 3.3V momentarily to the ERASE pad of the netduino. You may do this using a single strand wire between the ERASE pad and the 3V3 pin. The contents of microcontroller's flash have now been erased. (In the circuit diagram above, note that I am using a tactile switch to apply 3.3V to the ERASE pad)
  2. Unplug and plug the netduino board from the computer. Windows will fail to find an appropriate driver for netduino.
  3. Load the correct driver for netduino - Right click on My Computer and select Manage. Switch to Device Manager. Scroll down and you will see an "Unknown Device" corresponding to netduino. Right click it and select "Update Driver Software". Manually browse to the folder named "drv" inside the location where you had installed SAM-BA (in my case this was: C:\Program Files (x86)\Atmel\sam-ba_2.12\drv) and load the driver from there. Windows will complain about the driver not being signed, but you must proceed to load the software anyway.
  4. netduino will now be enumerated as a virtual serial port. Verify this in the device manager and note the COM port number.
    netduino appears as an Unknown Device when first plugged in

    netduino driver successfully installed
  5. After successful installation, netduino appears as a USB to Serial convertor.
    Make sure to note the COM port number.
  6. With the netduino board still connected, fire up SAM-BA and from the "Select the connection" dropdown list select the appropriate COM port corresponding to the "AT91 USB to Serial Converter" as noted from the Device Manager. This COM port will appear in the dropdown list prefixed with "\USBserial\".
    Starting SAM-BA
  7. From the "Select your board" dropdown list, select "at91sam7x512-ek". Make sure to select this correct entry from the dropdown list, it is easy to confuse it with some other entry. Press the Connect button once done.
  8. Many flash programming software require the input file in .bin format, while others require it to be in the Intel HEX (.hex) format. The makefile tells the compiler to generate both formats during compilation. SAM-BA requires the .bin format of the file. So after SAM-BA starts up, presss the folder icon next to the "Send File Name" textbox and browse over to your Eclipse workspace path and locate the file named blinky_rom.bin. In my case this file was located at "C:\Users\Anurag Chugh\Documents\Eclipse_yagarto\blinky\blinky_rom.bin" and click open.
  9. After the path appears in the "Send File Name" textbox, click on the "Send File" Button. This will cause SAM-BA to send commands to netduino and program its flash memory. Once it is done, SAM-BA will ask you if you want to lock the programmed  region of the flash memory. You may select either Yes or No as in our case we are not concerned with securing the contents of our flash memory.
  10. Once the flash memory is program, we need to configure the micrcontroller to Boot from Flash whenever it is reset. To do this, select "Boot from Flash (GPNVM2) Script" from the Scripts dropdown list and press Execute.
  11. Now you can unplug and plug the netduino. Once you do that, you will notice the LED flashing, indicating that your program is running successfully on your netduino.

Method 2 - Programming over USB

If you look at the circuit diagram presented to you at the beginning of the post, you will note that, there are two USB to Serial converter chips (FT232) that I have connected to a few pins (USART0 pins and DBGU pins) of the netduino. I use them to send and receive text to and from a computer. The DBGU (aka Debug UART) can also be used to program the netduino. To use DBGU port to program the netduino, follow these steps:

  1. Connect the FT232 connected to the DBGU pins to a spare USB port on the computer and make sure the drivers for FT232 are installed. Note the COM port number corresponding to this FT232 chip from the Device Manager.
  2. Power up the netduino using an external power supply - using the DC Jack or cell phone charger with a USB socket. Powering the netduino is essential to ensure that when it starts up, it does not receive any data over the USB port. 
  3. Erase the flash memory of netduino by applying 3.3 volts to the ERASE pad and cycle the power to netduino
  4. After ERASE and power up, the microcontroller's Boot Program will wait for USB enumeration by the computer, if that fails, it will switch to listening for commands over DBGU serial port. Since we have powered the netduino using an external supply, the microcontroller will default to listening for commands over Debug UART.
  5. At this point start up SAM-BA and select the COM port corresponding to FT232 chip connected to the DBGU port. From there on follow the same steps as specified in the previous method of programming the netduino over USB

Notes:

  1. The part number of the microcontroller present on board the plain version of the netduino is AT91SAM7X512. It is manufactured by Atmel.
  2. The microcontroller on board the netduino runs off of a 18.432MHz crystal.
  3. When select "Build Project" in Eclipse and compiler the program, you will notice that a lot of build artifacts are generated (files with the extensions .lst, .o, .bin, .map, .elf etc.. and a folder called .dep). You can remove these files by right clicking on the blinky project and selecting "Clean Project". The .bin file will also be deleted when you clean the project.
  4. Here is the description of all the files contained in the archive above that you had extracted to your blinky project folder:
    1. startup.s - Assembly language startup functions, executed the first thing after microcontroller is powered up. Configures the internal RAM and ROM. Configures settings for various ARM modes.
    2. low_level_init.c - After executing startup.s, the code in this file is executed. Clock registers are written to. Memory Controller is configured. Watchdog timer is configured.
    3. main.c - Contains the main() function. Actual code which makes the LED blink.
    4. AT91SAM7X512.h - Contains definitions for various hardware registers of the AT91SAM7X512 microcontroller. It allows us to read and write registers by their names - this file takes care of mapping those names to actual addresses.  This file is included in low_level_init.c and main.c. 
    5. makefile - A test file containing instructions (a.k.a recipies) for compiling and linking and generating the .bin file from the C and Assembly source files. This file is read by make.exe (part of YAGARTO tools) which in turn executes the compiler and linker as per the build recipes.
    6. sam7x512_rom.ld - This is the linker script file. Linking is the last step of compilation process. The script file specifies what sections (code, data etc..) are placed where in the memory. (More info here and here)
  5. Flashing netduino with the blinky program will erase its bootloader. You will need to restore the bootloader if you want to use it with Microsoft Visual C# Express 2010 again. To know how to restore the bootloader, watch this video.

Going Further:

  1. You can now delete the Eclipse workspace folder that you had created and instead use "GitHub for Windows" to clone the following repository (which is also an Eclipse Workspace) containing a few more simple C programs for netduino:
    http://github.com/lithiumhead/netduino_eclipse_yagarto
  2. You can try out FreeRTOS on your netduino.

0 comments:

Post a Comment