Problem uploading to 328p

Hi, I have some very strange upload problem. I have two boards
an Arduino and a Freeduino. And I have to processors a 168 and a 328. Together with Arduino IDE both work as they should. So I am pretty sure that they both actually work.

If I try to flash them from the command line the troubles begin. I can flash the 168 in both boards and the 328 in none. So I am sure that the issue should not be related to any of the boards.

The commandline that I am using is

./pulsedtr.py /dev/ttyUSB0 && /home/udo/Desktop/electronics/arduino-0016/hardware/tools/avrdude -q -V -p atmega328p -C /home/udo/Desktop/electronics/arduino-0016/hardware/tools/avrdude.conf -F -c stk500v1 -b 19200 -P /dev/ttyUSB0 -U flash:w:build-cli/test.hex:i

And what I get is for the 328

avrdude: stk500_recv(): programmer is not responding
avrdude: stk500_recv(): programmer is not responding

whereas for the 168 it always works as expected.

Has anyone and idea how this could be fixed?

Thanks!

P.S. The conf file entry is

#------------------------------------------------------------

ATmega328

#------------------------------------------------------------

part
id = "m328p";
desc = "ATMEGA328P";
has_debugwire = yes;
flash_instr = 0xB6, 0x01, 0x11;
eeprom_instr = 0xBD, 0xF2, 0xBD, 0xE1, 0xBB, 0xCF, 0xB4, 0x00,
0xBE, 0x01, 0xB6, 0x01, 0xBC, 0x00, 0xBB, 0xBF,
0x99, 0xF9, 0xBB, 0xAF;
stk500_devcode = 0x86;

avr910_devcode = 0x;

signature = 0x1e 0x95 0x0F;
pagel = 0xd7;
bs2 = 0xc2;
chip_erase_delay = 9000;
pgm_enable = "1 0 1 0 1 1 0 0 0 1 0 1 0 0 1 1",
"x x x x x x x x x x x x x x x x";

chip_erase = "1 0 1 0 1 1 0 0 1 0 0 x x x x x",
"x x x x x x x x x x x x x x x x";

timeout = 200;
stabdelay = 100;
cmdexedelay = 25;
synchloops = 32;
bytedelay = 0;
pollindex = 3;
pollvalue = 0x53;
predelay = 1;
postdelay = 1;
pollmethod = 1;

pp_controlstack =
0x0E, 0x1E, 0x0F, 0x1F, 0x2E, 0x3E, 0x2F, 0x3F,
0x4E, 0x5E, 0x4F, 0x5F, 0x6E, 0x7E, 0x6F, 0x7F,
0x66, 0x76, 0x67, 0x77, 0x6A, 0x7A, 0x6B, 0x7B,
0xBE, 0xFD, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00;
hventerstabdelay = 100;
progmodedelay = 0;
latchcycles = 5;
togglevtg = 1;
poweroffdelay = 15;
resetdelayms = 1;
resetdelayus = 0;
hvleavestabdelay = 15;
resetdelay = 15;
chiperasepulsewidth = 0;
chiperasepolltimeout = 10;
programfusepulsewidth = 0;
programfusepolltimeout = 5;
programlockpulsewidth = 0;
programlockpolltimeout = 5;

memory "eeprom"
paged = no;
page_size = 4;
size = 1024;
min_write_delay = 3600;
max_write_delay = 3600;
readback_p1 = 0xff;
readback_p2 = 0xff;
read = " 1 0 1 0 0 0 0 0",
" 0 0 0 x x x a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" o o o o o o o o";

write = " 1 1 0 0 0 0 0 0",
" 0 0 0 x x x a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" i i i i i i i i";

loadpage_lo = " 1 1 0 0 0 0 0 1",
" 0 0 0 0 0 0 0 0",
" 0 0 0 0 0 0 a1 a0",
" i i i i i i i i";

writepage = " 1 1 0 0 0 0 1 0",
" 0 0 x x x x a9 a8",
" a7 a6 a5 a4 a3 a2 0 0",
" x x x x x x x x";

mode = 0x41;
delay = 5;
blocksize = 4;
readsize = 256;
;

memory "flash"
paged = yes;
size = 32768;
page_size = 128;
num_pages = 256;
min_write_delay = 4500;
max_write_delay = 4500;
readback_p1 = 0xff;
readback_p2 = 0xff;
read_lo = " 0 0 1 0 0 0 0 0",
" 0 0 a13 a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" o o o o o o o o";

read_hi = " 0 0 1 0 1 0 0 0",
" 0 0 a13 a12 a11 a10 a9 a8",
" a7 a6 a5 a4 a3 a2 a1 a0",
" o o o o o o o o";

loadpage_lo = " 0 1 0 0 0 0 0 0",
" 0 0 0 x x x x x",
" x x a5 a4 a3 a2 a1 a0",
" i i i i i i i i";

loadpage_hi = " 0 1 0 0 1 0 0 0",
" 0 0 0 x x x x x",
" x x a5 a4 a3 a2 a1 a0",
" i i i i i i i i";

