View previous topic :: View next topic |
Author |
Message |
Marco27293
Joined: 09 May 2020 Posts: 126
|
PIC18F47J53 CCS C Compiler 5.090 mmcsd.c library issue |
Posted: Thu Jul 02, 2020 7:57 am |
|
|
Hi,
I'm using PIC18F47J53 CCS C compiler 5.090 and I want to write/read some data on a 1GB SD card using mmcsd library:
https://www.ccsinfo.com/forum/viewtopic.php?t=53787
This my user config in the library and a portion of my main which handles write and reading:
Code: | /////////////////////
//// ////
//// User Config ////
//// ////
/////////////////////
#include <stdint.h>
#PIN_SELECT SDO2=PIN_D2
#PIN_SELECT SDI2=PIN_D3
#PIN_SELECT SCK2=PIN_D4
#ifndef MMCSD_SPI_XFER
#if defined(MMCSD_SPI_HW)
#use spi(MASTER, MMCSD_SPI_HW, BITS=8, MSB_FIRST, MODE=0, baud=400000, stream=mmcsd_spi)
#else
#ifndef MMCSD_PIN_SCL
#define MMCSD_PIN_SCL PIN_D4 //o
#define MMCSD_PIN_SDI PIN_D3 //i
#define MMCSD_PIN_SDO PIN_D2 //o
#define MMCSD_PIN_SELECT PIN_B3 //o
#endif
#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=0, baud=400000, stream=mmcsd_spi)
#endif
#define MMCSD_SPI_XFER(x) spi_xfer(mmcsd_spi, x)
#endif
.....
//write case
case Wsd:
int16 resw=999;
int16 w_test=0;
int32 wwdata=0;
char varw=2;
MicroSD_Init();
for(w_test=0;w_test<1024;w_test++)
{
if(mmcsd_write_data(80000+w_test, 1, &varw)==0)
{
wwdata+=varw;
if(w_test % 512 == 0 && w_test > 500)
varw+=8;
if(debug)
usb_print_int16(wwdata, (char*)"W= ");
}
else
{
if(debug)
usb_print_int16(resw, (char*)"W= ");}
}
break;
//Read case
case Rsd:
int16 r_test=0;
int32 rrdata=0;
char varr;
MicroSD_Init();
for(r_test=0;r_test<1024;r_test++)
{
mmcsd_read_data(80000+r_test, 1, &varr);
rrdata+=varr;
if(debug)
usb_print_int16(rrdata, (char*)"R= ");
}
break;
|
I'm facing the following problem:
When I write more than 512 bytes to SD and then I read them mmcsd_read_data() functions read only the last 512 bytes recursively (e.g. if I write from address 0 to address 512 'A' from address 513 to 1024 'B' when I read from 0 to 1024 I get only 'B').
Could you help me please?
I'm currently blocked on a really important project...
Regards,
Marco |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9241 Location: Greensville,Ontario
|
|
Posted: Thu Jul 02, 2020 8:56 am |
|
|
While I don't use that PIC or SD cards.....
2 quick comments.
1) Is PIC VDD = 3 volts ? All SD cards I know are 3 volts, so you need proper logic level conversion to/from a 5 volt PIC
2) Are variable types correct ? you need unsigned int16 for numbers >255
Hopefully others who have used your PIC/SD combination will reply soon...
Jay |
|
|
Marco27293
Joined: 09 May 2020 Posts: 126
|
|
Posted: Thu Jul 02, 2020 9:02 am |
|
|
1) SD cards is supplied by 3.3 V (same of PIC)
2) Yes all variable types are correct |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Jul 02, 2020 9:48 am |
|
|
You need to call the mmcsd_flush_buffer at the end of your write.
Since you are sending 1024 bytes, this happens automatically when you
write the 513rd byte, but when you stop at 1024, the second sector
won't have been sent....
Seriously, writing and reading a block will be enormously more efficient. |
|
|
Marco27293
Joined: 09 May 2020 Posts: 126
|
|
Posted: Thu Jul 02, 2020 9:52 am |
|
|
if I use mmcsd_flush_buffer it returns a non-zero value.
It seems that I can see SD, I can write and read from RAM buffer but I'm unable to flush data from buffer to SD.
What could be the problem ?
Marco |
|
|
Marco27293
Joined: 09 May 2020 Posts: 126
|
|
Posted: Fri Jul 03, 2020 2:09 am |
|
|
Please give, me a feedback.
Now I try to write/read a block:
Code: | case Wsd:
int8 buff_sd[512]={};
int32 w_test=0;
int8 varw=0;
MicroSD_Init();
for(w_test=0;w_test<512;w_test++)
{
buff_sd[w_test]=varw++;
}
usb_print_int16(mmcsd_write_block(1024, 512, buff_sd),(char*)"WB= ");
delay_ms(4000);
for(w_test=0;w_test<512;w_test++)
{
buff_sd[w_test]=0;
}
usb_print_int16(mmcsd_read_block(1024, 512, buff_sd),(char*)"RK= ");
for(w_test=0;w_test<512;w_test++)
{
usb_print_int16(buff_sd[w_test],(char*)"SD= ");
delay_ms(300);
}
break; |
The write/read block functions return error 5 or 224 what does it mean ?
I also try mmcsd_flush_buffer() after performing mmcsd_write_data(a, n, p), but it also returns 5 or 224
Please help, I wait your reply |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Jul 03, 2020 9:17 am |
|
|
The code returned is the R1 code from the MMC card. Returned by
mmcsd_get_r1
Quote: |
bit: Signifies:
-----------------------------
7 - Command args out of range / CSD overwrite attempt
6 - Erase parameter is bad
5 - Write protected area violation
4 - Card ECC failed - internal data correction failed
3 - CC error - card controller failure
2 - General or unknown error occured
1 - Write protect erase skip
0 - Card is locked
|
So you are getting a 'general or unknown error', and 'card is locked'. For 5,
and 224, is a combination of the top three errors.
Honestly pretty much certainly implies you are not correctly talking to
the card.
Questions:
1) Do you have a good reservoir capacitor really close to the card. This is
totally 'required' the current drawn by an SD card spikes at several hundred mA during some momentary parts of the transaction...
You need something like a 47uF in parallel with a ceramic capacitor very
close to the card's Vdd connection.
2) Do you have the 10K pullups on the SDI pin to the PIC, and on Dat0 to
Dat3?. These are required:
Quote: |
All hosts shall provide pull-up resistors on all
data lines DAT[3:0] as described in section 6 of the SD Physical
Specification Version 1.01.
|
3) do you have the SD WP line pulled to ground?. It needs to be. If this was
floating could explain the write protect error. The SD needs both ground
pins connected and the WP pin pulled down. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Jul 03, 2020 9:33 am |
|
|
I've had my own headaches with SD cards in the past. If it is a power consideration, then you can try the circuit shown on this page in the section "Cosideration to Bus Floating and Hot Insertion":
http://www.elm-chan.org/docs/mmc/mmc_e.html
There are also various other schematics on that page. |
|
|
Marco27293
Joined: 09 May 2020 Posts: 126
|
|
Posted: Fri Jul 03, 2020 9:52 am |
|
|
Thanks a lot for your help.
It seems that I fixed the problem changing the 1GB microsd with a formatted 16 GB microsd (I updated driver version in order to manage this kind of microsd card). On SPI communication bus I only have the microsd.
Regarding Ttelmah hardware considerations:
1) We do not have 47uF in parallel with a ceramic capacitor(value?) connected and very close to card Vdd but on this supply line ( shared by microsd with other components) we have some 1uF//100nF couples. Could they be sufficient?
2)We are using SPI protocol so why we need pull-up on MISO? Can I set a proper pull-up on PIN_D3 (MISO in my layout) via firmware using pic18f47j53 internal pull-up or I need to change layout?
3) I'm using a 8 pin 16 GB microSD, which is WP pin ? I saw a lot of schematics but I didn't find it.
Thanks a lot for your support,
Now my board is working well but I want to clarify all these points in order to optimize my layout.
Best Regards,
Marco |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Fri Jul 03, 2020 11:59 am |
|
|
The pullups provide three different functions.
First they guarantee how the card 'wakes'. If these are not present it is
not guarnteed to wake up in the mode required by the SPI driver.
Then they slightly speed the rising edges of the signals.
Then they ensure the lines do not float when the card is removed.
They are required by the specs. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jul 03, 2020 1:14 pm |
|
|
Marco27293 wrote: |
Regarding Ttelmah hardware considerations:
|
Most of the questions you ask are answered by asmallri in a link provided
by Ttelmah in this post at the top of the forum:
Quote: | Sticky: Interfacing a 3.3v SD card with a 5v PIC |
Here is the post by asmallri:
http://www.ccsinfo.com/forum/viewtopic.php?t=56979&start=38 |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Sat Jul 04, 2020 1:30 am |
|
|
Micro SD doesn't have the WP line.
So don't worry about that one.
You really do need the reservoir close to the card.
An SD card can draw up to 200mA when writing, but the key problem is that
momentarily (for a few nSec), there can be current spikes, that are many
times this. Manufacturers recommend a capacitor with a good HF performance
is fitted within 10mm of the card's supply connection. It needs to be close. |
|
|
|