C 언어 제어 구성 직렬 포트(S3C2440 개발 보드 기반)

6172 단어 C 언어 구동
소스 주소:https://github.com/774639008/MyProject/commit/2129384d2e0004dc859caa4d8b81d8669ecf5850?diff=unified
uart입니다.c 파일
#include 
#include 
#include "lcd.h"
#include "irq.h"
#include "uart_cmd.h"
#include 
#include 
#include "heap.h"

/*------------------------ Type Declarations ---------------------------------*/
//UART   UART   IO    ,UART     。UART         
#define FCLK    (405000000) //405M
#define HCLK    (101000000) //101M
#define    PCLK     (50000000)  //50.5M


int UART0_init(int baudrate)
{
    
    //     
    int nUBRDIV = PCLK/16/baudrate - 1;
    
    //     ,      
    GPHCON = 0xA0;
    GPHUP = 0x0C;
 
    //        :   UART      
    //              1      8     
    ULCON0 = (0x3);
//    ULCON1 = (0<<6)|(0<<3)|(0<<2)|(3<<0);
//    ULCON2 = (0<<6)|(0<<3)|(0<<2)|(3<<0);

//        :  UART     
//    [10]       [9]     [8]        [7]        [6]      [5]         [4]           [3:2]        [1:0]
//     ,  Tx   ,  Rx   , Rx     , Rx   ,     ,       ,          ,         
//     0          1       0    ,     0          1        0           0     ,       01          01
//   PCLK                                                                  
    
    UCON0 = (0x5); //     
//    UCON1 = (0<<12)|(2<<10)|(0<<9)|(0<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1<<0);
//    UCON2 = (0<<12)|(2<<10)|(0<<9)|(0<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1<<0);

//  FIFO   
//           [7:6]               [5:4]                 [3]           [2]        [1]            [0]
//    Tx FIFO       Rx FIFO                          Tx FIFO          Rx FIFO          FIFO  
//                0                    0                /        0                1               1
//                                                                                    
    UFCON0 = (0<<6)|(0<<4)|(0<<2)|(1<<1)|(1<<0);
    //UFCON1 = (0<<6)|(0<<4)|(0<<2)|(1<<1)|(1<<0);
    //UFCON2 = (0<<6)|(0<<4)|(0<<2)|(1<<1)|(1<<0);

    //    
    UMCON0 = 0;
    //UMCON1 = 0;
    //UMCON2 = 0;

    //     
    UBRDIV0 = nUBRDIV;
    //UBRDIV1 = nUBRDIV;
    //UBRDIV2 = nUBRDIV;

    return 0;
}


//    
int UART0_SendByte(unsigned char cSendChar)
{
    //   ,    ,                   
    while(!(UTRSTAT0 & 0x2));
    UTXH0 = (int)cSendChar;
    
    return 0;
}

//    
unsigned char UART0_RecvByte(void)
{
    //   ,    ,          
    while(!(UTRSTAT0 & 0x1));
    return (unsigned char)URXH0;
}

//        
int UART0_IsTransFinish(void)
{
    return (UTRSTAT0 & (1<<2));
}

//  FIFO    
void UART0_cleanRX(void)
{
    int num =     UFSTAT0 & 0x1f;   //  FIFO      
    while(num > 0)
    {
        char ch = URXH0;     //          FIFO       
        num--;
    }   
}
  
//------------     、       -------------
int UART0_RecvString(unsigned char * buf, int len)
{
    int i = 0;
    while(i < len)
    {
       buf[i++] = UART0_RecvByte();
    }

    return i;
}

int UART0_SendString(unsigned char * buf, int len)
{
    int i;
    
  for(i = 0; i < len; i++)
    {
       UART0_SendByte(buf[i]);
    }

    return i;
}


//----------FIFO    ----------------------
int ReceiveIndex = 0;
char ReceiveBuffer[200] = {0};
int flag_Receive_end = 0;
int count2 = 0;
unsigned char *p = (unsigned char*)0x31000000;    

 
static void UART0_IRQ_Receive(void)
{ 
    char temp ;
    char count, i;
    //p = xmalloc(480*272*3);

    count = UFSTAT0&0x3F;       //           
    for(i = 0; i < count; i++)
    {
        temp = URXH0;
        ReceiveBuffer[ReceiveIndex] = temp;
        ReceiveIndex++;
    }
    
    memcpy(p+count2,ReceiveBuffer,32);
    count2+=count;
//    gg[0]=p;
//    UART0_RecvString((unsigned char*)(0x31000000),count);    
    //    printf("%d",count2);

    //page++;
//  p+=480*272*2*page;
    ReceiveIndex=0;
    
    if(count < 32)
    {
        ReceiveBuffer[ReceiveIndex] = 0;//    memcpy   

        //        
        uartcmd_run(ReceiveBuffer);
        
        ReceiveIndex = 0;
        flag_Receive_end = 1;//flag_Receive_end       ,  1          
        printf("%s", ReceiveBuffer); //          
    }
}

//     
void UART0_InitFIFO(int baudrate)
{
    //     
    int nUBRDIV = PCLK/16/baudrate - 1;

    GPHCON = 0xA0;
    GPHUP = 0x0C;

    //        :   UART      
    //              1      8     
    ULCON0 = (0<<6)|(0<<3)|(0<<2)|(3<<0);

    UCON0 = (0 << 12) | (2 << 10) |  (1 << 7) | (1 << 2) | (1 << 0);      //          ;     PCLK       
    UFCON0 = (2 << 6) | (3 << 4) | (0 << 3) | (1 << 2) | (1 << 1) | (1 << 0);   //  FIFO,        (0  ),        (32  )    
    UMCON0 = 0x00;    //         
    UBRDIV0 = nUBRDIV;      //    115200,PCLK = 50MHz 

    //        
    irq_install(IRQ_SUB_RXD0, UART0_IRQ_Receive);  //    FIFO  
    //irq_install(IRQ_SUB_TXD0, UART0_IRQ_Send); //      
}


//   
int fputc(int ch,FILE *p)  //     ,   printf       
{
    UART0_SendByte((unsigned char)ch);    
    return ch;
}

----------------------------------------------------------화려한 분할선 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
uart입니다.h 파일
#ifndef __UART_H_
#define __UART_H_

#include "stdio.h"

int UART0_init(int baudrate);

//    
int UART0_SendByte(unsigned char cSendChar);

//    
unsigned char UART0_RecvByte(void);

//        
int UART0_SendString(unsigned char* buf, int len);
int UART0_RecvString(unsigned char * buf, int len);


// printf      ,        
int fputc(int ch, FILE *p);  //     ,   printf       

// FIFO    
//     
void UART0Init_FIFO(int baudrate);


#endif /* __UART_H_ */

좋은 웹페이지 즐겨찾기