writepage = " 0 1 0 0 1 1 0 0",
" 0 0 a13 a12 a11 a10 a9 a8",
" a7 a6 x x x x x x",
" x x x x x x x x";

mode = 0x41;
delay = 6;
blocksize = 128;
readsize = 256;

;

memory "lfuse"
size = 1;
min_write_delay = 4500;
max_write_delay = 4500;
read = "0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0",
"x x x x x x x x o o o o o o o o";

write = "1 0 1 0 1 1 0 0 1 0 1 0 0 0 0 0",
"x x x x x x x x i i i i i i i i";
;

memory "hfuse"
size = 1;
min_write_delay = 4500;
max_write_delay = 4500;
read = "0 1 0 1 1 0 0 0 0 0 0 0 1 0 0 0",
"x x x x x x x x o o o o o o o o";

write = "1 0 1 0 1 1 0 0 1 0 1 0 1 0 0 0",
"x x x x x x x x i i i i i i i i";
;

memory "efuse"
size = 1;
min_write_delay = 4500;
max_write_delay = 4500;
read = "0 1 0 1 0 0 0 0 0 0 0 0 1 0 0 0",
"x x x x x x x x x x x x x o o o";

write = "1 0 1 0 1 1 0 0 1 0 1 0 0 1 0 0",
"x x x x x x x x x x x x x i i i";
;

memory "lock"
size = 1;
min_write_delay = 4500;
max_write_delay = 4500;
read = "0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0",
"x x x x x x x x x x o o o o o o";

write = "1 0 1 0 1 1 0 0 1 1 1 x x x x x",
"x x x x x x x x 1 1 i i i i i i";
;

memory "calibration"
size = 1;
read = "0 0 1 1 1 0 0 0 0 0 0 x x x x x",
"0 0 0 0 0 0 0 0 o o o o o o o o";
;

memory "signature"
size = 3;
read = "0 0 1 1 0 0 0 0 0 0 0 x x x x x",
"x x x x x x a1 a0 o o o o o o o o";
;
;

Try adding the switch -B 100
to the avrdude command line. This will slow the programming speed down. Sometimes
the ICs come with different initial fuse settings. After the first programming you should
be able to change the switch back to -B 1

(* jcl *)

I tried -B 100 but this made no difference at all. The problem persists. Do you have any other suggestions?

I already ordered some cheap STK500 compatible programmer. Maybe I can get rid of the issue by bypassing the bootloader completely.

Cheers, Udo

If the 328 works when you program it from the IDE, can you not set upload.verbose=true in the preferences.txt file and see what avrdude command the IDE uses? The same thing should then work from the command line.

Andrew

Took some minutes to figure out that the preferences are not in the arduino directory. Instead they are in a local copy. But now I changed everything to verbose. This does it. --> Very enlightning.

Thank you very much for you help.

Cheers, Udo

Sorry, I should have spelled out the details a bit more. I hesitated to make the suggestion at all actually as I thought you'd probably know about stuff like that already if you were doing advanced things like building and uploading from the command line. Glad you got it working.

Andrew

Well, I am sort of a beginner, but not totally. I needed some simple logic analyzer. Of course it is not very clean, and it can not really catch pin 0 and 1. But for my purposes it is good enough. However I did not figure out how this could work in the IDE.

Cheers, Udo

#include <WProgram.h>

int main() {
      const uint8_t mask = 0xff;
  
    const int max  = 1024;
      const int show = 1024;
    uint8_t log[max];
  
      // configure all port D pins for input
      for (int pin = 0; pin < 8; ++pin) {
            pinMode(pin,INPUT);
          digitalWrite(pin, HIGH);
      }

      // capture 1024 at 1 byte each 3 seconds 
      // trigger if any of the bits in mask changes

      uint8_t tmp;  // dummy for temporary register
      __asm__ __volatile__ (      
            "      cli"                      "\n\t"  

            // wait for trigger condition
            "      in   __tmp_reg__, %[port_d]"  "\n\t"     
            "   and  __tmp_reg__, %[mask]"    "\n\t"
            "1: in   %[tmp], %[port_d]"       "\n\t"      
            "   and  %[tmp], %[mask]"         "\n\t"
            "      cp   %[tmp], __tmp_reg__"     "\n\t"
            "   breq 1b"                      "\n\t"            

            // log 1024 bytes
            "      in   %[tmp], %[port_d]"    "\n\t"     
            "      st   %a[log]+, %[tmp]"     "\n\t"     
            "      in   %[tmp], %[port_d]"    "\n\t"     
            "      st   %a[log]+, %[tmp

 < many lines deleted >

            "      in   %[tmp], %[port_d]"    "\n\t"     
            "      st   %a[log]+, %[tmp]"     "\n\t"      
            
            "sei"                      "\n\t"   
      :                       // no output
        :      [log]    "e" (log),   // constraint: pointer register            
            [port_d] "M" (0x09),  // constraint: constant
            [mask]       "e" (mask),  // constraint: simple upper register                          
            [tmp]    "a" (tmp)    // constraint: simple upper register              
        :      "memory"            
      ); 


    Serial.begin(115200);  

    for (int line = 0; line < show; ++line) {    
        Serial.print(line);
        for (int i = 7; i >= 0; --i) {
            Serial.print(',');
            Serial.print((log[line] >> i) & 1);
        }    
        Serial.println("");
    }      
    for (;;) {}
}

