2025-01-08 11:23 PM - last edited on 2025-01-09 12:10 AM by SofLit
#include "main.h"
#include "fatfs.h"
#include "usart.h"
#include "usb_host.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ff_gen_drv.h"
#include "usb_host.h"
#include "usbh_diskio.h"
#include <stdint.h>
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define USE_USB_FS // Uncomment for Full-Speed USB
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
FATFS USBDISKFatFs; /* File system object for USB disk logical drive */
FIL MyFile; /* File object */
char USBDISKPath[4]; /* USB Host logical drive path */
USBH_HandleTypeDef hUsbHostFS; /* USB Host handle */
uint64_t count=0;
uint8_t operationCount = 0; /* Track the number of iterations */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_NVIC_Init(void);
void MX_USB_HOST_Process(void);
/* USER CODE BEGIN PFP */
void USB_POWER_SWITCH_ON(void);
void USB_POWER_SWITCH_OFF(void);
void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USB Power Control Functions */
void USB_POWER_SWITCH_ON(void)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_SET); // Example pin for VBUS control
printf("VBUS Enabled\n");
}
void USB_POWER_SWITCH_OFF(void)
{
HAL_GPIO_WritePin(GPIOG, GPIO_PIN_6, GPIO_PIN_RESET); // Turn off VBUS power
printf("VBUS Disabled\n");
}
void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_USART3_UART_Init();
MX_FATFS_Init();
MX_USB_HOST_Init();
/* Initialize interrupts */
MX_NVIC_Init();
/* USER CODE BEGIN 2 */
/* Power on USB VBUS */
USB_POWER_SWITCH_ON();
// HAL_Delay(200); // Short delay for power stabilization
printf("System Initialized\n");
/* Initialize USB Host */
USBH_Init(&hUsbHostFS, USBH_UserProcess, 0);
USBH_RegisterClass(&hUsbHostFS, USBH_MSC_CLASS);
USBH_Start(&hUsbHostFS);
printf("Logical Drive Path: %s\n", USBDISKPath);
/* Mount the filesystem */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();
/* USER CODE BEGIN 3 */
count++;
if (f_mount(&USBDISKFatFs, USBDISKPath, 0) == FR_OK)
{
printf("File System Mounted Successfully\n");
/* Create and write to a file */
if (f_open(&MyFile, "SABARI.txt", FA_OPEN_APPEND | FA_WRITE) == FR_OK)
{
const char *data = "HI";
UINT bytesWritten;
printf("File Opened for Writing\n");
if (f_write(&MyFile, data, strlen(data), &bytesWritten) == FR_OK && bytesWritten == strlen(data))
{
printf("File Written Successfully\n");
}
else
{
printf("Failed to Write to File\n");
}
f_close(&MyFile);
}
else
{
printf("Failed to Open File for Writing\n");
}
/* Read from the file */
if (f_open(&MyFile, "SABARI.txt", FA_READ) == FR_OK)
{
char buffer[64];
UINT bytesRead;
printf("File Opened for Reading\n");
if (f_read(&MyFile, buffer, sizeof(buffer) - 1, &bytesRead) == FR_OK)
{
buffer[bytesRead] = '\0'; // Null-terminate string
printf("File Content: %s\n", buffer);
}
else
{
printf("Failed to Read from File\n");
}
f_close(&MyFile);
}
else
{
printf("Failed to Open File for Reading\n");
}
/* Unmount the filesystem */
f_mount(NULL, USBDISKPath, 0);
printf("File System Unmounted\n");
}
else
{
printf("Failed to Mount File System\n");
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 72;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 3;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief NVIC Configuration.
* @retval None
*/
static void MX_NVIC_Init(void)
{
/* OTG_FS_IRQn interrupt configuration */
HAL_NVIC_SetPriority(OTG_FS_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(OTG_FS_IRQn);
}
Hi,
Board- STM32F756ZG , F7 series.
I have successfully implemented USB functionality (mounting, opening, writing to, and unmounting) on an STM32F756ZG microcontroller using the HAL library in a bare-metal environment. However, I am facing difficulties with USB read and write operations when transitioning to a FreeRTOS-based setup.
I would appreciate any guidance or example code on how to correctly implement USB operations (mount, open, write, and unmount) using FreeRTOS, ensuring that the functionality matches my previous bare-metal implementation while utilizing the RTOS for task management. Any assistance would be highly valuable.
2025-01-09 01:11 AM
I suggest you start a new project with CubeMX generated code. Enable FreeRTOS™. Configure a USB device. I can share with you an example application CDC with FreeRTOS™ on F4 if you think it might help you.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.