Arduino Forum

Using Arduino => Programming Questions => Topic started by: martin_bg on Apr 04, 2012, 01:32 pm

Title: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 04, 2012, 01:32 pm
Hi,

I have been trying out this SD card bootloader

http://seili.net/weblog/2010/07/14/avr2boots-dual-bootloader-released/

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


Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: skyjumper on Apr 04, 2012, 06:37 pm
Which file system is on the SD card?  2boots needs FAT16.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 05, 2012, 09:09 am
Sure, it is FAT16, <2GB
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 05, 2012, 02:54 pm
Hi,
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]
mmc_start_read_block(mbr->sector.partition[0].sectorOffset);
 
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?
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 05, 2012, 04:08 pm
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
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 05, 2012, 05:51 pm
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 ?

Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 05, 2012, 06:52 pm
Can you try to re-compile the code with that section commented out?

Code: [Select]

mmc_start_read_block(mbr->sector.partition[0].sectorOffset);
 
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.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 05, 2012, 07:00 pm
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..|
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 05, 2012, 07:20 pm
BTW there is a sketch that formats the SD card, but didn't work form me.. no idea why

http://code.google.com/p/arisgames/source/browse/trunk/arduino/MP3_Player_Example/sdfatlib20110702/SdFat/examples/SdFormatter/SdFormatter.pde?spec=svn2144&r=2144
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 05, 2012, 08:47 pm
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:
pinMode(10,OUTPUT);

I'll try to walk through the 2boots fat code tonight... (I should really get back to work!)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 11, 2012, 06:56 am
I am checking the revision history of mmc_fat.c

and this one may be a tip:
https://github.com/thseiler/embedded/commit/57ff5ba7c13bc637eee4754ef205c792d7a37bfb

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.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 11, 2012, 12:59 pm
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.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 12, 2012, 02:27 am
I tried it on an 8mhz arduino pro (sparkfun) and it still fails. I confirmed the sd library (and sdfat) work fine.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 27, 2012, 09:53 am
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. 

http://elm-chan.org/fsw/ff/00index_p.html

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.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Apr 30, 2012, 05:18 am
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 PORT_CS _SFR_IO_ADDR(PORTB), 0

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

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

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

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
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: skyjumper on Apr 30, 2012, 10:10 am

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. 

http://elm-chan.org/fsw/ff/00index_p.html



I se the link to the FAT module, but where is the bootloader?
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on Apr 30, 2012, 12:21 pm
inside this archive:

http://elm-chan.org/fsw/ff/pfsample.zip


look in the AVR_BOOT folder
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 05, 2012, 11:57 pm
hi everyone!,

