This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

SPI3 and SD Card

Hi Does anybody have a working example of an STM32F407VG reading and or writing an SD card using SPI3.

I have tried everything but always fails on mount with fsMediaError am at my wits end, i have even tried to follow example http://www.keil.com/appnotes/files/apnt_273.pdf and still not working.

Parents
  • SPI Config

    /* SPI3 init function */
    static void MX_SPI3_Init(void)
    {
    
      hspi3.Instance = SPI3;
      hspi3.Init.Mode = SPI_MODE_MASTER;
      hspi3.Init.Direction = SPI_DIRECTION_2LINES;
      hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
      hspi3.Init.NSS = SPI_NSS_SOFT;
      hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
      hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi3.Init.CRCPolynomial = 10;
      if (HAL_SPI_Init(&hspi3) != HAL_OK)
      {
        Error_Handler();
      }
      __SPI3_CLK_ENABLE();
    
    }
    
    

    GPIO Config

    static void MX_GPIO_Init(void)
    {
    
      GPIO_InitTypeDef GPIO_InitStruct;
    
      /* GPIO Ports Clock Enable */
      __HAL_RCC_GPIOC_CLK_ENABLE();
      __HAL_RCC_GPIOH_CLK_ENABLE();
      __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOB_CLK_ENABLE();
      __HAL_RCC_GPIOD_CLK_ENABLE();
    
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
    
      /*Configure GPIO pins : PB0 PB1 PB2 */
      GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
      /*Configure GPIO pin : PD3 */
      GPIO_InitStruct.Pin = GPIO_PIN_3;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
            GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
            GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
            GPIO_InitStruct.Pull = GPIO_PULLUP;
            GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
            GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
      HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    }
    

Reply
  • SPI Config

    /* SPI3 init function */
    static void MX_SPI3_Init(void)
    {
    
      hspi3.Instance = SPI3;
      hspi3.Init.Mode = SPI_MODE_MASTER;
      hspi3.Init.Direction = SPI_DIRECTION_2LINES;
      hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
      hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
      hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
      hspi3.Init.NSS = SPI_NSS_SOFT;
      hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
      hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
      hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
      hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
      hspi3.Init.CRCPolynomial = 10;
      if (HAL_SPI_Init(&hspi3) != HAL_OK)
      {
        Error_Handler();
      }
      __SPI3_CLK_ENABLE();
    
    }
    
    

    GPIO Config

    static void MX_GPIO_Init(void)
    {
    
      GPIO_InitTypeDef GPIO_InitStruct;
    
      /* GPIO Ports Clock Enable */
      __HAL_RCC_GPIOC_CLK_ENABLE();
      __HAL_RCC_GPIOH_CLK_ENABLE();
      __HAL_RCC_GPIOA_CLK_ENABLE();
      __HAL_RCC_GPIOB_CLK_ENABLE();
      __HAL_RCC_GPIOD_CLK_ENABLE();
    
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, GPIO_PIN_RESET);
    
      /*Configure GPIO pin Output Level */
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_RESET);
    
      /*Configure GPIO pins : PB0 PB1 PB2 */
      GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
      /*Configure GPIO pin : PD3 */
      GPIO_InitStruct.Pin = GPIO_PIN_3;
      GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
      GPIO_InitStruct.Pull = GPIO_NOPULL;
      GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
      HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
    
            GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12;
            GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
            GPIO_InitStruct.Pull = GPIO_PULLUP;
            GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
            GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
      HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    }
    

Children
  • Blink Thread - This Flashes every Second

    /* USER CODE BEGIN 4 */
    void Blink(void const *arg){
    
            while (1){
                    osDelay(1000);
                    HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_0);
                    WriteFile();
            }
    }
    

    It calls the writefile routine which should mount the card but fails.

    int32_t fs_mc_spi_control_ss (uint32_t drive_num, uint32_t ss){
            if (ss==0) {
                    HAL_GPIO_WritePin(GPIOD,GPIO_PIN_3,GPIO_PIN_SET);
                    return 1;
            }
              else {
                            HAL_GPIO_WritePin(GPIOD,GPIO_PIN_3,GPIO_PIN_RESET);
                return 0;
                    }
    }
    
    void WriteFile()
    {
            fsStatus st;
            if (finit("") == fsOK) {
            st=fmount("");
            if (st == fsOK) HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1); ;
            }
            FILE *f=fopen("try.txt","a");
                    if (f!=NULL) {
                            HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_2);
                            fprintf(f,"Test\n");
                            fclose(f);
                    }
    }
    
    

    The fs_mc_spi_control_ss function is setting the CS pin, I have tried reversing polarity.
    Also tried to lengthen the delay on the thread.

    Thanks so much for your time.

  • Enable the clock before you initialize the interface, the part is synchronous

    __SPI3_CLK_ENABLE(); // DO IT BEFORE

  • OK, I tried moving the __SPI3_CLK_ENABLE();
    Still no luck, could cry at this point :(