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.