基于i.MX233的UART测试程序

i.MX233是freescale公司退出的一款ARM9处理器,该芯片集成了PMU单元,并集成了各种常用接口,大大降低了系统成本。。

硬件配置如下:

1. USB线接PC机和开发板,进行数据下载和供电;

2. 连接串口,用于打印信息;

3. 最好不要安装LCD屏,防止USB供电不足,如果供电不足,也可以使用外置电源供电。

4. 启动模式配置为:0000,即USB启动

软件下载工具: 采用freescale的mtfg tool。

编译环境:

arm-none-linux-gnueabi-gcc和gcc-arm-linux-gnueabi

具体安装方式可参考:

程序代码:

代码部分比较简单,只有一个UART的简单初始化部分。即最重要的代码部分是uart_init()代码如下:

int Uart_init()
{
/* setting baud rate divisor */
HW_UARTDBGIBRD = DIVISOR >> 6; //Integer
HW_UARTDBGFBRD = DIVISOR & 0x3f;//Fractional 

/* BRD only updated when a write to LCR_H, so this should be at the end 
 * 8 data bits, no parity bit, 1 stop bits, FIFO enabled */
HW_UARTDBGLCR_H = 0x70;//Even Parity Select

/* uart enabled, and receive and transmit, disable flow control */
HW_UARTDBGCR = 0x301;

return 0;
}

init函数中只是设定了串口的波特率,校验位,数据位等必备的参数,代码波特率中设定的是115200,通过uart.h中的宏定义#define UARTCLK24000000 //24MHz来确定。然后通过设定uart enable就可以使用了。main()函数中只是调用了这个init函数然后就可以打印了。打印即和pc通过串口发送数据。

其他代码:

//file: uart.h

#define UART
#ifdef UART
#define HW_UARTDBGDR (* (volatile unsigned *) 0x80070000)// data register
#define HW_UARTDBGFR (* (volatile unsigned *) 0x80070018)// flag register 
#define HW_UARTDBGIBRD (* (volatile unsigned *) 0x80070024)// integer baud rate divisor 
#define HW_UARTDBGFBRD (* (volatile unsigned *) 0x80070028)// fractional baud rate divisor
#define HW_UARTDBGLCR_H (* (volatile unsigned *) 0x8007002C)// line control registe, high byte
#define HW_UARTDBGCR (* (volatile unsigned *) 0x80070030)// control register

#define Uart_out(ch)HW_UARTDBGDR = (unsigned char) (ch)
#define UARTCLK 24000000 //24MHz
#define BAUD_RATE 115200
#define DIVISOR UARTCLK*4 / BAUD_RATE //UARTCLK*4 is UARTCLK << 3
#endif 

#define HW_DIGCTL_MICROSECONDS (* (volatile unsigned *) 0x8001c0c0)

int print_string(char *str);
int Uart_init();
void delay(unsigned int us);
int error();

//uart.h end


//file:uart.c

#include "uart.h"

/*
.test
.globl _start //init  sp 0x0000 - 0x1000;
mov sp,0x1000
bl main
*/
int print_string(char *str)
{
int loop = 0;
while(*str)
{
while (HW_UARTDBGFR & 0x20) /* wait if transmit fifo is full */
{
loop++;
if (loop > 10000)break;
};

HW_UARTDBGDR = *str;
if (*str == '\n')
{
while(HW_UARTDBGFR & 0x20)
{
loop++;
if (loop > 10000)break;
}
HW_UARTDBGDR = '\r';
}
str++;
}
return 0;
}

/************************************************************/
/*Fuction: UART init
/*Mode: 
/************************************************************/
int Uart_init()
{
/* setting baud rate divisor */
HW_UARTDBGIBRD = DIVISOR >> 6; //Integer
HW_UARTDBGFBRD = DIVISOR & 0x3f;//Fractional 

/* BRD only updated when a write to LCR_H, so this should be at the end 
* 8 data bits, no parity bit, 1 stop bits, FIFO enabled */
HW_UARTDBGLCR_H = 0x70;//Even Parity Select

/* uart enabled, and receive and transmit, disable flow control */
HW_UARTDBGCR = 0x301;

return 0;
}

void delay(unsigned int us)
{
unsigned int start , cur;
start = cur = HW_DIGCTL_MICROSECONDS;
while (cur < start+us) {
cur = HW_DIGCTL_MICROSECONDS;
/*printf("0x%x\r\n",cur);*/
}
}

int error()
{
while(1);
return 0;
}

//uart.c end


//file:uart_test.c

#include "uart.h"


int main()
{
Uart_init();//uart init

print_string("\n...............................\n");
print_string(".    uart init is finished!   .\n");
print_string("...............................\n");
print_string("\nHello World!\n");
print_string("\nThanks!\n");
print_string("...............................\n");
print_string("test");
while(1);
return 0;
}

//uart_test.c end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值