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 CCS Technical Support

SPI Speed control
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

SPI Speed control
PostPosted: Wed Jun 12, 2013 7:31 am     Reply with quote

OK, I'm an trying to change the speed/clock of the SPI port (software - hw is not viable right now).

HOWEVER.
I am using SPI to talk to an SDCARD, now currently set at 10240 (thats 1 0 2 4 0 - no mistake).

The problem is, that this is great for initializing the card, but i want to UP the speed AFTER the card says ok (since the card has a much higher speed after initialization was completed).

This affects only one of the SDCARDS, but ideally want it to be fixed as this is pretty much a standard I've read.

How can I change the baud rate during software?
Can it be done? or does it HAVE to be hardware.

SPECS:

MCU: pic18F46k22

Using pins, C3, 4, 5 and 6.

The SDCARD SPI mode (mode =0 or mode =3 mode) appears to be working at baud rate 10240, its just much too slow for what I need.

ANY help would be helpful.

Thank you.

PS. sorry if this topic has been covered already, (couldn't find it..)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Jun 15, 2013 2:26 pm     Reply with quote

I assume you're using #use spi() ?

Post your existing #use spi() statement, and also your PIC oscillator
frequency, and your compiler version.
Ttelmah



Joined: 11 Mar 2010
Posts: 19502

View user's profile Send private message

PostPosted: Sat Jun 15, 2013 3:34 pm     Reply with quote

You can't change the frequency of a software SPI.

However you can just generate two #use SPI lines, with two streams on the same pins, using two different rates.

So:
Code:

#use spi(DI=PIN_B1, DO=PIN_B0, CLK=PIN_B2, baud=10240, STREAM=SLOW)
#use spi(DI=PIN_B1, DO=PIN_B0, CLK=PIN_B2, baud=1000000, STREAM=FAST)

//then

spi_xfer(SLOW,val); //sends a byte at 10240bps
spi_xfer(FAST,val); //sends the byte at 1Mbps (if your chip is fast enough)

The maximum speed you can do with software, is typically something like 1/20th your CPU MIPS rate.

Best Wishes
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

Speeds SPI control
PostPosted: Sun Jun 16, 2013 11:39 am     Reply with quote

here is a the code, its a total shambles!

16Mhz Crystal,
64Mhz runtime, 16MIPS


-------- PIC CONFIG -------
Code:

#include <18F46K22.h>
#device *=16


#FUSES NOXINST
#FUSES NOPBADEN                   //PORTB pins are configured as analog input channels on RESET
#fuses HSH,PUT,NOPROTECT,NOLVP,NOWDT,NOBROWNOUT,PLLEN,NOPBADEN,WDT128
#device WRITE_EEPROM = NOINT

#use delay(crystal=16M,  clock=64000000)

#use rs232(baud=115200,parity=N,xmit=PIN_D1,rcv=PIN_D2,bits=8,stream=PORT1)


#byte SSP1CON2   = 0xFC5
#byte SSP1CON1   = 0xFC6
#byte SSP1STAT   = 0xFC7
#byte SSP1ADD   = 0xFC8
#byte SSP1BUF   = 0xFC9

#bit SSP1STAT_BF   = SSP1STAT.0   // Buffer Full                                 
#bit SSP1STAT_UA   = SSP1STAT.1   // Update Address bit (10 bit slave address)
#bit SSP1STAT_RW   = SSP1STAT.2   // Read (1), Write (0)
#bit SSP1STAT_S      = SSP1STAT.3   // Start
#bit SSP1STAT_P      = SSP1STAT.4   // Stop
#bit SSP1STAT_DA   = SSP1STAT.5   // Data (1), Address Match (0)
#bit SSP1STAT_CKE   = SSP1STAT.6   // SMBus Select Bit
#bit SSP1STAT_SMP   = SSP1STAT.7   // Slew Rate Control 100kHz (1), 400kHz (0)

#bit SSP1CON1_CKP   = SSP1CON1.4

#ZERO_RAM



-------- CODE FOR SD INIT ------------
Code:


#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)    //SPI Mode 0
#define SPI_MODE_1  (SPI_L_TO_H)                      //SPI Mode 1
#define SPI_MODE_2  (SPI_H_TO_L)                      //SPI Mode 2
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)    //SPI Mode 3

#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=3, stream=mmcsd_spi,baud=20000,FORCE_HW)//
//#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=3, stream=mmcsd_spi,baud=115000)

