Uart串口实验--TQ2440

代码

@led start @2010-01-25 @jay .text .globl _start _start: b reset @预留着以后扩展中断向量表 reset: @disable watchdog ldr r0,=0x53000000 mov r1,#0 str r1,[r0] ldr sp,=4096 bl clock_init bl uart0_init bl uart b .

 

#include"tq2440.h" //各种初始化 #define PCLK 50000000 // 设置PCLK为50MHz #define UART_CLK PCLK // UART0的时钟源设为PCLK #define UART_BAUD_RATE 115200 // 波特率 #define UART_BRD ((UART_CLK / (UART_BAUD_RATE * 16)) - 1) #define MPLL_200HZ (0x5c<<12)|(1<<4)|(0x02) /* *MPLL就是main pll,它会产生3中不同的时钟 * 对于MPLLCON寄存器,[19:12]为MDIV,[9:4]为PDIV,[1:0]为SDIV * 有如下计算公式: * S3C2440: MPLL(FCLK) = (2 * m * Fin)/(p * 2^s) * 其中: m = MDIV + 8, p = PDIV + 2, s = SDIV * 对于本开发板,Fin = 12MHz * 设置CLKDIVN,令分频比为:FCLK:HCLK:PCLK=1:2:4, * FCLK=200MHz,HCLK=100MHz,PCLK=50MHz */ void clock_init() { CLKDIVN=0x3; //时钟分频器1 2 4 /* 如果HDIVN非0,CPU的总线模式应该从“fast bus mode”变为“asynchronous bus mode” */ __asm__( "mrc p15, 0, r1, c1, c0, 0/n" /* 读出控制寄存器 */ "orr r1, r1, #0xc0000000/n" /* 设置为“asynchronous bus mode” */ "mcr p15, 0, r1, c1, c0, 0/n" /* 写入控制寄存器 */ ); MPLLCON=MPLL_200HZ; //设置MPLL /* 现在,FCLK=200MHz,HCLK=100MHz,PCLK=50MHz */ } /* 初始化串口0,波特率=115200,8N1(8个数据位,无校验 1个停止位) GPH2 GPH3用于UART0 */ void uart0_init() { GPHCON=(0x2<<4)|(0x2<<6); //GPH23功能分别设置为UART0的发送和接收口 GPHUP &=~((1<<2)|(1<<3)); //上拉电阻,增加抗干扰能力 ULCON0=0x3; //8N1 (8个数据位,无较验,每帧1个停止位) UCON0=(1<<2)|(1); //波特率是PCLK,查询模式 UFCON0=0x0; //不使用fifo UMCON0=0x0; //不使用流控 UBRDIV0=UART_BRD; //设置波特率 波特率为115200 } /* 发送1个字符 */ void putc(unsigned char c) { //等待发送缓冲器为空,读取状态寄存器 while((UTRSTAT0&(1<<2))==0); /* 向UTXH0寄存器中写入数据,UART即自动将它发送出去 */ UTXH0=c; } //接收1个字符 unsigned char get_char() { //等待接收缓冲器收到数据,读取状态寄存器 while((UTRSTAT0&0x1)==0); /* 有数据来的时候会自动向URXH0寄存器中写入数据 只读的属性 */ return URXH0; } /*从串口接收数据,如果是数字或者字母,*/ void uart() { unsigned char c='J'; uart0_init(); while(1) { // c=get_char(); if(c) putc(c); } } void led_on() { GPBCON =GPB05OUT | GPB06OUT | GPB07OUT | GPB08OUT ; //out GPBDAT=0x17f; }

 

CC=arm-linux-gcc LD=arm-linux-ld CP=arm-linux-objcopy DP=arm-linux-objdump objs:=start.o uart.o uart.bin:$(objs) $(LD) -Tboot.lds -g -o mmu_elf $^ $(CP) -O binary -S mmu_elf $@ $(DP) -D -m arm mmu_elf > mmu.asm #makefile 注意 .s不可以大写 -T不可以少- %.o:%.c $(CC) -g -Wall -c -o $@ $< %.o:%.s $(CC) -g -Wall -c -o $@ S< clean: rm -f *.asm *.bin *_elf *.o

 

ENTRY(_start) SECTIONS { . =0x30004000; . =ALIGN(4); .text : { start.o (.text) *(.text) } . = ALIGN(4); .data :{ *(.data) } . =ALIGN(4); .rodata : { *(.rodata) } . =ALIGN(4); __bss_start = .; .bss :{*(.bss)} _end = . ; }

 

#define RH(x) (*(volatile unsigned long *)x) #define GPBCON RH(0x56000010) //映射的是0x56000010 #define GPBDAT RH(0x56000014) #define GPBUP RH(0x56000018) #define LOCKTIME RH(0x4c000000) #define MPLLCON RH(0x4c000004) #define CLKDIVN RH(0x4c000014) #define CLKDIVN RH(0x4c000014) // 中断 #define SRCPND RH(0x4a000000) #define INTMSK RH(0x4a000008) #define PRIORITY RH(0x4a00000c) #define INTPND RH(0x4a000010) #define INTOFFSET RH(0x4a000014) //指出IRQ中断请求源 //UART #define ULCON0 RH(0x50000000) #define UCON0 RH(0x50000004) #define UFCON0 RH(0x50000008) #define UMCON0 RH(0x5000000c) #define UTRSTAT0 RH(0x50000010) #define UBRDIV0 RH(0x50000028) #define UTXH0 RH(0x50000023) #define URXH0 RH(0x50000027) //timer 定时器配置寄存器 预分频器0 #define TCFG0 RH(0x51000000) #define TCFG1 RH(0x51000004) #define TCON RH(0x51000008) //time控制寄存器 #define TCNTB0 RH(0x5100000c) //初始值寄存器 #define TCMPB0 RH(0x51000010) //time0比较寄存器 #define TCNTO0 RH(0x51000014) //time0观察寄存器 //watchdog #define WTCON RH(0x53000000) #define WTDAT RH(0x53000004) #define WTCNT RH(0x53000008) //gpio-h #define GPHCON RH(0x56000070) #define GPHDAT RH(0x56000074) #define GPHUP RH(0x56000078) #define GPB05OUT (1<<(5*2)) // datasheet info led1 out:01 #define GPB06OUT (1<<(6*2)) #define GPB07OUT (1<<(7*2)) #define GPB08OUT (1<<(8*2))

你可能感兴趣的:(c,扩展,asynchronous,2010)