本实例功能是在串口上输入一个字符,板子收到后让它的ASCII值加1后,从串口输出。
head.S
.extern main .text .global _start _start: ldr sp, =4096 bl disable_watch_dog bl clock_init bl memsetup bl copy_steppingstone_to_sdram ldr pc, =on_sdram on_sdram: ldr sp, =0x34000000 ldr lr, =halt_loop ldr pc, =main halt_loop: b halt_loopinit.c
#include "s3c24xx.h" void disable_watch_dog(void); void clock_init(void); void memsetup(void); void copy_steppingstone_to_sdram(void); void disable_watch_dog(void) { WTCON = 0; } #define S3C2410_MPLL_200MHZ ((0x5c<<12)|(0x04<<4)|(0x00)) #define S3C2440_MPLL_200MHZ ((0x5c<<12)|(0x01<<4)|(0x02)) void clock_init(void) { CLKDIVN = 0x03; // FCLK:HCLK:PCLK=1:2:4, HDIVN=1,PDIVN=1 __asm__( "mrc p15, 0, r1, c1, c0, 0\n" "orr r1, r1, #0xc0000000\n" "mcr p15, 0, r1, c1, c0, 0\n" ); if ((GSTATUS1 == 0x32410000) || (GSTATUS1 == 0x32410002)) { MPLLCON = S3C2410_MPLL_200MHZ; } else { MPLLCON = S3C2440_MPLL_200MHZ; } } void memsetup(void) { volatile unsigned long *p = (volatile unsigned long *)MEM_CTL_BASE; p[0] = 0x22011110; //BWSCON p[1] = 0x00000700; //BANKCON0 p[2] = 0x00000700; //BANKCON1 p[3] = 0x00000700; //BANKCON2 p[4] = 0x00000700; //BANKCON3 p[5] = 0x00000700; //BANKCON4 p[6] = 0x00000700; //BANKCON5 p[7] = 0x00018005; //BANKCON6 p[8] = 0x00018005; //BANKCON7 /* REFRESH, * HCLK=12MHz: 0x008C07A3, * HCLK=100MHz: 0x008C04F4 */ p[9] = 0x008C04F4; p[10] = 0x000000B1; //BANKSIZE p[11] = 0x00000030; //MRSRB6 p[12] = 0x00000030; //MRSRB7 } void copy_steppingstone_to_sdram(void) { unsigned int *pdwSrc = (unsigned int *)0; unsigned int *pdwDest = (unsigned int *)0x30000000; while (pdwSrc < (unsigned int *)4096) { *pdwDest = *pdwSrc; pdwDest++; pdwSrc++; } }serial.c
#include "s3c24xx.h" #include "serial.h" #define TXD0READY (1<<2) #define RXD0READY (1) #define PCLK 50000000 // init.c中的clock_init函数设置PCLK为50MHz #define UART_CLK PCLK // UART0的时钟源设为PCLK #define UART_BAUD_RATE 115200 // 波特率 #define UART_BRD ((UART_CLK / (UART_BAUD_RATE * 16)) - 1) void uart0_init(void) { GPHCON |= 0xa0; GPHUP = 0x0c; ULCON0 = 0x03; UCON0 = 0x05; UFCON0 = 0x00; UMCON0 = 0x00; UBRDIV0 = UART_BRD; } void putc(unsigned char c) { while(!(UTRSTAT0 & TXD0READY)) ; UTXH0 = c; } unsigned char getc(void) { while(!(UTRSTAT0 & RXD0READY)) ; return URXH0; } int isDigit(unsigned char c) { if(c >= '0' && c <= '9') return 1; else return 0; } int isLetter(unsigned char c) { if(c >= 'a' && c <= 'z') return 1; else if(c >= 'A' && c <= 'Z') return 1; else return 0; }serial.h
void uart0_init(void); void putc(unsigned char c); unsigned char getc(void); int isDigit(unsigned char c); int isLetter(unsigned char c);main.c
#include "serial.h" int main(void) { unsigned char c; uart0_init(); while(1) { c = getc(); if(isDigit(c) || isLetter(c)) putc(c+1); } return 0; }s3c24xx.h
/* WOTCH DOG register */ #define WTCON (*(volatile unsigned long *)0x53000000) /* SDRAM regisers */ #define MEM_CTL_BASE 0x48000000 #define SDRAM_BASE 0x30000000 /* NAND Flash registers */ #define NFCONF (*(volatile unsigned int *)0x4e000000) #define NFCMD (*(volatile unsigned char *)0x4e000004) #define NFADDR (*(volatile unsigned char *)0x4e000008) #define NFDATA (*(volatile unsigned char *)0x4e00000c) #define NFSTAT (*(volatile unsigned char *)0x4e000010) /*GPIO registers*/ #define GPBCON (*(volatile unsigned long *)0x56000010) #define GPBDAT (*(volatile unsigned long *)0x56000014) #define GPFCON (*(volatile unsigned long *)0x56000050) #define GPFDAT (*(volatile unsigned long *)0x56000054) #define GPFUP (*(volatile unsigned long *)0x56000058) #define GPGCON (*(volatile unsigned long *)0x56000060) #define GPGDAT (*(volatile unsigned long *)0x56000064) #define GPGUP (*(volatile unsigned long *)0x56000068) #define GPHCON (*(volatile unsigned long *)0x56000070) #define GPHDAT (*(volatile unsigned long *)0x56000074) #define GPHUP (*(volatile unsigned long *)0x56000078) /*UART registers*/ #define ULCON0 (*(volatile unsigned long *)0x50000000) #define UCON0 (*(volatile unsigned long *)0x50000004) #define UFCON0 (*(volatile unsigned long *)0x50000008) #define UMCON0 (*(volatile unsigned long *)0x5000000c) #define UTRSTAT0 (*(volatile unsigned long *)0x50000010) #define UTXH0 (*(volatile unsigned char *)0x50000020) #define URXH0 (*(volatile unsigned char *)0x50000024) #define UBRDIV0 (*(volatile unsigned long *)0x50000028) /*interrupt registes*/ #define SRCPND (*(volatile unsigned long *)0x4A000000) #define INTMOD (*(volatile unsigned long *)0x4A000004) #define INTMSK (*(volatile unsigned long *)0x4A000008) #define PRIORITY (*(volatile unsigned long *)0x4A00000c) #define INTPND (*(volatile unsigned long *)0x4A000010) #define INTOFFSET (*(volatile unsigned long *)0x4A000014) #define SUBSRCPND (*(volatile unsigned long *)0x4A000018) #define INTSUBMSK (*(volatile unsigned long *)0x4A00001c) /*external interrupt registers*/ #define EINTMASK (*(volatile unsigned long *)0x560000a4) #define EINTPEND (*(volatile unsigned long *)0x560000a8) /*clock registers*/ #define LOCKTIME (*(volatile unsigned long *)0x4c000000) #define MPLLCON (*(volatile unsigned long *)0x4c000004) #define UPLLCON (*(volatile unsigned long *)0x4c000008) #define CLKCON (*(volatile unsigned long *)0x4c00000c) #define CLKSLOW (*(volatile unsigned long *)0x4c000010) #define CLKDIVN (*(volatile unsigned long *)0x4c000014) /*PWM & Timer registers*/ #define TCFG0 (*(volatile unsigned long *)0x51000000) #define TCFG1 (*(volatile unsigned long *)0x51000004) #define TCON (*(volatile unsigned long *)0x51000008) #define TCNTB0 (*(volatile unsigned long *)0x5100000c) #define TCMPB0 (*(volatile unsigned long *)0x51000010) #define TCNTO0 (*(volatile unsigned long *)0x51000014) #define GSTATUS1 (*(volatile unsigned long *)0x560000B0)uart.lds
SECTIONS { . = 0x30000000; .text : { *(.text) } .rodata ALIGN(4) : {*(.rodata)} .data ALIGN(4) : { *(.data) } .bss ALIGN(4) : { *(.bss) *(COMMON) } }Makefile
objs := head.o init.o serial.o main.o uart.bin: $(objs) arm-linux-ld -Tuart.lds -o uart_elf $^ arm-linux-objcopy -O binary -S uart_elf $@ arm-linux-objdump -D -m arm uart_elf > uart.dis %.o:%.c arm-linux-gcc -Wall -O2 -c -o $@ $< %.o:%.S arm-linux-gcc -Wall -O2 -c -o $@ $< clean: rm -f uart.bin uart_elf uart.dis *.o