Friday 21 November 2008

HOWTO: Flash Lego Mindstorms NXT firmware in Linux

This is a howto for flashing custom firmware into your Lego mindstorms NXT brick using a linux development host, without having to use the official NXT labview host software, which is available for windows / mac only.

Update: The method below uses the Atmel SAM-BA tool for flashing. It is much easier (though less educational =) to use the libNXT utilities to flash your NXT brick from linux. This uses libusb and automagically finds your NXT when you connect it to your computer.



The following was done using debian linux (unstable @ 21 Nov 2008)
Disclaimer: You can easily screw up your NXT brick doing this! You will also lose any saved data on your NXT brick. You use this howto entirely at your own risk.

Note that linux command-line instructions below are shown in italics, with the root-prompt shown as '#' and a user-prompt shown as '$'
  1. You first need a firmware image for the NXT brick. See references for examples of this.
  2. Download the Atmel SAM-BA flashing tool for linux
  3. I unzipped this to /opt/atmel/sam-ba_cdc_2.8.linux_01 and add it to my path by placing a soft link in /usr/local/bin (# cd /usr/local/bin; ln -s /opt/atmel/sam-ba_cdc_2.8.linux_01/sam-ba_cdc_2.8.linux_01 sam-ba)
  4. Make sure that the sam-ba software has its executable flag set (# chmod +x /opt/atmel/sam-ba_cdc_2.8.linux_01/sam-ba_cdc_2.8.linux_01)
  5. Follow the instructions in the README.linux file: install the tcl / tk dependencies (I was installed the packages tk, tcl and tclx8.4 with aptitude)
  6. Boot the NXT brick in SAM-BA download mode first by switching on the NXT brick and then by holding the reset button on the back of the brick for over 4 seconds, as described on p74 of the Lego Mindstorms User Guide supplied with the NXT box set. You will hear the brick repeatedly making a clicking noise when successful. This may take a few attempts, depending on what firmware is residing in your NXT brick.
  7. Connect the NXT brick to your computer via USB and check that it has been correctly recognized using the 'lsusb' command. You can tell if this is successful, as you will see a device with the description 'Atmel Corp. at91sam SAMBA bootloader', not 'Lego Group'. If you see 'Lego Group', you did not successfully boot into SAM-BA download mode. Go back to step 6 if this is the case.
  8. Remove and reinsert the usbserial module with the relevant parameters, as in the readme (# modprobe -r usbserial; modprobe usbserial vendor=0x03eb product=0x6124)
  9. Check that the kernel has seen your NXT device by issuing the command 'dmesg'. You should see a line containing the text "generic converter now attached to ttyUSB0"
  10. Start the sam-ba utility (should be started with just $ sam-ba if you followed the earlier installation steps)
  11. Select the "AT91SAM7S256-EK" board from the dropdown list. The tty will probably be autodetected, but make sure it is the same as the one from the earlier dmesg command.
  12. In the "flash" tab, make sure that the address is 0x100000 (1 + 6 zeros). This is the location of the 256kbyte flash memory in the AT91SAM7S256 uController.
  13. Select the firmware file that you want to put into flash in the "Send file name" text box. In my case this was "/var/tmp/nxt-lua-beta-16a/nxt-lua-Beta-16a.rfw" Hit the "Send File" button. If the NXT has stopped clicking, it has turned itself off, and you will have to press the reset button and start over from step 6.
  14. When prompted about unlocking / locking flash sectors, reply "yes" both times. This is just a warning to check you are OK about reflashing the device.
  15. It would be nice to then check the data flashed with the "Compare sent file with memory" button. I was unable to get this to work properly however.
  16. When you are finished, unplug the NXT brick from USB and press the reset button on the back on the NXT for a short duration (less than 4 seconds). Your custom firmware should now be flashed. You know when you have reset the brick correctly, because it stops clicking. Congratulations!
If you screw up, you should _probably_ be able to reset your NXT with the 4 second reset procedure, as described in step 6. I'm too tired to check the schematics / datasheet to see exactly how foolproof this is.

Further info:

2 comments:

xorly said...

hello, i decided to flash my NXT to 1.28 FW, i downloaded sam-ba etc. etc. and now, Iam on step
In the "flash" tab, make sure that the address is 0x100000 (1 + 6 zeros). This is the location of the 256kbyte flash memory in the AT91SAM7S256 uController.
what i the FLASH TAB??? where to check this value?
ma console output is -I- Waiting ...
-I- TCL platform : Linux
-I- SAM-BA OS : linux
-I- Search for device decription files in folder :
-I- /opt/Atmel/sam-ba_cdc_2.8.linux_01/sam-ba_cdc_2.8.linux_01/../lib/devices
-I- Reading AT91SAM9263.tcl
-I- Check the processor ID at address : 0xFFFFEE40
-I- Read chip ID : 0x00000000 at addr: 0xFFFFEE40
-I- Reading AT91SAM7S32.tcl
-I- Check the processor ID at address : 0xFFFFF240
-I- Read chip ID : 0x270D0940 at addr: 0xFFFFF240
-I- Found device : AT91SAM7S256 (Chip ID : 0x270D0940)
-I- Loading applet isp-flash-at91sam7s256.bin at address 0x202000

and my brick is still clicking, samaba freezed on dialog where is list of devices and connections and after some time is prints "Failed to initialize FLASH accesses and everithing stops. Any IDEA??

xorly said...

So libnxt had solve it, after some troubles I passed and new FW is loaded and OK, in the future, i'll have to read post from the title to the end, maybe I won't be scared from NXT which doesn't live.
THX for article...