just a few days ago I also tried to use 2boots bootloader without success... This forum has been very helpfull to me in order to know that the problem is not only mine! I also tried to format the micro SD from Linux with gParted and fdisk. With os x with disk utility and sdFormatter (and app from the micro SD website). For windows I tried with the normal format app and the "format" terminal order. Each time i made a hexdump and I found that depending on SIZE and OS, the headder is different every time  (always respecting the FAT16 (0x06) format.

So, I give up, Im trying to move to petitFat as you recomend but, I really dont know wich value I have to put in the BOOT_ADR field. Im using an Atmega328p, do you happen to know wich config params I have to use?

Thanks!!!
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 06, 2012, 04:40 am
I haven't had time to write it up yet, but I can send you a zip with the code. What Chip select are you using for the SD card?
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 06, 2012, 05:09 am
Im using pin number 4 (ethernet shield for arduino) which is PD4 on the Atmega328p.
If you have the code, please send me it to: *********************** <-edited for privacy

THANKS!
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on May 06, 2012, 12:07 pm
osbock sent me a compiled hex for arduino ethernet (CS is digital 4) but it didn't work for me, here is the HEX so you give it a try as well.

http://dl.dropbox.com/u/18771283/ArduinoEthernetSD.hex (http://dl.dropbox.com/u/18771283/ArduinoEthernetSD.hex)

Don't forget that the sketch to be uploaded has to be in .BIN not .HEX and that this is a 4K bootloader so you need to change fuses accordingly.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 06, 2012, 01:51 pm
Here are the avrdude commands that worked for me. Unfortunately I don't have an arduino ethernet to test:

avrdude -c usbtiny -p m328p -e -u -U lock:w:0x3f:m -U efuse:w:0x05:m -U hfuse:w:0xD8:m -U lfuse:w:0xFF:m
avrdude  -c usbtiny -p m328p -U flash:w:ArduinoEthernetSD.hex -U lock:w:0x0f:m

of course change the -c option if you have a different programmer (and add baud and other things if you need em.)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on May 06, 2012, 04:01 pm

I really dont know wich value I have to put in the BOOT_ADR field. Im using an Atmega328p


I use
Code: [Select]


MCU_TARGET  = atmega328p    # Target device to be used (32K or lager)
BOOT_ADR    = 0x3800 # Boot loader start address [byte]
F_CPU       = 16000000 # CPU clock frequency [Hz]



but still doesn't work. I guess the arduino ethernet is more picky..
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 06, 2012, 10:49 pm
It still not working...

Let me sumarize the steps I did:
1st: change asmfunc.S so I have:

#define   DDR_CS   _SFR_IO_ADDR(DDRD), 4   // MMC CS pin (DDR, PORT)
#define   PORT_CS   _SFR_IO_ADDR(PORTD), 4

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

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

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

2nd: change the makefile to:

MCU_TARGET  = atmega328p   # Target device to be used (32K or lager)
BOOT_ADR    = 0x7000   # Boot loader start address [byte]
F_CPU       = 16000000   # CPU clock frequency [Hz]

3th: compile and burn the hex file with:

avrdude -c avrisp -p m328p -P /dev/tty.usbmodem12341 -b 19200 -e -u -U lock:w:0x3f:m -U efuse:w:0x05:m -U hfuse:w:0xD8:m -U lfuse:w:0xFF:m
avrdude -c avrisp -p m328p -P /dev/tty.usbmodem12341 -b 19200 -U flash:w:avr_boot.hex -U lock:w:0x0f:m

4th: compile some app (the blink one) and take the .elf file and do:

avr-objcopy -I elf32-avr -O binary Blink.cpp.elf app.bin

5th and final:
burn that bin file to my 2GB FAT32 uSD and try it, but is not working, the led is not blinking  :(
*I also tried with FAT16

Am I missing something?????
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 07, 2012, 12:52 am
ok, I think that I found what is wrong, but I dont know how to fix it...

As you can see on: http://arduino.cc/en/Main/ArduinoEthernetShield

"Note that because the W5100 and SD card share the SPI bus, only one can be active at a time. If you are using both peripherals in your program, this should be taken care of by the corresponding libraries. If you're not using one of the peripherals in your program, however, you'll need to explicitly deselect it. To do this with the SD card, set pin 4 as an output and write a high to it. For the W5100, set digital pin 10 as a high output. "

So, I should set PORTB 2 as high output In that assembler asmfunc.S.... But I dont know how! Does anybody knows how?

Thanks!!

UPDATE: I finally did it! for the people who have arduino ethernet, change the mmc.c file by: ( I just added a few lines before the call of init_spi() and the defines and includes). If anybody can do the same in the assembler file, it would be better.

/*-----------------------------------------------------------------------/
/  PFF - Generic low level disk control module            (C)ChaN, 2010
/------------------------------------------------------------------------/
/
/  Copyright (C) 2010, ChaN, all right reserved.
/
/ * This software is a free software and there is NO WARRANTY.
/ * No restriction on use. You can use, modify and redistribute it for
/   personal, non-profit or commercial products UNDER YOUR RESPONSIBILITY.
/ * Redistributions of source code must retain the above copyright notice.
/
/------------------------------------------------------------------------/
/* Dec 6, 2010  First release
*/

#include "pff.h"
#include "diskio.h"
#include <avr/io.h>

void init_spi (void);      /* Initialize SPI port (asmfunc.S) */
void deselect (void);      /* Select MMC (asmfunc.S) */
void select (void);         /* Deselect MMC (asmfunc.S) */
void xmit_spi (BYTE d);      /* Send a byte to the MMC (asmfunc.S) */
BYTE rcv_spi (void);      /* Send a 0xFF to the MMC and get the received byte (asmfunc.S) */
void dly_100us (void);      /* Delay 100 microseconds (asmfunc.S) */



/*--------------------------------------------------------------------------

   Module Private Functions

---------------------------------------------------------------------------*/

#define SPI_PORT PORTB
#define SPI_DDR  DDRB
#define SPI_SS   PB2

/* Definitions for MMC/SDC command */
#define CMD0   (0x40+0)   /* GO_IDLE_STATE */
#define CMD1   (0x40+1)   /* SEND_OP_COND (MMC) */
#define   ACMD41   (0xC0+41)   /* SEND_OP_COND (SDC) */
#define CMD8   (0x40+8)   /* SEND_IF_COND */
#define CMD16   (0x40+16)   /* SET_BLOCKLEN */
#define CMD17   (0x40+17)   /* READ_SINGLE_BLOCK */
#define CMD24   (0x40+24)   /* WRITE_BLOCK */
#define CMD55   (0x40+55)   /* APP_CMD */
#define CMD58   (0x40+58)   /* READ_OCR */


/* Card type flags (CardType) */
#define CT_MMC            0x01   /* MMC ver 3 */
#define CT_SD1            0x02   /* SD ver 1 */
#define CT_SD2            0x04   /* SD ver 2 */
#define CT_BLOCK         0x08   /* Block addressing */


static
BYTE CardType;


/*-----------------------------------------------------------------------*/
/* Send a command packet to MMC                                          */
/*-----------------------------------------------------------------------*/

static
BYTE send_cmd (
   BYTE cmd,      /* 1st byte (Start + Index) */
   DWORD arg      /* Argument (32 bits) */
)
{
   BYTE n, res;


   if (cmd & 0x80) {   /* ACMD<n> is the command sequense of CMD55-CMD<n> */
      cmd &= 0x7F;
      res = send_cmd(CMD55, 0);
      if (res > 1) return res;
   }

   /* Select the card */
   select();

   /* Send a command packet */
   xmit_spi(cmd);                  /* Start + Command index */
   xmit_spi((BYTE)(arg >> 24));      /* Argument[31..24] */
   xmit_spi((BYTE)(arg >> 16));      /* Argument[23..16] */
   xmit_spi((BYTE)(arg >> 8));         /* Argument[15..8] */
   xmit_spi((BYTE)arg);            /* Argument[7..0] */
   n = 0x01;                     /* Dummy CRC + Stop */
   if (cmd == CMD0) n = 0x95;         /* Valid CRC for CMD0(0) */
   if (cmd == CMD8) n = 0x87;         /* Valid CRC for CMD8(0x1AA) */
   xmit_spi(n);

   /* Receive a command response */
   n = 10;                        /* Wait for a valid response in timeout of 10 attempts */
   do {
      res = rcv_spi();
   } while ((res & 0x80) && --n);

   return res;         /* Return with the response value */
}




/*--------------------------------------------------------------------------

   Public Functions

---------------------------------------------------------------------------*/

/*-----------------------------------------------------------------------*/
/* Initialize Disk Drive                                                 */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (void)
{
   BYTE n, cmd, ty, ocr[4];
   UINT tmr;

 
  SPI_PORT |= 1<<SPI_SS;   //PB2 output: High (deselect other SPI chips)
   SPI_DDR  |= 1<<SPI_SS; // SPI Data -> Output
   
   init_spi();                     /* Initialize ports to control MMC */
   for (n = 100; n; n--) dly_100us();   /* 10ms delay */
   for (n = 10; n; n--) deselect();   /* 80 Dummy clocks with CS=H */

   ty = 0;
   if (send_cmd(CMD0, 0) == 1) {         /* Enter Idle state */
      if (send_cmd(CMD8, 0x1AA) == 1) {   /* SDv2 */
         for (n = 0; n < 4; n++) ocr[n] = rcv_spi();      /* Get trailing return value of R7 resp */
         if (ocr[2] == 0x01 && ocr[3] == 0xAA) {         /* The card can work at vdd range of 2.7-3.6V */
            for (tmr = 10000; tmr && send_cmd(ACMD41, 1UL << 30); tmr--) dly_100us();   /* Wait for leaving idle state (ACMD41 with HCS bit) */
            if (tmr && send_cmd(CMD58, 0) == 0) {      /* Check CCS bit in the OCR */
               for (n = 0; n < 4; n++) ocr[n] = rcv_spi();
               ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;   /* SDv2 (HC or SC) */
            }
         }
      } else {                     /* SDv1 or MMCv3 */
         if (send_cmd(ACMD41, 0) <= 1)    {
            ty = CT_SD1; cmd = ACMD41;   /* SDv1 */
         } else {
            ty = CT_MMC; cmd = CMD1;   /* MMCv3 */
         }
         for (tmr = 10000; tmr && send_cmd(cmd, 0); tmr--) dly_100us();   /* Wait for leaving idle state */
         if (!tmr || send_cmd(CMD16, 512) != 0)         /* Set R/W block length to 512 */
            ty = 0;
      }
   }
   CardType = ty;
   deselect();

   return ty ? 0 : STA_NOINIT;
}



/*-----------------------------------------------------------------------*/
/* Read partial sector                                                   */
/*-----------------------------------------------------------------------*/

DRESULT disk_readp (
   BYTE *buff,      /* Pointer to the read buffer (NULL:Read bytes are forwarded to the stream) */
   DWORD lba,      /* Sector number (LBA) */
   WORD ofs,      /* Byte offset to read from (0..511) */
   WORD cnt      /* Number of bytes to read (ofs + cnt mus be <= 512) */
)
{
   DRESULT res;
   BYTE rc;
   WORD bc;


   if (!(CardType & CT_BLOCK)) lba *= 512;      /* Convert to byte address if needed */

   res = RES_ERROR;
   if (send_cmd(CMD17, lba) == 0) {      /* READ_SINGLE_BLOCK */

      bc = 40000;
      do {                     /* Wait for data packet */
         rc = rcv_spi();
      } while (rc == 0xFF && --bc);

      if (rc == 0xFE) {            /* A data packet arrived */
         bc = 514 - ofs - cnt;

         /* Skip leading bytes */
         if (ofs) {
            do rcv_spi(); while (--ofs);
         }

         /* Receive a part of the sector */
         do {
            *buff++ = rcv_spi();
         } while (--cnt);

         /* Skip trailing bytes and CRC */
         do rcv_spi(); while (--bc);

         res = RES_OK;
      }
   }

   deselect();

   return res;
}

Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on May 07, 2012, 09:02 am
I got it working with the modifications suggested by eudaldcp. Thanks!

Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 08, 2012, 04:55 pm
I was on a similar track, but I don't have the ethernet, so can't test. I think I know where to change it in asmfunc. I changed it, and if someone could test, I'll put a fork up on github so there'll be an arduino friendly version for folks.

http://baldwisdom.com/data/ArduinoEthernetSDBoot.hex

And the hacked asmfunc.S:
http://baldwisdom.com/data/asmfunc.S
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 08, 2012, 07:17 pm
well done osbok! it works with Arduino ethernet
thanks for your contribution!!
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 08, 2012, 07:26 pm
I put the code up on github. I need to add more doc, improve the makefile, etc but it's there for use now.

https://github.com/osbock/avr_boot

Note: The original author at elm-chan.org published this under a very liberal open source license, so this is legit.
I hope to find his/her actual name so I can at least give credit.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on May 08, 2012, 07:31 pm
I also forked the code on Github https://github.com/mharizanov/avr_boot

My modifications are:

1) only re-flash if the sketch has changed. the original bootloader flashes everytime, this is not so good.
2) to achieve the above, I discarded FAT12 compatibility (who uses fat12 anyway)

