Writing to a AT89S52 through a USBasp

41 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys, this is probably not the correct forum and Im sorry if this is well trodden territory. I have read our friend Jecksons attempts to try to make an USBasp program a At89S52 via AVRdude. As a matter of fact, im trying to do the exact same thing.

Im actually able to read and partially write to the AT82S52 through the USBasp and Avrdude 5.10 with a modified conf.

Here is how i did it.

0. WinXP system
1. Bought/soldered a usbasp
2. Knowing that the usbasp functions fully (tested to program atmega8s all day with it) i interconnected the MISO, MOSI, SCK, GND, RST, VCC and GND of the Atmega8 on the USBasp and the target µC AT89S52.
3. Hooked up a 4,096mhz Xtal and 33pf capacitor to the target µC.
4. Connected the entire setup to the usb
5. Edited the conf file of avrdude with the work of Joy Shukla:

#------------------------------------------------------------
# Below chips by added by me .. Joy Shukla(joy_shukla@yahoo.in)
# these chips can be programmed with my usbasp programmer(changed atmega8 program)
# after adding avrdude support in this file ..
#------------------------------------------------------------

#------------------------------------------------------------
# AT89S52
#------------------------------------------------------------
part
    id               = "8052";
    desc             = "AT89S52";
    signature        = 0x1E 0x52 0x06;
    chip_erase_delay = 20000;
    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      = 0;

    memory "flash"
        size            = 8192;
        paged           = no;
        min_write_delay = 4000;
        max_write_delay = 9000;
        readback_p1     = 0xff;
        readback_p2     = 0xff;
        read            = "  0   0   1   0    0   0   0   0",
                          "  x   x   x a12  a11 a10  a9  a8",
                          " a7  a6  a5  a4   a3  a2  a1  a0",
                          "  o   o   o   o    o   o   o   o";

        write           = "  0   1   0   0    0   0   0   0",
                          "  x   x   x a12  a11 a10  a9  a8",
                          " a7  a6  a5  a4   a3  a2  a1  a0",
                          "  i   i   i   i    i   i   i   i";
   mode      = 0x21;
   delay      = 12;
      ;

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

5. Copied my target hex "Loghman.hex" of 380 (threehundred and eighty bytes) of led-blinking-code to avrdude's location

6. Fired up avrdude with the following command:
avrdude -p 8052 -c usbasp -e -U flash:"blabla\loghman.hex" -vvvv

7. Got the following screen:

Conclusion:
A) AVRdude can write to the AT89S52 but only 110 bytes, and NOT the full 380 bytes. Avrdude can read the supposedly written 110 bytes and it looks like this (gibberish?):

oߟ¯¯¯¯¯¯¯¯¯Ï__ï/ÿï?ÿï?ÿï?ÿÏŸߟ/ŸÏïÿÿÿÿÏïŸÿÿïŸOÿïŸOÿïŸOÿ/ïÿßÿÿ

Discussion:
A) I know through previous searches that AVR and the AT89 chips are programmed in a different way but hoped that the .conf file modification might do the trick. Obviously it didnt or i have due to ignorance missed something. Would any of the more versed AVR experts care to comment on what i might have done wrong?

B) Also, im getting a warning message that avrdude cannot set sck period. Tried to hook pin 25 to GND to set the SCK to slow mode but get the exact same warning. Any suggestions would be appriciated.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to note that the source of avrdude (and usbasp for that matter) is open so if there's really a demand to add 8051 programming to it then it's quite possible for someone to do this then contribute the work back to the project (under the "beerware" or "Free BSD" licence)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

try this config, it works.
add this to the config file near the other defines

#define AT89START   0xE0
#define AT89S51	  0xE0
#define AT89S52	  0xE1

add this at the end of the config file

 #------------------------------------------------------------
  # AT89S51
  #------------------------------------------------------------
  part
      id               = "8052";
      desc             = "AT89S51";
      signature        = 0x1E 0x51 0x06;
      chip_erase_delay = 500000;
      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      = 0;
  
      memory "flash"
          size            = 4096;
          paged           = no;
          min_write_delay = 4000;
          max_write_delay = 9000;
          readback_p1     = 0xff;
          readback_p2     = 0xff;
          read            = "  0   0   1   0    0   0   0   0",
                            "  x   x   x a12  a11 a10  a9  a8",
                            " a7  a6  a5  a4   a3  a2  a1  a0",
                            "  o   o   o   o    o   o   o   o";
  
          write           = "  0   1   0   0    0   0   0   0",
                            "  x   x   x a12  a11 a10  a9  a8",
                            " a7  a6  a5  a4   a3  a2  a1  a0",
                            "  i   i   i   i    i   i   i   i";
     mode      = 0x21;
     delay      = 12;
        ;
  
      memory "signature"
          size            = 3;
          read            = "0  0  1  0   1  0  0  0   x  x  x  0   0  0 a1 a0",
                            "0  0  0  0   0  0  0  0   o  o  o  o   o  o  o  o";
        ;
    ; 
