当前位置:网站首页>STM32 USB virtual serial port communication
STM32 USB virtual serial port communication
2022-07-18 14:32:00 【NO BUG..】
Catalog
One 、 Use STM32 HAL Library Configuration
add to USB Library and select virtual serial port mode
Two 、HAL library USB Driver function introduction
3、 ... and 、 Driver function writing
One 、 Use STM32 HAL Library Configuration
choice USB Slave mode

add to USB Library and select virtual serial port mode

Two 、HAL library USB Driver function introduction
static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
// effect :USB Virtual serial port receiving function ( Equivalent to interrupt callback )
// Parameters : uint8_t* Buf: Receive data buffer
uint32_t *Len: Length of received data
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
// effect :USB Virtual serial port sending function
// Parameters : uint8_t* Buf: Send data buffer
uint16_t Len: Send data length 3、 ... and 、 Driver function writing
#ifndef _USBD_INIT_H_
#define _USBD_INIT_H_
#include <stdio.h>
#include <stdlib.h>
#include "stm32h7xx.h"
#include "stm32h7xx_hal.h"
#include "usbd_def.h"
#include "usbd_core.h"
#include "usbd_cdc.h"
#include "usb_device.h"
#include "types.h"
#include "stdarg.h"
#include "usbd_cdc_if.h"
typedef struct USB_ISR
{
void (*USBTXOver)(void); //USB Send callback function
void (*USBRXOperation)(uint8_t *RxData,uint32_t *Len); //USB Receive callback function
}USB_ISR;
extern USB_ISR Usb_ISR;
/*******************************************************************************
* function : STM32_USBD_Init
* describe : USB Initialization function
* Input : Tx_Over: Send callback function
RxFunction: Receive callback function
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void STM32_USBD_Init(void Tx_Over(void), void (*RxFunction)(uint8_t *RX_Data,uint32_t *Len));
/*******************************************************************************
* function : USBD_SendBuff
* describe : USB Sending function
* Input :Buf: Send data buffer
Len: Send data length
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void USBD_SendBuff(uint8_t* Buf, uint16_t Len);
/*******************************************************************************
* function : usb_printf
* describe : printf Redefining functions
* Input : None
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void usb_printf(const char *format, ...);
/*******************************************************************************
* function : RecvMemcpy
* describe : Receive data callback function
* Input : None
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void RecvMemcpy(uint8_t *str,uint32_t *Len);
/*******************************************************************************
* function : RecvProcess
* describe : Receive data processing function
* Input : None
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void RecvProcess(uint8_t *str,uint32_t *Len);
#endif#include "usbd_init.h"
extern USBD_HandleTypeDef hUsbDeviceFS;
extern PCD_HandleTypeDef hpcd_USB_OTG_FS;
extern uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
USB_ISR Usb_ISR;
/*******************************************************************************
* function : STM32_USBD_Init
* describe : USB Initialization function
* Input : Tx_Over: Send callback function
RxFunction: Receive callback function
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void STM32_USBD_Init(void Tx_Over(void), void (*RxFunction)(uint8_t *RX_Data,uint32_t *Len))
{
MX_USB_DEVICE_Init();
Usb_ISR.USBTXOver = Tx_Over;
Usb_ISR.USBRXOperation = RxFunction;
}
/*******************************************************************************
* function : USBD_SendBuff
* describe : USB Sending function
* Input :Buf: Send data buffer
Len: Send data length
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void USBD_SendBuff(uint8_t* Buf, uint16_t Len)
{
CDC_Transmit_FS(Buf,Len);
}
/*******************************************************************************
* function : usb_printf
* describe : printf Redefining functions
* Input : None
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void usb_printf(const char *format, ...)
{
va_list args;
uint32_t length;
va_start(args, format);
length = vsnprintf((char *)UserTxBufferFS, APP_TX_DATA_SIZE, (char *)format, args);
va_end(args);
CDC_Transmit_FS(UserTxBufferFS, length);
}
void OTG_FS_IRQHandler(void)
{
HAL_PCD_IRQHandler(&hpcd_USB_OTG_FS);
}
/*******************************************************************************
* function : RecvMemcpy
* describe : Receive data callback function
* Input : None
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void RecvMemcpy(uint8_t *str,uint32_t *Len)
{
uint8_t *mainstr = (uint8_t *)malloc(sizeof(uint8_t)*(*Len));
memcpy(mainstr,str,*Len);
RecvProcess(str,Len);
}
/*******************************************************************************
* function : RecvProcess
* describe : Receive data processing function
* Input : None
* Output : None
* return : None
* Be careful : None
*******************************************************************************/
void RecvProcess(uint8_t *str,uint32_t *Len)
{
}边栏推荐
- UE 在Editor的资源右键菜单上添加两个按钮
- [pytorch quantitative practice (1)]
- 基于单片机的可燃气烟雾系统设计(#0489)
- 【Leetcode】2115. Find All Possible Recipes from Given Supplies
- Key points for upgrading firmware of Hezhou air820ug
- QT使用多线程
- Tomato学习笔记-Vscode配置Makefile(使用task.jason和launch.jason)
- [the most complete and detailed] seven distributed global ID generation strategies
- Basic interview questions
- [leetcode brush questions]
猜你喜欢
![[每周一更]-(第3期):Web开发安全注意事项](/img/2e/64e2f7aca24abd6b68d844e0b78a3e.jpg)
[每周一更]-(第3期):Web开发安全注意事项

Huawei cloud stack opens its framework to the south to help ecological partners enter the cloud efficiently

Design of Bluetooth electronic scale system based on MCU (0493)

【PyTorch量化实践(1)】

Codeforces Round #803 (Div. 2) B. Rising Sand

自定义MVC

深度学习(2020李宏毅)学习记录
![[phantom engine UE] package abnormal problem collection](/img/99/0db3caf19fafdec17b05b863f23903.png)
[phantom engine UE] package abnormal problem collection

基于单片机的蓝牙电子秤系统设计(#0493)

线上MySQL的自增id用尽怎么办?
随机推荐
[summary of quantitative methods of pytorch model]
Reconstructing the geometric form of weight space with training set
In depth learning (2020 Li Hongyi) learning records
认识多银行资金系统(二)---------今日首页
If you don't want to step on those holes in SaaS, you must first understand the "SaaS architecture"
Basic use of Anaconda
Binary build kubernetes
函数关系简
[cityengine] cityengine2019 installation and use and city model construction
XGBoostError: [10:19:14] C:\dev\libs\xgboost\src\objective\objective.cc:23:
合宙Air820ug升级固件要点
Go-Excelize API源码阅读(一)——NewFile()
[atlas quick start]
第一章 环境配置
nn. Gru() use
【PyTorch量化实践(2)】
【AI芯片CAISA】
Arm64 Galaxy Kirin desktop system V10 (SP1) markdown editor retext replaces typora
How to turn off win11 system protection? Win11 system protection shutdown method
Codeforces Round #804 A The Third Three Number Problem