MMCSD_err mmcsd_init(){
   uint8_t
      i,
      r1;
     
   setup_timer_2(T2_DIV_BY_16,32,1);
   setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_T2);
   //setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_16);

     
    printf("CARD Starting... \r\n");
   g_CRC_enabled = TRUE;
   g_mmcsdBufferAddress = 0;

 
 
   output_drive(MMCSD_PIN_SCL);
   output_drive(MMCSD_PIN_SDO);
   output_drive(MMCSD_PIN_SELECT);
   output_float(MMCSD_PIN_SDI);

   mmcsd_deselect();
   delay_ms(15);
     
   /* begin initialization */
   i = 0;
   do{
      delay_ms(1);
      mmcsd_select();     
      r1=mmcsd_go_idle_state();
      mmcsd_deselect();
      i++;
      if(i == 0xFF){
         mmcsd_deselect();
           return r1;
      }
   } while(!bit_test(r1, 0));

   i = 0;
   do{
      delay_ms(1);
      mmcsd_select();
      r1=mmcsd_send_op_cond();
      mmcsd_deselect();
      i++;
      if(i == 0xFF){
         mmcsd_deselect();
         return r1;
      }
   } while(r1 & MMCSD_IDLE);

   /* figure out if we have an SD or MMC */
   mmcsd_select();
   r1=mmcsd_app_cmd();
   r1=mmcsd_sd_send_op_cond();
   mmcsd_deselect();
     printf("The card is: 0x%2X => ",r1);
   /* an mmc will return an 0x04 here */
   if(r1 == 0x04){ 
      g_card_type = MMC; printf("MMC.\r\n");}
   
   else{
      g_card_type = SD;  printf("SD.\r\n");
       //  mmcsd_print_cid();
       //  mmcsd_print_csd();
   }
   delay_ms(200);
   /* set block length to 512 bytes */
   mmcsd_select();
   r1 = mmcsd_set_blocklen(MMCSD_MAX_BLOCK_SIZE);
 
     if(r1 != MMCSD_GOODEC){
      mmcsd_deselect();
      return r1;
   }
   mmcsd_deselect();

   mmcsd_select();
   r1 = mmcsd_crc_on_off(0);
   if(r1 != MMCSD_GOODEC) {
      mmcsd_deselect();
      return r1;
   }
   mmcsd_deselect();
   

   r1 = mmcsd_load_buffer();
   
   g_mmcsdPartitionOffset = 0;
   //mmcsd_check_part(0x1EE);
   //mmcsd_check_part(0x1DE);
   //mmcsd_check_part(0x1CE);
   //mmcsd_check_part(0x1BE);
   //we need to calculate the offset adresss between phy. and logic. address from the offset sector at the address 0x1C6

   //  printf("find offset from physical to logical %X\r\n",g_mmcsd_buffer[0x1C6] ) ;
   //more detail at  http://www.secure-digital-card-source-code-driver.com/layout-of-a-mmc-or-sd-card-with-fat/the-boot-record
   if (g_mmcsd_buffer[0x1C6] < 80 )
      g_mmcsdBufferAddress = g_mmcsd_buffer[0x1C6]*0x200;
      // for card => 1GByte need to add more offset
   else
      g_mmcsdBufferAddress = g_mmcsd_buffer[0x1C6]*0x200 + 0x10000;

    printf("offset Adr : %lX\r\n",g_mmcsdBufferAddress ) ;

   g_mmcsdPartitionOffset=g_mmcsdBufferAddress; ;

    r1 = mmcsd_load_buffer();

   if (g_mmcsd_buffer[0] == 0xEB){
      printf("Boot sector found.\n" ) ;
   }
 
 
    //setup_timer_2(T2_DIV_BY_4,1,1);
    //setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_T2);
   setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_16);
   //SSP1STAT_SMP=0;
   //setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);
   

    return r1;
}

MMCSD_err mmcsd_read_data(uint32_t address, uint16_t size, uint8_t* ptr)
{
   MMCSD_err r1;
   uint16_t i;  // counter for loops

   for(i = 0; i < size; i++)
   {
      r1 = mmcsd_read_byte(address++, ptr++);
      if(r1 != MMCSD_GOODEC)
         return r1;
   }
   
   return MMCSD_GOODEC;
}



As you can see its a mixture of other codes, i understand most of it,

but as you can tell, i started using #use spi
and Setup_spi.

