cancel
Showing results for 
Search instead for 
Did you mean: 

USB MSC sometimes is detected by my PC and sometimes is not

BGavr.3
Associate II

So I'm trying to implement the USB mass storage with an SD card connected to the STM32L4.

I am using the STM32l476RG Nucleo board and have added the HSE clock source to the board.

I have successfully implemented 1-bit SDIO with the FATFs middle-ware which allows me to read and write to the SD card, but now I would like to be able to read the contents of the SD card using a USB connection.

I enabled the USB Device_Only mode on CubeMX as well as the HSE in RCC. I also enabled the USB_DEVICE middle-ware and set it to Mass Storage Class.

Before generating the code I increased the heap and stack size (0x3000, 0x5000 respectively). After generating the code, I edited usbd_storage_if.c to be able to communicate with the SD card.

I followed this tutorial for editing usbd_storage.if.c

https://microtechnics.ru/en/stm32-i-usb-mass-storage-sd-card/

The problem that I am running into is that most of the time the usb device is not detected by my PC, however sometimes it does show up as a mass storage device and I can see it in my computer under removable storage. But when it does show up (rarely) it does not display the capacity of the card nor does it show the contents of the drive.

I am not sure if the issue is hardware or software related, because it does seem to connect sometimes so I feel like it might be a hardware issue. This is how I am connecting the USB pins.

D+ and D- go directly to PA12 and PA11

D+ has a 1.5k pull-up resistor to 3.3v

ID goes to PA10 which is a GPIO Pin set to GND

I have attached main.c and usbd_storage_if.c but here are the important sections from those.

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_DMA_Init();
  MX_ADC1_Init();
  MX_SDMMC1_SD_Init();
  MX_FATFS_Init();
  MX_TIM2_Init();
  MX_I2C1_Init();
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
  */
int8_t STORAGE_GetCapacity_FS(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
  /* USER CODE BEGIN 3 */
  HAL_SD_GetCardInfo(&hsd1, &SDCardInfo);
  *block_num  = SDCardInfo.BlockNbr;
  *block_size = SDCardInfo.BlockSize;
  return (USBD_OK);
  /* USER CODE END 3 */
}
 
/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_IsReady_FS(uint8_t lun)
{
  /* USER CODE BEGIN 4 */
  return (USBD_OK);
  /* USER CODE END 4 */
}
 
/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_IsWriteProtected_FS(uint8_t lun)
{
  /* USER CODE BEGIN 5 */
  return (USBD_OK);
  /* USER CODE END 5 */
}
 
/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_Read_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  /* USER CODE BEGIN 6 */
  HAL_SD_ReadBlocks(&hsd1, buf, blk_addr, (uint32_t) blk_len, 10);
 
  return (USBD_OK);
  /* USER CODE END 6 */
}
 
/**
  * @brief  .
  * @param  lun: .
  * @retval USBD_OK if all operations are OK else USBD_FAIL
  */
int8_t STORAGE_Write_FS(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len)
{
  /* USER CODE BEGIN 7 */
  HAL_SD_WriteBlocks(&hsd1, buf, blk_addr, (uint32_t) blk_len, 10);
 
  return (USBD_OK);
  /* USER CODE END 7 */
}
 
/**
  * @brief  .
  * @param  None
  * @retval .
  */
int8_t STORAGE_GetMaxLun_FS(void)
{
  /* USER CODE BEGIN 8 */
  return (STORAGE_LUN_NBR - 1);
  /* USER CODE END 8 */
}

Any advice is appreciated, I'm just completely stumped at the moment

Thanks

2 REPLIES 2
BGavr.3
Associate II

So I managed to fix the issue I was having this morning, and decided to post my solution in case someone else runs into the same problem.

I forgot to include this line of code in my main.c

 /* Enable USB power on Pwrctrl CR2 register */

 HAL_PWREx_EnableVddUSB();

So apparently the VddUSB pin is isolated from VDD until the USV bit in the PWR_CR2 register is set. I had just assumed that the VddUSB pin was set to VDD automatically.

I still don't understand why CubeMX doesnt set that bit for you since it knows you want to use the USB pins.

Also I wonder why sometimes my pc was able to detect the drive even without setting USV bit.

BGavr.3
Associate II

well apparently that was not the issue, because after leaving my desk and coming back the USB device is no longer recognized by my PC....

Now I am convinced it has to be a hardware issue because 10 mins ago I was able to read and write data to the SD and now I cannot.