UART程序:
一、head.S文件的編寫 作用是啟動代碼
@******************************************************************************
@ File:head.S
@ 功能:初始化,系統(tǒng)模式的棧
@******************************************************************************
.extern main
.text
.global _start
_start:
b Reset
Reset:
ldr sp, =4096 @ 設(shè)置棧指針,以下都是C函數(shù),調(diào)用前需要設(shè)好棧
bl disable_watch_dog @ 關(guān)閉WATCHDOG,否則CPU會不斷重啟
bl clock_init @ 設(shè)置MPLL,改變FCLK、HCLK、PCLK
bl memsetup @ 設(shè)置存儲控制器以使用SDRAM
bl inituart @編寫的inituart()串口初始化函數(shù) 在init.c中
bl copy_steppingstone_to_sdram @ 復(fù)制代碼到SDRAM中
ldr pc, =on_sdram @ 跳到SDRAM中繼續(xù)執(zhí)行
on_sdram:
msr cpsr_c, #0xdf @ 進(jìn)入系統(tǒng)模式
ldr sp, =0x34000000 @ 設(shè)置系統(tǒng)模式棧指針,
ldr lr, =halt_loop @ 設(shè)置返回地址
ldr pc, =Main @ 調(diào)用main函數(shù)
halt_loop:
b halt_loop
二、init.c 文件 其中聲明了一些需要使用的函數(shù)及實(shí)現(xiàn)
#include "2440addr.h"
void disable_watch_dog(void);
void clock_init(void);
void memsetup(void);
void copy_steppingstone_to_sdram(void);
void inituart(void);
void disable_watch_dog(void)
{
rWTCON = 0; // 關(guān)閉WATCHDOG很簡單,往這個寄存器寫0即可
}
#define S3C2410_MPLL_200MHZ ((0x5c<<12)|(0x04<<4)|(0x00))
#define S3C2440_MPLL_200MHZ ((0x5c<<12)|(0x01<<4)|(0x02))
void clock_init(void)
{
// LOCKTIME = 0x00ffffff; // 使用默認(rèn)值即可
rCLKDIVN = 0x03; // FCLK:HCLK:PCLK=1:2:4, HDIVN=1,PDIVN=1
__asm__(
"mrc p15, 0, r1, c1, c0, 0n"
"orr r1, r1, #0xc0000000n"
"mcr p15, 0, r1, c1, c0, 0n"
);
if ((rGSTATUS1 == 0x32410000) || (rGSTATUS1 == 0x32410002))
{
rMPLLCON = S3C2410_MPLL_200MHZ;
}
else
{
rMPLLCON = 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
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++;
}
}
void inituart(void)
{
rGPBCON = 0x015551;
rGPBUP = 0x7ff;
rGPBDAT = 0x1e0;
rGPHCON = 0x00faaa; //使用UART0功能
rGPHUP = 0x7ff;
rULCON0 = 0x3; //設(shè)置UART0無奇偶校驗(yàn),一位停止位,8位數(shù)據(jù)
rUCON0 = 0x245; //PCLK為時鐘源,接收和發(fā)送數(shù)據(jù)為查詢或中斷方式
rUFCON0 = 0; //
rUMCON0 = 0; //
rUBRDIV0 = 26;
}
三、uart.c 這個程序是使用查詢方式實(shí)現(xiàn)的串口通信
#include"2440addr.h"
void Main(void)
{
char ch;
//設(shè)置波特率,PCLK為50MHz,波特率為115.2kHz
while(!(rUTRSTAT0 & 0x2)); //等待并判斷發(fā)送緩存是否為空
rUTXH0 = 0xaa; //是空,則發(fā)送0xAA字節(jié)
while(1)
{
while(!(rUTRSTAT0 & 0x1)); //等待并判斷接收緩存是否準(zhǔn)備好
ch = rURXH0; //接收一個字節(jié)數(shù)據(jù)
while(!(rUTRSTAT0 & 0x2)); //等待并判斷發(fā)送緩存是否為空
rUTXH0 = ch; //發(fā)送一個字節(jié)數(shù)據(jù)
switch(ch) //根據(jù)所接收數(shù)據(jù)的不同,執(zhí)行不同的程序
{
case 0x11: //滅LED
rGPBDAT |= 0x1e0;
break;
case 0x22: //亮LED
rGPBDAT &= 0x1f;
break;
case 0x33: //蜂鳴器不響
rGPBDAT &= 0x1e0;
break;
case 0x44: //蜂鳴器響