I cant have it like that, since setup_spi and #use sometimes conflict (sorry)

i assume the cards are fast enough since the cards can write using a SD adaptor for PC, can xfer files at 3M/s
download 6m/s there arounds (not accurate numbers)

so i should be able to play wav or any complex high deff sounds.,

now im using 5v PIC18F46k44 (diagram being built now,m will post later)
i have an LE33 (3.3 Regulator)

so electronically i believe its all ok,

using Restistor dividers to translate levels .

words PERFECT at lower speeds so im assuming electronically the SDCARD is working fine.
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

the circuit
PostPosted: Sun Jun 16, 2013 12:27 pm     Reply with quote

Please note, this is an extremely basic setup, the values are what im using tho.
The Speaker setup is just for show here, it will be going to an amplifier

neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

Ttelmah - Tried it
PostPosted: Sun Jun 16, 2013 2:00 pm     Reply with quote

i tried your two use spi methods.

it sort of works

my FASTEST mode is set to 2000000 (2 million) and it still showing up on my clock rate as 214khz...
anything beyond 400Khz seams to just do no better :(

the maths works out to about 200Hhz,

But the Dual SPI, setting, i need hardware to get full [spam] speed
Ttelmah



Joined: 11 Mar 2010
Posts: 19502

View user's profile Send private message

PostPosted: Sun Jun 16, 2013 3:06 pm     Reply with quote

You are not going to get much faster, unless you go to hardware SPI.

You should be able to get to about 1/24th the CPU clock rate with software SPI. I have tested on a couple of chips, and a CPU at 48MHz, happily generates just on 470KHz. At your 64Mhz clock, you should be able to get to perhaps 600K. However this will be degraded if any interrupts are occurring. I suspect this is what is happening to you. However, have you actually verified you are running at 64MHz, with a basic 'flash an LED' test?. You have not answered the 'what compiler version' question, and quite a few don't enable the PLL correctly on chips where this is software controlled, unless you call setup_oscillator to do so.

Then, your hardware makes no sense. You show a 3.3v PIC, yet you show divider resistors on the output lines to the SD card. Then you have the lines connecting to pins labelled as D+/D-, but the PIC you show does not have USB, and if it did, these pins would not be useable as general I/O. Then you have no pull-up resistors on the SD card lines. A couple of these are _essential_ or the card can initiate incorrectly. They are in the spec. I'm amazed anything even remotely works.
However you then say you are using a 5vPIC, but give a chip number that doesn't exist. This makes things about 10* harder. You can't use this without proper level translation on the data, except at relatively restricted rates. Software SPI only, and even then potential problems as you try to go faster.

You need to start with some serious tidying.

Then you are not going to get anywhere near the speed you want unless you start using the SPI hardware.

Remember though you do have to switch speed. The clock rate allowed for SD initialisation is much slower than for later running.

The big problem is not translating the levels down (5v -> 3.3v), but going the other way. The SPI hardware on a 5v PIC, _requires_ input signals that go up to over 3.3v. Since you are not going to get the speed you want without this, you need to rethink your hardware.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Jun 16, 2013 3:09 pm     Reply with quote

Edit: I see Mr. T. replied same time as I was typing my message. Because I have a few other remarks I leave my reply unchanged anyway.

I'm new into this thread, but first thing I notice when looking at your schematic is that you are running the PIC at 5V. Why????
The SD card needs 3.3V so why add the extra voltage regulator and voltage divider resistors when you have a modern PIC that just as happily will run at 3.3V too?
You will save power (better for the environment), money and board space.
In addition: your voltage adapter from SD card to PIC is wrong so it is never going to work reliable anyway.

Problems:
1) The SD card needs pull-up resistors on the SS, SDI and SDO lines. These are missing. Even worse, you have pull down resistors.
2) RC4 of the PIC is the hardware SDI, RC5 is the hardware SDO. In your schematic you have these reversed. Because you have no current limiting resistor on RC5 to the SD card it is possible you have destroyed the SD card.
3) For TTL the input is seen as a logic 'high' when the voltage is larger than 2.0V (datasheet table 27.8). No problem for your 3.3V SD card. But for highest speed you want hardware SPI and then the same pin C5 becomes a Schmitt-trigger input where the minimum voltage for a 'high' should be 0.8 * 5V = 4V. You are never going to make that !!!

