1. 程式人生 > >S3C2440裸機程式【2】串列埠uart程式

S3C2440裸機程式【2】串列埠uart程式

         學習ARM7晶片stm32時,裸機程式開發可以很方便的根據庫函式在工程模板上進行開發,而ARM9主要是移植Linux開發,很少有裸機程式開發,因此在玩S3C2440希望最終形成一個keil環境下的模板。用的淘寶上的JZ2440v2板子,因為是裸機程式開發其他板子都一樣。


         S3C2440.s是keil自動生成的啟動檔案,其他都是按stm32庫函式模板寫的,技術很差,自學娛樂,若有需求可自行改動。

main.c檔案:

#include "S3C2440Reg.h"   
#include "S3C2440Init.h"  
#include "led.h"
#include "uart.h"


void delay(int t)
{
	int a,b;
	for(a=0;a<t;a++)
		for(b=0;b<100;b++);
}

int main(void)
{
	unsigned char ch;
	Led_Port_Init();
	uart0_init();
	
	while(1)
	{
		GPFDAT &= Led2_On;
		delay(1000);
		GPFDAT |= Led2_Off;
		delay(1000);
		
		uart0SendString(" \r\nInput a char: ");
		ch = uart0ReceiveData();
		uart0SendData(ch);
	}
	return 0;
}

暫存器對映表S3C2440Reg.h檔案

#ifndef __S3C2440REG_H
#define __S3C2440REG_H

typedef unsigned char  uint8;                   
typedef signed   char  int8;                    
typedef unsigned short uint16;                  
typedef signed   short int16;                   
typedef unsigned int   uint32;                  
typedef signed   int   int32;                   
typedef float          fp32;                    
typedef double         fp64;                    

/* 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)


#endif

初始化檔案S3C2440Init.h

#ifndef __S3C2440Init_H
#define __S3C2440Init_H

/* Main clock */
#define FIN 	(12000000)
#define FCLK 400000000
#define HCLK (FCLK/4)
#define PCLK (HCLK/2)

/* USB clock */
#define UCLK 48000000

/* BUSWIDTH : 16,32	*/
#define BUSWIDTH    (32)

/*64MB
* 0x30000000 ~ 0x30ffffff : Download Area (16MB) Cacheable
* 0x31000000 ~ 0x33feffff : Non-Cacheable Area
* 0x33ff0000 ~ 0x33ff47ff : Heap & RW Area
* 0x33ff4800 ~ 0x33ff7fff : FIQ ~ User Stack Area
* 0x33ff8000 ~ 0x33fffeff : Not Useed Area
* 0x33ffff00 ~ 0x33ffffff : Exception & ISR Vector Table
*/
#define	_RAM_STARTADDRESS 		0x30000000
#define	_ISR_STARTADDRESS 		0x33ffff00     
#define	_MMUTT_STARTADDRESS		0x33ff8000
#define	_STACK_BASEADDRESS		0x33ff8000
#define	_NONCACHE_STARTADDRESS	0x31000000

/*USB Device Options */
#define USBDMA		1
#define USBDMA_DEMAND 	0	/* the downloadFileSize should be (64*n) */
#define BULK_PKT_SIZE	64

#define	UPDATE_REFRESH(IN_HCLK)	( rREFRESH = (rREFRESH & ~(0x7ff)) | 2049-(U32)((float)IN_HCLK*7.8/1000000) )

/* CLKSEL0/1 Clock selection value define */
#define	CLK_HCLK	(3)
#define	CLK_PCLK	(4)
#define	CLK_DCLK	(5)

#define MEGA	(1000000)

#include "S3C2440Reg.h"
void disable_watch_dog(void);

#endif

串列埠設定檔案uart.c

#include "uart.h"

#define UART_CLK        PCLK        //  UART0ʱÖÓÉèÖÃ
#define UART_BAUD_RATE  115200      //  ²¨ÌØÂÊÉèÖÃ
#define UART_BRD        ((UART_CLK  / (UART_BAUD_RATE * 16)) - 1)

//#define TXD0READY   (1<<2)
//#define RXD0READY   (1)

/*
 * ³õʼ»¯UART0
 * 115200,8N1,ÎÞÁ÷¿Ø
 */
void uart0_init(void)
{
    GPHCON  |= 0xa0;    // GPH2,GPH3ÓÃ×÷TXD0,RXD0
    GPHUP   = 0x0c;     // GPH2,GPH3ÄÚ²¿ÉÏÀ­

    ULCON0  = 0x03;     // 8N1
    UCON0   = 0x05;     // ²éѯ·½Ê½,UARTʱÖÓΪPCLK
    UFCON0  = 0x00;     // ²»Ê¹ÓÃFIFO
    UMCON0  = 0x00;     // ²»Ê¹ÓÃÁ÷¿Ø
    UBRDIV0 = UART_BRD; // ²¨ÌØÂÊ115200
}

/*
 * ·¢ËÍÒ»¸ö×Ö·û
 */
void uart0SendData(unsigned char ch)
{
		//Ö±µ½·¢ËÍ»º³åÇøÊý¾ÝÒÑÈ«²¿·¢³ö
    while (!(UTRSTAT0 & 0x4));
    UTXH0 = ch;
}

/*
 * ·¢ËÍÒ»¸ö×Ö·û´®
 */
void uart0SendString(unsigned char *str)
{
		while(0!=*str)
		{
			uart0SendData(*str);
			str++;
		}
}

/*
 * ½ÓÊÕÒ»¸ö×Ö·û
 */
unsigned char uart0ReceiveData(void)
{
    //Ö±µ½½ÓÊÕ»º³åÇøÓÐÊý¾Ý
    while (!(UTRSTAT0 & 0x1));
    return URXH0;
}

註釋為亂碼,應該是編譯器設定問題。

硬體上:nCTS0--->GPH0,nRTS0--->GPH1,TXD0--->GPH2,RXD0--->GPH3

查詢S3C2440使用者手冊,相關串列埠控制暫存器設定如下:


因此,ULCON0  = 0x03; //8位傳輸,無校驗,一位停止位




因此,UCON0   = 0x05; //選擇PCLK作為串列埠時鐘,傳送和接收都是中斷或者查詢模式(polling mode)


因此,UFCON0  = 0x00;//不使用FIFO,每個UART通道包含了兩個64-byte的FIFO佇列用於接收器和傳送器,可以儲存更多數。


因此,UMCON0  = 0x00;//不使用流控,只需要接Rxd0和Txd0,不用接nCTS0和nRTS0

狀態暫存器


         

最終程式碼連線:http://download.csdn.net/detail/tiandawangliang/9549257