cancel
Showing results for 
Search instead for 
Did you mean: 

Programming Issue

erik
Associate II
Posted on April 30, 2013 at 11:30

Hi guys,

I've been working on interfacing an SD Card with this board but I'm having some issues on programming. The problem is that I've made a function to read a cluster on the sd card however as soon as I use that specific function the board just stop working by not executing anything that is in the main. The compiler doesn't give any errors so I don't know if it's a program issue or compiler . Here is the Main and the function that read the cluster.The IDE i'm using is CooCox CoIDE

//Here is the Main
int
main(
void
){
uint16_t Color = ACQUA;
uint8_t Buffer[4096];
Setup();
LCD_DrawLine(10,8 ,400,398,Color);
//These DrawLine doesn't works when I use the function SD_ReadCluster
LCD_DrawLine(10,9 ,400,399,Color);
LCD_DrawLine(10,10,400,400,Color);
LCD_DrawLine(10,11,400,401,Color);
LCD_DrawLine(10,12,400,402,Color);
SD_ReadCluster(2,Buffer);
//This is the function the makes the board not working
while
(1)
{
Delay(0xFFFFFF);
}
}
void
SD_ReadCluster(uint32_t cluster_number,uint8_t Cluster[]){
uint32_t lba_addr = cluster_begin_lba + (cluster_number - 2) * sectors_per_cluster;
uint16_t Var = 0xFF;
uint8_t i;
SD_CS_Clr;
SD_Data(CMD18);
SD_Data(lba_addr >> 24);
SD_Data(lba_addr >> 16);
SD_Data(lba_addr >> 8);
SD_Data(lba_addr);
SD_Data(0xFF);
while
(Var != R1_READY_STATE){
Var = SD_Read();
}
for
(i = 0;i < 8;i++){
while
(Var != DATA_START_BLOCK){
Var = SD_Read();
}
for
(Var = 0;Var < 512;Var++){
Cluster[Var + 512 * i] = SD_Read(0xFF);
}
SD_Read(); 
//16 bit CRC
SD_Read(); 
//16 bit CRC
}
SD_Data(CMD12);
SD_Data(0x00);
SD_Data(0x00);
SD_Data(0x00);
SD_Data(0x00);
SD_Data(0xFF);
while
(Var != R1_READY_STATE){
Var = SD_Read();
}
SD_CS_Set;
SD_Data(0xFF);
}
These Are the Functions SD_Read() and SD_Data()
void SD_Data(uint8_t Data){
 SPI1->DR = Data;
 while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
}
uint8_t SD_Read(){
 SPI1->DR = 0xFF;
 while( !(SPI1->SR & SPI_I2S_FLAG_RXNE) ); // wait until receive complete
 return SPI1->DR;
}

Thanks in advance for any help. #rtfm
15 REPLIES 15
Posted on April 30, 2013 at 15:05

''How do I make the stack bigger?''

 

The documentation for your chosen toolset will tell you that.
dthedens23
Associate II
Posted on April 30, 2013 at 17:39

you really did not solve the problem.  you just moved it.  Look at the size of the two large automatic variables. they add up to more than the new stack size.

look.  They teach you in school to use automatic variables.  But in the real world, those two big buffers should be global.  If sometime down the road, you create a function with another large automatic array variable, your stack will overflow again.

you should understand that microprocessors are resource limited and that part of the engineers job is to manage those resources.  Not just to make some code work.

erik
Associate II
Posted on April 30, 2013 at 21:27

Thank's for the hint but I tried to make it global and it didn't worked.

I tried this way:

In Main.c:
#include <SDCard.h>
int
main(
void
){
SD_Init();
SD_ReadCluster(2,Cluster);
while
(1)
{
Delay(0xFFFFFF);
}
}
In SDCard.h
extern
uint8_t Cluster[];

void SD_ReadCluster(uint32_t cluster_number,uint8_t Cluster[]);

void
SD_Init();
In SDCard.c
uint8_t Cluster[4096];

void
SD_ReadCluster(uint32_t cluster_number,uint8_t Cluster[]){
uint32_t lba_addr = cluster_begin_lba + (cluster_number - 2) * sectors_per_cluster;
uint16_t Var = 0xFF;
uint8_t i;
SD_CS_Clr;
SD_Data(CMD18);
SD_Data(lba_addr >> 24);
SD_Data(lba_addr >> 16);
SD_Data(lba_addr >> 8);
SD_Data(lba_addr);
SD_Data(0xFF);
while
(Var != R1_READY_STATE){
Var = SD_Read();
}
for
(i = 0;i < 8;i++){
while
(Var != DATA_START_BLOCK){
Var = SD_Read();
}
for
(Var = 0;Var < 512;Var++){
Cluster[Var + 512 * i] = SD_Read();
}
SD_Read(); 
//16 bit CRC
SD_Read(); 
//16 bit CRC
}
SD_Data(CMD12);
SD_Data(0x00);
SD_Data(0x00);
SD_Data(0x00);
SD_Data(0x00);
SD_Data(0xFF);
while
(Var != R1_READY_STATE){
Var = SD_Read();
}
SD_CS_Set;
SD_Data(0xFF);
}

Shouldn't this work?
Posted on May 01, 2013 at 15:09

''it didn't worked''

Tells us nothing.

http://www.catb.org/~esr/faqs/smart-questions.html#code

In what way(s), exactly, did it ''not work''?

What were you expecting to happen?

What actually happened?

What debugging have you  done to investigate the problem?

What did you discover in your debugging?

Some debugging tips:

http://www.8052.com/faqs/120313

http://www.eetimes.com/discussion/break-point/4027526/Developing-a-good-bedside-manner?pageNumber=0

erik
Associate II
Posted on May 01, 2013 at 16:22

The problem is the same as when I was having stack overflow despite I've now declared the array uint8_t Cluster[4096] as global, wich is that the function SD_ReadCluster() doesn't fill the array Cluster[] with any usefull data as it gets filled with 0 as debugger shows me, while it should have data.

Posted on May 01, 2013 at 18:22

Then you perhaps need to broaden your horizon, and look at the interactions with the SD card, is it throwing some type of error, can you read some other data/structures off the card. If you have a logic analyzer, may be you want to look at the signals to/from the card. Look at the functions being called.

It's not clear from your interactions here so far, what chip you're using, a board, if there is other software which is demonstrably working with the card. Do other SD or SDIO examples in the firmware libraries work with it?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..