[edit] for the future I plan that the bootloade reads the firmware name from EEPROM (default will still be "app.bin") so that will allow the applicatiom to re-flash with needed portions by itself.. neat
Also, I will make .HEX compatibility since the .BIN is additional step that isn't really needed
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 08, 2012, 07:45 pm
Do you want to work  together on this?
I have code for doing the hex file (from my bootdrive project) but I'm wondering if it's worth the space. It's only a small simple command to convert it.

I was going to try to squeeze it down to either:
1. get it out of the 4k bootloader zone (into at least the 2k zone)
2. add back serial programming. The Optiboot code is very small and should be easy to integrate (I ported it into my bootdrive code)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on May 08, 2012, 07:58 pm
Yes, that would be great. I have doubts about the 2K target, I'd rather focus on fixing the 2boots if we go in that direction. I guess it is something really small that is missing to get it working.

My ideal bootloader will have these features, in order of importance:

* SD card flashing (only if new file is uploaded)
* EEPROM based firmware file name
* Serial programming
* .HEX rather than .BIN
* 2K

With the EEPROM based firmware file name, the application may switch itself to a TFTP sketch that will run for 20 minutes and revert back to the original application. The TFTP will wait for incomming connection and if found will save the uploaded file to the SD card (new firmare)..
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 08, 2012, 07:59 pm
i also have the code for loading from eeprom EDIT: I mean: the code for reading the filename from eeprom

