Pages: [1]   Go Down
Author Topic: Program At89S51 with Arduino SPI library  (Read 174 times)
1 Member and 2 Guests are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello to everyone,

I'm wondering if it's possible to program an At89s51 with arduino using the SPI library only.  I'm newbie in SPI world, this why I found that to program the at89s51 will allow me to understand the SPI protocol as well as play with this great uController.

I'm asking my self also on how the data is programmed on the chip after sending the "Programming enable instruction" is by using MOSI itself or I have to put something on the address line and data line ? I asked this question because I found in the datasheet "Serial programming instruction set"  page 20 the byte 2, 3, 4 have "xxxx A11 A10 A9 A8" or "A7 A6 A5 A4 A3 ..." and I'm struggling to understand what they mean by these.

I've created a test circuit to send the first programming instruction to the chip and try to get the data response back from it, but it's not working. I've put a 6Mhz crystal osillator with two 22pF cap for the uController. (I'm not sure if this is correct or not too ?)

My request is there someone can answer me about  what I've asked and tell me if this project is feasible or not ?

I'm newbie in electronic so what you can do in 2 hours I will do it in 2 weeks.  smiley-mr-green

Thanks in advance.

Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 114
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I'm asking my self also on how the data is programmed on the chip after sending the "Programming enable instruction" is by using MOSI itself or I have to put something on the address line and data line ?
SPI is a serial interface, everything is sent on MOSI and read from MISO.
Quote
"...Serial programming instruction set"  page 20 the byte 2, 3, 4 have "xxxx A11 A10 A9 A8" or "A7 A6 A5 A4 A3 ..." and I'm struggling to understand what they mean by these.

It means you first send the opcode (byte1), then in byte 2 you send the highest bits of the address (highest 4 bits are don't care: xxxx),  byte 3 holds the 8 least significant bits of the address and finally byte 4 is the data you want to write at that address.

Quote
My request is there someone can answer me about  what I've asked and tell me if this project is feasible or not ?
It is feasable and a good exercise as you indicate.
But assure yourself you want to do the exercise: incidently I just bought a  programmer (an sp200s clone) for only 12$, that can do the at89s52. It seems to work but it is black box, you don't see what is happening so this does not learn you anything on ISP or SPI of coarse.

If you are still convinced you want to do it from your arduino, this link may be interesting:
http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=99161
It indicates you can program the at89s52 with avrdude + usbasp. I would expect that avrdude + arduinoISP should work too.



Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks for your reply.

So, it can be done. However, can I use the SPI library from arduino or should I make my own avr code ? Because I think the SPI protocol change from a device to another.

What you think ?

Regards.
Logged

Denmark
Offline Offline
Edison Member
*
Karma: 35
Posts: 1072
Happy Hobbyist
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This might help.
Programming At89s52 with ArduinoISP:

http://softgeniedoc.dk/contents/projects/atmel89sxx/89s5xx.php
Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 114
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting link. Though it is not using ArduinoISP, it uses some sketch dedicated to the programming tool also available on that side.

In my reply above, my expectation that avrdude+ArduinoISP could do the job is wrong: ArduinoISP does not interprete info from avrdude about e.g. what command to issue for a "program enable". All this is hard coded... When I come to think about it, this is a pity: ArduinoISP is used by a lot of people and it could be a lot more usefull if it used more of the info coming from avrdude...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Indeed is a great link. I'll study it minutely, especially the arduino side.

Last question, the AT89S51 SPI mode  must be switched to slave mode or is already in it

Regards.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have coded a sketch that can send (read byte command ,  programming enable command, chip erase). However, I still can't understand how data is sent to me ? Should I read the MISO pin each time I send a bit or should I wait till the command is completely sent ?

I try to read the output from the chip when the programming enable command is sent.

Just for info, I've used the first method, I read MISO each time I send a bit but I got the same command that I send just before. Is that behavior correct ?

This is my code:

Code:
#define ENABLE_PROGRAMMING 0xAC530069L
#define CHIP_ERASE         0xAC800000L
#define READ_BYTE          0x20000000L
#define WRITE_BYTE         0x40000041L

int RST_P = 9;
int MOSI_P = 10;
int MISO_P = 11;
int CLK_P  = 12;

void setup()
{
  Serial.begin(9600);
 
  pinMode(MOSI_P, OUTPUT);
  pinMode(MISO_P, INPUT);
  pinMode(RST_P, OUTPUT);
  pinMode(CLK_P, OUTPUT);
 
  digitalWrite(MOSI_P, HIGH);
  digitalWrite(RST_P, HIGH);
  digitalWrite(CLK_P, HIGH);

}

void enableRST()
{
  digitalWrite(RST_P, HIGH);
}

void disableRST()
{
  digitalWrite(RST_P, LOW);
}

void sendCmd(long cmd, boolean output)
{
  digitalWrite(CLK_P, LOW);
  delay(1);
 
  for (int i=31; i>=0; i--)
  {
    if (cmd&bit(i))
    {
      digitalWrite(MOSI_P, HIGH);
      //Serial.println(1);
    }
    else
    {
      digitalWrite(MOSI_P, LOW);
      //Serial.println(0);
    }
   
    digitalWrite(CLK_P, HIGH);
    delay(1);
 
    if (output)
    Serial.print(digitalRead(11));
   
    digitalWrite(CLK_P, LOW);
    delay(1);
  }
}

void programmingEnable(boolean x)
{
  if (x)
  {
    digitalWrite(MOSI_P, LOW);
    digitalWrite(RST_P, LOW);
    digitalWrite(CLK_P, LOW);
    delay(50);
    digitalWrite(RST_P, HIGH);
    delay(50);
    //digitalWrite(CLK_P, HIGH);
  }
  else
  {
    digitalWrite(RST_P, LOW);
    digitalWrite(MOSI_P, HIGH);
    digitalWrite(CLK_P, HIGH);
  }
 
  delay(1);
 
}

void loop()

  programmingEnable(true);
  sendCmd(ENABLE_PROGRAMMING, true);
  delay(1);
  //sendCmd(CHIP_ERASE, false);
  //delay(1000);
  //sendCmd(WRITE_BYTE, false);
  //delay(1000);
  //sendCmd(READ_BYTE, true);
  while(1);
}