3) Your program is incomplete: the defines for MMCSD_PIN_SDI and MMCSD_PIN_SDO are missing.
4) You have both setup_spi and the #USE SPI commands in your program. This is dangerous as both commands do not mix very well. When they are sharing 1 pin it could result in undefined behaviour. Because you didn't post the pin defines we can not check this.
5) Always post your compiler version number. (it looks like 4.xxx, note the 3 characters after the dot).

Considering all the above I think the slow SPI clock isn't your main problem. Please change your PIC to 3.3V and save everybody, including yourself, a lot of time.
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

OK... looking now
PostPosted: Sun Jun 16, 2013 3:26 pm     Reply with quote

datasheet for the pic18f46k22
http://ww1.microchip.com/downloads/en/DeviceDoc/41412F.pdf

here.

Sorry I forgot to add the 5V on VCC.

also, the pull downs are from another example i found online... (will try changing over now) but the data sheet say otherwise, from what i can tell.
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

COMPILER AND INFOS
PostPosted: Sun Jun 16, 2013 3:30 pm     Reply with quote

Thanks, forgot about that... ok

Compiler 4.130 .

also, if you note: in the diagram i have set it so if you follow SDO from the chip it goes INTO SDI on the sdcard (stops complications later for me)


---- again thank you so much for the help here! ----


--- this is in my main.h file ---
i can use 3.3 volts on my PIC, but its more because the rest of my circuit is running on 5v..

easily changable though

IF i do change down to 3.3v, i can remote the resistors from the chip to the sdcard... but pull them HIGH?


Code:

#define MMC_CS    PIN_c2
#define MMC_CLK   PIN_c3
#define MMC_DI    PIN_C4
#define MMC_DO    PIN_C5


#define MMCSD_PIN_SELECT  MMC_CS //o
#define MMCSD_PIN_SCL     MMC_CLK //o
#define MMCSD_PIN_SDI     MMC_DI //i
#define MMCSD_PIN_SDO     MMC_DO //o


#use FAST_IO(C)

Ttelmah



Joined: 11 Mar 2010
Posts: 19502

View user's profile Send private message

PostPosted: Sun Jun 16, 2013 3:35 pm     Reply with quote

There are two groups of people trying to use SD cards on the PIC.

The first bother to read the specs, and the specs of the PIC pins, and the hardware SPI port. They then either run the PIC at 3.3v, or use proper hardware buffers for the return data. They get basically 100% reliable operation, and good speed.

The second lot, base their 'designs', on any one of a huge number of circuits posted on the web, and try to run without the hardware buffers at 5v. They continuously post here saying that the CCS code doesn't work (it won't with their hardware), or that they have speed problems (not realising that the resistor approach only works with software SPI, with is slow).

You are currently sitting firmly in group 2. You need to start _thinking_ to have any hope of moving to group 1.

You say "im using 5v PIC18F46k44".
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

Ttelmah
PostPosted: Sun Jun 16, 2013 3:37 pm     Reply with quote

LOL I can agree to that, its the first time i've used SDCARDS, so yeah, gunna make a lot of mistakes, from what i've read though, everything worked well...

im not gonna blame CCS C, cos i KNOW its my code / design.

i've researched as much as i could, it worked, i got a wav player, but only 50/50% .. as you say, im in group too.

im just not 100% familure with CCS C yet.

im altering the circuit now to use 3.3v, ive removed the series resistors on the pin outs to the chip, this chip CAN run from 2.3V to 5.5V Operation – PIC18FXXK22 devices ( i am using the F device)

[edit] found the circuit diagram mistake... DAMN IT [/edit]
... setup_ocilators have caused issues before, on some chips... i'll give that a blast


Last edited by neochrome32 on Sun Jun 16, 2013 3:46 pm; edited 1 time in total
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

Pin outs
PostPosted: Sun Jun 16, 2013 3:56 pm     Reply with quote

leg:23 RC4 = SDI1, SDA1 (using a DIL40) big chip.

whats doing it, is the initializing the sdcard, at 100Khz, then UPPING the speed AFTER, i want to use Hardware,



------ snippet from each -------

---- MAIN.C -------
Code:

