cancel
Showing results for 
Search instead for 
Did you mean: 

cubemx- USB Host MSC FATFS code

prabhu
Associate II
Posted on December 21, 2015 at 12:51

I tried to generate code for STM32F407VG using cubemx[4.12] with following settings for STM32F4-Discovery.

   - RCC -  High Speed Clock(HSE) –Crystal/Ceramic Resonator,   

   - USB_OTG_FS – Mode [Host Only ] &  Activate_Vbus [ticked],

   - USART6 – Mode [Asynchronous]

   - FATFS –USBDisk[ticked],  

   - USBHOST-Class for FS IP [Mass storage Host class]

   - GPIO - PC0- GPIOOutput

   - Clock -  8Mhz HSE, HCLK 168Mhz, PLL M(4), N(168), P(2), Q(7)

   - USB_HOST-Platform settings-

     Supported Ips [Gpio output] Found solutions[pc0]

Attached here .ioc project file and modified main.c is listed below. The program fails in f_open call after passing through f_mount call. Increasing of stack and heap size did not help.

 

Any solution or clue would be useful.

---------

Listing of modified main.c

#include ''stm32f4xx_hal.h''

#include ''fatfs.h''

#include ''usb_host.h''

UART_HandleTypeDef huart6;

FATFS USBDISKFatFs;         

FIL MyFile;

extern ApplicationTypeDef Appli_state;

char USB_Path[4]; 

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART6_UART_Init(void);

void MX_USB_HOST_Process(void);

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  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;

  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = 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;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

/* USART6 init function */

void MX_USART6_UART_Init(void){

  huart6.Instance = USART6;

  huart6.Init.BaudRate = 115200;

  huart6.Init.WordLength = UART_WORDLENGTH_8B;

  huart6.Init.StopBits = UART_STOPBITS_1;

  huart6.Init.Parity = UART_PARITY_NONE;

  huart6.Init.Mode = UART_MODE_TX_RX;

  huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;

  huart6.Init.OverSampling = UART_OVERSAMPLING_16;

  HAL_UART_Init(&huart6);

}

void MX_GPIO_Init(void)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */

  __GPIOH_CLK_ENABLE();

  __GPIOC_CLK_ENABLE();

  __GPIOA_CLK_ENABLE();

  /*Configure GPIO pin : PC0 */

  GPIO_InitStruct.Pin = GPIO_PIN_0;

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

//============================================================================

 

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

//-----------------------------------------------------------------------------

PUTCHAR_PROTOTYPE

{  HAL_UART_Transmit(&huart6, (uint8_t*)&ch, 1, 5);

  return ch;

}

//-----------------------------------------------------------------------------

static void Error_Handler(void){

  printf(''error....\r\n'');

  while(1)  {  }

}

//-----------------------------------------------------------------------------

#ifdef  USE_FULL_ASSERT

void assert_failed(uint8_t* file, uint32_t line){  while (1)  {  }}

#endif

//-----------------------------------------------------------------------------

static void MSC_Application(void){

  FRESULT res;                                         

  uint32_t byteswritten;                    

  uint8_t wtext[] = ''This is STM32 working with FatFs'';                                   

  printf(''in MSC_Application ...usbh path %s \r\n'',USBH_Path);

  if(f_mount(&USBDISKFatFs, (TCHAR const*)USB_Path, 0) != FR_OK) {printf(''f_mount fail ...\r\n''); Error_Handler();}/* FatFs Initialization Error */

  else{

  printf(''mount ok...\r\n'');

      if(f_open(&MyFile, ''STM32.TXT'', FA_CREATE_ALWAYS | FA_WRITE) != FR_OK){

          printf(''f_open  fail \r\n'');Error_Handler();

      }

      else{

   printf(''f_write ...\r\n'');

   res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);

   if((byteswritten == 0) || (res != FR_OK)) { Error_Handler(); }

   else {

    f_close(&MyFile); 

      printf(''success....\r\n'');}

   }

  }

 }

}

//-----------------------------------------------------------------------------

int main(void){

   HAL_Init();

 SystemClock_Config();

 MX_GPIO_Init();

 MX_USART6_UART_Init();

 HAL_UART_MspInit(&huart6);

 MX_FATFS_Init();

   MX_USB_HOST_Init();

 

 while (1) {

  MX_USB_HOST_Process();

  switch(Appli_state)  {

   case APPLICATION_START:

    MSC_Application();  Appli_state = APPLICATION_IDLE; break;

   case APPLICATION_IDLE:

   default:  break;     

  }

 }

}

//------------------------------------------------

1 REPLY 1
mats23
Associate II
Posted on August 09, 2016 at 06:27