Audioino?

#ifdef ARDUINO_PLATFORM

There seems to be a misunderstanding: The compilation for "ARDUINO_PLATFORM" is meant for debugging purpose. If you complile it with this option, you can use an Arduino to make the received values visible, it is not meant to be used for "bootloading".
If you want to compile a bootloader you should use one of the "microcontroller" definitions.

OK, fair enough. I got it to compile at the command line on my Mac using this shell script:

avr-gcc -mmcu=atmega328p \
    -D__AVR_ATmega328P__ \
    -DF_CPU=16000000L \
    -Os \
    -gdwarf-2 \
    -fno-inline-small-functions \
    -fno-split-wide-types \
    -fno-tree-scev-cprop  \
    -fno-exceptions -ffunction-sections -fdata-sections \
    -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -mno-tablejump  \
    -fno-keep-inline-functions -fno-common \
    -std=gnu99 \
    -Wl,--section-start=.text=7C00 \
    -Wl,--relax -nostartfiles \
    -Wl,--gc-sections \
    -oaudioboot.elf chAudioBoot.c 
rm audioboot.hex
avr-objcopy -O ihex --set-section-flags=.eeprom=alloc,load \
    --no-change-warnings \
    --change-section-lma \
    .eeprom=0 \
    -R .eeprom audioboot.elf audioboot.hex

avr-objdump -j .sec1 -d -m avr5 audioboot.hex > audioboot.lst

I modified the source file slightly to get it to compile. The diffs are:

*** chAudioBoot.c	2012-12-25 18:08:02.000000000 +1100
--- /Users/nick/Documents/AudioBoot/chaudioboot.c	2012-12-30 07:57:42.000000000 +1100
***************
*** 124,134 ****
  // here starts the code for the stand alone microcontroller
  //***************************************************************************************
  
  #ifdef UNSPECIFIED_PLATTFORM
  
  	#include <avr/io.h>
  	#include <avr/interrupt.h>
- 	#include <avr/signal.h>
  	#include <stdlib.h>
  	#include <avr/boot.h>
  
--- 124,136 ----
  // here starts the code for the stand alone microcontroller
  //***************************************************************************************
  
+ void a_main();
+ int main(void) __attribute__ ((naked)) __attribute__ ((section (".init9")));
+ 
  #ifdef UNSPECIFIED_PLATTFORM
  
  	#include <avr/io.h>
  	#include <avr/interrupt.h>
  	#include <stdlib.h>
  	#include <avr/boot.h>
  
***************
*** 177,183 ****
  	#define PINVALUE (PIND&INPUTAUDIOPIN)
  	
  	//turn on pull up and set Poti GND
! 	#define INITPORT {	PORTD|=INPUTAUDIOPIN;\	
  						DDRD|=(1<<PD2);}		
  
  	#define PINLOW (PINVALUE==0)
--- 179,185 ----
  	#define PINVALUE (PIND&INPUTAUDIOPIN)
  	
  	//turn on pull up and set Poti GND
! 	#define INITPORT {	PORTD|=INPUTAUDIOPIN;\
  						DDRD|=(1<<PD2);}		
  
  	#define PINLOW (PINVALUE==0)

Basically:

  • Removed an obsolete include for signal.h
  • Did a function prototype for a_main
  • Did a prototype for main which put it in .init9 section and made it naked
  • Got rid of a space after a backslash

The amended file is attached (as well as the build script).

Compile by using the shell script:

$ ./build.sh

Resulting files:

$ ls -l
total 104
-rwxr-xr-x  1 nick  staff   5714 30 Dec 08:05 audioboot.elf
-rw-r--r--  1 nick  staff   2055 30 Dec 08:05 audioboot.hex
-rw-r--r--  1 nick  staff  13306 30 Dec 08:05 audioboot.lst
-rwxr-xr-x@ 1 nick  staff    805 30 Dec 07:44 build.sh
-rwxr-xr-x@ 1 nick  staff  16992 30 Dec 07:57 chAudioBoot.c

The resulting file indeed seems to start at 0x7C00:

