记录学习基于STM32F104ZET6的UART串口通信封装。

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 __ _H

typedef 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_H

void 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)
    {
        
        
    }


}

发送成功了。我是个小白,欢迎提意见呀!!

  • 22
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值