View previous topic :: View next topic |
Author |
Message |
Guest
|
MMC read_block help |
Posted: Tue Jun 05, 2007 8:26 pm |
|
|
I'm trying to implement code that will read data from an MMC card using a 16F690. There is no writing to the MMC card, so I believe the memory required to store 512 bytes is unnecessary as it is my understanding this is required only for writing.
I've written a small test program that reads the first 10 addresses of the MMC card and I always read back 0xE7 for all ten addresses. I've looked at the card contents on a hex viewer and I would expect to see something much different.
It appears the card is initializing into SPI mode and the read bytes are valid. The data is simply not what it should be.
If anyone can tell me what I am doing wrong, I would appreciate it. I am using the MMC driver provided by CCS: mmc_spi.c. I've included the mmc_read_block function I am using below.
Here is my code:
Code: | #include <16F690.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN
#use delay(clock=4000000)
#use RS232(Baud=9600,XMIT=PIN_B7)
#include <mmc_spi.c>
int ptra;
int32 address = 0x00;
void main(void){
mmc_init();
for(address = 0x00; address < 0x0A; address++){
mmc_read_block(address, 1, *ptra);
printf("\n\r ptra = %X\n\r", ptra);
}
}
|
and the mmc_read_block function from mmc_spi.c:
Code: | //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// mmc_read_block()
// Reads a single block to the mmc
//
// Paramaters: address: address to read from
// block_size: number of bytes in block
// ptr: value to store bytes
//
// Returns: TRUE if valid data
// FALSE if not valid data
//
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MMC_EC mmc_read_block(int32 address, int16 block_size, int *ptr) {
int16 i;
int16 result;
mmc_select();
result=mmc_send_cmd(MMC_CMD_SET_BLOCKLEN,block_size,1);
mmc_deselect();
if (result) { // sets the block size
MMC_DEBUG(" ERR: couldn't set read block size\r\n");
return(MMC_EC_SET_READ_BLOCKSIZE_FAIL);
}
mmc_select();
result=mmc_send_cmd(MMC_CMD_READ_SINGLE_BLOCK,address,1);
if(result) // command to read byte at address
{
mmc_deselect();
MMC_DEBUG(" ERR: couldnt set read address\r\n");
return(MMC_EC_SET_READ_ADDRESS_FAIL);
}
mmc_receive_data(ptr,block_size);
mmc_deselect();
return(MMC_EC_OK);
} |
Thanks in advance for your help! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 05, 2007 8:39 pm |
|
|
Quote: | mmc_read_block(address, 1, *ptra); |
The last parameter is not correct. It's expecting a pointer to a buffer.
You are giving it the contents of an unsigned 8-bit integer variable. |
|
|
Guest
|
|
Posted: Wed Jun 06, 2007 11:17 am |
|
|
Thanks for the help.
I've modified the code so the last parameter in the read_block function is now an address. All I did was change the ptra variable to a 1x1 array and use the array name as the address. I'm still not receeiving correct data; the printf statements all return 0x00. Did I implement the pointer correctly?
New code:
Code: | #include <16F690.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR,NOBROWNOUT,NOIESO,NOFCMEN
#use delay(clock=4000000)
#use RS232(Baud=9600,XMIT=PIN_B7)
#include <mmc_spi.c>
int ptra[1];
int32 address = 0x00;
void main(void){
mmc_init();
for(address = 0x00; address < 0x0A; address++){
mmc_read_block(address, 1, ptra);
printf("\n\rptra = %X\n\r", ptra[0]);
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 06, 2007 11:53 am |
|
|
1. Post the #define statements that show the connections between
the PIC and the MMC.
2. Post your compiler version. |
|
|
RyanW Guest
|
|
Posted: Wed Jun 06, 2007 12:06 pm |
|
|
Here are the #define statements which are the very first lines of code for mmc_spi.c:
Code: |
/////*** USER CONFIG ***/////
//SanDisk’s MultiMediaCards clock data in on the rising edge and out on the falling edge.
#ifndef MMC_CLK
#define MMC_CLK PIN_B6
#endif
#ifndef MMC_DI
#define MMC_DI PIN_B4
#endif
#ifndef MMC_DO
#define MMC_DO PIN_C7
#endif
#ifndef MMC_CS
#define MMC_CS PIN_C5
#endif |
and my compiler version is 4.037
Again, thanks for your help |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 06, 2007 12:29 pm |
|
|
1. Here is a page with the MMC connector pin assignments.
http://www.interfacebus.com/Multi_Media_Card_Pinout_MMC.html
Post the connections between the MMC pins and your PIC pins.
Post the physical connections between pins that exist in actual wiring.
For example:
PIC pin 11 (RB6) goes to pin MMC pin 1 (CS).
2. Do you have power and ground connected to the MMC ?
(Do you have a ground connection between the PIC and the MMC ?)
3. What is the Vdd voltage of your PIC ? |
|
|
kevcon
Joined: 21 Feb 2007 Posts: 142 Location: Michigan, USA
|
|
Posted: Wed Jun 06, 2007 12:35 pm |
|
|
Don't know if it applies to 16F parts but there is a bug with 4.037 and serial communications on 18F parts. |
|
|
RyanW Guest
|
|
Posted: Wed Jun 06, 2007 2:31 pm |
|
|
1. MMC connections:
PIC pin 5 (RC5) goes to MMC pin 1 (nCS).
PIC pin 9 (RC7) goes to MMC pin 2 (Data-in - reference to MMC)
PIC pin 11 (RB6) goes to MMC pin 5 (CLK)
PIC pin 13 (RB4) goes to MMC pin 7 (Data-out - reference to MMC)
All of these lines have 10k pullups.
2. Both MMC and PIC have good power and ground connections. Currently, the power supply is a lab supply and the power is clean. No ripple.
3. Vdd voltage of PIC is same as MMC: 3.3V
One more note: I have updated the code to read 32 bytes at a time and then it loops and starts reading all of the addresses. I have found some of the bits can be successfully read: namely the 511th and 512th byte of the card (the last 2 bytes of the first sector). I have tried two cards and both have the same value for these bytes: 0x55 and 0xAA respectively. All other bits are read as 0x00. These two, however, are read correctly and consistently. These are very unique bytes as they consist of a series of 1s and 0s (0x55 = 0b01010101 and 0xAA = 0b10101010). I know there is some significance of this: I just haven't figured out what it is yet |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
RyanW Guest
|
|
Posted: Wed Jun 06, 2007 4:17 pm |
|
|
YES! Thanks PCM programmer... so everything was working fine the whole time. Once I corrected the initial offset, all the bits match what I expect. Seems the only issue I had this whole time was a hex editor that did not display the first MBR. UGH!
I appreciate your help! Thanks again. |
|
|
SAMQUR
Joined: 29 Sep 2011 Posts: 1
|
|
Posted: Mon Nov 19, 2012 2:40 am |
|
|
RyanW wrote: | YES! Thanks PCM programmer... so everything was working fine the whole time. Once I corrected the initial offset, all the bits match what I expect. Seems the only issue I had this whole time was a hex editor that did not display the first MBR. UGH!
I appreciate your help! Thanks again. |
RyanW could you please share your working code with us. I am trying to interface MMC card with Pic18F4520, but it not working at all.
I am using following driver and example mention in this code, but its not working :(
Code: |
// If you want to use hardware SPI comment this line.Software SPI are slower, but you can use any clock and data pins that you want..
//#define MMC_SPI_SOFTWARE //***************
#use FAST_IO(B) // Change this to set the right tris for your pins
#define SET_MMC_TRIS() TRISC= 0b10010000
/*
#define ChipDout pin_B6 // SPI-Data out pin MOSI
#define ChipDin pin_B4 // SPI-Data in pin MISO
*/
#define ChipSel pin_c2 // Chip-Select pin
#define ChipClk pin_C3 // SPI-Clock pin
#define ChipDout pin_C5 // SPI-Data out pin MOSI
#define ChipDin pin_C4 // SPI-Data in pin MISO
#use spi(DI=PIN_C4, DO=PIN_C5, CLK=PIN_C3, BITS=8,MSB_FIRST) |
++++++++++++++++++++++++
1453 lines deleted. Replaced with differences from Miniman code:
https://www.ccsinfo.com/forum/viewtopic.php?t=31532
Do not re-post code from Code Library. Post differences only.
- Forum Moderator
++++++++++++++++++++++++
Will you please help me.
Thanks In advance |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Nov 19, 2012 3:55 am |
|
|
Quote: | RyanW could you please share your working code with us. |
How much chance do you give yourself to get an answer from a guest user who was here over 5 years ago?
As you can read from the discussed topic, the latest code posted was the working version.
When your code is not working then I suspect there is another problem, not related to the problem discussed here.
Can you get the ex_mmcsd.c example working?
If not, then start a new topic, don't reply below here . Describe how your PIC is connected to the flash card. What type of flash card and size? Your CCS software version.
Don't post the ex_mmcsd program code, we all have it. Only post the lines you modified. |
|
|
|