ARM Notes: UART Program

Publisher:幸福的时光Latest update time:2017-11-17 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

UART program:
1. The writing function of head.S file is to start the code

@******************************************************************************

@ File:head.S

@ Function: Initialization, system mode stack

@******************************************************************************       

   

.extern     main

.text 

.global _start 

_start:

     

    b   Reset

 

Reset:                  

    ldr sp, =4096 @ Set the stack pointer. The following are all C functions. You need to set the stack before calling them.

    bl disable_watch_dog @ Disable WATCHDOG, otherwise the CPU will restart continuously

    bl clock_init @ Set up MPLL, change FCLK, HCLK, PCLK

    bl memsetup @ Set up the memory controller to use SDRAM

   bl inituart@ written inituart() serial port initialization function in init.c

    bl copy_steppingstone_to_sdram @ Copy code to SDRAM

    ldr pc, =on_sdram @ Jump to SDRAM and continue execution

on_sdram:

 

    msr cpsr_c, #0xdf @ Enter system mode

    ldr sp, =0x34000000 @ Set the system mode stack pointer,

     

    

    

    ldr lr, =halt_loop @ Set the return address

    ldr pc, =Main @ Call the main function

halt_loop:

    b   halt_loop

2. init.c file, which declares some functions and implementations that need to be used 

 

#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; // Turning off WATCHDOG is very simple, just write 0 to this register

}

#define S3C2410_MPLL_200MHZ     ((0x5c<<12)|(0x04<<4)|(0x00))

#define S3C2440_MPLL_200MHZ     ((0x5c<<12)|(0x01<<4)|(0x02))

void clock_init(void)

{

    // LOCKTIME = 0x00ffffff; // Use the default value

    rCLKDIVN  = 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 ((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; //Use UART0 function

rGPHUP  = 0x7ff;

 

rULCON0 = 0x3; //Set UART0 to no parity, one stop bit, 8 data bits

rUCON0 = 0x245; //PCLK is the clock source, receiving and sending data is in query or interrupt mode

rUFCON0 = 0;                     //

rUMCON0 = 0; //

rUBRDIV0 = 26;  

}

3. uart.c This program uses the query method to implement serial communication

#include"2440addr.h"

void Main(void)

{

       char ch;

               //Set the baud rate, PCLK is 50MHz, and the baud rate is 115.2kHz

   

while(!(rUTRSTAT0 & 0x2)); //Wait and determine whether the send buffer is empty

       rUTXH0 = 0xaa; //If it is empty, send 0xAA byte

 

while(1)

{

              while(!(rUTRSTAT0 & 0x1)); //Wait and determine whether the receive buffer is ready

 

 ch = rURXH0; //Receive one byte of data

              

              while(!(rUTRSTAT0 & 0x2)); //Wait and determine whether the send buffer is empty

              

              rUTXH0 = ch; //Send one byte of data

 

switch(ch) //Execute different programs according to the received data

              {

       case 0x11: // Turn off LED

                           rGPBDAT |= 0x1e0;

       break;

                                 case 0x22: //light up the LED

                             rGPBDAT &= 0x1f;

                     break;

       case 0x33: //Buzzer does not sound

                               rGPBDAT &= 0x1e0;

                           break;

                                case 0x44: //Buzzer sounds

                                     rGPBDAT |= 0x1;

                                     break;

                                default: //LED off, buzzer off

                                     rGPBDAT = 0x1e0;

                                     break;

}

}

}

4. The specific function of the uart.lds file is not very clear. I personally feel that it should be used for segment declarations and entry addresses.

SECTIONS {

    . = 0x30000000;

    .text          :   { *(.text) }

    .rodata ALIGN(4) : {*(.rodata)} 

    .data ALIGN(4) : { *(.data) }

    .bss ALIGN(4)  : { *(.bss)  *(COMMON) }

}

5. Makefile 

objs := head.o uart.o init.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

With the above five files, you can directly use the command to generate the uart.bin file under Linux, burn it into NAND FLASH, and then start it from NAND FLASH to run. Use the serial port debugging tool to perform data communication.


Reference address:ARM Notes: UART Program

Previous article:ARM Notes: Application of timer interrupts
Next article:ARM Notes: UART Learning Notes

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号