2.串口驱动_引入HAL(HardwareAbstractionLayer硬件抽象层)的概念
1.前言:
在嵌入式程序中,可以将软件分为硬件抽象层->组件层->应用层三类,引入HAL的概念具有如下优势:1.有利于多人协同开发:可以按三个层次对人员的工作进行分配,定义好接口,可实现三层软件同时开发,大大提高开发效率2.有利于不同平台的移植:在移植时:应用 层可不过修改,组件层修改量很少,只需要着重修改hal层就可满足要求;
有关文件结构,相对应的软件所在分层,可查看此篇文章:
2.项目概述:
基于MC3172芯片,应用软件分层概念,实现串口数据的发送。
3).软件调试:
3.1HAL层:作用:1).调用芯片的驱动2).供组件层调用接口,完成初始化和服务。
源码如下:
#include "../Inc/MC3172_hal_usart.h"
#include "../../../SYSTEM/delay/MC3172Delay.h"
void HAL_USART_SendMsg(UART_HandleTypeDef UART_Init,char pStr)
{
u32 i = 0;
while(*(pStr + i) != '\0')
{
GPCOM_PUSH_TX_DATA(UART_Init->Instance,*(pStr + i));
DelayNms(1);
i++;
}
}
void HAL_USART_Init( UART_HandleTypeDef *UART_Init)
{
INTDEV_SET_CLK_RST(UART_Init->Instance,(INTDEV_RUN|INTDEV_IS_GROUP0|INTDEV_CLK_IS_CORECLK_DIV4))
GPCOM_SET_COM_MODE(UART_Init->Instance,GPCOM_UART_MODE)
//tx
GPCOM_SET_OUT_PORT(UART_Init->Instance,( \
GPCOM_P0_OUTPUT_DISABLE|GPCOM_P3_OUTPUT_DISABLE|GPCOM_P2_OUTPUT_DISABLE|GPCOM_P1_OUTPUT_DISABLE|UART_Init->Hardware.TxPinEnable| \
GPCOM_P0_IS_HIGH |GPCOM_P3_IS_TXD |GPCOM_P2_IS_HIGH |GPCOM_P1_IS_HIGH \
))
//rx
GPCOM_SET_OVERRIDE_GPIO(UART_Init->Instance, ( \
GPCOM_P0_OVERRIDE_GPIO|GPCOM_P1_OVERRIDE_GPIO |GPCOM_P2_OVERRIDE_GPIO|GPCOM_P3_OVERRIDE_GPIO | \
UART_Init->Hardware.RxPinEnable \
))
GPCOM_SET_IN_PORT(UART_Init->Instance,(UART_Init->Hardware.RxPinIs))
GPCOM_SET_COM_SPEED(UART_Init->Instance,12000000,UART_Init->Init.BaudRate)
}
结构体定义及函数声明如下:
typedef struct
{
u32 BaudRate; /*!< This member configures the UART communication baud rate.
The baud rate register is computed using the following formula:
- If oversampling is 16 or in LIN mode,
Baud Rate Register = ((PCLKx) / ((huart->Init.BaudRate)))
- If oversampling is 8,
Baud Rate Register[15:4] = ((2 * PCLKx) / ((huart->Init.BaudRate)))[15:4]
Baud Rate Register[3] = 0
Baud Rate Register[2:0] = (((2 * PCLKx) / ((huart->Init.BaudRate)))[3:0]) >> 1 */
u32 WordLength; /*!< Specifies the number of data bits transmitted or received in a frame.
This parameter can be a value of @ref UARTEx_Word_Length */
u32 StopBits; /*!< Specifies the number of stop bits transmitted.
This parameter can be a value of @ref UART_Stop_Bits */
u32 Parity; /*!< Specifies the parity mode.
This parameter can be a value of @ref UART_Parity
[url=home.php?mod=space&uid=1902110]@NOTE[/url] When parity is enabled, the computed parity is inserted
at the MSB position of the transmitted data (9th bit when
the word length is set to 9 data bits; 8th bit when the
word length is set to 8 data bits). */
u32 Mode; /*!< Specifies whether the Receive or Transmit mode is enabled or disabled.
This parameter can be a value of @ref UART_Mode */
u32 HwFlowCtl; /*!< Specifies whether the hardware flow control mode is enabled
or disabled.
This parameter can be a value of @ref UART_Hardware_Flow_Control */
u32 OverSampling; /*!< Specifies whether the Over sampling 8 is enabled or disabled, to achieve higher speed (up to fPCLK/8).
This parameter can be a value of @ref UART_Over_Sampling */
u32 OneBitSampling; /*!< Specifies whether a single sample or three samples' majority vote is selected.
Selecting the single sample method increases the receiver tolerance to clock
deviations. This parameter can be a value of @ref UART_OneBit_Sampling */
}UART_InitTypeDef;
typedef struct
{
u32 RxPinEnable;
u32 RxPinIs;
u32 TxPinEnable;
}UART_HardwareTypeDef;
/**
- [url=home.php?mod=space&uid=2666770]@Brief[/url] UART handle Structure definition
*/
typedef struct
{
u32 Instance;
UART_InitTypeDef Init;
UART_HardwareTypeDef Hardware;
}UART_HandleTypeDef;
void HAL_USART_Init(UART_HandleTypeDef* UART_Init);
void HAL_USART_SendMsg(UART_HandleTypeDef UART_Init,char pStr);
3.2组件层:作用:1).调用hal层接口函数,2).实现特定硬件的初始化,仅关注重要配置参数,3).实现服务供应用层调用。
源码如下:
#include "MC3172Usart.h"
#include "../delay/MC3172Delay.h"
char USART_RX_BUF[USART_REC_LEN]={0};
UART_HandleTypeDef UART_Init;
void UartInit()//u32 gpcom_sel
{
UART_Init.Instance = GPCOM8_BASE_ADDR;
UART_Init.Init.BaudRate = 115200;
UART_Init.Hardware.RxPinEnable = GPCOM_P2_INPUT_ENABLE;
UART_Init.Hardware.RxPinIs = GPCOM_RXD_IS_P2;
UART_Init.Hardware.TxPinEnable = GPCOM_P3_OUTPUT_ENABLE;
HAL_USART_Init(&UART_Init);
}
void UartSendMsg(char* pStr)
{
HAL_USART_SendMsg(&UART_Init,pStr);
}
3.3应用层:1).调用组件层接口,2).注重应用的实现,不在关心硬件。
源码如下:
//uart
void UserFunction2()
{
UartInit();
while(1)
{
DelayNms(1000);
UartSendMsg("0123456789");
}
}
4.硬件接入:
采用MC3172芯片,串口引脚为Rx:PC2 Tx:PC3
注:从代码处查看硬件接线,参看组件层即可:
卡发版引脚图如下所示:
实际接线如下图所示
5.实际效果:
正常显示字符串如下图所示: