CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

stack problem with mmcsd_load_buffer

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Fabrici



Joined: 11 Feb 2013
Posts: 19
Location: Toulouse - France

View user's profile Send private message

stack problem with mmcsd_load_buffer
PostPosted: Wed Mar 06, 2013 11:15 am     Reply with quote

Hello,

I'm using CCS PCD C Compiler, Version 4.140, 7054

I'm currently trying to communicate between a DSPIC33EP256GP506 and a micro SD card. The function "mmcsd_init()" is returning a bad code (MMCSD_ADDR_ERR), so I build a minimal code to try to isolate the problem. Here is my test code :

Code:
#include <33EP256GP506.h>

#FUSES ICSP1   // ICD uses PGC1/PGD1 pins
#FUSES NOJTAG   // JTAG disabled
#FUSES DEBUG   // Debug mode for use with ICD
#fuses NOALTI2C1   // I2C1 mapped to ASDA1/ASCL1 pins
#fuses NOALTI2C2   // I2C2 mapped to ASDA2/ASCL2 pins
#FUSES NOWDT   // No watchdog timer
#FUSES FRC_PLL  //Internal Fast RC oscillator with PLL

#BUILD (STACK=0x1000:0x1200)
#USE delay (clock=50000000)

// Déclarations pour carte SD
#use fast_io(a)
#use fast_io(c)
#define MMCSD_PIN_SCL      PIN_C3      // o
#define MMCSD_PIN_SDI      PIN_A9      // i
#define MMCSD_PIN_SDO      PIN_A4      // o
#define MMCSD_PIN_SELECT   PIN_C12      // o
#include <mmcsd.c>

/////////////////////////
// Programme principal //
/////////////////////////

void main()
{
   int iRes;

   //
   //// Lecture des valeurs dans la carte SD

   iRes = mmcsd_init();

///////////////////////////
//// Boucle principale ////
///////////////////////////

   do
   {

   } while (TRUE); 
}


When I debug the code, everything seems to go well until the last instruction of the mmcsd_init() : r1 = mmcsd_load_buffer(); .

code of the mmcsd_load_buffer() :

Code:
MMCSD_err mmcsd_load_buffer(void)
{
   g_MMCSDBufferChanged = FALSE;
   return(mmcsd_read_block(g_mmcsdBufferAddress, MMCSD_MAX_BLOCK_SIZE, g_mmcsd_buffer));
}


Here, g_mmcsdBufferAddress is set to 0x00000000 . But when I continue to debug :

Code:
MMCSD_err mmcsd_read_block(uint32_t address, uint16_t size, uint8_t* ptr)

   MMCSD_err ec;
   uint16_t i; // counter for loops
...


address has been changed to 0x20082008 .... and code will exit with MMCSD_ADDR_ERR.

I'm not fluent with assembler language, but I hope it will help : here are some parts of the .lst file :

Code:
....................    return(mmcsd_read_block(g_mmcsdBufferAddress, MMCSD_MAX_BLOCK_SIZE, g_mmcsd_buffer));
0053A:  PUSH    1402
0053C:  POP     140A
0053E:  PUSH    1404
00540:  POP     140C
00542:  MOV     #200,W4
00544:  MOV     W4,140E
00546:  MOV     #1202,W4
00548:  MOV     W4,1410
0054A:  CALL    46C


I'm a bit surprised with the adresses of the push and pop, which seems to not be in the stack, but as I said before I'm not very fluent in assembler language.

Has anybody encountered this kind of problem ?

Best regards

Fabrice
Fabrici



Joined: 11 Feb 2013
Posts: 19
Location: Toulouse - France

View user's profile Send private message

PostPosted: Thu Mar 07, 2013 11:56 am     Reply with quote

Looking at the .sym file, I was surprised to see same address allocated to different symbols involved in my problem :

1408 mmcsd_init.i
1409 mmcsd_init.r1
140A mmcsd_crc_on_off.crc_enabled
140A-140D mmcsd_set_blocklen.blocklen
140A-140D mmcsd_read_block.address

140E-140F mmcsd_read_block.size
1410-1411 mmcsd_read_block.ptr
1412 mmcsd_read_block.ec

But maybe this is a misunderstanding on my part. Could someone explain to me this assignment ?

Best regards,

Fabrice
Ttelmah



Joined: 11 Mar 2010
Posts: 19343

View user's profile Send private message

PostPosted: Thu Mar 07, 2013 12:54 pm     Reply with quote

That is quite normal.
Variables form a 'tree'. The bottom layer is the global variables. Then static variables. Then local variables in main, then local variables in each function, and so on as functions call functions.
A function at the top of the tree uses a variable in a memory location. When it exits, this memory is no longer in use, and another function at the same level can re-use the same memory. This is why it is vital to never 'assume' that a variable will hold it's value (and to declare as static if you want it to do so).
Memory is re-used, and several variables in different routines can use the same area. The key is (of course), that functions that call other functions, cannot have an area re-used by routines that it calls.
Your problem has come up not that long ago. If I remember, I tried the code, duplicated the problem, and fixed it, and posted this. I can't remember what it was and on a different machine, so can't look up my notes. A search should find the thread. Was it the one where the functions required ANSI mode?. I think it might be.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group