:D
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 08, 2012, 08:03 pm
Cool. Can we agree to use one github? We can all be contributors, or we could designate one master and do forks/pulls.

Martin, I like the load only if new, and would incorporate that right away. The LED thing should be under ifdef with some defines to make it board dependent.
I have an idea how to handle this in the makefile (similar to 2boots).
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: martin_bg on May 08, 2012, 08:11 pm
I think it will be best to do it this way. As I am less experienced, I suggest you take the lead.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 08, 2012, 08:12 pm
Hi guys,
I agree to make a colaborative github, also, I would like to give an other chance to the 2boots code. I sent a mail to the main developer of 2boots explaining our problems. I still waiting for an answer...

But 2boots is based in another code => https://www.mikrocontroller.net/topic/67047 and this one might work for FAT16, if works, we will have 2K extra for including all this stuf!!
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 08, 2012, 08:20 pm
This is great.
Martin, I added you as a collaborator to
https://github.com/osbock/avr_boot

Eudaldcp, what's your github id?

I spent a lot of time on 2boots, and actually got about 50% done rewriting it using the sdfat library code. It was really hard to debug. I'm hoping that changes when I get the Dragon I ordered (allows breakpoints.)

hold off on putbacks to the repository until I branch (to preserve the current working version.)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: eudaldcp on May 08, 2012, 08:24 pm
Ive just created an account. My ID in github is: eudaldcp :)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on May 08, 2012, 08:30 pm
OK, you are added. WE can probably take this offline to github, unless they are general ideas you feel others would be interested.
I created the v.1 branch which nobody should touch (putback to master)
Later today I'll draft some groundrules. Basically if you are confident with your change, you can put it back directly. If you want a review, it is best to fork the repository, and then do a pull request.

