cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 copy file from sd card to usb drive (pen drive) using FATFS

venky474
Associate II

Hey there I've just started learning STM32 so I don't know if its a small error or big one but can you help me solve.

I'm using STM32f407z and the task is to copy a file from SD card to the USB drive (pen drive) using reference blog of elm-chan FATFS. (http://elm-chan.org/fsw/ff/doc/open.html

The given code shows 0 error after compiling(build) but while debugging, It shows FR_DISK_ERR when f_open function is running for "myUSBFile". Thus it sometimes create the file but does not copy the content. Is there an issue with buffer or anything else I cant solve this error.  @Tesla DeLorean , @TDK , @waclawek.jan@Peter BENSCH 

these are the config settings in CUBE MX,

SDIO->enabled  -> SD 4 bit wide bus,

USB_OTG_FS-> HOST_ONLY,

FATFS-> SD card and USB Disk both are enabled, (USE_LFN->enabled with static working buffer on the BSS, MAX_SS->4096)

USB_host->MSC (mass storage class),

oscillator->8MHZ

CLOCK -> 168MHz

USB CLOCK -> 48MHZ

This is my Code:

 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "fatfs.h"
#include "usb_host.h"
 
/* 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 */
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
 
/* USER CODE BEGIN PV */
SD_HandleTypeDef hsd;
 FATFS  SDFatFs, USBFatfs;    
 FIL    MySdFile, MyUSBFile;     
 BYTE buffer[4096];   
 FRESULT fr;          
 UINT br, bw;   
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SDIO_SD_Init(void);
void MX_USB_HOST_Process(void);
static void USBH_UserProcess(USBH_HandleTypeDef * phost, uint8_t id);
//bool UsbTest_Write(void);
 
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* 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_SDIO_SD_Init();
  MX_FATFS_Init();
  MX_USB_HOST_Init();
  /* USER CODE BEGIN 2 */
 
// Writing in SD_Card
  f_mount(&SDFatFS, "0:", 0);
  f_mount(&USBFatfs,"1:",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 */
 
 
/* Open source file on the drive 1 */
       fr = f_open(&MySdFile, "0:filename.txt", FA_READ);
if (fr) return (int)fr;
fr = f_open(&MyUSBFile, "1:filename2.txt", FA_CREATE_ALWAYS | FA_WRITE);
if (fr) return (int)fr;
 
 
for (;;) {
        fr = f_read(&MySdFile, buffer, sizeof buffer, &br); 
        if (br == 0) break; 
        fr = f_write(&MyUSBFile, buffer, br, &bw); 
        if (bw < br) break; 
}
 
   f_close(&MyUSBFile);
       f_close(&MySdFile);
}  
}
 
  /* 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 the main internal regulator output voltage
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** 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_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 7;
  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_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief SDIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_SDIO_SD_Init(void)
{
 
  /* USER CODE BEGIN SDIO_Init 0 */
 
  /* USER CODE END SDIO_Init 0 */
 
  /* USER CODE BEGIN SDIO_Init 1 */
 
  /* USER CODE END SDIO_Init 1 */
  hsd.Instance = SDIO;
  hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;
  hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_DISABLE;
  hsd.Init.ClockDiv = 0;
  /* USER CODE BEGIN SDIO_Init 2 */
 
  /* USER CODE END SDIO_Init 2 */
 
}
 
/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOF_CLK_ENABLE();
  __HAL_RCC_GPIOG_CLK_ENABLE();
  __HAL_RCC_GPIOE_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOD_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(led1_GPIO_Port, led1_Pin, GPIO_PIN_RESET);
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(led2_GPIO_Port, led2_Pin, GPIO_PIN_RESET);
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(led3_GPIO_Port, led3_Pin, GPIO_PIN_RESET);
 
  /*Configure GPIO pin : led1_Pin */
  GPIO_InitStruct.Pin = led1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(led1_GPIO_Port, &GPIO_InitStruct);
 
  /*Configure GPIO pin : led2_Pin */
  GPIO_InitStruct.Pin = led2_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(led2_GPIO_Port, &GPIO_InitStruct);
 
  /*Configure GPIO pin : led3_Pin */
  GPIO_InitStruct.Pin = led3_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(led3_GPIO_Port, &GPIO_InitStruct);
 
}
 
/* 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.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
  /* USER CODE BEGIN 6 */
  /* 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 */
--------------------------------------------------------------------------------------------------------
 
 

 

 

4 REPLIES 4

I'd imagine  MX_USB_HOST_Process(); has to be called periodically, you can't have blocking code within the loop.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

I did remove the loop but still it doesn't work, while debugging it shows FR_DISK_ERR.

Hello

I suppose the problem is that you try to mount usb disk too early. Check your usb_host.c file and connect mount operation to the state when usb connection is estabilished. You must call mx_usb_host_process in loop to get usb connected. 

After the connection is ready, you can start file copy. During the coping, calling the mx_usb_host_process is not necessary.

Br J.T

 

Thank you for your reply,

I did what you said but still  the error is same,

and I tried to call the "switch(Appli_state)" function to the main and put the logic inside the ready state but when I did debugging it stopped at ready state. 

see the code below (I took a variable "int step=0;" to stop the copy function after the first time because it's in the loop.),

int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_SDIO_SD_Init();
MX_FATFS_Init();
MX_USB_HOST_Init();

 

f_mount(&SDFatFS, "0:", 0);

while (1)
{

/* USER CODE END WHILE */
MX_USB_HOST_Process();

/* USER CODE BEGIN 3 */
switch(Appli_state)
{
case APPLICATION_IDLE:
break;


case APPLICATION_START:
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_1,GPIO_PIN_SET);

break;

case APPLICATION_READY:

if (step==0)
{
step=1;
f_mount(&USBHFatFS, "1:", 1);

fr = f_open(&USBHFile, "1:Blue.jpg", FA_READ);
fr = f_open(&SDFile, "0:color.jpg", FA_WRITE | FA_CREATE_ALWAYS);

for (;;)
{
fr = f_read(&USBHFile, buffer, sizeof buffer, &br); /* Read a chunk of data from the source file */
if (br == 0) break; /* error or eof */
fr = f_write(&SDFile, buffer, br, &bw); /* Write it to the destination file */
if (bw < br) break; /* error or disk full */
}
f_close(&USBHFile);
f_close(&SDFile);
}
break;

case APPLICATION_DISCONNECT:
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_1,GPIO_PIN_RESET);
step=0;
break;

default:
break;
}

/* USER CODE END 3 */

}
}

-----------------------------------------------------------------------------------------------------------------------------

And I also tried to copy a file from same usb to usb  with different file names and it works, also the same case for the sd card it works But when I try to copy from sd to usb or vice versa  it get stuck on ready state.