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

MMCSD problem

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



Joined: 02 Aug 2015
Posts: 16
Location: Australia

View user's profile Send private message

MMCSD problem
PostPosted: Sun Oct 24, 2021 4:25 pm     Reply with quote

I am trying to implement an SD card and feel I am the only person in the world to fail this simple task!
I am using a CCS PIC18F67J60 demonstration PCB and a SanDisk 8GB SD card.
I am using standard libraries. I have moved the files and changed their names so I can try and debug my issue.

I have modified the mmcsd file to add the loop to switch the card to SD mode and some print commands to see what is happening.


    Serial listing
    ex_mmcsd.c

    mmcsd Initilisation
    Set to SD Mode
    r1 after go idle cmd 0x01
    Send op cond
    time out r1 after send op cond 0x01
    Could not init the MMC/SD!!!!



Any assistance would be appreciated.


Code:

//////////////////////////////////////////////////////////////////////////
////                      ex_mmcsd.c                                 ////
////                                                                 ////
//// Similar to ex_extee.c, an example that demonstrates             ////
//// writing and reading to an MMC/SD card.                          ////
////                                                                 ////
////  Select either the ICD or your own RS232-to-PC connection       ////
////  for the text I/O.                                              ////
////                                                                 ////
/////////////////////////////////////////////////////////////////////////
////     (C) Copyright 2007,2018 Custom Computer Services            ////
//// This source code may only be used by licensed users of the CCS  ////
//// C compiler.  This source code may only be distributed to other  ////
//// licensed users of the CCS C compiler.  No other use,            ////
//// reproduction or distribution is permitted without written       ////
//// permission.  Derivative programs created using this software    ////
//// in object code form are not restricted in any way.              ////
/////////////////////////////////////////////////////////////////////////

//These settings are for the CCS PICEEC development kit which contains
//an MMC/SD connector.
#include <18F67J60.h>
//#use delay(clock=25M)
#use delay(crystal=25000000)
//!#use rs232(icd)                        //Text through the ICD
#use rs232(baud=9600, UART1, errors) //Text through the UART

#include <stdlib.h> // for atoi32
#define LCD_RS_PIN      PIN_F1
#define LCD_RW_PIN      PIN_F2
#define LCD_ENABLE_PIN  PIN_F3
#define LCD_DATA4       PIN_F4
#define LCD_DATA5       PIN_F5
#define LCD_DATA6       PIN_F6
#define LCD_DATA7       PIN_F7
#include <lcd.c>
//meda library, a compatable media library is required for FAT.
#use fast_io(c)
#define MMCSD_PIN_SCL     PIN_C3 //o
#define MMCSD_PIN_SDI     PIN_C4 //i
#define MMCSD_PIN_SDO     PIN_C5 //o
#define MMCSD_PIN_SELECT  PIN_C2 //o
#include <mmcsd-play.c>

#include <input-play.c>

void main(void)
{
   BYTE value, cmd;
   int32 address;
   lcd_init();   
   printf(lcd_putc,"\fnex_mmcsd.c\n");
   printf("\r\n\nex_mmcsd.c\r\n\n");
   
   if (mmcsd_init())
   {
      printf("Could not init the MMC/SD!!!!\r\n");
      while(TRUE);
   }
   
   do {
      do {
         printf("\r\nRead or Write: ");
         cmd=getc();
         cmd=toupper(cmd);
         putc(cmd);
      } while ( (cmd!='R') && (cmd!='W') );

      printf("\n\rLocation: ");

      address = gethex();
      address = (address<<8)+gethex();

      if(cmd=='R')
      { 
         mmcsd_read_byte(address, &value);
         printf("\r\nValue: %X\r\n", value);
      }

      if(cmd=='W') {
         printf("\r\nNew value: ");
         value = gethex();
         printf("\n\r");
         mmcsd_write_byte(address, value);
         mmcsd_flush_buffer();
      }
   } while (TRUE);   
}


Code:

   printf("mmcsd Initilisation\r\n");
   mmcsd_deselect();
   delay_ms(15);
   /* fill send data with all ones - 80 bits long to    */
   /* establish SPI link with SD card this fulfills the */
   /* 74 clock cycle requirement...  */
   printf("Set to SD Mode\r\n");
   for(i = 0; i < 10; i++)               
      MMCSD_SPI_XFER(0xFF);     
   /* begin initialization */
   i = 0;
   do
   {
      delay_ms(1);
      mmcsd_select();     
      r1=mmcsd_go_idle_state();
      mmcsd_deselect();
      i++;
      if(i == 0xFF)
      {
         mmcsd_deselect();
         printf("time out r1 after go idle cmd 0x%X\r\n",r1);
         return r1;
      }
   } while(!bit_test(r1, 0));
   printf("r1 after go idle cmd 0x%X\r\n",r1);
   
   printf("Send op cond \r\n");
   i = 0;
   do
   {
      delay_ms(1);
      mmcsd_select();
      r1=mmcsd_send_op_cond();
      mmcsd_deselect();
      i++;
      if(i == 0xFF)
      {
         mmcsd_deselect();
         printf("time out r1 after send op cond 0x%X\r\n",r1);
         return r1;
      }
   } while(r1 & MMCSD_IDLE);
   printf("r1 after send op cond 0x%X\r\n",r1);
    Ttelmah



    Joined: 11 Mar 2010
    Posts: 19539

    View user's profile Send private message

    PostPosted: Mon Oct 25, 2021 12:54 am     Reply with quote

    Go to the code library.
    There are a number of issues with the standard supplied libraries. In the
    library there are the fixes for these. Since your card is over 4GB, at least
    two of these fixes are _needed_ to get it working.

    Biggest 'common problem' with SD, is the need for the large capacitor
    adjacent to the card. This is absolutely essential, since these cards draw
    huge spikes on the power rails doing certain operations. Also, have you
    got the required pull up resistors on the lines?.
    Typically something like a low ESR 22uF close to the card, and pull ups on
    CS, Data_OUT - SDI(to the PIC), DAT1, and DAT2.
    The extra pull-ups on DAT1 & DAT2, are needed to ensure how the card
    wakes up. The SDI one is in the specifications to improve the speed
    of the rising edge on that signal. The CS is needed because this line can
    otherwise float before the PIC wakes up. The card itself is meant to have
    a 50K pull up on this line internally. Better to be safe though.... Use
    10KR for all the resistors.

    As another comment, are you sure the library you are using supports
    using fast_io?. Most of the supplied libraries do not.
    HaveAchat



    Joined: 02 Aug 2015
    Posts: 16
    Location: Australia

    View user's profile Send private message

    PostPosted: Mon Oct 25, 2021 11:49 pm     Reply with quote

    Thank you Ttelmah,
    I am embarrassed to ask but where is the "code library"?

    I have the pullups but no Cap so I will add that over the next few days.
    HaveAchat



    Joined: 02 Aug 2015
    Posts: 16
    Location: Australia

    View user's profile Send private message

    PostPosted: Mon Oct 25, 2021 11:51 pm     Reply with quote

    NO!!!!! don't tell me!!!!! its in my face!!!!!!
    What can I say.....
    Ttelmah



    Joined: 11 Mar 2010
    Posts: 19539

    View user's profile Send private message

    PostPosted: Tue Oct 26, 2021 10:18 am     Reply with quote

    Hidden in plain sight!.. Very Happy
    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