View previous topic :: View next topic |
Author |
Message |
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
Atmel Dataflash |
Posted: Wed Feb 09, 2005 11:59 am |
|
|
Hello.. I used some of the codes posted here for driving a 4Mbit dataflash. I can get the Status byte (so the SPI communication is good, and the Dataflash response). But when I tried to write or read the Buffer 1. It seems like it didn't write!
The pieces of code that I will post are the rutine to write and read the buffer (Note: Instead of using a 9bits address for the buffer, I used an 8bits one, and set the 9th to 0).
unsigned int8 ReadByteFromDataFlashBuffer1(unsigned int8 Address)
{
unsigned int8 Data;
DATA_FLASH_CS_LO();
//command
wrSPI(0x54); //BUFFER 1 READ
//don't care 8 bits
wrSPI(0);
//2 high address bits
wrSPI(0);
//8 low address bits
wrSPI(Address);
//don't care 8 bits
wrSPI(0);
Data = rdSPI();
DATA_FLASH_CS_HI();
return (Data);
}
void WriteByteToDataFlashBuffer1(unsigned int8 Address, unsigned int8 Data)
{
DATA_FLASH_CS_LO();
//command
wrSPI(0x84); //BUFFER 1 WRITE
//don't care 8 bits
wrSPI(0);
//2 high address bits
wrSPI(0);
//8 low address bits
wrSPI(Address);
//write data byte
wrSPI(Data);
DATA_FLASH_CS_HI();
}
This is part of the Main.. the part that writes and reads from the buffer 1
for (counter = 0; counter <= 200; counter++){
WriteByteToDataFlashBuffer1(counter,counter+40);
delay_ms(15);
}
delay_ms(1000);
for (counter = 0; counter <= 200; counter++) {
c = ReadByteFromDataFlashBuffer1(counter);
fprintf(PC,"Byte %C ::\n\r",c);
}
the %C caracter printed on the screen is nothing (0). Looking with an Scope, I see that really the Dataflash is "sending" 0. So the write is wrong...
Anybody may give me a hand?
THANKS VERY VERY MUCH
Kimi |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Feb 09, 2005 12:19 pm |
|
|
What does your rdSPI() function look like? Is it using spi_read()?
If your PIC is the SPI master, then you should generate clock pulses by calling read_spi() with a parameter, e.g. read_spi(0). |
|
|
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
|
Posted: Wed Feb 09, 2005 12:58 pm |
|
|
void wrSPI(unsigned int8 Shift)
{
unsigned int8 BitCnt = 8;
do
{
output_low(SCK);
delay_ms(2);
if(Shift & 0x80)
output_high(SDO);
else
output_low(SDO);
Shift <<= 1;
output_high(SCK);
delay_ms(2);
} while (--BitCnt);
}
unsigned int8 rdSPI(void)
{
unsigned int8 BitCnt = 8;
unsigned int8 Shift;
do
{
output_low(SCK);
delay_ms(2);
Shift <<= 1;
if(!(input(SDI))) //SDI Input 0
Shift &= ~1;
else
Shift |= 1; //SDI Input 1
output_high(SCK);
delay_ms(2);
} while (--BitCnt);
return(Shift);
}
These are the two rutines that drives the SPI. It's soft SPI in Master Mode. |
|
|
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
|
Posted: Wed Feb 09, 2005 2:52 pm |
|
|
I changed my main with this little main...
for (counter = 0; counter <=3; counter++) {
data = ReadDataFlashStatus();
fprintf (PC," %X ",data);
} //Receive 4 sattus bytes... al of them 9C (my dataflash is AT45DB041
while(ReadDataFlashStatus() != 0x9C);
WriteByteToDataFlashBuffer1(1,0x20);
while(ReadDataFlashStatus() != 0x9C);
data = ReadByteFromDataFlashBuffer1(1);
fprintf(PC," %X ",data); //Expected 20, but I see 0
while(ReadDataFlashStatus() != 0x9C);
WriteByteToDataFlashBuffer1(2,0x21);
while(ReadDataFlashStatus() != 0x9C);
data = ReadByteFromDataFlashBuffer1(1);
fprintf(PC," %X ",data); //Expected 21 in HEX, but I see 0
while(ReadDataFlashStatus() != 0x9C);
WriteByteToDataFlashBuffer1(3,0x22);
while(ReadDataFlashStatus() != 0x9C);
data = ReadByteFromDataFlashBuffer1(1);
fprintf(PC," %X ",data); //Expected 22, but I see 0
while(ReadDataFlashStatus() != 0x9C);
WriteByteToDataFlashBuffer1(4,0x23);
while(ReadDataFlashStatus() != 0x9C);
data = ReadByteFromDataFlashBuffer1(1);
fprintf(PC," %X ",data); //Expected 23, but I see 0
What's wrong with this?? asking and reading the Status byte is okey...but I cannot read from the Buffer.. NOTE: Again the scope says that the bytes are going out from the pic, and that the memory sends correctly every byte...
thanks
Kimi |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Wed Feb 09, 2005 4:50 pm |
|
|
good evening
your code seems to be correct, but not very clear
1 Which PIC is which frequency has
2 Which version of the compiler
3 how is to carry out convertion of voltage between PIC 5V and the Dataflash 3.3 V
bus SPI of the dataflash is faster than that of the PIC 10 MHz for a pic18 20Mhz for the dataflash
therefore you do not need to use the function delay_ms() between each bit
You must connect pine 1 of the dataflash for test the state of the dataflash (ready/busy)
before each reading or writing of data
After dataflash (CS = 0) you must await at least 200ns before start the reading writing
before putting CS = 0 you must position the clock signal correctly
reading SCK = 1
writing SCK = 0
I you council of reading the document of the dataflash to have more information
bon courrage
Alain |
|
|
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
|
Posted: Wed Feb 09, 2005 4:59 pm |
|
|
Thanks
I didn't know that I have to put the SCK in high or low before setting CS. As the Status Read function is working, the rest fo the code should work okey.
PICC Version is 3.216, the PIC18F877 running at 16Mhz.
I was reading the datasheet at the dataflash consumes 20mA or more when writing...mmm.. perhaps that's too much for the 3 diodes supply that Atmel propose in their datasheet, and that I use.
Thanks! Tomorrow at work.. I will reply to this messege and tell you how it was everything!
Thanks again
Kimi |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Feb 09, 2005 6:13 pm |
|
|
The 18F877 has a hardware SPI unit. Why don't you use it? Much faster, less code and you can use the CCS supplied functions for spi_read and spi_write. |
|
|
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
|
Posted: Wed Feb 09, 2005 8:52 pm |
|
|
When trying to use the HArdware SPI I cannot even read the status byte of the dataflash...
Up to now, the software version is working more than the Hardware |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Thu Feb 10, 2005 2:30 am |
|
|
bonjour
hello,
I had the same problem.
that came from my interface
you must connect
Pin1 (ready/Busy) converter 3.3 to 5V
Pin16 (SO) converter 3.3 to V
Pin2 (RESET) to 3.3 V
Pin3 (WP) to 3.3 V
Pin7 (VCC) to 3.3 V
Pin8 (GND) to GND !!
Pin13 (CS) converter 5 v to 3.3V
Pin14 (SCK) converter 5 v to 3.3V
Pin15 (SI) converter 5 v to 3.3V
SPI setting
Code: |
//--- PIC18F452 10Mhz+PLL
//--- Setup du SPI
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_4|SPI_SAMPLE_AT_END);
|
as long as you will not be able to read the status dataflash with bus SPI,
your installation is not correct
Bon courraga
Alain |
|
|
Kimi
Joined: 30 Jan 2005 Posts: 23 Location: Argentina
|
|
Posted: Thu Feb 10, 2005 4:59 am |
|
|
Well.... I have a 74LS245.. me be it's a good option to make the converter. Which IC do you use?. My dataflash doesn't have Ready/busy Pin...
I was thinking about doing this..
(SO) converter 3.3 V to 5 V (mey be a Transistor?, or a buffer?)
(RESET) to 3.3 V
(WP) to 3.3 V
(VCC) to 3.3 V
(GND) to GND !!
(CS) directly to PIC (as the Dataflash has 5V tolerant pins)
(SCK) directly to PIC (as the Dataflash has 5V tolerant pins)
(SI) directly to PIC (as the Dataflash has 5V tolerant pins)
All the schematic I saw, doesn't used converter, because the use the PIC at 3V... but I can't use the PIC at that voltage.
Thanks Very Much.
Kimi |
|
|
dvsoft
Joined: 28 Nov 2003 Posts: 46
|
|
Posted: Thu Feb 10, 2005 6:01 am |
|
|
re-bonjout
Ok,
i send to your email, ma drawing
Alain |
|
|
|