audioboot.hex:     file format ihex


Disassembly of section .sec1:

00007c00 <.sec1>:
    7c00:	11 e0       	ldi	r17, 0x01	; 1
    7c02:	a0 e0       	ldi	r26, 0x00	; 0
    7c04:	b1 e0       	ldi	r27, 0x01	; 1
    7c06:	01 c0       	rjmp	.+2      	;  0x7c0a
    7c08:	1d 92       	st	X+, r1
    7c0a:	a5 38       	cpi	r26, 0x85	; 133
    7c0c:	b1 07       	cpc	r27, r17
    7c0e:	e1 f7       	brne	.-8      	;  0x7c08
    7c10:	25 9a       	sbi	0x04, 5	; 4
    7c12:	59 9a       	sbi	0x0b, 1	; 11
    7c14:	fa d0       	rcall	.+500    	;  0x7e0a

I haven't tested it - I don't have the audio hardware set up - but this is closer to what you want. Now you want the fuses for a 1024 byte (512 word) bootloader (high fuse = 0xDC, I think).

I attach the .hex file as well just in case you don't plan to modify the bootloader. But I can't guarantee it works, as I said.

chAudioBoot.c (16.6 KB)

build.sh (805 Bytes)

audioboot.hex (2.01 KB)

Looking at the bootloader code a bit more it seems to use interrupts, a bit. You might want to omit the "naked" part. I'm not absolutely sure how well interrupts are going to work with it. Possibly it will work unchanged. Possibly not.

I was able to successfully bootload the hex file that you provided but I dont think the programming through the bootloader is working. I was also trying to compile your bootloader again after removing the naked main script and I also wanted to change the clock speed to 8mhz rather than 16 but I keep getting an error while trying to build it.

here is what i get when i run ./build.sh

http://cl.ly/image/211E2U35070R

You can copy and paste those messages you know, you don't need to take screen shots of them.

Let's see what version of avr-gcc you have. I have:

$ avr-gcc --version

avr-gcc (GCC) 4.3.3
Copyright (C) 2008 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
~ $ avr-gcc --version
avr-gcc (GCC) 4.6.2
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Can you paste that error message into a forum message please? Helps talk about it.

Here you go, I'll stop doing screenshots haha.

~/Desktop/AudioBoot $ ./build.sh
cc1: error: unrecognized command line option '-mno-tablejump'
avr-objcopy: 'audioboot.elf': No such file
avr-objdump: 'audioboot.hex': No such file
avr-objdump: section '.sec1' mentioned in a -j option, but not found in any input file

before running the script

~/Desktop/AudioBoot $ ls -1
audioboot.hex
build.sh
chAudioBoot.c

After running the script

~/Desktop/AudioBoot $ ls -1
audioboot.lst
build.sh
chAudioBoot.c

Try omitting '-mno-tablejump' from the build.sh file.

So I omitted that part, and when i tried to bootload it did not work properly. I then remembered I commented out your naked main function and so I UNCOMMENTED it and then recompiled and bootloaded and it bootloaded successfully and was able to boot. However, the same error blink occured when i tried to program. Not sure what the problem could be anymore. I even tried to change everything to the internal 8mhz clock and again it successfully boots but cannot actually be reprogrammed.

I was thinking it might help to run the bootloader in the arduino definition and when i changed the chAudioboot.c file to change the definition and then tried to build the project again this is the error I got.

~/Desktop/AudioBoot $ ./build.sh
chAudioBoot.c: In function 'setup':
chAudioBoot.c:98:4: warning: implicit declaration of function 'pinMode' [-Wimplicit-function-declaration]
chAudioBoot.c:98:20: error: 'OUTPUT' undeclared (first use in this function)
chAudioBoot.c:98:20: note: each undeclared identifier is reported only once for each function it appears in
chAudioBoot.c:99:4: error: 'Serial' undeclared (first use in this function)
chAudioBoot.c: In function 'loop':
chAudioBoot.c:104:4: warning: implicit declaration of function 'a_main' [-Wimplicit-function-declaration]
chAudioBoot.c: At top level:
chAudioBoot.c:127:6: warning: conflicting types for 'a_main' [enabled by default]
chAudioBoot.c:104:4: note: previous implicit declaration of 'a_main' was here
chAudioBoot.c:299:1: error: unknown type name 'uint8_t'
chAudioBoot.c:312:1: error: unknown type name 'uint8_t'
chAudioBoot.c: In function 'receiveFrame':
chAudioBoot.c:314:3: error: unknown type name 'uint16_t'
chAudioBoot.c:316:3: error: unknown type name 'uint16_t'
chAudioBoot.c:317:3: error: unknown type name 'uint16_t'
chAudioBoot.c:318:3: error: unknown type name 'uint16_t'
chAudioBoot.c:319:3: error: unknown type name 'uint8_t'
chAudioBoot.c:320:3: error: unknown type name 'uint8_t'
chAudioBoot.c:321:3: error: unknown type name 'uint8_t'
chAudioBoot.c:322:3: error: unknown type name 'uint16_t'
chAudioBoot.c:327:5: error: 'PINB' undeclared (first use in this function)
chAudioBoot.c:327:5: error: 'PB4' undeclared (first use in this function)
chAudioBoot.c:332:3: error: 'TCNT2' undeclared (first use in this function)
chAudioBoot.c:390:5: error: unknown type name 'uint16_t'
chAudioBoot.c:390:19: error: 'uint16_t' undeclared (first use in this function)
chAudioBoot.c:390:28: error: expected ',' or ';' before 'FrameData'
chAudioBoot.c:396:5: error: 'Serial' undeclared (first use in this function)
chAudioBoot.c:407:31: error: 'HEX' undeclared (first use in this function)
chAudioBoot.c:425:26: error: 'true' undeclared (first use in this function)
chAudioBoot.c:426:15: error: 'false' undeclared (first use in this function)
chAudioBoot.c: At top level:
chAudioBoot.c:437:25: error: unknown type name 'uint32_t'
chAudioBoot.c:437:40: error: unknown type name 'uint8_t'
chAudioBoot.c: In function 'runProgramm':
chAudioBoot.c:477:2: error: 'DDRB' undeclared (first use in this function)
chAudioBoot.c:478:2: error: 'DDRC' undeclared (first use in this function)
chAudioBoot.c:479:2: error: 'DDRD' undeclared (first use in this function)
chAudioBoot.c:480:2: warning: implicit declaration of function 'cli' [-Wimplicit-function-declaration]
chAudioBoot.c: In function 'a_main':
chAudioBoot.c:505:3: error: 'Serial' undeclared (first use in this function)
chAudioBoot.c:508:3: error: unknown type name 'uint8_t'
chAudioBoot.c:510:3: error: unknown type name 'uint16_t'
chAudioBoot.c:511:3: error: unknown type name 'uint8_t'
chAudioBoot.c:514:3: error: unknown type name 'uint8_t'
chAudioBoot.c:518:8: error: 'TCNT2' undeclared (first use in this function)
chAudioBoot.c:524:10: warning: implicit declaration of function 'digitalWrite' [-Wimplicit-function-declaration]
chAudioBoot.c:524:10: warning: implicit declaration of function 'digitalRead' [-Wimplicit-function-declaration]
chAudioBoot.c:535:12: error: 'LOW' undeclared (first use in this function)
chAudioBoot.c:541:11: error: 'PINB' undeclared (first use in this function)
chAudioBoot.c:541:11: error: 'PB4' undeclared (first use in this function)
chAudioBoot.c:550:3: error: 'HIGH' undeclared (first use in this function)
rm: audioboot.hex: No such file or directory
avr-objcopy: 'audioboot.elf': No such file
avr-objdump: 'audioboot.hex': No such file
avr-objdump: section '.sec1' mentioned in a -j option, but not found in any input file

It looks like in "Arduino" mode you are expected to compile it inside the IDE, that gives you access to the data types, Serial stuff and so on.

