Go Down

Topic: Flashing a sketch from SD card using 2boot bootloader (Read 43994 times) previous topic - next topic


Apr 04, 2012, 01:32 pm Last Edit: Apr 04, 2012, 01:34 pm by martin_bg Reason: 1

I have been trying out this SD card bootloader


but was unable to get it working with the SD card part, the normal sketch upload over Arduino's IDE works fine but not the flashing from SD card.

Can someone else please try it and confirm? It may be me doing it wrong.. :)

I am testing it with "Arduino Ethernet", WIZnet CS is on Digital 4; Programmer is USBTiny

I am using a modified accordingly flash.sh to upload 2boots-arduino-atmega328p-16000000L-PD4.hex


Which file system is on the SD card?  2boots needs FAT16.


Apr 05, 2012, 02:54 pm Last Edit: Apr 05, 2012, 05:47 pm by osbock Reason: 1
Martin thanks for starting this topic and pointing out 2boots!
I have also been having difficulty getting it to work. My platform has a different chip select, so I added the appropriate targets. The serial part of the bootloader works fine, but it never loads anything from SD. I've tried both 1GB and 2GB cards, formatting from Windows 7 explorer, Storage Manager, and on Linux using mkdosfs -F16, and the SD association's official formatter.

I dug in a little (I had to comment out the stk500v1 code to have room for debugging, I suppose I could have changed the linker arguments...)
and it's failing in this format test:
Code: [Select]
if ((vbr->bsFileSysType[0] != 'F') || (vbr->bsFileSysType[4] != '6')){
  return 2; // No FAT16 found
The actual bytes in both those fields are zero.
Seems like it's sensitive to format, and that could be improved. After browsing the wikipedia article on the FAT "standard" I can see how that might be challenging!
Can anyone recommend a  format utility or method (such as a dd-able image) that I can at least try this?


Here's a little more info on my card (as currently formatted, I tried a canon powershot this time.)
(parted) print
Model: SD SU01G (sd/mmc)
Disk /dev/mmcblk0: 988MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End    Size   Type     File system  Flags
1      69.1kB  988MB  988MB  primary  fat16


Apr 05, 2012, 05:51 pm Last Edit: Apr 05, 2012, 06:54 pm by martin_bg Reason: 1
I am playing with http://www.partitionwizard.com/ and trying to ensure the conditions you mention below are met; It allows me to set the file system type to 6, I am not sure about the 'F' , probably it is "F"AT ?


Apr 05, 2012, 06:52 pm Last Edit: Apr 05, 2012, 06:55 pm by martin_bg Reason: 1
Can you try to re-compile the code with that section commented out?

Code: [Select]

if ((vbr->bsFileSysType[0] != 'F') || (vbr->bsFileSysType[4] != '6')){
  return 2; // No FAT16 found <<<<<<<<<<<<<< REMOVE TO TEST

From what I see here:

   uint8_t         bsFileSysType[8];    // 54-61 Filesystem type (E.g. "FAT12   ", "FAT16   ", "FAT     ", or all zero.)

obviously that line is looking for a string 'FAT16' (positions 0 and 4 must be 'F' and '6' respectively) and not finding it, probably something that today's format tools omit?? strange indeed.


Apr 05, 2012, 07:00 pm Last Edit: Apr 05, 2012, 07:03 pm by osbock Reason: 1
I did try removing it, and it failed somewhere further downstream. I'll have to poke some more
Here's a hex dump of the beginning of my card (formatted by a camera) The string is there, but probably in a different place...

00000000  eb 3e 90 50 77 72 53 68  6f 74 20 00 02 20 01 00  |.>.PwrShot .. ..|
00000010  02 00 02 00 00 f8 ec 00  3f 00 20 00 87 00 00 00  |........?. .....|
00000020  79 73 1d 00 80 00 29 ca  21 c5 50 43 41 4e 4f 4e  |ys....).!.PCANON|
00000030  5f 44 43 20 20 20 46 41  54 31 36 20 20 20 33 ff  |_DC   FAT16   3.|
00000040  8e df be 00 7c 8d 9c e4  01 8e 47 02 fc b9 00 02  |....|.....G.....|
00000050  f3 a4 c7 07 58 00 ff 2f  8c c8 fa 8e d0 bc 00 06  |....X../........|
00000060  fb 8b ec 83 ec 16 c5 36  78 00 89 76 f6 8c 5e f8  |.......6x..v..^.|
00000070  8d 7e ea b9 0b 00 57 f3  a4 5f 8e d9 be 78 00 89  |.~....W.._...x..|


Thanks for that Martin,
I got the SDformatter to work, but no change in results from 2boots.
the SDfat library hasn't been converted to 1.0 yet, so I used Arduino-022
make sure you change:
const uint8_t chipSelect = SS_PIN;
to whatever your chip select is.
I also remembered that one of the current SD examples says that you have to set the SS pin to output even if you aren't using it:

I'll try to walk through the 2boots fat code tonight... (I should really get back to work!)


I am checking the revision history of mmc_fat.c

and this one may be a tip:

The description reads "wait longer for cards to answer. Might solve problems at 16mhz."

Maybe the bootloader was originally designed for a 8Mhz speed and timing issues could be the root of the problem.


I've messed around with delays, but haven't gotten one that works. The MMC card isn't replying properly, so the routine to read the boot sector returns all zeros. I have pulled all the mmc_fat  code out into a sketch so I can do prints, etc.

My next step (though I haven't had much time) is to try to adapt the code from the SD library.


I tried it on an 8mhz arduino pro (sparkfun) and it still fails. I confirmed the sd library (and sdfat) work fine.


As this has obviously stalled,

here is another similar project using Petit-FatFs, theck out the sample project download link on the bottom of the page, inside there is a "AVR BOOT" example. 


I don't have the experience to deal with bootloader modifications yet, but that one seems pretty straighforward and only requires adjustment of the makefile to work on atmega328

Disadvantages are that it is a 4K bootloader and lacks the option to upload a sketch over the FTDI, but hey: I can live with that.


Apr 30, 2012, 05:18 am Last Edit: May 02, 2012, 07:57 pm by osbock Reason: 1
Thanks Martin,
That works.
You need to also change asmfunc.s for the SPI pin assignment. Here are the settings I used for the sparkfun micro-sd card:
#define DDR_CS  _SFR_IO_ADDR(DDRB), 0   // MMC CS pin (DDR, PORT)

#define DDR_CK  _SFR_IO_ADDR(DDRB), 5   // MMC SCLK pin (DDR, PORT)

#define DDR_DI  _SFR_IO_ADDR(DDRB), 3   // MMC DI pin (DDR, PORT)

#define PIN_DO  _SFR_IO_ADDR(PINB), 4   // MMC DO pin (PIN, PORT)

The CS pin is the one that changes with different shields.

also, to convert the sketch output to binary:
avr-objcopy -I elf32-avr -O binary BlinkWithoutDelay.cpp.elf app.bin
(note, originally this post had a typo with hex instead of elf...)

Also, you need to change the boot fuses to accomodate the 4k bootloader:

-U lfuse:w:0xff:m -U hfuse:w:0xd8:m -U efuse:w:0xfd:m

Go Up