S3c2451_uart_arm Serial port operation

Source: Internet
Author: User
Tags min volatile

Here's a look at my s3c2451 UART Multi-Interface Initialization program

Automatically configure the serial port baud rate as long as you enter the string number you want to configure, the baud rate, the bit width, and the stop bit
and send accept characters, send string, serial interrupt and other functions
If there is anything wrong with the place want to enlighten.

/*uart*/#define ULCON0 (* (volatile unsigned long *) 0x50000000) #define ULCON1 (* (volatile unsigned long *) 0x50004000) #define ULCON2 (* (volatile unsigned long *) 0x50008000) #define ULCON3 (* (Volatile Unsigne  D long *) 0x5000c000) #define UCON0 (* (volatile unsigned long *) 0x50000004) #define UCON1 (* (volatile unsigned long *) 0x50004004) #define UCON2 (* (volatile unsigned long) 0x50008004) #define UCON3 (* (VO      Latile unsigned long *) 0x5000c004) #define UFCON0 (* (volatile unsigned long *) 0x50000008) #define UFCON1 (* (volatile unsigned long *) 0x50004008) #define UFCON2 (* (volatile unsigned long) 0x50008008) #define Ufco     N3 (* (volatile unsigned long *) 0x5000c008) #define UMCON0 (* (volatile unsigned long *) 0x5000000c) #define UMCON1 (* (volatile unsigned long *) 0x5000400c) #define UMCON2 (* (volatile unsigned long *) 0x5000800c) #d Efine UTRSTAT0 (*(Volatile unsigned long *) 0x50000010) #define UTRSTAT1 (* (volatile unsigned long) 0x50004010) #define UTRSTAT2 (* (volatile unsigned long *) 0x50008010) #define UTRSTAT3 (* (volatile unsigned long *) 0x5000c010) #define U      ERSTAT0 (* (volatile unsigned long *) 0x50000014) #define UERSTAT1 (* (volatile unsigned long *) 0x50004014) #define

UERSTAT2 (* (volatile unsigned long *) 0x50008014) #define UERSTAT3 (* (volatile unsigned long *) 0x5000c014) #define UFSTAT0 (* (volatile unsigned long *) 0x50000018) #define UFSTAT1 (* (volatile unsigned long *) 0x5000 4018) #define UFSTAT2 (* (volatile unsigned long *) 0x50008018) #define UFSTAT3 (* (volatile unsigned long *) 0x5000c018) #define UMSTAT0 (* (volatile unsigned long *) 0x5000001c) #define UMSTAT1 (* (Volatile unsigned long *) 0x5000401c) #define UMSTAT2 (* (volatile unsigned long *) 0x5000801c) #define UTXH0 (* (Volatile UN Signed long *) 0x50000020) #define UTXH1 (* (volatile unsigned long *) 0x50004020) #define UTXH2 (* (volatile unsigned long *) 0x50  008020) #define UTXH3 (* (volatile unsigned long *) 0x5000c020) #define URXH0 (* (volatile unsigned long *) 0x50000024) #define URXH1 (* (volatile unsigned long *) 0x50004024) #define URXH2 (* (Volatile Unsigne D long *) 0x50008024) #define URXH3 (* (volatile unsigned long *) 0x5000c024) #define UBRDIV0 (* (volatile unsigned long *) 0x50000028) #define UBRDIV1 (* (volatile unsigned long *) 0x50004028) #define UBRDIV2 (* (vol   Atile unsigned long *) 0x50008028) #define UBRDIV3 (* (volatile unsigned long *) 0x5000c028) #define UDIVSLOT0 (* (volatile unsigned long *) 0x5000002c) #define UDIVSLOT1 (* (volatile unsigned long) 0x5000402c) #define Udivs LOT2 (* (volatile unsigned long *) 0x5000802c) #define UDIVSLOT3 (* (volatile unsigned long *) 0x5000c02c)
/* Enumerate string numbers */
typedef enum
{
    UART0 = 0,
    UART1,
    UART2,
    UART3

}uart_typedef;

/* Enumerate bit width */
typedef enum
{
    data_width_5bit = 0,
    data_width_6bit,
    data_width_7bit,
    DATA _width_8bit
}data_wtdth_typedef;

/* Enumeration Stop bit */
typedef enum
{
    stop_bit0 = 0,
    stop_bit1
}stop_bit_typedef;
#ifndef _typedef_h_
#define _TYPEDEF_H_

#define U32 unsigned int
#define U16 unsigned short
# Define S32 int
#define S16 short int
#define U8  unsigned char
#define S8  Char

