mini2440,串口的使用。

从mini2440开发板的原理图可知开发板引出的是uart0,通过查阅数据手册可知uart0的接收、发送引脚是GPH2,GPH3复用。


Makefile:

obj = start.o init.o uart.o main.o
uart.bin:$(obj)
	arm-linux-ld -Tuart.lds $^ -o uart
	arm-linux-objcopy -O binary uart $@
%.o : %.c
	arm-linux-gcc -c $< -o $@
%.o : %.S
	arm-linux-gcc -c $< -o $@

clean:
	rm -f uart uart.bin *.o
连接脚本:

SECTIONS 
{
    . = 0x30000000;
    .text  :  { *(.text) }
    .data  : ALIGN(4) {*(.data)}
    .bss   : ALIGN(4) {*(.data)}
}
初始化CPU:

.text

.global _start 
_start:
    
      ldr sp,   =4096       @设置堆栈
      bl  close_wtdog       @关闭看门狗
      bl  init_clk          @初始化时钟
      bl  init_mem          @初始化存储管理器
      bl  copy              @拷贝代码到SDARM
      ldr sp,   =0x34000000 @设置堆栈指针
      ldr pc,   =main       @调用main函数
初始化CPU相关C代码:

#include "mini2440.h"

/*关闭看门狗*/
void close_wtdog()
{
  WTDOG = 0x0;
}

/*刷新频率在2^11 - (100MHZ * 6.8 / 1MHZ)*/
/*初始化存储器管理器*/
void init_mem()
{
   BWSCON   = 0x22011110;   
   BANKCON0 = 0x00000700;    
   BANKCON1 = 0x00000700;   
   BANKCON2 = 0x00000700;   
   BANKCON3 = 0x00000700;
   BANKCON4 = 0x00000700;
   BANKCON5 = 0x00000700;
   BANKCON6 = 0x00018005;
   BANKCON7 = 0x00018005;
   REFRESH  = 0x008C04F4;
   BANKSIZE = 0x000000B1;
   MRSRB6   = 0x00000030;
   MRSRB7   = 0x00000030;
                                  
}

/*初始化时钟*/
/*F = 200MHZ H = 100MHZ P = 50 MHZ*/
void init_clk()
{
  CLKDIVN =  0x3; 
  /*设置总线模式为异步总线模式*/ 
  __asm(
    "mrc    p15, 0, r1, c1, c0, 0\n"       
    "orr    r1, r1, #0xc0000000\n"         
    "mcr    p15, 0, r1, c1, c0, 0\n"       
    );
  MPLLCON = (0x5c << 12 | 0x01 << 4 | 0x2 << 0);
   
}

/*拷贝4KB代码到SDARM*/
void copy()
{
  unsigned int *bas = (unsigned int*)0x0;
  unsigned int *start = (unsigned int*)0x30000000;
  while(bas < (unsigned int*)4096)
  {
    *start = *bas;
    start++;
    bas++;
  }
}
串口初始化,发送,接收。

#include "mini2440.h"

void init_uart()
{
   /*初始化端口*/
   GPHCON |= (0x2 << 6 | 0x2 << 4);
   GPHUP  |= 0x3 << 2;
   
   /*波特率115200,无奇偶校验,1位停止位*/
   ULCON0 |= 0x3;
   UCON0  |= (0x1 << 2 | 0x1 << 0);
   UBRDIV0 = 26;
}

/*接收一个字符*/
char getchar()
{
  while(!(UTRSTAT0 & (0x1 << 0)));
  return URXH0;
}

/*发送一个字符*/
void outchar(char c)
{
  while(!(UTRSTAT0 & (0x1 << 2)));
  UTXH0 = c;
}

/*发送一个字符串*/
void outstring(char* str)
{
  while(*str)
  {
    outchar(*str++);
  }
}

#ifndef _UART_H_
#define _UART_H_

void init_uart();
char getchar();
void outchar(char c);
void outstring(char* str);

#endif
测试:

#include "mini2440.h"
#include "uart.h"

int main()
{
  GPBCON |= 0x1 << 10;
  GPBDAT &= (~(0x1 << 5)); 
  init_uart();
  outstring("\r\n***************************\r\n");
  outstring("********wellcome***********\r\n");
  outstring("***************************\r\n");

  while(1);
}
本实验没有使用中断,采用轮询发送、接收状态标记位。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值