setup_oscillator(64000000,0);               

   /* my chip specifics */
   setup_wdt(WDT_OFF);
   setup_timer_2(T2_DIV_BY_16,255,1);      //256 us overflow, 256 us interrupt
   
   setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
   setup_timer_4(T4_DIV_BY_4,50,1);
   setup_timer_5(T5_DISABLED | T5_DIV_BY_1);
   //setup_timer_6(T6_DIV_BY_16,124,1);
   setup_timer_6(T6_DIV_BY_16,45,1);
    setup_adc(ADC_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard   
   
   

   setup_ccp2(CCP_PWM|CCP_USE_TIMER3_AND_TIMER4);

   printf("\n\nBooting...\n\n");



....

   while(TRUE)   {
      //TODO: User Code
      /*
      SoundBuffer.left[SoundBuffer.writePos] = sumL + (1 << BITDEPTH - 1);
       SoundBuffer.right[SoundBuffer.writePos] = sumR + (1 << BITDEPTH - 1);
       SoundBuffer.writePos++;
       SoundBuffer.writePos &= SOUNDBUFFERSIZE - 1;
       */

//      if(buffer_write_pos<buffer_read_pos-16){
         fread(chunks, 256,fs);
         for(readloc=0; readloc<256; readloc++){
         
            buffer[buffer_write_pos]=chunks[readloc];
            buffer_write_pos++;
            buffer_write_pos &= SOUNDBUFFERSIZE - 1;
         }
//      }
      //delay_us(130);

   }

....

};//end program


----- main.h -------
Code:

#include <18F46K22.h>
#device *=16


#FUSES NOXINST
#FUSES NOPBADEN                   //PORTB pins are configured as analog input channels on RESET
#fuses HSH,PUT,NOPROTECT,NOLVP,NOWDT,NOBROWNOUT,PLLEN,NOPBADEN,WDT128
#device WRITE_EEPROM = NOINT

#use delay(crystal=16M,  clock=64000000)

#use rs232(baud=115200,parity=N,xmit=PIN_D1,rcv=PIN_D2,bits=8,stream=PORT1)


#byte SSP1CON2   = 0xFC5
#byte SSP1CON1   = 0xFC6
#byte SSP1STAT   = 0xFC7
#byte SSP1ADD   = 0xFC8
#byte SSP1BUF   = 0xFC9

#bit SSP1STAT_BF   = SSP1STAT.0   // Buffer Full                                 
#bit SSP1STAT_UA   = SSP1STAT.1   // Update Address bit (10 bit slave address)
#bit SSP1STAT_RW   = SSP1STAT.2   // Read (1), Write (0)
#bit SSP1STAT_S      = SSP1STAT.3   // Start
#bit SSP1STAT_P      = SSP1STAT.4   // Stop
#bit SSP1STAT_DA   = SSP1STAT.5   // Data (1), Address Match (0)
#bit SSP1STAT_CKE   = SSP1STAT.6   // SMBus Select Bit
#bit SSP1STAT_SMP   = SSP1STAT.7   // Slew Rate Control 100kHz (1), 400kHz (0)

#bit SSP1CON1_CKP   = SSP1CON1.4

#ZERO_RAM




--- mmcsd.h ---

Code:



#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)    //SPI Mode 0
#define SPI_MODE_1  (SPI_L_TO_H)                      //SPI Mode 1
#define SPI_MODE_2  (SPI_H_TO_L)                      //SPI Mode 2
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)    //SPI Mode 3

#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=0, stream=mmcsd_spi,baud=20000,FORCE_HW)//
//#use spi(MASTER, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=3, stream=mmcsd_spi,baud=115000)

