View previous topic :: View next topic |
Author |
Message |
artohautala
Joined: 17 Nov 2011 Posts: 187
|
what's wrong with my code to use M25p128 flash memory |
Posted: Fri Jan 06, 2012 6:20 am |
|
|
hello dear friends,
(sorry my bad english I'm from finland)
I am hobbyist and that is why I am not so good in programming embedded systems ... sorry
I'm trying to get M25P128 flash memory module (digilent inc.) to work...
I tested my hardware sending pulses to outputs via software and look
those pulses with oscilloscope and I send to input 3.3 V pulses from function generator--- they seems to be OK I think. The memory module use 3.3V
supply voltage and I use level controllers (ready made modules) and of course 3.3 v power supply...
but please help and tell me what's wrong with my code!
Code: |
//***********************************************************************************************
int flash_test()
{
//I'm using flash memory M25P128
//chip is in the module: PmodSF - Serial Flash ROM, 128 Mbit
// link to digilent: PmodSF - Serial Flash ROM, 128 Mbit:
//http://www.digilentinc.com/Products/Detail.cfm?Prod=PMOD-SF
//I think my flash memory is bulk erased ready because it's the new one... but I'm not sure
//but in the datasheet they tell it's ready bulk erased (all the bits are HIGH)
// function definitions:
//int flash_test();
//void send_FLASH_address(void);
//short int flash_busy(void);
//definitions before main():
//#define flash_SEL PIN_C2 //flash-memory chip select, hardware tested OK
//#define flash_SCL PIN_C3 //flash-muistin kellolinja, hardware tested OK
//#define flash_MISO PIN_C4 //flash-memory serial out MISO (master in slave out)TEST OK
//#define flash_MOSI PIN_C5 //flash-memory serial in MOSI (master out slave in)TEST OK
//#define flash_WREN 0x06 //flash memory write enable
//#define PAGE_prog 0x02 //flash memory page program enable
//#define flash_RSTATUS 0x05 //flash memory READ status
//#define flash_READ 0x03 //flash memory READ enable
//#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
//#define SPI_MODE_1 (SPI_L_TO_H)
//#define SPI_MODE_2 (SPI_H_TO_L)
//#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
//global int32 flash_addr;
// this setup is in main(): setup_spi(spi_master|spi_mode_0 |spi_clk_div_16 );
int8 data = 0xAA;
int8 read_data;
output_low(flash_sel); //flash memory selected
spi_write(flash_WREN); //write ON
output_high(flash_sel); //flash memory not selected
output_low(flash_sel); //flash memory ON
spi_write(PAGE_prog); //page programming ON
flash_addr = 0xFC0000;
send_FLASH_address(); //send flash address
spi_write(data); //send data
output_high(flash_sel); //flash memory OFF
clearTextBuffer();clear_text();
clearTextBuffer();strcpy(teksti,"flash writes"); text_xy(0,3);text(teksti);
while(flash_busy()); //wait until writing has finished
//MY TEST PROGRAM STAYS HERE FOREVER !!!
delay_ms(2000);
//read if writing is ok:
output_low(flash_sel);
spi_write(flash_READ);
send_FLASH_address();
read_data = spi_read(0);
output_high(flash_sel);
if(read_data == data)
{
clearTextBuffer();clear_text();
clearTextBuffer();strcpy(teksti,"flash_test OK!"); text_xy(0,3);text(teksti);
}
else
clearTextBuffer();clear_text();
clearTextBuffer();strcpy(teksti,"flash_test EI!!OK!"); text_xy(0,3);text(teksti);
//end of the test program
poo:;goto poo;
}
//***********************************************************************************************
short int flash_busy(void)
{
int8 status;
output_low(flash_sel); //flash ON
spi_write(flash_RSTATUS); //read memory status
output_high(flash_sel); //flash OFF
output_low(flash_sel); //flash ON
status = spi_read(0);
output_high(flash_sel); //flash OFF
return (bit_test(status, 0)); //test. write in progress WIP bit if busy bit is HIGH
}
//***********************************************************************************************
void send_FLASH_address(void) //send flash address
{
spi_write(((flash_addr >> 16) & 0xff));
spi_write(((flash_addr >> 8) & 0xff));
spi_write((flash_addr & 0xff));
}
//*********************************************************************************************** |
|
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Fri Jan 06, 2012 6:42 am |
|
|
I'm pretty sure your flash_busy() routine should not deselect and then reselect the flash memory. It should do it in one SPI session:
Code: |
short int flash_busy(void)
{
int8 status;
output_low(flash_sel); //flash ON
spi_write(flash_RSTATUS); //read memory status
// These next two lines cause a problem - they are not needed.
// output_high(flash_sel); //flash OFF
// output_low(flash_sel); //flash ON
status = spi_read(0);
output_high(flash_sel); //flash OFF
return (bit_test(status, 0)); //test. write in progress WIP bit if busy bit is HIGH
}
|
...and the #defines should not be comments. Its simply
Code: |
#define SOMETHING
not
//#define SOMETHING
|
RF Developer
Last edited by RF_Developer on Fri Jan 06, 2012 6:44 am; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Jan 06, 2012 6:44 am |
|
|
First problem you don't tell us which PIC and compiler you're using !
We HAVE to know that before we can comment on the code
however...
...since it stays waiting for 'flash_busy' to go low, I'd check that you have proper pullup resistors on all the SPI lines.
One common reoccouring problem is trying to get a 5V PIC to work with 3V 'devices'.Unless you have the proper 'logic level convertor' interface chip in between, your project will never work.
Post your PIC type and compiler and we can help.. |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 6:53 am |
|
|
RF_Developer wrote: | I'm pretty sure your flash_busy() routine should not deselect and then reselect the flash memory. It should do it in one SPI session:
Code: |
short int flash_busy(void)
{
int8 status;
output_low(flash_sel); //flash ON
spi_write(flash_RSTATUS); //read memory status
// These next two lines cause a problem - they are not needed.
// output_high(flash_sel); //flash OFF
// output_low(flash_sel); //flash ON
status = spi_read(0);
output_high(flash_sel); //flash OFF
return (bit_test(status, 0)); //test. write in progress WIP bit if busy bit is HIGH
}
|
...and the #defines should not be comments. Its simply
Code: |
#define SOMETHING
not
//#define SOMETHING
|
RF Developer |
Thanks for your answer sure I know that #define it was only because of this discussion forum ... they are ok ...
but I think you are the real think reader because I just before you answered my question tested in the same way :
// These next two lines cause a problem - they are not needed.
// output_high(flash_sel); //flash OFF
// output_low(flash_sel); //flash ON
but it did not solved the problem ...
thanks for your answer anyway
[/quote] |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 7:51 am |
|
|
temtronic wrote: | First problem you don't tell us which PIC and compiler you're using !
We HAVE to know that before we can comment on the code
however...
...since it stays waiting for 'flash_busy' to go low, I'd check that you have proper pullup resistors on all the SPI lines.
One common reoccouring problem is trying to get a 5V PIC to work with 3V 'devices'.Unless you have the proper 'logic level convertor' interface chip in between, your project will never work.
Post your PIC type and compiler and we can help.. |
OK thank for your answer
pic chip is pic18f452 and compiler version is 4.018
maybe the problem occurs for pullup ressitors because I'm using the level converter like this but I think there are pullups resistors ....
10 kohms...
this is chematic of my levael converter:
but how I can send my pdf file ?? |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Jan 06, 2012 8:01 am |
|
|
Classic 5V vs 3V problem !
You MUST use a 'logic level' convertor IC between the 5 volt PIC and the 3 volt SPI device
or
use an 'L' version ( Low voltage ) PIC.
NO combination of 'pullup' resistors will work, as the 3 volt device cannot ever gibe the 5 volt PIC the 4 volt minimum for a logic '1'. |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 8:13 am |
|
|
temtronic wrote: | Classic 5V vs 3V problem !
You MUST use a 'logic level' convertor IC between the 5 volt PIC and the 3 volt SPI device
or
use an 'L' version ( Low voltage ) PIC.
NO combination of 'pullup' resistors will work, as the 3 volt device cannot ever gibe the 5 volt PIC the 4 volt minimum for a logic '1'. |
I like to send my shematics [/img]
but how I can send my pdf files to this forum ?
if you look my first question I'm using logic converter and now I like to send my shematic level converter and pic18f452
thanks for your good answers!
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 8:20 am |
|
|
temtronic wrote: | First problem you don't tell us which PIC and compiler you're using !
We HAVE to know that before we can comment on the code
however...
...since it stays waiting for 'flash_busy' to go low, I'd check that you have proper pullup resistors on all the SPI lines.
One common reoccouring problem is trying to get a 5V PIC to work with 3V 'devices'.Unless you have the proper 'logic level convertor' interface chip in between, your project will never work.
Post your PIC type and compiler and we can help.. |
my chip is pic18f452 and compiler version is 4.018 |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Jan 06, 2012 8:51 am |
|
|
You need to save /post your schematic on one of the free web hosting sites then add that 'link' in a posting here.
There is much confusion about proper interfacing of mixed voltage devices here and some think that 'pullup' resistors ARE logic level convertors, which is wrong.
When posting code it's nice to get rid of any comments in the program from previous attempts that may confuse readers. |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 9:08 am |
|
|
temtronic wrote: | You need to save /post your schematic on one of the free web hosting sites then add that 'link' in a posting here.
There is much confusion about proper interfacing of mixed voltage devices here and some think that 'pullup' resistors ARE logic level convertors, which is wrong.
When posting code it's nice to get rid of any comments in the program from previous attempts that may confuse readers. |
ok sorry I understand this:
<When posting code it's nice to get rid of any comments in the program from previous attempts that may confuse readers>
ok I understand this:
<then add that 'link' in a posting here.>
<You need to save /post your schematic on one of the free web hosting sites>
but this is too difficult to me understand .... please explain more
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 12:08 pm |
|
|
temtronic wrote: | You need to save /post your schematic on one of the free web hosting sites then add that 'link' in a posting here.
There is much confusion about proper interfacing of mixed voltage devices here and some think that 'pullup' resistors ARE logic level convertors, which is wrong.
When posting code it's nice to get rid of any comments in the program from previous attempts that may confuse readers. |
Ok... this is link to my level converter shematic:
http://tinyurl.com/7ft6fvh
and this link is shematic:
http://tinyurl.com/76fo9rx |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Jan 06, 2012 12:32 pm |
|
|
With those 10k resistors in the level converter speed may be an issue. Could you verify with a scope that the signal slew rates are OK. Or try 1K resistors. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
Posted: Fri Jan 06, 2012 12:50 pm |
|
|
SherpaDoug wrote: | With those 10k resistors in the level converter speed may be an issue. Could you verify with a scope that the signal slew rates are OK. Or try 1K resistors. |
I don't believe it is the reason
and I'm using ready made module and it is very difficult to change
those SMS resistors |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
artohautala
Joined: 17 Nov 2011 Posts: 187
|
|
|
|