#define TRUE   1
#define FALSE  0

#endif
/*======================================================== function: Serial port Initialization program Arguments:art_typedef UARTX string number Data_wtdth_typedef bit_w bit width stop_bit_typedef stop stop bit ======================================== ================*/void Uart_init (Uart_typedef uartx, U32 bps, data_wtdth_typedef bit_w, Stop_bit_typedef STOP) {Floa T femp;  Floating-point value switch (UARTX) {case UART0: {ULCON0 &= ~0x3, used to connect div_val; Clear the minimum two bits (data width) switch (bit_w) {//Set data width by parameter 5, 6, 7, 8bit case Data_w
                Idth_5bit:ulcon0 |= 0x00;break;
                Case Data_width_6bit:ulcon0 |= 0x01;break;
                Case Data_width_7bit:ulcon0 |= 0x02;break;
            Case Data_width_8bit:ulcon0 |= 0x03;break;  } ULCON0 &= ~0x4;
            Clear Stop bit if (stop = = stop_bit1) {ULCON0 |= 0x4; }//Configure baud rate//HCLK gives all devices mounted on the AHB bus using//PCLK to all devices hung on the APB bus using//UART0123 on APB//Div_val = (PCLK/(b PS x))-1 = (66000000/(115200x16))-1 = 34.807//div_val = 34.807 UBRDIV0 = (int) ((66000000/ (bps * 16))
            -1);
            Femp = (float) (66000000/(float) (bps *))-1-ubrdiv0;
            Voidset (UART0, femp);
        Break  } case UART1: {ULCON1 &= ~0x3;
                Çå¿õxîµíá½î»£¨êý¾ý¿í¶è£©switch (Bit_w) {//Set data width 5, 6, 7, 8bit according to parameters
                Case Data_width_5bit:ulcon1 |= 0x00;break;
                Case Data_width_6bit:ulcon1 |= 0x01;break;
                Case Data_width_7bit:ulcon1 |= 0x02;break;
            Case Data_width_8bit:ulcon1 |= 0x03;break;  } ULCON1 &= ~0x4;
            çå³ýí£ö¹î»£¬ê¹æäîª0 if (stop = = stop_bit1) {ULCON1 |= 0x4; } UBRDIV1 = (int) (66000000/(bps * 16))-1);
            Femp = (float) (66000000/(float) (bps *))-1-ubrdiv1;
            Voidset (UART1, femp);
        Break  
            } case UART2: {ULCON2 &= ~0x3; Switch (BIT_W) {//Set data width 5, 6, 7, 8bit according to the parameter Data_width_5bit:ulcon2 |= 0x00;b
                Reak;
                Case Data_width_6bit:ulcon2 |= 0x01;break;
                Case Data_width_7bit:ulcon2 |= 0x02;break;
            Case Data_width_8bit:ulcon2 |= 0x03;break;  } ULCON2 &= ~0x4;
            Clear the stop bit so that it is 0 if (stop = = stop_bit1) {ULCON2 |= 0x4;
            } UBRDIV2 = (int) ((66000000/(bps * 16))-1);
            Femp = (float) (66000000/(float) (bps *))-1-ubrdiv2;
            Voidset (UART2, femp);
        Break  } case UART3: {ULCON3 &= ~0x3; Çå¿õxîµíá½î»£¨êý¾ý¿í¶è£©switch (Bit_w)
            {//Set data width according to parameters 5, 6, 7, 8bit case Data_width_5bit:ulcon3 |= 0x00;break;
                Case Data_width_6bit:ulcon3 |= 0x01;break;
                Case Data_width_7bit:ulcon3 |= 0x02;break;
            Case Data_width_8bit:ulcon3 |= 0x03;break;
            } ULCON3 &= ~0x4;
            if (stop = = stop_bit1) {ULCON3 |= 0x4;
            } UBRDIV3 = (int) ((66000000/(bps * 16))-1);
            Femp = (float) (66000000/(float) (bps *))-1-ubrdiv3;
            Voidset (UART3, femp);
        Break }}}/*==================================================================== function: Configure the corresponding Udivslot AR based on the Div_val floating point number Guments:art_typedef uartx string number femp: Float floating point part ====================================================
    ================*/void Voidset (Uart_typedef uartx, float femp) {S32 i;
    float absnum = 0.0;
    float min = femp; float SRC[16] = {0,0.0625,0.125,0.18175,0.25,0.3125,0.375,0.4375,0.5,0.5625,0.625,0.6875,0.75,0.8125,0.875,0.9375};
        Find data with femp absolute minimum for (i = 0; I <=; i++) {absnum = src[i]-femp;
        if (Absnum < 0) {absnum =-absnum;
        } if (Absnum < min) {min = Absnum;
        }}//The subscript of the element is found based on the smallest absolute value for (i = 0; I <=; i++) {absnum = src[i]-femp;
        if (Absnum < 0) {absnum =-absnum;
        } if (absnum = = min) {break; 
                }}//Set Udivslot switch (UARTX) {case UART0 according to subscript: {switch (i) {
                Case 0:udivslot0 = 0x0000;break;
                Case 1:udivslot0 = 0x0080;break;
                Case 2:udivslot0 = 0x0808;break;
                Case 3:udivslot0 = 0x0888;break;
                Case 4:udivslot0 = 0x2222;break; Case 5:udivslot0 = 0x4924;break;
                Case 6:udivslot0 = 0x4a52;break;
                Case 7:udivslot0 = 0x54aa;break;
                Case 8:udivslot0 = 0x5555;break;
                Case 9:udivslot0 = 0xd555;break;
                Case 10:udivslot0 = 0xd5d5;break;
                Case 11:udivslot0 = 0xddd5;break;
                Case 12:udivslot0 = 0xdddd;break;
                Case 13:udivslot0 = 0xdfdd;break;
                Case 14:udivslot0 = 0xdfdf;break;               
                Case 15:udivslot0 = 0xffdf;break;       
            Default:break;
        } break;
                } case UART1: {switch (i) {case 0:udivslot1 = 0x0000;break;
                Case 1:udivslot1 = 0x0080;break;
                Case 2:udivslot1 = 0x0808;break;
                Case 3:udivslot1 = 0x0888;break;
                Case 4:udivslot1 = 0x2222;break;
                Case 5:udivslot1 = 0x4924;break; Case 6:UDIVSLOT1 =0x4a52;break;
                Case 7:udivslot1 = 0x54aa;break;
                Case 8:udivslot1 = 0x5555;break;
                Case 9:udivslot1 = 0xd555;break;
                Case 10:udivslot1 = 0xd5d5;break;
                Case 11:udivslot1 = 0xddd5;break;
                Case 12:udivslot1 = 0xdddd;break;
                Case 13:udivslot1 = 0xdfdd;break;
                Case 14:udivslot1 = 0xdfdf;break;               
                Case 15:udivslot1 = 0xffdf;break;      
            Default:break;
        } break;
                } case UART2: {switch (i) {case 0:udivslot2 = 0x0000;break;
                Case 1:udivslot2 = 0x0080;break;
                Case 2:udivslot2 = 0x0808;break;
                Case 3:udivslot2 = 0x0888;break;
                Case 4:udivslot2 = 0x2222;break;
                Case 5:udivslot2 = 0x4924;break;
                Case 6:udivslot2 = 0x4a52;break; Case 7:udivslot2 = 0x54aa;break;
                Case 8:udivslot2 = 0x5555;break;
                Case 9:udivslot2 = 0xd555;break;
                Case 10:udivslot2 = 0xd5d5;break;
                Case 11:udivslot2 = 0xddd5;break;
                Case 12:udivslot2 = 0xdddd;break;
                Case 13:udivslot2 = 0xdfdd;break;
                Case 14:udivslot2 = 0xdfdf;break;
                Case 15:udivslot2 = 0xffdf;break;                      
            Default:break;
        } break;
                } case UART3: {switch (i) {case 0:udivslot3 = 0x0000;break;
                Case 1:udivslot3 = 0x0080;break;
                Case 2:udivslot3 = 0x0808;break;
                Case 3:udivslot3 = 0x0888;break;
                Case 4:udivslot3 = 0x2222;break;
                Case 5:udivslot3 = 0x4924;break;
                Case 6:udivslot3 = 0x4a52;break;
                Case 7:udivslot3 = 0x54aa;break; Case 8:UDIVSLOT3 = 0x5555;break;
                Case 9:udivslot3 = 0xd555;break;
                Case 10:udivslot3 = 0xd5d5;break;
                Case 11:udivslot3 = 0xddd5;break;
                Case 12:udivslot3 = 0xdddd;break;
                Case 13:udivslot3 = 0xdfdd;break;
                Case 14:udivslot3 = 0xdfdf;break;
                Case 15:udivslot3 = 0xffdf;break;      
            Default:break;
        } break;
        } default: {break;
    }}} void Uart_senddata (U8 data) {UTXH0 = data; 

while ((utrstat0& (1<<2)) = = 0); } void Uart_senddatabuf (unsigned char *dat,unsigned int len)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.