MMCSD_err mmcsd_init(){
   uint8_t
      i,
      r1;
     
   setup_timer_2(T2_DIV_BY_16,32,1);
   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_T2);
   //setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_16);

     
    printf("CARD Starting... \r\n");
   g_CRC_enabled = TRUE;
   g_mmcsdBufferAddress = 0;

 
 
   output_drive(MMCSD_PIN_SCL);
   output_drive(MMCSD_PIN_SDO);
   output_drive(MMCSD_PIN_SELECT);
   output_float(MMCSD_PIN_SDI);

   mmcsd_deselect();
   delay_ms(15);
     
   /* begin initialization */
   i = 0;
   do{
      delay_ms(1);
      mmcsd_select();     
      r1=mmcsd_go_idle_state();
      mmcsd_deselect();
      i++;
      if(i == 0xFF){
         mmcsd_deselect();
           return r1;
      }
   } while(!bit_test(r1, 0));

   i = 0;
   do{
      delay_ms(1);
      mmcsd_select();
      r1=mmcsd_send_op_cond();
      mmcsd_deselect();
      i++;
      if(i == 0xFF){
         mmcsd_deselect();
         return r1;
      }
   } while(r1 & MMCSD_IDLE);

   /* figure out if we have an SD or MMC */
   mmcsd_select();
   r1=mmcsd_app_cmd();
   r1=mmcsd_sd_send_op_cond();
   mmcsd_deselect();
     printf("The card is: 0x%2X => ",r1);
   /* an mmc will return an 0x04 here */
   if(r1 == 0x04){ 
      g_card_type = MMC; printf("MMC.\r\n");}
   
   else{
      g_card_type = SD;  printf("SD.\r\n");
       //  mmcsd_print_cid();
       //  mmcsd_print_csd();
   }
   delay_ms(200);
   /* set block length to 512 bytes */
   mmcsd_select();
   r1 = mmcsd_set_blocklen(MMCSD_MAX_BLOCK_SIZE);
 
     if(r1 != MMCSD_GOODEC){
      mmcsd_deselect();
      return r1;
   }
   mmcsd_deselect();

   mmcsd_select();
   r1 = mmcsd_crc_on_off(0);
   if(r1 != MMCSD_GOODEC) {
      mmcsd_deselect();
      return r1;
   }
   mmcsd_deselect();
   

   r1 = mmcsd_load_buffer();
   
   g_mmcsdPartitionOffset = 0;
   //mmcsd_check_part(0x1EE);
   //mmcsd_check_part(0x1DE);
   //mmcsd_check_part(0x1CE);
   //mmcsd_check_part(0x1BE);
   //we need to calculate the offset adresss between phy. and logic. address from the offset sector at the address 0x1C6

   //  printf("find offset from physical to logical %X\r\n",g_mmcsd_buffer[0x1C6] ) ;
   //more detail at  http://www.secure-digital-card-source-code-driver.com/layout-of-a-mmc-or-sd-card-with-fat/the-boot-record
   if (g_mmcsd_buffer[0x1C6] < 80 )
      g_mmcsdBufferAddress = g_mmcsd_buffer[0x1C6]*0x200;
      // for card => 1GByte need to add more offset
   else
      g_mmcsdBufferAddress = g_mmcsd_buffer[0x1C6]*0x200 + 0x10000;

    printf("offset Adr : %lX\r\n",g_mmcsdBufferAddress ) ;

   g_mmcsdPartitionOffset=g_mmcsdBufferAddress; ;

    r1 = mmcsd_load_buffer();

   if (g_mmcsd_buffer[0] == 0xEB){
      printf("Boot sector found.\n" ) ;
   }
 
 
   //setup_timer_2(T2_DIV_BY_4,0,16);
   //setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_T2);
   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);
   //setup_spi(SPI_MASTER | SPI_MODE_3 | SPI_CLK_DIV_64);
   

    return r1;
}



--- i want to remove the use of setup_spi! since this sort of works, but not great ---
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

Made some changes
PostPosted: Sun Jun 16, 2013 4:11 pm     Reply with quote

OK, everything is now running on 3.3V. the circuit diagram is changed




removed the resistors and pulled the lines up.

from what i've read and learned today, this is how is should be...
neochrome32



Joined: 09 Jun 2013
Posts: 153

View user's profile Send private message Visit poster's website

Clocking issue then
PostPosted: Sun Jun 16, 2013 4:16 pm     Reply with quote

Code:

  for(;;){
      output_high(pin_b1);
      output_low(pin_b1);
   }


on a 64Mhz, clock

shouldn't this be more than 2.6Mhz???
the serial port is displaying the data normally

Code:

   byte cardtype;
   int8 soundvol;
   
   setup_oscillator(64000000,0);               

   /* my chip specifics */
   setup_wdt(WDT_OFF);
   setup_timer_2(T2_DIV_BY_16,255,1);      //256 us overflow, 256 us interrupt
   
   setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
   setup_timer_4(T4_DIV_BY_4,50,1);
   setup_timer_5(T5_DISABLED | T5_DIV_BY_1);
   //setup_timer_6(T6_DIV_BY_16,124,1);
   setup_timer_6(T6_DIV_BY_16,45,1);
    setup_adc(ADC_OFF);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard   
   
   

   setup_ccp2(CCP_PWM|CCP_USE_TIMER3_AND_TIMER4);

   printf("\n\nBooting...\n\n");


hmm

[edit]
PLL confirmed Smile 2.66Mhz is just cos im not talking directly to the pins[edit]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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