1.打开我们的STM32CUBEMX配置串口。
我们采用独立封装模块的方式,首先开始封装:
#ifndef __CONFIG_H
#define __CONFIG_H
#define USE_GPIO_DEVICE (1)
#define USE_UART_DEVICE (1)
/*管理自定义库是否使用*/
#define USE_PRINTF_LIB (1)
#endif
一.创建UART设备对象的管理文件
1.UART设备对象的封装
#ifndef __ _H
#define __ _Htypedef struct UARTDev{
char *name;
unsigned char channel;
int (*Init)(struct UARTDev *PDev);
int(*Write)(struct UARTDev *PDev,unsigned char *Wbuf,unsigned int Length);
int(*Read)(struct UARTDev *PDev,unsigned char *Rwbuf,unsigned int Length);
struct UARTDev *next;
}UARTDevice;
void UARTDevReigest(void);
void UARTDeviceIsnit(struct UARTDev *PData);
UARTDevlice *UARTDeviceFind(const char *name);
#endif
}
2.对设备对象的管理进行插入删除操作.
#include "drv_uart.h"
#include "dev_uart.h"
#include "stm32f4xx_hal.h" // Device header
#include "string.h"static UARTDevlice *PHead=NULL;
void UARTDevReigest(void)
{
UARTDeviceCreate();}
void UARTDeviceIsnit(UARTDevlice *PData)
{
if(NULL==PHead)
{
PHead=PData;
}
else{
PData->next=PHead;
PHead=PData;
}
UARTDevlice *UARTDeviceFind(const char *name)
{
UARTDevlice *PD=PHead;
while(PD!=NULL)
{
if(strstr(PD->name,name))
{
return PD;
}
PD=PD->next;
}
return NULL;
}
二.对UART的驱动管理
#ifndef __DRIVERS_H
#define __DRIVERS_H#include "devices.h"
#ifdef USE_UART_DRIVER
#include "drv_uart.h"
#endif
#endif
#ifndef __DRV_UART_H
#define __DRV_UART_Hvoid UARTDeviceCreate(void);
#endif
实现UART的驱动。
#include "stm32f4xx_hal.h" // Device header
#include "dev_uart.h"
#include "error.h"
#include "./drv_config/uart_config.h"
#include "usart.h"
static int UARTDeviceInit(struct UARTDev *PData);
static int UARTDeviceWinte(struct UARTDev *PData,const unsigned char*Buf,unsigned short Length);
static int UARTDeviceRead(struct UARTDev *PData, unsigned char*Buf,unsigned short Length);
static UARTDevlice GUARTDevices[] = {
DGBUART
};
void UARTDeviceCreate(void)
{
unsigned int Num=sizeof(GUARTDevices)/sizeof(struct UARTDev);
for(unsigned int i=0;i<Num;i++)
{
UARTDeviceIsnit(&GUARTDevices[i]);
}}static int UARTDeviceInit(struct UARTDev *PData)
{
if(NULL==PData)
{
return -EINVAL;
}
switch(PData->channel)
{
case 1:break;case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
default :break;
}
return ESUCCESS;}
static int UARTDeviceWinte(struct UARTDev *PData,const unsigned char*Buf,unsigned short Length)
{
if(NULL==PData) return -EINVAL;
if(NULL==PData->name) return -EINVAL;
if(0==Length) return -EINVAL;
switch(PData->channel)
{
case 1:
{
HAL_StatusTypeDef status=HAL_UART_Transmit(&huart1,(unsigned char*)Buf,Length,Length*10);
if(HAL_OK!=status) return -EIO;/*发送失败返回硬件错误EIO*/
break;
}case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
default :break;
}
return (int)Length;/*发送成功返回数据个数*/
}
static int UARTDeviceRead(struct UARTDev *PData, unsigned char*Buf,unsigned short Length)
{
if(NULL==PData) return -EINVAL;
if(NULL==PData->name) return -EINVAL;
if(0==Length) return -EINVAL;
switch(PData->channel)
{
case 1:
{
HAL_StatusTypeDef status=HAL_UART_Receive(&huart1,(uint8_t *)Buf,Length,Length*10);
if(HAL_OK!=status) return -EIO;/*发送失败返回硬件错误EIO*/
break;
}case 2:break;
case 3:break;
case 4:break;
case 5:break;
case 6:break;
default :break;
}
return (int)Length;/*发送成功返回数据个数*/}
三.编写测试函数
#include "devices.h"
#include "stm32f4xx_hal.h" // Device header
#include "error.h"
#include "drv_uart.h"
#include "string.h"
#include "libs.h"
void App_uart_test(void)
{IODevReigest();
UARTDevReigest();
UARTDevlice *pDbgUart=UARTDeviceFind("DeBug UART");
if(NULL==pDbgUart) return ;
if(pDbgUart->Init(pDbgUart)!=ESUCCESS) return ;pDbgUart->Winte(pDbgUart,(unsigned char*)"Hello Word!\r\n",14);
while(1)
{
}
}
发送成功了。我是个小白,欢迎提意见呀!!