4435 views|3 replies

171

Posts

0

Resources
The OP
 

【ST NUCLEO-H743ZI Review】+ USB OTG+FATFS [Copy link]

This post was last edited by sylar^z on 2020-5-19 00:38

The NUCLEO-H743ZI board has its own USB interface, which can be used as a device USB port or as a main USB port to connect external USB flash drives, HID devices, etc.

1. Hardware Schematic Diagram

Hardware circuit of the USB interface of the NUCLEO-H743ZI board. USB_VBUS, USB_DM, USB_DP, USB_ID, GND are the 5 USB pins. USB_PowerSwitchOn is the power output control pin. USB_OverCurrent is the power model detection pin.

2. Create a project with STM32CubeMX

First, create a project through STM32CubeMX. Friends who are not familiar with STM32CubeMX can refer to my previous post ( https://en.eeworld.com/bbs/thread-1122042-1-1.html ) for instructions on how to create a NUCLEO-H743ZI project with STM32CubeMX.

Enter the configuration interface of NUCLEO-H743ZI, mainly configure the following function items:

1. USB_OTG_FS configuration

Select USB_OTG_FS in the list on the left, set the configuration mode to Host_Only, and enable VBUS_sensing. Enable global interrupts. Leave other settings as default.

2. USB middle layer function configuration

Select USB_HOST in the list on the left, set the configuration mode to MSC, and the mass storage mode. Select IO output for the power driver, and the pin is PG6. Other settings are as default.

3. FATFS function configuration

Select FATFS in the list on the left, and set the configuration mode to USB Disk. In the configuration, select CODE_PAGE to support simplified Chinese. Other settings are as default.

4. Add key interrupt

The user button on the board is PC13, which is configured by default to trigger the interrupt on the rising edge, that is, it is triggered when the button is pressed.

5. After saving the project, click GENERATE CODE in the upper right corner to automatically generate the project code

3. Programming

1. The source code file generated by STM32CubeMX has made basic configuration for the USB OTG function. After configuration, you need to enable the USB power output, which is the red part in the code.

/**

* Init USB host library, add supported class and start the library

* @retval None

*/

void MX_USB_HOST_Init(void)

{

/* USER CODE BEGIN USB_HOST_Init_PreTreatment */

/* USER CODE END USB_HOST_Init_PreTreatment */

/* Init host Library, add supported class and start the library. */

if (USBH_Init(&hUsbHostFS, USBH_UserProcess, HOST_FS) != USBH_OK)

{

Error_Handler();

}

if (USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS) != USBH_OK)

{

Error_Handler();

}

if (USBH_Start(&hUsbHostFS) != USBH_OK)

{

Error_Handler();

}

/* USER CODE BEGIN USB_HOST_Init_PostTreatment */

/* Activate VBUS on the port */

USBH_LL_DriverVBUS(&hUsbHostFS, 0);

/* USER CODE END USB_HOST_Init_PostTreatment */

}

2. Add FATFS function code in USB connection, disconnection and other events.

void MX_FATFS_Init(void)

{

/*## FatFS: Link the USBH driver ###########################*/

retUSBH = FATFS_LinkDriver(&USBH_Driver, USBHPath);

/* USER CODE BEGIN Init */

/* additional user code for init */

/* USER CODE END Init */

}

/*

* user callback definition

*/

static void USBH_UserProcess (USBH_HandleTypeDef *phost, uint8_t id)

{

/* USER CODE BEGIN CALL_BACK_1 */

switch(id)

{

case HOST_USER_SELECT_CONFIGURATION:

break;

case HOST_USER_DISCONNECTION:

Appli_state = APPLICATION_DISCONNECT;

FATFS_UnLinkDriver(USBHPath);

f_mount(NULL, "", 0);

break;

case HOST_USER_CLASS_ACTIVE:

Appli_state = APPLICATION_READY;

break;

case HOST_USER_CONNECTION:

Appli_state = APPLICATION_START;

f_mount(&USBHFatFS, (TCHAR const*)USBHPath, 0);

break;

default:

break;

}

/* USER CODE END CALL_BACK_1 */

}

3. Key interrupt processing. Set a flag.

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

if(USER_Btn_Pin == GPIO_Pin)

{

btnClickFlag = 1;

}

}

4. Press the button to create a file. Add the function code in the while loop of the main function. Test to generate a txt file named testFile in the USB flash drive.

if(btnClickFlag)

{

btnClickFlag = 0;

//Create a file

f_open(&testFile, "testFile.txt", FA_CREATE_ALWAYS | FA_WRITE);

f_close(&testFile);

}

4. Test the USB OTG board by connecting it to a USB flash drive. Please note that the USB flash drive must be in FAT format, as NTFS, exFAT, etc. cannot be recognized.

5. File viewing: Generate a testFile.txt file in the USB flash drive. Since there is no content, the file size is 0Kb.

This post is from stm32/stm8

Latest reply

Try writing a paragraph of hello eeworld in testFile.txt   Details Published on 2020-6-15 20:54
 

9702

Posts

24

Resources
2
 

Try writing a paragraph of hello eeworld in testFile.txt

This post is from stm32/stm8

Comments

I'm on a business trip today. I can't change this until I get back.  Details Published on 2020-6-15 22:15
 
Personal signature虾扯蛋,蛋扯虾,虾扯蛋扯虾
 

171

Posts

0

Resources
3
 
littleshrimp posted on 2020-6-15 20:54 Try writing a paragraph of hello eeworld in testFile.txt

I'm on a business trip today. I can't change this until I get back.

This post is from stm32/stm8
 
 

171

Posts

0

Resources
4
 

Finally back.

After building the FATFS system and generating files, it is relatively simple to add some content to the files.

Just add the fwrite() function to write the content.

The headbuff[17] array in the code only displays its contents here and is commented out at this time. It is defined in the file header in the program.

This post is from stm32/stm8
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list