However I did not figure out how this could work in the IDE.

The code you posted compiles ok in the IDE if you put in loop and add a setup function.

I tried but the IDE seems to have issues with the assembler part.

I tried but the IDE seems to have issues with the assembler part.

I have compiled inline assembler in library files using the IDE without problems.

A very quick look at the assembler produced by the IDE from the code you produced looks like it is being handled correctly – here is some output from avr_objdump:

 182:      09 b0             in      r0, 0x09      ; 9
 184:      0e 22             and      r0, r30
 186:      29 b1             in      r18, 0x09      ; 9
 188:      2e 23             and      r18, r30
 18a:      20 15             cp      r18, r0
 18c:      e1 f3             breq      .-8            ; 0x186 <loop+0x50>
 18e:      29 b1             in      r18, 0x09      ; 9
 190:      2d 93             st      X+, r18
 192:      29 b1             in      r18, 0x09      ; 9
 194:      29 b1             in      r18, 0x09      ; 9
 196:      2d 93             st      X+, r18
 198:      78 94             sei

What where the issues?

Hi Mem,

I had several issues. First I got error messages that I could not properly relate. When I changed to the makefile I got everything working. After your suggestion I changed back to the IDE but this time it just hung the IDE. I did not try much further.
Besides this strange behavior I do not like this IDE anyway. Although it is very nice for sketching some lines of code and for a quick start it soon becomes a pain. The main reason is that the IDE generates prototypes. If it would allow to take over control over function prototypes things would be a little bit different. But as it is today the IDE seems not worth the hassle for me.

However on the other hand: if it would be possible to put the code back into the IDE then I could make the code sample easier accessible for other people. Since obviously make files are not everyones business.

Best regards, Udo

Hi Mem,

I tried it again today with Arduino 17. Same result though. If I replace main by loop and add an empty setup it will hang while trying to compile. No clue why it gets into an endless loop. I just can not get this to work as a normal sketch.

How exactly did you get it to compile?

Cheers, Udo

This compiles in 0016 and 0017, is it different from what you tried?

void setup(){
}

void loop() {
  const uint8_t mask = 0xff;

  const int max  = 1024;
  const int show = 1024;
  uint8_t log[max];

  // configure all port D pins for input
  for (int pin = 0; pin < 8; ++pin) {
    pinMode(pin,INPUT);
    digitalWrite(pin, HIGH);
  }

  // capture 1024 at 1 byte each 3 seconds
  // trigger if any of the bits in mask changes

  uint8_t tmp;  // dummy for temporary register
  __asm__ __volatile__ (
  "      cli"                      "\n\t"

    // wait for trigger condition
  "      in   __tmp_reg__, %[port_d]"  "\n\t"
    "   and  __tmp_reg__, %[mask]"    "\n\t"
    "1: in   %[tmp], %[port_d]"       "\n\t"
    "   and  %[tmp], %[mask]"         "\n\t"
    "      cp   %[tmp], __tmp_reg__"     "\n\t"
    "   breq 1b"                      "\n\t"

    // log 1024 bytes
  "      in   %[tmp], %[port_d]"    "\n\t"
    "      st   %a[log]+, %[tmp]"     "\n\t"
    "      in   %[tmp], %[port_d]"    "\n\t"

    ///!!!! perhaps there is problem with one of the deleted lines < many lines deleted >

  "      in   %[tmp], %[port_d]"    "\n\t"
    "      st   %a[log]+, %[tmp]"     "\n\t"

    "sei"                      "\n\t"
:                       // no output
:      
  [log]    "e" (log),   // constraint: pointer register
  [port_d] "M" (0x09),  // constraint: constant
  [mask]       "e" (mask),  // constraint: simple upper register
  [tmp]    "a" (tmp)    // constraint: simple upper register
:      
  "memory"
    );


  Serial.begin(115200);

  for (int line = 0; line < show; ++line) {
    Serial.print(line);
    for (int i = 7; i >= 0; --i) {
      Serial.print(',');
      Serial.print((log[line] >> i) & 1);
    }
    Serial.println("");
  }
  for (;;) {
  }
}

If I take your code it compiles. If I take mine it hangs the IDE. The only difference I can see is that I have 2000+ lines of assembler code more. I double checked that the lines are all identical. Of course they were. Outside the IDE it compiles perfectly well. And the lines were not typed by hand. Of course I replicated them.

So I have no clue why it fails in the IDE.

Cheers, Udo

Its probably choking on all those lines when the build process creates auto-prototypes.
See: Redirecting

I suggest you put the assembler in a function in separate c file and call the funciton from your sketch. The arduino build does not auto-prototype files that don't have a pde extension.