Thanks for your explanations.
Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 114
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It is not defined (in the datasheet) what is shifted in while you shift out the first 3 bytes.
But while you shift out the 4th byte, you should see 0x69 being shifted in.
It does not matter what you shift out as 4th byte but is makes sense to shift out 0x00 and check whether you shift in 0x69:
Code:
#define ENABLE_PROGRAMMING 0xAC530000L


I tried your sketch on a leonardo connected to an at89s52. I see this:
Code:
11111111111111111111111101101001
The 4th byte reads 0x69 indeed.

I extended the skech so it also reads in the signature bytes:
Code:

#define READ_SIGNATURE_0  0x28000000L
#define READ_SIGNATURE_1   0x28010000L
#define READ_SIGNATURE_2  0x28020000L
...

sendCmd(READ_SIGNATURE_0, true);
sendCmd(READ_SIGNATURE_1, true);
sendCmd(READ_SIGNATURE_2, true);

This gives me this extra output:
Code:

00000000001010000000000000011110
00000000001010000000000101010010
00000000001010000000001000000110
The last bytes from each line are: 0x1E 0x52 0x06, which is indeed the signature of an at89s52.



Logged

Offline Offline
Full Member
***
Karma: 9
Posts: 114
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A couple of years ago I wanted to explore the 8051. For this purpose I sampled two types of the more recent at89lp series. I added at89lp support into ArduinoISP so I could use avrdude to program the chips.
I digged up that project and am adding support for the at89s52 which looks not a lot of work. I can keep you posted if you want.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 21
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks PeterVH.

I don't know why my sketch doesn't work ?

The circuit is very easy; I have 4 pins connected to arduino for serial commands, 6Mhz crystal oscillator (The only one that I have for the time being)  with 2 22pF caps and Vcc + Gnd.

What do you think, the chip is faulty ?

Thks.
Logged

Pages: [1]   Go Up
Jump to: