2013-04-30 02:30 AM
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
2013-04-30 02:50 AM
The compiler doesn't give any errors so I don't know if it's a program issue or compiler
The compiler will translate any nonsense, as long as it is syntactically correct. So I bet it IS a programm issue. Some more interesting parts of the source code are missing, especially your peripheral setup.
LCD_DrawLine(10,8 ,400,398,Color);
//These DrawLine doesn't works when I use the function SD_ReadCluster
Have you checked that your SD card driver does not try to use the same IO lines as your LCD driver software ?
2013-04-30 03:41 AM
Yes SD Card Init is not interfering with the LCD one and
a prove of that is that SD_ReadSector(), wich is a similar function, works./*-----------------------------------------------------------------*/
/*----------------------SD_ReadSector---------------------------*/ This is a similar function to SD_ReadCluster the only change
/*-----------------------------------------------------------------*/ is that it need a 512 uint8_t array instead of 4096
void SD_ReadSector(uint32_t Address,uint8_t Buffer[]){
uint16_t Var;
SD_CS_Clr;
SD_Data(CMD17);
SD_Data(Address >> 24);
SD_Data(Address >> 16);
SD_Data(Address >> 8);
SD_Data(Address);
SD_Data(0xFF);
while (Var != R1_READY_STATE){
Var = SD_Read();
}
while (Var != DATA_START_BLOCK){
Var = SD_Read();
}
for (Var = 0;Var <
512
;Var++){
Buffer[Var] = SD_Read();
}
SD_Read(); //16 bit CRC
SD_Read(); //16 bit CRC
SD_CS_Set;
SD_Data(0xFF);
}
/*-----------------------------------------------------------------*/
/*----------------------SD_ReadCluster---------------------------*/--Corrected a few Errors I found but still not working.
/*-----------------------------------------------------------------*/
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);
}
#define CMD17 0X11 + 0x40
#define CMD18 0x12 + 0x40
#define CMD12 0x0C + 0x40
#define R1_READY_STATE 0X00
#define DATA_START_BLOCK 0XFE
2013-04-30 04:02 AM
Yes SD Card Init is not interfering with the LCD one and
a prove of that is that SD_ReadSector(), wich is a similar function, works.
Not, it is not. In case you inadvertendly try to use one GPIO pin for two purposes, the last one initialised will win. Did you try to test you SD card software alone, i.e. without LCD and other code ? Have you tried the debugger ?
2013-04-30 05:08 AM
I've tested the SD Card library I made alone and it works as long as I don't use SD_ReadCluster() and I've also noticed one thing wich is that using that function breakes everything for example I tried to do SD_ReadSector(address,Buffer) before SD_ReadCluster(address,Cluster) and it somehow corrupted the result in the array of readSector since it's not returning the same thing as when I use it alone at least that is what debugger shows me.
int main(void){
uint8_t Buffer[512];
uint8_t Cluster[4096];
SD_Init();
SD_ReadSector(cluster_begin_lba,Buffer);
SD_ReadCluster(2,Cluster);
//Buffer array gets filled with 0 instead of data
//Cluster gets filled with 0 till around the 2057 element and the rest has some data
while(1)
{
Delay(0xFFFFFF);
}
}
I've also checked again pins and the init functions, and they aren't conflicting at all because the LCD is on PORTB and PORTD while the sd card is only on PA4,PA5,PA6,PA7.
I really don't know what's wrong with this...
2013-04-30 05:17 AM
I really don't know what's wrong with this...
int main(void){
uint8_t Buffer[512];
uint8_t Cluster[4096];
A plain stack overflow, perhaps ?
2013-04-30 05:20 AM
And how to solve such problem? Sorry but I don't really know what's a stack overflow.
2013-04-30 05:28 AM
Choices:
Move the local allocation, which consumes stack space, to being a global or static. Make the stack bigger.2013-04-30 05:35 AM
I tried to make them global and static but both doesn't work as it returns both array completly filled with 0. How do I make the stack bigger?
EDIT: Problem Solved the stack size was of only 512 words.All I need was to to change in the startup.c file the #define STACK_SIZE 0x00000200 to #define STACK_SIZE 0x00001000.Thank you very much for the help guys.2013-04-30 06:05 AM
I tried to make them global and static but both doesn't work as it returns both array completly filled with 0.
After startup, this is normal, as it is becomes initialised when global/static global. If it's still zero after your application is assumed to have it filled, you probably did something wrong.
How do I make the stack bigger?
Not sure how to do it in Coocox/CoIDE. You could consult the documentation. At worst you might need to hack the linker scipt /startup code.