I copied and pasted the original loader into the IDE, changed 3 lines, and it compiled:

#define ARDUINO_PLATFORM 	 // ( with Atmega168 ) bootloader development on arduino plattform   // uncommented it
...
	#define LEDPORT (1<<DDB5);     // was  PB5
...
	#define INPUTAUDIOPIN (1<<PORTD1)  // was PD1

Ive was able to successfully add the arduino verison of the bootloader and same problem of not being able to program it after bootloading. I think we have hit a wall and Im not sure if its worth my time anymore to continue this unless someone else can do some testing. I wish I knew more about bootloaders so I could write my own. I really like the idea of an audio based bootloader since it would be especially attractive in a commerical product and overall is easier to implement 1 wire rather than 6 for ICSP.

Do you know of any other neat bootloaders? I am trying to make my project easier to program than using ICSP, hoping to bring it down to 2 datawires.

The standard bootloader only uses 3 wires: Tx, Rx, Gnd. So when you say 1 wire you mean 2 wires (as you acknowledge).

I'm not sure you are saving a heap by going down this path.

In a commercial product reliability would count for something, and having 1-wire (plus Gnd) means you don't get any acknowledgement so you don't know for sure if you changed the program.

But serial uploading requires a FTDI chip right? Thats like an extra 4 bucks added to my project cost. Im assuming you cannot program through software serial?

Ive was able to successfully add the arduino verison of the bootloader

As mentioned before: If you compile the software with "ARDUINO_PLATFORM" enabled, you will not get a bootloader. This option is meant to compile a debugging platform to check the correct data transmission. If you take a look at the source code, you will understand.

Chris, is there anyway you could please recompile a functional bootloader for the atmega328p chip? Ive been trying for multiple days so far and although i can get it to boot up, I cannot get it to program successfully. I am not sure what the issue is anymore.

Thank you!

sdinnu:
But serial uploading requires a FTDI chip right? Thats like an extra 4 bucks added to my project cost. Im assuming you cannot program through software serial?

You only need an FTDI cable (which you share between multiple targets). You don't need one per board. You could conceivably program through software serial, but what does that save? You still need the FTDI interface.

Doesn't the audio interface add cost anyway?

How often are you planning to reprogram? Just using the ICSP interface (6 wires or not) is the simplest anyway.

yea the main thing that I was hoping to solve by using the audio setup vs serial or icsp is that end users would not require any extra hardware and reprogramming would be extremely simplified since they would not even have to use the arduino IDE or anything since one could simple send out the audio file and people could update their devices. ICSP requires either an arduino as ISP or some external programmer which is added cost. And ftdi, although integrates the hardware in to the final product, still requires someone to use the IDE/avrdude to reprogram the device. Overall, although insigificant for someone experienced, for the nontechy these methods of reprogramming are bit more confusing.

But you are correct in that the devices will not be programmed that often. I mainly want to keep the option open just in case there is a patch that needs to be deployed or updates to code over time.

Also, can you trim the reset button off of the ICSP and just require a manual reset press? This would allow one to wire the ICP pins into say a microusb port which would be alot nicer than the current interface.

You could trim off the power, since presumably the device is internally powered anyway. (Of course the progammer would then need its own power, eg. a battery).

end users would not require any extra hardware ...

Apart from an audio player and some wires? And an interface circuit?

You could make a dedicated ICSP programmer (eg. in a small box with a battery). Load the new code into it in program memory (like my bootloader programmer described here), tell the user to shove in the wire you described, and hit the "program" button, and wait until an LED changes colour (a second later).

The programmer itself would only need to be an Atmega328 (or similar) chip, a couple of capacitors, a resonator, a couple of resistors and LEDs, the box and a switch. Should be able to make it for $20. And you could have a similar socket in the programmer for reprogramming it (with the new firmware).

I'm pretty sure there are commercial devices that do just that, I remember CrossRoads was talking about getting one.

i actually need the power and ground since it will be used to charge my device, so i only have 3 pins left on a microusb which would be used for miso, mosi, and sck. but that means reset would have to be left out. would that not work properly?