#------------------------------------------------------------
# AT89S52
#------------------------------------------------------------
part
    id               = "8052";
    desc             = "AT89S52";
    signature        = 0x1E 0x52 0x06;
    chip_erase_delay = 500000;
    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      = 0;

    memory "flash"
        size            = 8192;
        paged           = no;
        min_write_delay = 4000;
        max_write_delay = 9000;
        readback_p1     = 0xff;
        readback_p2     = 0xff;
        read            = "  0   0   1   0    0   0   0   0",
                          "  x   x   x a12  a11 a10  a9  a8",
                          " a7  a6  a5  a4   a3  a2  a1  a0",
                          "  o   o   o   o    o   o   o   o";

        write           = "  0   1   0   0    0   0   0   0",
                          "  x   x   x a12  a11 a10  a9  a8",
                          " a7  a6  a5  a4   a3  a2  a1  a0",
                          "  i   i   i   i    i   i   i   i";
   mode      = 0x21;
   delay      = 12;
      ;

    memory "signature"
        size            = 3;
        read            = "0  0  1  0   1  0  0  0   x  x  x  0   0  0 a1 a0",
                          "0  0  0  0   0  0  0  0   o  o  o  o   o  o  o  o";
      ;
  ; 
#------------------------------------------------------------ 
# AT89S8253 
#------------------------------------------------------------ 
part 
    id               = "8253"; 
    desc             = "AT89S8253"; 
    chip_erase_delay = 20000; 
    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      = 0; 

    memory "flash" 
        size            = 12288; 
        paged           = no; 
        min_write_delay = 4000; 
        max_write_delay = 9000; 
        readback_p1     = 0xff; 
        readback_p2     = 0xff; 
        read            = "  0   0   1   0    0   0   0   0", 
                          "  x   x a13 a12  a11 a10  a9  a8", 
                          " a7  a6  a5  a4   a3  a2  a1  a0", 
                          "  o   o   o   o    o   o   o   o"; 

        write           = "  0   1   0   0    0   0   0   0", 
                          "  x   x a13 a12  a11 a10  a9  a8", 
                          " a7  a6  a5  a4   a3  a2  a1  a0", 
                          "  i   i   i   i    i   i   i   i"; 
   mode      = 0x21; 
   delay      = 12; 
      ; 

    memory "signature" 
        size            = 2; 
        readback_p1     = 0x1E; 
        readback_p2     = 0x73; 
        read            = "0  0  1  0   1  0  0  0   x  x  x  x   x  x  x  x", 
                          "x  x  1  1   0  0  0 a0   o  o  o  o   o  o  o  o"; 
      ; 
  ; 

this is the bat file I use:

avrdude -c usbasp -p AT89S52 -C avrdude.conf -B 250 -e -U flash:w:file.hex

ensure you have the right code in the usbasp. If you are having trouble with avrdude, try progisp172 here:
http://www.zhifengsoft.com/download/progisp172.rar

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your replies guys. Followed the instructions but still get the 348/110 problem, meaning my hexfile is detected by avrdude as Intel hex and of size 110 bytes although the size obviously is 348bytes large.

Does anyone know what might be the problem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I get a different behaviour.

1. You get a 'warning' about not setting SCK frequency.
I can set SCK frequency or just leave as default.

2. You can erase successfully.
I get a 'target does not answer' after the erase.

3. You say that your file is 384 bytes. I am sure that avrdude can correctly count 110 bytes.
I can successfully program and verify providing that I use the -D flag. i.e. I need to erase in one session and program in a different session.

My other anomaly is with the SCK setting.

Usbasp starting from cold will happily communicate at SCK=750kHz.
However any subsequent 'run' needs to use SCK=32kHz.

Usbasp seems happy programming AVRs without issue.

Incidentally, I get the -e and -D behaviour with the STK500 too.

n.b. AT89 parts have an active-high RESET pin. If you do not use an inverter, you need to add a 'nRST' line to usbasp. i.e. it does the inverse of the regular /SS line.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Isn't the size confusion the classical "intelhex file size does not match binary size" problem?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

