Skip to main content
Visitor II
April 11, 2024
Question

Interfacing of stm32F446re with SD card

  • April 11, 2024
  • 2 replies
  • 1360 views
#include <stdio.h>
#include <string.h>

#include "fatfs.h"
#include "fatfs_sd.h"
#include "main.h"

FATFS fs;
FIL fil;
FATFS* pfs;
DWORD fre_clust;
uint32_t totalSpace, freeSpace;
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
char buffer[100];

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
void read_and_print_file(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int _write(int file, char* ptr, int len) {
 int i;
 for (i = 0; i < len; i++) {
 ITM_SendChar(*ptr++);
 }
 return len;
}

/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void) {
 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick.
 */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_SPI1_Init();
 MX_FATFS_Init();
 /* USER CODE BEGIN 2 */
 CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
 ITM->LAR = 0xC5ACCE55; // Unlock the ITM register
 ITM->TCR = 0x00010005; // Enable ITM with stimulus port 0
 ITM->TPR = 0x0000000F; // Set privilege level to allow tracing in user code

 if (!(GPIO_PIN_4)) {
 printf("SD card is not initialized\n");
 } else {
 printf("SD card is initialized\n");
 }

 f_getfree("", &fre_clust, &pfs);
 totalSpace = (uint32_t)((pfs->n_fatent - 2) * pfs->csize * 0.5);
 freeSpace = (uint32_t)(fre_clust * pfs->csize * 0.5);

 printf("t_space :%d\n" + totalSpace);
 printf("f_space :%d\n" + freeSpace);

 while (1) {
 f_mount(&fs, "", 0);
 printf("Creating the file\n");
 f_open(&fil, "/abc.txt", FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
 printf("File is created\n");
 f_puts("AutoTec Systems\n", &fil);
 printf("Data is stored in the SD card\n");
 f_close(&fil);

 // Read and print file contents
 read_and_print_file();
 printf("data is read\n");

 HAL_Delay(500);

 void SystemClock_Config(void) {
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 __HAL_RCC_PWR_CLK_ENABLE();
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLM = 8;
 RCC_OscInitStruct.PLL.PLLN = 72;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLQ = 2;
 RCC_OscInitStruct.PLL.PLLR = 2;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
 RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
 Error_Handler();
 }
 }
 static void MX_SPI1_Init(void) {
 /* SPI1 parameter configuration*/
 hspi1.Instance = SPI1;
 hspi1.Init.Mode = SPI_MODE_MASTER;
 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi1) != HAL_OK) {
 Error_Handler();
 }
 }

 static void MX_GPIO_Init(void) {
 GPIO_InitTypeDef GPIO_InitStruct = {0};

 /* GPIO Ports Clock Enable */
 __HAL_RCC_GPIOH_CLK_ENABLE();
 __HAL_RCC_GPIOA_CLK_ENABLE();
 __HAL_RCC_GPIOB_CLK_ENABLE();

 /*Configure GPIO pin Output Level */
 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);

 /*Configure GPIO pin : PA4 */
 GPIO_InitStruct.Pin = GPIO_PIN_4;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
 GPIO_InitStruct.Pull = GPIO_NOPULL;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 void Error_Handler(void) {
 __disable_irq();
 while (1) {
 }
 /* USER CODE END Error_Handler_Debug */
 }
 void read_and_print_file(void) {
 f_open(&fil, "abc.txt", FA_READ);
 // Reads line by line until the end
 while (f_gets(buffer, sizeof(buffer), &fil)) {
 // Print the content to SWV terminal
 printf("%s", buffer);
 // Clear the buffer
 memset(buffer, 0, sizeof(buffer));
 }
 f_close(&fil);
 }

..this is our code, it executes succesfully with 0 errors, nut it didint create any file into the sd card why? give solution for this problem..

    This topic has been closed for replies.

    2 replies

    ST Employee
    May 9, 2024

    Hello @Premaa , 

    Would you please be able to share the IOC file associated with your project? Additionally, I believe there may be an issue with the check performed on GPIO_PIN_4. For accuracy, it would be advisable to read the value of the pin using the HAL_GPIO_ReadPin API and then compare it to the expected value. Thank you for your attention to these details.

    ST Employee
    May 15, 2024

    Hello @Premaa ,

    Any updates concerning this thread ?