Also, if you have "application specific features" that aren't of general use, you should probably just put those in your own fork. You can have as many as you want.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: niki77 on Aug 22, 2012, 05:53 pm
Hi to everyone, interesting thread.

I've a lot of problems to let it working.
I'm using an arduino ethernet board
4gb Microsd sdhc, partitioned to only 2gb fat16 formatted.

I've dowload the code from here :https://github.com/osbock/avr_boot/downloads

I've compiled and flash fw into my board making only little modifications at the file provided then i've put a app.bin sketch file named in the root of sdcard.
Nothing happen, after reset, app.bin fw not loaded, i can see only the board led (in my board connected to pin9) always on.

Any suggestion?
Thanks in advance.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Aug 22, 2012, 06:54 pm
Did you set the chipselect?
It's been a while since I tried this and some folks have made some changes. I'll try to look and see if I can see anything obvious. Unfortunately I don't have an arduino ethernet to test
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: niki77 on Aug 22, 2012, 09:48 pm
No i've not  set any chipselect.

For what Board that fw is ready TO go?
It's possibile that problem come from sdhc card?
Thanks again tor your time.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: osbock on Aug 22, 2012, 09:53 pm
By default it's set up for Arduino Ethernet (someone else tested it, though I emulated it with a specially wired sd holder)
I think your problem might be that it is looking for the program name in eeprom. It is supposed to default to app.bin, but I'm not sure that's tested.

You are trying to load a binary file right? this firmware doesn't read hex files.
This was a feature that someone wanted, but I think I'm going to revert back to a fixed filename. Your program could always rename another  program to app.bin .
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: niki77 on Aug 22, 2012, 10:35 pm
Sure i try TO upload a bin file, not an hex .

I thought that this bootloader does not read filename from eprom but it is already made fixed to app.bin.
I need to  see  deep the code...
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: niki77 on Aug 23, 2012, 09:37 am
Little update:

i've  let it working flashing directly this hex file for bootloader : http://baldwisdom.com/data/ArduinoEthernetSDBoot.hex

In wich is different from the code on github?

