source: ctrl/firmware/Main/CubeMX/Core/Src/sdmmc.c

Last change on this file was 60, checked in by Zed, 3 days ago

SD card init function was changed not to stop if SD-card not present.

File size: 4.2 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3  ******************************************************************************
4  * @file    sdmmc.c
5  * @brief   This file provides code for the configuration
6  *          of the SDMMC instances.
7  ******************************************************************************
8  * @attention
9  *
10  * Copyright (c) 2024 STMicroelectronics.
11  * All rights reserved.
12  *
13  * This software is licensed under terms that can be found in the LICENSE file
14  * in the root directory of this software component.
15  * If no LICENSE file comes with this software, it is provided AS-IS.
16  *
17  ******************************************************************************
18  */
19/* USER CODE END Header */
20/* Includes ------------------------------------------------------------------*/
21#include "sdmmc.h"
22
23/* USER CODE BEGIN 0 */
24
25#include <stdio.h>
26
27/* USER CODE END 0 */
28
29SD_HandleTypeDef hsd1;
30
31/* SDMMC1 init function */
32
33void MX_SDMMC1_SD_Init(void)
34{
35
36  /* USER CODE BEGIN SDMMC1_Init 0 */
37
38  /* USER CODE END SDMMC1_Init 0 */
39
40  /* USER CODE BEGIN SDMMC1_Init 1 */
41
42  /* USER CODE END SDMMC1_Init 1 */
43  hsd1.Instance = SDMMC1;
44  hsd1.Init.ClockEdge = SDMMC_CLOCK_EDGE_RISING;
45  hsd1.Init.ClockPowerSave = SDMMC_CLOCK_POWER_SAVE_ENABLE;
46  hsd1.Init.BusWide = SDMMC_BUS_WIDE_4B;
47  hsd1.Init.HardwareFlowControl = SDMMC_HARDWARE_FLOW_CONTROL_ENABLE;
48  hsd1.Init.ClockDiv = 0;
49  if (HAL_SD_Init(&hsd1) != HAL_OK)
50  {
51    //Error_Handler();
52        printf("Cannot initialize SD-card!\n");
53  }
54  /* USER CODE BEGIN SDMMC1_Init 2 */
55
56  /* USER CODE END SDMMC1_Init 2 */
57
58}
59
60void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
61{
62
63  GPIO_InitTypeDef GPIO_InitStruct = {0};
64  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
65  if(sdHandle->Instance==SDMMC1)
66  {
67  /* USER CODE BEGIN SDMMC1_MspInit 0 */
68
69  /* USER CODE END SDMMC1_MspInit 0 */
70
71  /** Initializes the peripherals clock
72  */
73    PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_SDMMC;
74    PeriphClkInitStruct.SdmmcClockSelection = RCC_SDMMCCLKSOURCE_PLL;
75    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
76    {
77      Error_Handler();
78    }
79
80    /* SDMMC1 clock enable */
81    __HAL_RCC_SDMMC1_CLK_ENABLE();
82
83    __HAL_RCC_GPIOC_CLK_ENABLE();
84    __HAL_RCC_GPIOD_CLK_ENABLE();
85    /**SDMMC1 GPIO Configuration
86    PC8     ------> SDMMC1_D0
87    PC9     ------> SDMMC1_D1
88    PC10     ------> SDMMC1_D2
89    PC11     ------> SDMMC1_D3
90    PC12     ------> SDMMC1_CK
91    PD2     ------> SDMMC1_CMD
92    */
93    GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
94                          |GPIO_PIN_12;
95    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
96    GPIO_InitStruct.Pull = GPIO_NOPULL;
97    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
98    GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
99    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
100
101    GPIO_InitStruct.Pin = GPIO_PIN_2;
102    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
103    GPIO_InitStruct.Pull = GPIO_NOPULL;
104    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
105    GPIO_InitStruct.Alternate = GPIO_AF12_SDMMC1;
106    HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
107
108    /* SDMMC1 interrupt Init */
109    HAL_NVIC_SetPriority(SDMMC1_IRQn, 0, 0);
110    HAL_NVIC_EnableIRQ(SDMMC1_IRQn);
111  /* USER CODE BEGIN SDMMC1_MspInit 1 */
112
113  /* USER CODE END SDMMC1_MspInit 1 */
114  }
115}
116
117void HAL_SD_MspDeInit(SD_HandleTypeDef* sdHandle)
118{
119
120  if(sdHandle->Instance==SDMMC1)
121  {
122  /* USER CODE BEGIN SDMMC1_MspDeInit 0 */
123
124  /* USER CODE END SDMMC1_MspDeInit 0 */
125    /* Peripheral clock disable */
126    __HAL_RCC_SDMMC1_CLK_DISABLE();
127
128    /**SDMMC1 GPIO Configuration
129    PC8     ------> SDMMC1_D0
130    PC9     ------> SDMMC1_D1
131    PC10     ------> SDMMC1_D2
132    PC11     ------> SDMMC1_D3
133    PC12     ------> SDMMC1_CK
134    PD2     ------> SDMMC1_CMD
135    */
136    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
137                          |GPIO_PIN_12);
138
139    HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
140
141    /* SDMMC1 interrupt Deinit */
142    HAL_NVIC_DisableIRQ(SDMMC1_IRQn);
143  /* USER CODE BEGIN SDMMC1_MspDeInit 1 */
144
145  /* USER CODE END SDMMC1_MspDeInit 1 */
146  }
147}
148
149/* USER CODE BEGIN 1 */
150
151/* USER CODE END 1 */
Note: See TracBrowser for help on using the repository browser.