0
\$\begingroup\$

I am trying to initialize SD card for a project I managed to get reply for CMD0 with 0x01 but when i send CMD8 I get response of 0x09 which means CRC error however I am sending the correct value for CRC (I am using stm32f103c8t6 and libopencm3) Here is the code I wrote.

#define CMD0_RST    0x40
#define DATA_CMD0   0x00000000
#define CMD8_VER    0x48
#define DATA_CMD8   0x000001AA
#define CRC_CMD8    0x87
#define CRC         0x95
#define DUMMY       0xFF
    void spi_sel(){


   spi_set_nss_low(SPI1);
}
void spi_desel(){


          spi_set_nss_high(SPI1);    
}
void sd_dummy_to_init(){
  spi_desel();
  spi_enable(SPI1);
  int i ;
  for(i = 0;i < 8;i++)
      spi_send(SPI1, DUMMY);
  spi_disable(SPI1);  
}
    char send_and_rsp(uint32_t spi , char cmd , uint32_t data , char crc ){
  sd_dummy_to_init();
  spi_sel();
  spi_enable(spi);
  int i;
  for(i = 0; i < 8; i++)
    spi_send(spi , DUMMY);
  send_command( spi ,  cmd ,  data , crc );
  char R = response_cmd( spi ,  cmd );
  for(i = 0; i < 8; i++)
    spi_send(spi , DUMMY);  
  //spi_xfer(spi , DUMMY);
  spi_disable(spi);
  spi_desel();
   sd_dummy_to_init();
  return R;
}

void  send_command(uint32_t spi , char cmd , uint32_t data , char crc ){
   //  spi_enable(spi);    
    spi_send(spi , cmd);
    spi_send(spi ,(uint8_t) data >> 24 );
    spi_send(spi ,(uint8_t) data >> 16);
    spi_send(spi ,(uint8_t) data >> 8);
    spi_send(spi ,(uint8_t) data);
    spi_send(spi , crc);
}
char response_cmd(uint32_t spi , char cmd ){
  char c;
    int i = 10;
    while(((c = spi_xfer(spi,DUMMY)) != 0x01) && i)
    {
      //      uart_putc(i +'0');
      uart_putc(c +'0');
      i--;
    }
    return c + '0';
}

And here is the intialization from libopencm3

static void spi_setup(void) {

    rcc_periph_clock_enable(RCC_SPI1);
    gpio_set_mode(
        GPIOA,
                GPIO_MODE_OUTPUT_50_MHZ,
            GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
                GPIO4|GPIO5|GPIO7       // NSS=PA4,SCK=PA5,MOSI=PA7
    );
    gpio_set_mode(
        GPIOA,
        GPIO_MODE_INPUT,
        GPIO_CNF_INPUT_FLOAT,
        GPIO6               // MISO=PA6
    );
    spi_reset(SPI1); 
    spi_init_master(
        SPI1,
                SPI_CR1_BAUDRATE_FPCLK_DIV_256,
                SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE,
        SPI_CR1_CPHA_CLK_TRANSITION_1,
            SPI_CR1_DFF_8BIT,
            SPI_CR1_MSBFIRST
    );
        spi_disable_software_slave_management(SPI1);
     spi_enable_ss_output(SPI1); 
}

And here is the main function

int main(void){
  rcc_clock_setup_in_hse_8mhz_out_72mhz();
  rcc_periph_clock_enable(RCC_GPIOA);

    spi_setup();
        uart_setup();
    //sd_dummy_to_init();
    char c = send_and_rsp(SPI1 , CMD0_RST , DATA_CMD0 , CRC);
    uart_putc(c);
        int i;
    for(i =0 ;i < 1000000 ;i++);
    char v = send_and_rsp(SPI1 , CMD8_VER , DATA_CMD8 , CRC_CMD8);
    uart_putc(v);

  return 0;
}

Any advice will be appreciated .

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.