时钟设置和uart基本配置

开发板采用友善之臂的mini2440

以下代码包含了uart配置,uart需要的时钟配置,FCLK:HCLK:PCLK = 1:2:4=200MHz:100MHz:50MHz
时钟控制逻辑给整个芯片提供3种时钟:FCLK用于cpu核;HCLK用于AHB(advanced high performance bus)总线上设备(高性能模块),比如CPU核,存储器控制器,中断控制器,lcd控制器,DMA和USB主机模块等;PCLK用于APB(advance peripheral bus)总线上设备(低带宽的周边外设),比如看门狗,IIS,I2C,PWM定时器,MMC接口,ADC,UART、GPIO,RTC和SPI

2440有两个PLL:MPLL用于FCLK、HCLK、PCLK,UPLL用于usb设备
其他寄存器的设置和计算公式查看技术手册,可到我的下载里找到相应资源

init.S

.equ SDRAM_BASE,   0x30000000

.text
.global _start
_start:
    bl disable_watch_dog
    ldr sp, =4096
    bl clock_init
    bl mem_ctrl_setup
    bl copy_steppingstone_to_sdram
    ldr pc, =on_sdram

on_sdram:
    ldr sp, =0x34000000
    ldr pc, =main
loop:
    b loop

test:
    ldr r0, =0x56000010
    ldr r1, =0x11400
    str r1, [r0]
    ldr r0, =0x56000014
    mov r1, #0
    str r1, [r0]
    mov pc, lr

disable_watch_dog:
    ldr r0, =0x53000000
    mov r1, #0
    str r1, [r0]
    mov pc, lr

copy_steppingstone_to_sdram:
    mov r0, #0
    add r1, r0, #4096       @1024*4 4K byte
    ldr r2, =SDRAM_BASE
1:
    ldr r3, [r0], #4
    str r3, [r2], #4
    cmp r0, r1
    bne 1b      @relative to pc 1 is below, (b:below f:forward)
    mov pc, lr   

uart.c

#include<stdio.h>

#define GPBCON (*(volatile int *)0x56000010)
#define GPBDAT (*(volatile int *)0x56000014)
#define GPHCON (*(volatile int *)0x56000070)
#define GPHUP (*(volatile int *)0x56000078)

#define ULCON0 (*(volatile int *)0x50000000)
#define UCON0 (*(volatile int *)0x50000004)
#define UFCON0 (*(volatile int *)0x50000008)
#define UMCON0 (*(volatile int *)0x5000000c)
#define UBRDIV0 (*(volatile int *)0x50000028)

#define UTXH0 (*(volatile int *)0x50000020)
#define URXH0 (*(volatile int *)0x50000024)
#define UTRSTAT0 (*(volatile int *)0x50000010)

#define MEM_CTL_BASE  (volatile int *)0x48000000
#define MPLLCON (*(volatile int *)0x4C000004)
#define CLKDIVN (*(volatile int *)0x4C000014)
//#define MPLLVALUE ((0x11 << 12) | (0x01 << 4) | (0x00 << 0)) 测试插口回显不正确原因未找到
#define MPLLVALUE ((0x5c << 12) | (0x01 << 4) | (0x02)) //完全手册上的配置

void Delay(int time) {
    int i;

    for (; time > 0; time--) {for (i = 0; i < 600; i++);
    }
}

unsigned char uart_getc(void) 
{
    while (!(UTRSTAT0 & 0x01));

    return URXH0;
}
void uart_putc(unsigned char c) 
{
    while (!(UTRSTAT0 & 0x4));

    UTXH0 = c;
}

void clock_init() 
{
    CLKDIVN = 0x03;

	//设置异步模式
    __asm__ ("mrc p15,0,r0,c1,c0,0\n""orr r0,r0,#0xC000000 ;#R1_nF:OR:R1_iA\n""mcr p15,0,r0,c1,c0,0\n"
    );
MPLLCON = MPLLVALUE; }

void mem_ctrl_setup()
{
    volatile unsigned int *p = MEM_CTL_BASE;
    
    p[0] = 0x22011110;
    p[1] = 0x00000700;
    p[2] = 0x00000700;
    p[3] = 0x00000700;
    p[4] = 0x00000700;
    p[5] = 0x00000700;
    p[6] = 0x00000700;
    p[7] = 0x00018005;
    p[8] = 0x00018005;
    p[9] = 0x008C04F5;
    p[10] = 0x000000B1;
    p[11] = 0x00000030;
    p[12] = 0x00000030;
}

int main(int argc, char *argv[]) 
{
    unsigned char c = 0;

	//GPIO设置uart功能
    GPHCON &= ~(0xF << 4);
    GPHCON |= 0xA0;
    GPHUP = 0x0c;

	//uart配置,此处为最基本的配置,fifo未用,mda未用
    //ULCON0 = 0x28;
    ULCON0 = 0x03;
    UCON0 = 0x05;
    UFCON0  = 0x00;
    UMCON0  = 0x00;
    //UBRDIVn = (int)( UART clock / ( buad rate x 16 )  ) –1
    UBRDIV0 = (int)(50000000 / (115200 * 16)) - 1; //115200

	//设置led灯用来调试程序使用
    GPBCON &= (~0x03 << 10);
    GPBCON |= (0x1 << 10);
    GPBDAT |= ((0x1 << 5));

    while (1) {
    ┊   c = uart_getc();
    ┊   GPBDAT &= (~(0x1 << 5)); //lightuart_putc(c + 1);
    ┊   GPBDAT |= ((0x1 << 5));
    }

    return 0;
} 

Makefile

.PHONY:clean

bin:
    arm-linux-gcc -c init.S -o init.o
    arm-linux-gcc -c uart.c -o uart.o
    arm-linux-ld -Ttext=0x30000000 init.o uart.o -o uart
    arm-linux-objcopy -O binary uart uart.bin
    arm-linux-objdump -D -m arm uart > uart.dis

clean:
    rm -rf init.o uart.o uart uart.bin uart.dis 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值