Thanks again osbock.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: mrjonny2 on Mar 27, 2013, 06:42 pm
Hi, I am very interested in this project and I would like to get involved.  In addition make it compatible with the 2560.  Can someone tell me where it currently stands what the latest builds are etc?
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: travelvietnam on Mar 27, 2013, 09:20 pm
I haven't had time to write it up yet, but I can send you a zip with the code. What Chip select are you using for the SD card?
_________________________
Unique Mui Ne Resort (http://www.hotels-in-vietnam.com/asia/vietnam/hotels_phanthiet/mui_ne_unique_resort.html)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: mrjonny2 on Mar 27, 2013, 09:43 pm
I am using a 2560v running at 8Mhz so the whole board is running at 3.3v so I have no level shifter.  The SD card is connected directly to the SPI lines.

https://www.sparkfun.com/products/10744 (https://www.sparkfun.com/products/10744)
My board is one of these with an W5200 Ethernet module attached. (Same way as the W5100)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: fat16lib on Apr 03, 2013, 05:37 pm
Over a year ago there was interest in a very small SD read program as the base for a boot loader.  I wrote the attached program which may be useful.

It is FAT16 only for standard (2GB or less) SD cards.

I estimate the size is about 1200 bytes.

It has two functions
Code: [Select]

bool fileOpen(uint8_t* dirName);
int fileRead();

dirName is the filename in directory format, 11 bytes with blank fill like this.
Code: [Select]

  // open TEST.BIN
  if (!fileOpen((uint8_t*)"TEST    BIN")) {
    // open failed
  }

fileRead() reads a single byte or returns -1 for error or EOF.

The programs are packaged as a library.  Try the examples.

You must be logged in to download the attached zip file.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: mrjonny2 on Apr 04, 2013, 01:08 am
My goal is to place a sketch on the SD Card and run program that sketch to the chip itself or run the sketch off the chip.  travelvietnam I am using pin 53 as chip select for the SD
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: snirni on May 11, 2013, 01:52 pm
hello everyone,
i'm trying to do something similar to this by uploading a sketch from an external memory.
my goal is to rewrite the bootloader that started in this post, with osbock, and make it first compile under atmel studio (i hade to change some global variables to constants...).

finally i've succeeded compiling that bootloader under atmel studio and now i have a few  quwstions please:

1) this bootloader can't upload .hex file at all and specifically from the arduino ide, correct? if so why not?
2) if i want that bootloader to upload only sketches with the correct name (app.bin or lets say app.hex) will it check for changes? on restart and program the sketch only if it's new? if not why?
3) if i won't to do the same thing but from external flash chip, on a breadboard and make the bootloader check whether there is a "new" file on the uSD or ext. Flash, what changes are needed? any reference?

4) what are the correct numbers for this bootloader on arduino mega 2560 with sparkfun micro-sd shield (not eth') (speed, legs CS etc, memory addresses... etc)

thank you,
snir.
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: desmondttm123 on May 22, 2013, 03:58 pm
hello, where is the current stand of this project?
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: mrjonny2 on May 26, 2013, 06:41 pm
I think the project has somewhat stalled.  I have tried to message the original contributors with no results.  :(
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: GregM on Jul 09, 2013, 10:00 pm
What a pity this has stalled. Just what I was looking for :(
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: willie1968 on Sep 18, 2013, 12:17 pm

What a pity this has stalled. Just what I was looking for :(

I think, it was stalled, but then i found this.
The thread owner has found a way to make it work.
http://harizanov.com/2013/01/2boots-bootloader-on-the-uiot-serial-mmc-bootloader-in-2kb/
His changes are on his own github repository...
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: zbeny on May 21, 2014, 12:05 am
Hi, I am serching for some info about this quite a long time. And still I am not sure which project is actualy working or how to use it. Could somebody help me, give me some clues and summarize the situation. thx
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: willie1968 on Jun 16, 2014, 10:37 am
Hi,

because of the problems (only FAT16 and endless loop with FAT32 sd card in on boot) with the 2boots bootloader, i switched to the avr_boot. (https://github.com/osbock/avr_boot)
I have made some changes for my project.
The main differences between 2boot and avr_boot:


my changes: (you can find it in https://github.com/willie68/OpenSeaMapLogger/tree/master/bootloader/avr_boot-master)
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: jmFischer on Jul 03, 2014, 11:51 am
Hello forum,

I finally managed to write the bootloader to my atmega 328p (with some help of a friend to compile it) and ran into the next problem. what format the file on the sd card must have? what must i do with the .ino file to make the thing work.

Thanks in advance for your answers, Michael
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: willie1968 on Jul 23, 2014, 12:14 pm
Which bootloader?
Some prefer hex files (which the arduino IDE already generate, look into the build folder in the temp folder.), my avr_boot needs a binary file, so i must convert the hex file into a binary version...
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: garfield38 on Dec 01, 2014, 12:37 pm
Hi all,

just a short message to thanks everybody that contribute to release avr_boot.

After a long time and many tries, I find the right flow to be able to burn the new boot loader on an atmega328p dip28 and load the blink example project into the avr from a 512Mo SD card:

- get the github from
https://github.com/willie68/OpenSeaMapLogger/tree/master/bootloader/avr_boot-master

- check your hardware SD <-> Arduino with avr_boot-master/asmfunc.S

- make

- burn fuse
avrdude -p atmega328p -c usbasp -P usb -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xd8:m -Ulfuse:w:0xff:m

- burn boot loader
avrdude -p atmega328p -c usbasp -P usb -U flash:w:avr_boot-master/avr_boot.hex:i -Ulock:w:0x0F:m

- compile blink project

- find the compiled file
find $TMPDIR -name Blink.cpp.elf

- convert hex to binary
avr-objcopy -O binary Blink.cpp.elf OSMFIRMW.BIN

- plug the SD card and wait the reboot (it may take long time)

Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: ysinotelodigo on Dec 05, 2014, 11:01 pm
...

After a long time and many tries, I find the right flow to be able to burn the new boot loader on an atmega328p dip28 and load the blink example project into the avr from a 512Mo SD card:

- get the github from
https://github.com/willie68/OpenSeaMapLogger/tree/master/bootloader/avr_boot-master

- check your hardware SD <-> Arduino with avr_boot-master/asmfunc.S

- make

- burn fuse
avrdude -p atmega328p -c usbasp -P usb -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xd8:m -Ulfuse:w:0xff:m

- burn boot loader
avrdude -p atmega328p -c usbasp -P usb -U flash:w:avr_boot-master/avr_boot.hex:i -Ulock:w:0x0F:m

- compile blink project

- find the compiled file
find $TMPDIR -name Blink.cpp.elf

- convert hex to binary
avr-objcopy -O binary Blink.cpp.elf OSMFIRMW.BIN

- plug the SD card and wait the reboot (it may take long time)


Hello,

I would like to be able to load the blink example from microSD card.

I have an Arduino Uno. I downloaded avr_boot.hex from repository and added a configuration in the board.txt file with a program called "board.txt Editor".

My configuration is:
uno___2.name=Arduino SD
uno___2.build.mcu=atmega328p
uno___2.build.f_cpu=16000000L
uno___2.build.core=arduino
uno___2.build.variant=standard
uno___2.upload.protocol=arduino
uno___2.upload.maximum_size=32256
uno___2.upload.speed=115200
uno___2.bootloader.low_fuses=0xff
uno___2.bootloader.high_fuses=0xd8
uno___2.bootloader.extended_fuses=0x05
uno___2.bootloader.path=bootsd
uno___2.bootloader.file=avr_boot.hex
uno___2.bootloader.unlock_bits=0x3F
uno___2.bootloader.lock_bits=0x0F

I have a 2 GB microSD with FAT16 and Ethernet-SD board. I generated blink.cpp.elf in Windows, generated the OSMFIRMW.BIN with avr-objcopy command and added the OSMFIRMW.BIN file in root folder.

I couldn't load the blink example from microSD. Do I need to buy a SD card? What thing did I do wrong?

Thank you
Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: Ghostwheel on Mar 02, 2015, 04:06 am
Howdy,

 First off, thank you for the effort you've put into this.  I'm following your instructions, and I want to check my work before I get too far along.

When I get to step 3, make, I get this error:

Code: [Select]
$ make
avr-gcc -gdwarf-2 -Wall -Os -mcall-prologues -mmcu=atmega328p -DBOOT_ADR=0x7000 -DF_CPU=16000000   -c -o main.o main.c
main.c:58:19: warning: 'mem_cmpP' declared 'static' but never defined [-Wunused-function]
 static inline int mem_cmpP(const void* dst, const void* src, int cnt);
                   ^
main.c:65:16: warning: 'pagecmp' defined but not used [-Wunused-function]
 static uint8_t pagecmp(uint16_t addr, uint8_t *data)
                ^
avr-gcc -gdwarf-2 -Wall -Os -mcall-prologues -mmcu=atmega328p -DBOOT_ADR=0x7000 -DF_CPU=16000000   -c -o pff.o pff.c
pff.c: In function 'get_fat':
pff.c:384:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   return LD_WORD(buf);
   ^
pff.c:388:3: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
   return LD_DWORD(buf) & 0x0FFFFFFF;
   ^
pff.c: In function 'pf_mount':
pff.c:757:5: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
     bsect = LD_DWORD(&buf[8]); /* Partition offset in LBA */
     ^
avr-gcc -gdwarf-2 -Wall -Os -mcall-prologues -mmcu=atmega328p -DBOOT_ADR=0x7000 -DF_CPU=16000000   -c -o mmc.o mmc.c
avr-gcc -c -mmcu=atmega328p -I. -x assembler-with-cpp -Wa,-adhlns=asmfunc.lst,-gstabs -DBOOT_ADR=0x7000 -DF_CPU=16000000 asmfunc.S -o asmfunc.o
avr-gcc -gdwarf-2 -Wall -Os -mcall-prologues -mmcu=atmega328p -DBOOT_ADR=0x7000 -DF_CPU=16000000 -Wl,-Map,avr_boot.map -Wl,--section-start,.text=0x7000 -o avr_boot.elf main.o pff.o mmc.o asmfunc.o
avr-objdump -h -S avr_boot.elf > avr_boot.lst
avr-objcopy -j .text -j .data -j .fuse -O ihex avr_boot.elf avr_boot.hex
avr-size -C --mcu=atmega328p avr_boot.elf
avr-size: invalid option -- C
Usage: avr-size [option(s)] [file(s)]
 Displays the sizes of sections inside binary files
 If no input file(s) are specified, a.out is assumed
 The options are:
  -A|-B     --format={sysv|berkeley}  Select output style (default is berkeley)
  -o|-d|-x  --radix={8|10|16}         Display numbers in octal, decimal or hex
  -t        --totals                  Display the total sizes (Berkeley only)
            --common                  Display total size for *COM* syms
            --target=<bfdname>        Set the binary file format
            @<file>                   Read options from <file>
  -h        --help                    Display this information
  -v        --version                 Display the program's version

avr-size: supported targets: elf32-avr elf32-little elf32-big srec symbolsrec verilog tekhex binary ihex
make: *** [size] Error 1


Seems to compile, but I don't know how critical the avr-size output is, and if this error keeps any further steps from occurring.  Running it by hand looks like this:

Code: [Select]
$ avr-size avr_boot.elf
   text   data    bss    dec    hex filename
   3932     26    173   4131   1023 avr_boot.elf


There is a avr_boot.hex, but at 11,174 bytes, it seems too big to me.

Am I on track here, or did I hose something?

-Chris


Hi all,

just a short message to thanks everybody that contribute to release avr_boot.

After a long time and many tries, I find the right flow to be able to burn the new boot loader on an atmega328p dip28 and load the blink example project into the avr from a 512Mo SD card:

- get the github from
https://github.com/willie68/OpenSeaMapLogger/tree/master/bootloader/avr_boot-master

- check your hardware SD <-> Arduino with avr_boot-master/asmfunc.S

- make

- burn fuse
avrdude -p atmega328p -c usbasp -P usb -e -Ulock:w:0x3F:m -Uefuse:w:0x05:m -Uhfuse:w:0xd8:m -Ulfuse:w:0xff:m

- burn boot loader
avrdude -p atmega328p -c usbasp -P usb -U flash:w:avr_boot-master/avr_boot.hex:i -Ulock:w:0x0F:m

- compile blink project

- find the compiled file
find $TMPDIR -name Blink.cpp.elf

- convert hex to binary
avr-objcopy -O binary Blink.cpp.elf OSMFIRMW.BIN

- plug the SD card and wait the reboot (it may take long time)


Title: Re: Flashing a sketch from SD card using 2boot bootloader
Post by: john1993 on Sep 07, 2015, 11:29 pm
I think, it was stalled, but then i found this.
The thread owner has found a way to make it work.
http://harizanov.com/2013/01/2boots-bootloader-on-the-uiot-serial-mmc-bootloader-in-2kb/
His changes are on his own github repository...
that is exactly what myself and many others here would find useful but unfortunately his only compiled version was for a rather queer 6mhz clock (what goes through these guys heads?). most need something for 16mhz uno/promini. i tried:

https://github.com/thseiler/embedded/blob/master/avr/2boots/build/2boots-arduino-atmega328p-16000000L-PD4.hex

maybe that is one of the earlier non-functional versions. using 1gb fat16 sd with "BLINK3.HEX" file and running NameBoardSketch.pde altered for "BLINK3" but the sketch dont load. has anybody ever got this to run on a regular m328 ardunio?

it is surprising such a highly prized capability as loading from sd is not part of mainstream arduino.