2024-12-09 02:36 AM - last edited on 2024-12-10 01:32 AM by Andrew Neil
How can I combine SD card and USB initialization in an STM32 project?
Individually, both SD card and USB initialization work correctly. However, when combined, only the mount operation succeeds, and the process fails when opening a file. Testing each peripheral separately after generating both configurations also fails.
What could be causing this, and how can I make both functionalities work simultaneously?
Below is the code refer it:
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "fatfs.h"
#include "sdmmc.h"
#include "usart.h"
#include "usb_host.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ff_gen_drv.h"
#include "usb_host.h"
#include "usbh_diskio.h"
#include <stdint.h>
#include<stdio.h>
#include<string.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
#define USE_USB_FS // Uncomment for Full-Speed USB
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t retSD; /* Return value for SD */
char SDPath[4]; /* SD logical drive path */
FATFS SDFatFS; /* File system object for SD logical drive */
FIL SDFile; /* File object for SD */
FRESULT res; /* FatFs function common result code */
uint32_t byteswritten, bytesread; /* File write/read counts */
uint8_t wtext[] = "STM32 FATFS works great!"; /* File write buffer */
uint8_t rtext[_MAX_SS];/* File read buffer */
///////////usb ///////////
FATFS USBDISKFatFs; /* File system object for USB disk logical drive */
FIL MyFile; /* File object */
char USBDISKPath[4]; /* USB Host logical drive path */
USBH_HandleTypeDef hUsbHostFS; /* USB Host handle */
uint64_t count=0;
uint8_t operationCount = 0; /* Track the number of iterations */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);
void MX_USB_HOST_Process(void);
/* USER CODE BEGIN PFP */
int __io_putchar(int ch);
/* Redirecting printf to UART ------------------------------------------------*/
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart3, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void USB_POWER_SWITCH_ON(void);
void USB_POWER_SWITCH_OFF(void);
void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
void USB_POWER_SWITCH_ON(void)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET); // Example pin for VBUS control
printf("VBUS Enabled\n");
}
void USB_POWER_SWITCH_OFF(void)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET); // Turn off VBUS power
printf("VBUS Disabled\n");
}
void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
/* 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_SDMMC1_SD_Init();
MX_USART3_UART_Init();
MX_DMA_Init();
MX_USB_HOST_Init();
MX_FATFS_Init();
/* Initialize interrupts */
MX_NVIC_Init();
/* USER CODE BEGIN 2 */
/* Debugging Start --------------------------------------------------------*/
if(f_mount(&SDFatFS, (TCHAR const*)SDPath, 0) != FR_OK)
{
Error_Handler();
}
// {
// if(f_mkfs((TCHAR const*)SDPath, FM_ANY, 0, rtext, sizeof(rtext)) != FR_OK)
// {
// Error_Handler();
// }
// else
if(1)
{
//Open file for writing (Create)
if(f_open(&SDFile, "STM32k.TXT", FA_OPEN_APPEND | FA_WRITE) != FR_OK)
{
Error_Handler();
}
else
{
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
Error_Handler();
}
else
{
f_close(&SDFile);
}
}
}
// }
f_mount(&SDFatFS, (TCHAR const*)NULL, 0);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
if (f_mount(&USBDISKFatFs, USBDISKPath, 0) == FR_OK)
{
printf("File System Mounted Successfully\n");
/* Create and write to a file */
if (f_open(&MyFile, "stm123.txt", FA_OPEN_APPEND | FA_WRITE) == FR_OK)
{
const char *data = "HI";
UINT bytesWritten;
printf("File Opened for Writing\n");
if (f_write(&MyFile, data, strlen(data), &bytesWritten) == FR_OK && bytesWritten == strlen(data))
{
printf("File Written Successfully\n");
}
else
{
printf("Failed to Write to File\n");
}
f_close(&MyFile);
}
else
{
printf("Failed to Open File for Writing\n");
}
/* Read from the file */
if (f_open(&MyFile, "SABARI.txt", FA_READ) == FR_OK)
{
char buffer[64];
UINT bytesRead;
printf("File Opened for Reading\n");
if (f_read(&MyFile, buffer, sizeof(buffer) - 1, &bytesRead) == FR_OK)
{
buffer[bytesRead] = '\0'; // Null-terminate string
printf("File Content: %s\n", buffer);
}
else
{
printf("Failed to Read from File\n");
}
f_close(&MyFile);
}
else
{
printf("Failed to Open File for Reading\n");
}
/* Unmount the filesystem */
f_mount(NULL, USBDISKPath, 0);
printf("File System Unmounted\n");
}
else
{
printf("Failed to Mount File System\n");
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
/** 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_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 72;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 3;
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_1) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief NVIC Configuration.
* @retval None
*/
static void MX_NVIC_Init(void)
{
/* OTG_FS_IRQn interrupt configuration */
HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* file: pointer to the source file name
* line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
Kindly help me to figure out the problem .
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
2024-12-09 03:51 AM - edited 2024-12-09 03:54 AM
Hello @dharanik_1399 ,
In next time, please kindly use </> button to paste your code. I've edited your post then.. Please review the tips in this link.
Thank you for your understanding.
2024-12-10 01:25 AM
Hello,
Please don't duplicate the same question on multiple threads.
Thank you for your understanding.