angrysurgeon wrote:
Thanks for your replies guys. Followed the instructions but still get the 348/110 problem, meaning my hexfile is detected by avrdude as Intel hex and of size 110 bytes although the size obviously is 348bytes large.

Does anyone know what might be the problem?

you do realize that the size of the hex, is not the size of the program writen to the part. The hex will be more than 2x the size of what is written due to the encoding scheme used in hex files.

Back-calculating from your 110 bytes, which requires 220 hex bytes. Encoding that at 16 hex bytes per line, plus 11 bytes of overhead per line, add a segment definition line [15], and the EOF marker [11], plus a CRLF [2] per line, I get 341 bytes for the hex file.

Writing code is like having sex.... make one little mistake, and you're supporting it for life.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

you do realize that the size of the hex, is not the size of the program writen to the part. The hex will be more than 2x the size of what is written due to the encoding scheme used in hex files.

so in other words, im actually able to program the AT89S52, its just that my ledblinking code is not working?

So here is "my" code (actually the code i use from a AT89S52 tutorial):

#include 
#include 

unsigned long time, ON_time; //Global Variables

void main(){
    P1_3 = 1;     //Set up P1_3 as input pin
    ON_time = 100000;
    while(1){
         if (time < ON_time){
            time++;    // start or continue counting
            P1_0 = 0;     //Turn ON the LED
         }else{
            P1_0 = 1;    // Turn OFF the LED
        }
        if (P1_3 == 0){     // if the switch is pressed,
            time = 0;    // reset 'time' to 0
        }
    }
} 

Using Keil µvision 4.02, I get this compilation:

http://www.mediafire.com/?uba0td7qfxpb3br

To startup the At89S52 i use 2x 22pf a 11.0952Mhz crystal, connected (pin20), connected VCC 5.0V (pin 40) and to pin 1, i connect a red LED of 1.9V attached to the 5v via a 500ohm-1K resistor attached to 5V.

Unfortunately it does not work. With an oscilloscope, Ive tried to check if the oscillator swings correctly but all i get is gibberish, not the expected nice sine curve with the correct frequency that i get with Atmega8 (or any other microcontroller for that matter) when the power is turned on.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

David, i soldered the SCK to gnd on the usbasp that I use, but I was getting the exact same warningmessage before i did that "maneuver" so I just left it on since the AT89S52 manual mentions that the programming frequency must be 1/16th of that of the attached crystal.

And what do you mean with the following: " you need to add a nRST' line to usbasp", how would i go about doing that? That the RST of the AT89S52 must be 5V during programming?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Did you try progisp172? This way you will know your hardware is working.

If you get the SCK warning in avrdude, you don't have the latest code in the usbasp.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

angrysurgeon wrote:
David, i soldered the SCK to gnd on the usbasp that I use, but I was getting the exact same warningmessage before i did that "maneuver" so I just left it on since the AT89S52 manual mentions that the programming frequency must be 1/16th of that of the attached crystal.

I presume that you are referring to the 'SCK_slow' i/p pin on the usbasp board. This is nothing to do with the SCK o/p pin on the ISP header.

Quote:
And what do you mean with the following: " you need to add a nRST' line to usbasp", how would i go about doing that? That the RST of the AT89S52 must be 5V during programming?

An AVR cpu and most civilised cpu's in all history have an active-low /RESET pin.
The MCS51 family have an active-high RESET pin. wek has written an excellent explanation of this 'feature'.

So any ISP programmer must be capable of sending the
correct polarity of signal to the /RESET or RESET pin.
I simply duplicated the usbasp code that affects the /RESET o/p. i.e. I selected another GPIO pin for nRST, and always set it to the inverse of the RST o/p pin.

I have also added a flag to allow SPI mode#1 for the AT89S8253. All AVRs and other AT89 parts use regular mode#0 SPI !
The ispEnterProgrammingMode() function now accepts byte#4 == 0x69 (what the AT89S52 replies) as well as the byte#3 == 0x53 (what AVRs and 8253 replies)

I discovered the reason for my strange clock speed behaviour. The usbasp was simply driving a naked chip on a breadboard. I had a pull-down resistor for the AT89's RESET. But I did not have a pull-up resistor for the SS pin of the usbasp. The AVR SPI peripheral must never have the SS pin as a floating i/p.

Incidentally, your 'at89.c' program works just fine. But of course it needs human intervention. A simple Blinky.c is easier to check.

n.b. the AT89S52 is a 5V part. You can read it at 3.3V. But erase or program needs to have a full 5V.
AVR parts are very tolerant of low voltage operation.

David.

Pages