View previous topic :: View next topic |
Author |
Message |
rtrombetta
Joined: 14 Oct 2011 Posts: 8
|
SD Card Speed using PIC18F4550 |
Posted: Fri Oct 28, 2011 8:08 am |
|
|
Hi!
I´m using PIC18F4550 (CCSC v4.120) running at 5V with bulk mode USB and a SanDisk 2.0G micro SD Card running in SPI mode (using PIC HW SPI). The SD card is using 3.3V and a 74HC126 buffer powered with 3.3v between PIC and SD in CS,SCLK and SDI lines. A pull up resistor (15K) between 3.3v and SD SDO line. Everything is working well except the speed.
I need to read the card as fast as possible and send data to USB port using bulk mode. I´m using the mmcsd.c library from CCS.
This library starts with...
Code: |
#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=0, stream=mmcsd_spi)
|
the pins specified at the header matches hw_spi of 18f4550, but I receive an error when i call mmcsd_init();
I think that is because I need to init the card with 400Kbps, then i putted a line at the start of the mmcsd_init function and other at the end:
Code: |
// start of mmcsd_init
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_64);
// before the "return" of mmcsd_init function
// I´m playing with this line to find a suitable speed without errors
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_T2);
|
and this worked well and the card inits without errors, but I still having problems to speed up the read rates.
fuses:
Code: |
#fuses MCLR
#fuses HSPLL
#fuses NOWDT
#fuses PROTECT
#fuses NOLVP
#fuses NODEBUG
#fuses USBDIV
#fuses PLL5
#fuses CPUDIV1
#fuses VREGEN
#fuses NOCPD
#fuses NOBROWNOUT
#fuses NOFCMEN
#fuses PUT
#use delay(clock=48000000)
|
most relevant code of main routine that reads the sdcard and send to the usb port:
Code: |
// the next line is used to switch the spi bus speed st runtime
while (address < size) {
if (mmcsd_read_data(address,256,&out_data)) {
lcd_putc("\fError reading card");
delay_ms(1000);
}
if (!usb_puts(1,out_data,256,100)) {
lcd_putc("\fError sending data");
delay_ms(1000);
}
address = 256;
} |
The data rate between PIC and PC is 44KB/s. I commented out lines for read the SD card and the data rate rises to 300KB/s (2.4Mbps). So i think that the problem is the spi bus speed.
Using
Code: |
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_64);
|
everything works well, but i can only send data at 33KB/s to USB.
using
Code: |
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16); // div_16 or less
|
the mmcsd_read_data() function returns error.
using
Code: |
setup_timer_2(T2_DIV_BY_1,2,1);
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_T2);
|
everything works well and data rate rises to 44KB/s. If I try to rise the timer_2 speed, the read function returns error too.
All parts are mounted using a pre-drilled experimenters board and wired using hard disk flat cables. What can be happening ? Someone experimented these problems too ? Can be these issues due a high capacitance ? How can I measure capacitance at the clock line ? I have an old osciloscope (analog 20Mhz) and tryed to measure the clock line but I think there´s something wrong with this scope. It´s giving about 100ns of rise time. I don´t know if it is a osciloscope problem, or if really there´s a high capacitance at the clock line.
Last edited by rtrombetta on Fri Oct 28, 2011 11:24 am; edited 2 times in total |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Fri Oct 28, 2011 10:31 am |
|
|
The following assumes you have a pullup on the DO output of the card.
When initializing the SD/MMC interface the SPI bus speed must be less that 400Khz. Once the basic initialization has been done you can increase the SPI clock speed to 10MHz or higher.
I cannot tell from your code snippet where you are in the initialization sequence. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
rtrombetta
Joined: 14 Oct 2011 Posts: 8
|
spi speed |
Posted: Fri Oct 28, 2011 11:22 am |
|
|
Hi Andrew!
Thanks for reply!
Yes, I'm using a pull up resistor between 3.3v and card's SDO line.
I'm using mmcsd.c library from CCS to r/w the card.
At the start of this library the "#use spi" is called without parameters so it will try to communicate as fast as possible with the card.
For this reason I modified the mmcsd_init() function and putted a setup_spi at the start of this function to fall the speed to an acceptable speed (SPI_CLK_DIV_64).
At the end of this function (before it returns) I putted setup_spi again to play with clock rates after the initialization.
I really don't know if the "setup_spi()" function works together with "#use spi()". I think this is the only way to change the speed at runtime. |
|
|
rtrombetta
Joined: 14 Oct 2011 Posts: 8
|
Osciloscope Images |
Posted: Fri Oct 28, 2011 4:17 pm |
|
|
I found the problem, but not the solution.
These are the pictures that i took from oscope:
1-Clock Line at SPI_CLK_DIV_64 (750Khz).
1v/div x 0.2us/div
Level ~3.2V
rise time=320ns (very high)
2-Clock Line at SPI_CLK_DIV_64 (750Khz).
1v/div x 0.5us/div
Level ~3.2V
3-Clock Line at SPI_CLK_DIV_16 (3Mhz).
1v/div x 0.2us/div
Level ~2.4V (clock signal does not rises to 3.3V)
4-Clock Line at SPI_CLK_DIV_4 (12Mhz).
1v/div x 0.2us/div
Level min~1V max~2.4v (clock signal does not rises to 3.3v and not falls to 0V)
5-Clock Line at SPI_CLK_T2 (2Mhz).
1v/div x 0.2us/div
Does anyone have any sugestion to correct this problem ? Maybe this will not happen when I build this circuit in a PCB with the correct layout. |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
Re: Osciloscope Images |
Posted: Fri Oct 28, 2011 6:19 pm |
|
|
rtrombetta wrote: | I found the problem, but not the solution.
These are the pictures that i took from oscope:
1-Clock Line at SPI_CLK_DIV_64 (750Khz).
1v/div x 0.2us/div
Level ~3.2V
rise time=320ns (very high)
2-Clock Line at SPI_CLK_DIV_64 (750Khz).
1v/div x 0.5us/div
Level ~3.2V
3-Clock Line at SPI_CLK_DIV_16 (3Mhz).
1v/div x 0.2us/div
Level ~2.4V (clock signal does not rises to 3.3V)
4-Clock Line at SPI_CLK_DIV_4 (12Mhz).
1v/div x 0.2us/div
Level min~1V max~2.4v (clock signal does not rises to 3.3v and not falls to 0V)
5-Clock Line at SPI_CLK_T2 (2Mhz).
1v/div x 0.2us/div
Does anyone have any sugestion to correct this problem ? Maybe this will not happen when I build this circuit in a PCB with the correct layout. |
You have a hardware fault. Do you have filtering and bypass capacitors? Is your 3.3 volt rail stable? _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Sun Oct 30, 2011 11:05 pm |
|
|
Are you using a x10 probe that hasn't been adjusted to the scope's input?
I can make waveforms exactly like that with a x10 and any square wave while the compensation cap is out of cal.
Also, what's the bandwidth of your scope (and the probe - they can be different)
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1634 Location: Perth, Australia
|
|
Posted: Sun Oct 30, 2011 11:18 pm |
|
|
bkamen wrote: | Are you using a x10 probe that hasn't been adjusted to the scope's input?
I can make waveforms exactly like that with a x10 and any square wave while the compensation cap is out of cal.
Also, what's the bandwidth of your scope (and the probe - they can be different)
-Ben |
I initially thought something similar but that would not explain the fourth trace. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
rtrombetta
Joined: 14 Oct 2011 Posts: 8
|
Re: Osciloscope Images |
Posted: Wed Nov 23, 2011 3:20 pm |
|
|
Quote: |
You have a hardware fault. Do you have filtering and bypass capacitors? Is your 3.3 volt rail stable? |
Hi Andrew!
Sorry about the delayed answer.
Yes, I have both filtering and bypass capacitors. I tried to use an acid-lead battery as a power source to avoid noise but the same happens. At the 74HC126 datasheet the input rise/fall time at 2V is 0 to 1us, at 4.5V is 0 to 500ns and at 6V is 0 to 400ns.
I tried to use resistor divider to interface between the PIC and the SD card but the rise/fall time was increased.
I will try to use a 74LVC126 instead the 74HC126. The 74LVC126 datasheet says that rise/fall time is typically 9ns.
Last edited by rtrombetta on Wed Nov 23, 2011 3:28 pm; edited 1 time in total |
|
|
rtrombetta
Joined: 14 Oct 2011 Posts: 8
|
|
Posted: Wed Nov 23, 2011 3:24 pm |
|
|
bkamen wrote: | Are you using a x10 probe that hasn't been adjusted to the scope's input?
I can make waveforms exactly like that with a x10 and any square wave while the compensation cap is out of cal.
Also, what's the bandwidth of your scope (and the probe - they can be different)
-Ben |
Hi Ben! Thanks for reply!
The bandwidth of both scope and probe is 20Mhz. The probe compensation cap is correctly calibrated. I think the problem is 74HC126. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Wed Nov 23, 2011 3:47 pm |
|
|
rtrombetta wrote: | bkamen wrote: | Are you using a x10 probe that hasn't been adjusted to the scope's input?
I can make waveforms exactly like that with a x10 and any square wave while the compensation cap is out of cal.
Also, what's the bandwidth of your scope (and the probe - they can be different)
-Ben |
Hi Ben! Thanks for reply!
The bandwidth of both scope and probe is 20Mhz. The probe compensation cap is correctly calibrated. I think the problem is 74HC126. |
have you looked up the rise/fall time of that 74HC126? _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
rtrombetta
Joined: 14 Oct 2011 Posts: 8
|
|
Posted: Wed Nov 23, 2011 4:22 pm |
|
|
Quote: | have you looked up the rise/fall time of that 74HC126? |
In the 74HC126 datasheet the input rise/fall time at 2V is 0 to 1us, at 4.5V is 0 to 500ns and at 6V is 0 to 400ns.
I tried to use resistor divider to interface between the PIC and the SD card but the rise/fall time was increased.
I will try to use a 74LVC126 instead the 74HC126. The 74LVC126 datasheet says that rise/fall time is typically 9ns. |
|
|
|