本帖最后由 ddllxxrr 于 2011-9-12 07:11 编辑
新唐库同流明LM3S库的区别
现在学习ARM的人有福啦,听说不用去看什么寄存器只要调用库函数就行,那个流明是那样,根本不用搞什么寄存器。现在学新唐了,我很想快点编程,但也不能太急应摸清其库的来龙去脉。
我手里正好有流明库,流明库是以宏定义为基础以寄存器为单位的,比如:
无论哪个流明的程序,都包含或间接包含一个文件这就是hw_type.h。
在其中就定义了如下内容:
//*****************************************************************************
//
// Macros for hardware access, both direct and via the bit-band region.
//
//*****************************************************************************
#define HWREG(x)
\
(*((volatile unsigned long *)(x)))
#define WREGH(x)
\
(*((volatile unsigned short *)(x)))
#define HWREGB(x)
\
(*((volatile unsigned char *)(x)))
#define HWREGBITW(x, b)
\
HWREG(((unsigned long)(x) & 0xF0000000) | 0x02000000 |
\
(((unsigned long)(x) & 0x000FFFFF) << 5) | ((b) << 2))
#define HWREGBITH(x, b)
\
HWREGH(((unsigned long)(x) & 0xF0000000) | 0x02000000 |
\
(((unsigned long)(x) & 0x000FFFFF) << 5) | ((b) << 2))
#define HWREGBITB(x, b)
\
HWREGB(((unsigned long)(x) & 0xF0000000) | 0x02000000 |
\
(((unsigned long)(x) & 0x000FFFFF) << 5) | ((b) << 2))
//*****************************************************************************
而在每个库函数文件里都有个述宏的痕迹:
比如:
I2c.h 中就有:
//*****************************************************************************
void
I2CMasterIntEnable(unsigned long ulBase)
{
//
// Check the arguments.
//
ASSERT((ulBase == I2C0_MASTER_BASE) || (ulBase == I2C1_MASTER_BASE));
//
// Enable the master interrupt.
//
HWREG(ulBase + I2C_O_MIMR) = 1;
}
可见流明的库是以宏定义为工具,以读写单个寄存器来实现地。
而新唐不一样,今天早晨打开新唐的看了下是以结构体为单位地。
如下程序:
STR_UART_T param;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
//
使能UART时钟
DrvGPIO_InitFunction(E_FUNC_UART0);
//
复用功能引脚设置
param.u32BaudRate
= 115200;
//
波特率
param.u8cDataBits
= DRVUART_DATABITS_8;
//
数据位
param.u8cStopBits
= DRVUART_STOPBITS_1;
//
停止位
param.u8cParity
= DRVUART_PARITY_NONE;
//
校验位
param.u8cRxTriggerLevel
= DRVUART_FIFO_1BYTES;
//
FIFO存储深度 1 字节
param.u8TimeOut
= 0;
//
FIFO超时设定
DrvUART_Open(UART_PORT0, ¶m);
//
串口开启、结构体整体赋值
可见这是从STR_UART_T这个结构体来地。点右键查看这个结构体。
/*---------------------------------------------------------------------------------------------------------*/
/*Define UART data structure
/*---------------------------------------------------------------------------------------------------------*/
typedef struct DRVUART_STRUCT
{
uint32_t
u32BaudRate;
E_DATABITS_SETTINS
u8cDataBits;
E_STOPBITS_SETTINS
u8cStopBits;
E_PARITY_SETTINS
u8cParity;
E_FIFO_SETTINGS
u8cRxTriggerLevel;
uint8_t
u8TimeOut ;
}STR_UART_T;
而这个结构体同寄存器的操作,就是靠
int32_t DrvUART_Open(E_UART_PORT u32Port, STR_UART_T *sParam)
{
UART_T * tUART;
/*-----------------------------------------------------------------------------------------------------*/
/* Check UART port
/*
*/
/*-----------------------------------------------------------------------------------------------------*/
if ((u32Port != UART_PORT0) &&
(u32Port != UART_PORT1) &&
(u32Port != UART_PORT2))
{
return E_DRVUART_ERR_PORT_INVALID;
}
/*-----------------------------------------------------------------------------------------------------*/
/* Check the supplied parity
*/
*/
*/
/*-----------------------------------------------------------------------------------------------------*/
if ((sParam->u8cParity != DRVUART_PARITY_NONE) &&
(sParam->u8cParity != DRVUART_PARITY_EVEN) &&
(sParam->u8cParity != DRVUART_PARITY_ODD)
&&
(sParam->u8cParity != DRVUART_PARITY_MARK) &&
(sParam->u8cParity != DRVUART_PARITY_SPACE))
{
return E_DRVUART_ERR_PARITY_INVALID;
}
/*-----------------------------------------------------------------------------------------------------*/
/* Check the supplied number of data bits
*/
*/
*/
/*-----------------------------------------------------------------------------------------------------*/
else if ((sParam->u8cDataBits != DRVUART_DATABITS_5) &&
(sParam->u8cDataBits != DRVUART_DATABITS_6) &&
(sParam->u8cDataBits != DRVUART_DATABITS_7) &&
(sParam->u8cDataBits != DRVUART_DATABITS_8))
{
return E_DRVUART_ERR_DATA_BITS_INVALID;
}
/*-----------------------------------------------------------------------------------------------------*/
/* Check the supplied number of stop bits
/*
*/
/*-----------------------------------------------------------------------------------------------------*/
else if ((sParam->u8cStopBits != DRVUART_STOPBITS_1) &&
(sParam->u8cStopBits != DRVUART_STOPBITS_2) &&
(sParam->u8cStopBits != DRVUART_STOPBITS_1_5)
)
{
return E_DRVUART_ERR_STOP_BITS_INVALID;
}
/*-----------------------------------------------------------------------------------------------------*/
/* Check the supplied nember of trigger level bytes
/*
*/
/*-----------------------------------------------------------------------------------------------------*/
else if ((sParam->u8cRxTriggerLevel != DRVUART_FIFO_1BYTES) &&
(sParam->u8cRxTriggerLevel != DRVUART_FIFO_4BYTES) &&
(sParam->u8cRxTriggerLevel != DRVUART_FIFO_8BYTES) &&
(sParam->u8cRxTriggerLevel != DRVUART_FIFO_14BYTES)&&
(sParam->u8cRxTriggerLevel != DRVUART_FIFO_30BYTES)&&
(sParam->u8cRxTriggerLevel != DRVUART_FIFO_46BYTES)&&
(sParam->u8cRxTriggerLevel != DRVUART_FIFO_62BYTES))
{
return E_DRVUART_ERR_TRIGGERLEVEL_INVALID;
}
if(u32Port == UART_PORT0)
{
/* Reset IP */
SYS->IPRSTC2.UART0_RST = 1;
SYS->IPRSTC2.UART0_RST = 0;
/* Enable UART clock */
SYSCLK->APBCLK.UART0_EN = 1;
}
else if(u32Port == UART_PORT1)
{
/* Reset IP */
SYS->IPRSTC2.UART1_RST = 1;
SYS->IPRSTC2.UART1_RST = 0;
/* Enable UART clock */
SYSCLK->APBCLK.UART1_EN = 1;
}
else
{
/* Reset IP */
SYS->IPRSTC2.UART2_RST = 1;
SYS->IPRSTC2.UART2_RST = 0;
/* Enable UART clock */
SYSCLK->APBCLK.UART2_EN = 1;
}
tUART = (UART_T *)((uint32_t)UART0 + u32Port);
/* Tx FIFO Reset & Rx FIFO Reset & FIFO Mode Enable */
tUART->FCR.TFR =1;
tUART->FCR.RFR =1;
/* Set Rx Trigger Level */
tUART->FCR.RFITL = sParam->u8cRxTriggerLevel;
/* Set Parity & Data bits & Stop bits */
tUART->LCR.SPE
=((sParam->u8cParity)&0x4)?1:0;
tUART->LCR.EPE
=((sParam->u8cParity)&0x2)?1:0;
tUART->LCR.PBE
=((sParam->u8cParity)&0x1)?1:0;
tUART->LCR.WLS
=sParam->u8cDataBits;
tUART->LCR.NSB
=sParam->u8cStopBits;
/* Set Time-Out */
tUART->TOR.TOIC
=sParam->u8TimeOut;
/* Set BaudRate */
BaudRateCalculator(GetUartClk(), sParam->u32BaudRate, &tUART->BAUD);
return E_SUCCESS;
}
从上述可见,新唐的库是以结构体为单位的。所以编程时一定要了解寄存器的内容哟。
|