USB MSC demo for STM32 doesn't recognize device

Discuss with MikroElektronika software developers about current library development.
Post Reply
Author
Message
dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

USB MSC demo for STM32 doesn't recognize device

#1 Post by dogburd » 27 Feb 2019 19:19

Hi,

My goal is to use a USB stick to store data from my application which runs on MikroMedia7 board.

I started with the Host MSC example for STM32 from the USB_Host_ARM package from LibStock. I tried to get the example running with minimal changes to make it run on the MM7 board, as it was written for the EasyMx_PRO_v7_for_STM32_ARM board, but even this doesn't work.

What happens is that the MM7 turns on power at the USB stick, about 6 mS later the D+ line goes high (indicating a Full Speed device), then after about 60 mS it goes low. There is no more activity on the D+ and D- lines after that, so the code is not even attempting to enumerate the device, much less recognize it as a storage device with FAT32. I have verified that Vusb is good, and that no Vusb error is reported to the ARM.

Can anyone show me what I'm missing? Thanks in advance.

What I've done to the example:
1. Loaded the STM32F407ZG_PLL_25_to_120MHZ_USB scheme.
2. Changed the TFT control and USB power control pins to match the MM7 board.
3. Added some MCU and TFT initialization code copied from my application.

All software changes were done in the Host_MSC.mbas file, which is listed below.

Some things I have already tried include two different MM7 boards; two different USB sticks, both of which are confirmed as FAT32 and working on a PC; change system clock divider to 2:1 for a 60 MHz system clock; in debug mode verified that the endless loop calling USBHost_Main() gets reached, that neither the interrupt handler nor the event handler nor even the USBHost_MSCApplication() function ever get called, and that some OTG_FS_xxx registers are modified during initialization.

Code: Select all

' *
' * Project name:
'     Host_MSC
' * Description:
'     Prints content of USB Flash stick on TFT display.
' * Test configuration:
'     MCU:             STM32F107VC
'     Dev.Board:       EasyMx_PRO_v7_for_STM32_ARM
'                      http://www.mikroe.com/eng/products/view/852/easymx-pro-v7-for-stm32/
'     Oscillator:      72000000 Hz
'     SW:              mikroBasic PRO for ARM
'                      http://www.mikroe.com/mikrobasic/arm/
' * NOTES:
'     Turn on USB and TFT switches. Plug into board USB Flash stick with FAT32
'     filesystem and some files and folders on it.
'
'     In order to compile this example you will need to install FAT32 library,
'     and check in Library Manager FAT32_Defs_STM32, FAT32_STM32 and FAT32_Types_STM32.
'     http://www.libstock.com/projects/view/108/fat32-library
' *

program Host_MSC

' TFT module connections
dim TFT_DataPort as word  at GPIOE_ODR
    TFT_RST as sbit  at GPIOF_ODR.B14
    TFT_RS as sbit  at GPIOF_ODR.B15
    TFT_CS as sbit  at GPIOF_ODR.B13
    TFT_RD as sbit  at GPIOF_ODR.B12
    TFT_WR as sbit  at GPIOF_ODR.B11
    TFT_BLED as sbit  at GPIOF_ODR.B10
' End TFT module connections

dim PSW as sbit at GPIOD_ODR.B6 sfr

const cmd_bmp as byte[7238] external               ' image
const Consolas6x13_Regular as byte[1640] external  ' font

const msgDevC as string[20] = "Device Connected ..."
const msgDevD as string[23] = "Device Disconnected ..."
const msg1 as string[31] = "Simple Mass Storage Class demo."
const msg2 as string[43] = "Plug USB Flash Stick with FAT32 filesystem."
const msg3 as string[38] = "Example will list root folder content."

dim xPos, yPos, buffPos as word
dim txtBuff as char[60]                  ' holds one line of text

dim MSC_App_State as byte  ' main application state

sub procedure USB0Interrupt() iv IVT_INT_OTG_FS
  USBHost_IntHandler()
end sub

sub procedure ClearDisplay()
  TFT_Rectangle(0, 20, 320, 240)
end sub

sub procedure WriteLine(dim byref txt as string[1])
  TFT_Write_Text(txt, xPos, yPos)
  xPos = 0
  yPos = yPos + 10
end sub

sub procedure WriteChar(dim character as byte)
  txtBuff[buffPos] = character
  Inc(buffPos)
  if(character = 10)then
    txtBuff[buffPos] = 0
    WriteLine(txtBuff)
    buffPos = 0
  end if
end sub

' Application Part
sub function USBHost_MSCApplication() as byte
dim
  status as byte
  fhandle as short
  fsize, i, j as longword

  status = 0
  select case(MSC_App_State)
    case 0
      if (FAT32_Init() = 0) then
        MSC_App_State = 1
      end if

    case 1
      FAT32_Dir()
      MSC_App_State = 2

  end select
  
  result = 0
end sub

sub procedure EventHandler(dim event as byte)
  select case(event)
  
    case _USB_HOST_EVENT_DEVICE_CONNECTED
      ClearDisplay()
      TFT_Write_Text(msgDevC, 10, 20)
      Delay_ms(500)
      ClearDisplay()
      MSC_App_State = 0

      xPos = 20
      yPos = 20
      buffPos = 0

    case _USB_HOST_EVENT_DEVICE_DISCONNECTED
      ClearDisplay()
      TFT_Write_Text(msgDevD, 10, 20)

  end select
end sub

sub procedure Write_to_Data_Lines(dim _hi, _lo as byte)
dim temp as word
  temp = GPIOE_ODR
  temp = temp and 0x00FF
  GPIOE_ODR = temp or (_hi << 8)
  temp = GPIOG_ODR
  temp = temp and 0xFF00
  GPIOG_ODR = temp or _lo
end sub

sub procedure Set_Index(dim index as byte)
  TFT_RS = 0
  Write_to_Data_Lines(0, index)
  TFT_WR = 0
  asm nop end asm
  TFT_WR = 1
end sub

sub procedure Write_Command(dim cmd as byte)
  TFT_RS = 1
  Write_to_Data_Lines(0, cmd)
  TFT_WR = 0
  asm nop end asm
  TFT_WR = 1
end sub

sub procedure Write_Data(dim _data as word)
  TFT_RS = 1
  Write_to_Data_Lines(Hi(_data), Lo(_data))
  TFT_WR = 0
  asm nop end asm
  TFT_WR = 1
end sub

sub procedure DisplayInit()
'  TFT_Init_ILI9341_8bit(320, 240)
  TFT_Set_Active(@Set_Index, @Write_Command, @Write_Data)
  TFT_Init_SSD1963_7Inch_800x480(800, 480)
  TFT_Set_DBC_SSD1963(255)

  TFT_Fill_Screen(CL_BLACK)

  TFT_Image(0, 0, @cmd_bmp, 1)

  TFT_Set_Font(@Consolas6x13_Regular, CL_WHITE, FO_HORIZONTAL)

  TFT_Set_Brush(1, CL_BLACK, 0, 0, 0, 0)
  TFT_Set_Pen(CL_BLACK, 1)
  
  TFT_Write_Text(msg1, 14, 50)
  TFT_Write_Text(msg2, 14, 77)
  TFT_Write_Text(msg3, 14, 104)

  Delay_ms(2000)
end sub

sub procedure Init_MCU()
  TFT_Set_Default_Mode()
  GPIO_Digital_Output(@GPIOG_BASE, 0x00FF)
  GPIO_Digital_Output(@GPIOE_BASE, 0xFF00)
  GPIO_Digital_Output(@GPIOB_BASE, _GPIO_PINMASK_4)
  GPIOB_ODR.B4 = 0
  TFT_Set_Default_Mode()
  TFT_Set_MM_Plus()
end sub


main:
  MSC_App_State = 0
  Init_MCU()            '
  DisplayInit()

  GPIO_Digital_Output(@GPIOD_BASE, _GPIO_PINMASK_6)
  PSW = 1  ' Turn off power switch
  Delay_ms(200)

  PSW = 0  ' Turn on power switch

  USBHost_MSCInit()
  USBHost_RegisterDeviceEventHandler(@EventHandler)
  USBHost_Init()

  NVIC_IntEnable(IVT_INT_OTG_FS)

  while(true)
    USBHost_Main()
  wend

end.

User avatar
petar.suknjaja
mikroElektronika team
Posts: 683
Joined: 05 Mar 2018 09:44
Location: Belgrade

Re: USB MSC demo for STM32 doesn't recognize device

#2 Post by petar.suknjaja » 15 Mar 2019 10:00

Hi,
Post the zipped project, in meanwhile I'll review the library examples and datasheet.
Kind regards,
Petar

dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

Re: USB MSC demo for STM32 doesn't recognize device

#3 Post by dogburd » 15 Jan 2020 22:40

HI,

Sorry it's been a while, this project got shelved, but it's back now!

Attached is a zip of the project. I haven't gotten any further really, after spending another day on it.

Thanks in advance for your help!
Attachments
Host_MSC for MM.zip
(641.64 KiB) Downloaded 185 times

dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

Re: USB MSC demo for STM32 doesn't recognize device

#4 Post by dogburd » 16 Jan 2020 15:30

Reading the help file, it seems like the Host_MSC project is missing calls like the following:

USBHost_RegisterClassRequestsHandler(USBHost_MSCClassRequestsHandler);
USBHost_RegisterClassHandler(USBHost_MSCClassHandler);

Can the demo work without these callback functions? If not, how do I implement them?

User avatar
stefan.filipovic
mikroElektronika team
Posts: 1135
Joined: 18 Dec 2018 10:30

Re: USB MSC demo for STM32 doesn't recognize device

#5 Post by stefan.filipovic » 16 Jan 2020 16:25

Hi,

You just need to add the following code line in the attached project in the main after calling DisplayInit() procedure:

Code: Select all

GPIO_Alternate_Function_Enable(@_GPIO_MODULE_OTG_FS_PA89_10_11_12)
Kind regards,
Stefan Filipović

dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

Re: USB MSC demo for STM32 doesn't recognize device

#6 Post by dogburd » 16 Jan 2020 21:57

Excellent!

It's now displaying the directory, as long as there's nothing with long file names.

Thank you.

dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

Re: USB MSC demo for STM32 doesn't recognize device

#7 Post by dogburd » 14 Feb 2020 21:45

So I now have the code opening files, writing data, and closing the files. That all seems to work nicely.

The problem now is that when I plug in the USB stick (or reboot with it already plugged in) the program usually (but not always) crashes a few seconds later. If it doesn't crash within a few seconds, it is fine, so it must be due to the USB enumeration.

If I run it on the debugger, I find that the program gets stuck in an endless loop in L__I2Cx_Read. The I2C bus is used for the touchpad and the accelerometer. I don't know if the accelerometer is enabled, but the touchpad certainly is. So somehow the USB is interfering with the I2C, and the I2C routine can't correct itself.

Anyone have any ideas?

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: USB MSC demo for STM32 doesn't recognize device

#8 Post by AntiMember » 15 Feb 2020 17:10

What is the I2C number ?

dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

Re: USB MSC demo for STM32 doesn't recognize device

#9 Post by dogburd » 18 Feb 2020 14:07

It looks like I2C1 is used for the touchscreen and no other I2C is used. I2C1 is also connected to the accelerometer, but I see no evidence that the accelerometer is ever accessed.

dogburd
Posts: 15
Joined: 10 Apr 2013 16:56

Re: USB MSC demo for STM32 doesn't recognize device

#10 Post by dogburd » 18 Feb 2020 16:39

It looks like some event happens about five seconds after powerup/reset. Before this, it looks like everything works. Afterward, the system either hangs or doesn't. If it doesn't, then it seems to be fine from then on.

I've noticed that with one USB stick, the system hangs almost every time, where with the other it rarely hangs.

If I change the I2C speed from 400 kHz to 200, it hangs less frequently. If I change it to 100, it seems to run fine with both sticks.

AntiMember
Posts: 135
Joined: 02 Jan 2020 19:00

Re: USB MSC demo for STM32 doesn't recognize device

#11 Post by AntiMember » 20 Feb 2020 09:49

I can not answer more specifically. I do not have such a board.
There is a conflict of I2C1 only with OTG_HS. But OTG_FS is used.

Post Reply

Return to “Library Development Discussion”