这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 综合技术 » 基础知识 » 2410,fifo 2410的fifo

共2条 1/1 1 跳转至

2410,fifo 2410的fifo

院士
2006-09-17 18:14:16     打赏
2410,fifo 2410的fifo

院士
2006-12-22 22:43:00     打赏
2楼
问 当我发送数据的时候,如果我把fifo的trigger leval设置成4,比如我要发送10个字,那是不是在第六个数发送出去的时候产生中断呢?
另外的一个主要的问题是,我如何才能把我要发的10个数送到fifo中去呢?因为上电以后fifo是复位状态的,里面是空的,自己中断是不可能了。那我的数得在什么时候采取什么方式放到fifo中去呢?
谢谢高手回复! 1: 我写的串口驱动自己看吧,很明了的。
你的问题就在这个源码中

serial.c
/*-----------------------------------------------------------------------
Project   :   
Author    :   kreal@163.net
Date      :   04-10-10
Funtion   :   串口驱动   
------------------------------------------------------------------------*/



#define  SERIAL_G

#include "sysincludes.h"




/*---------------------------------------------------------------------------
Function   :  Uart ISR
Input      :  None
Output     :  None
-----------------------------------------------------------------------------*/


void  __irq ISR_Uart0 (void)
{
    uchar iN, cnt ;
    
    OSIntEnter();

    if (rSUBSRCPND & BIT_SUB_TXD0) {
        while ((rUFSTAT0 & (1<<9)) == 0) {                  // 塞满 FIFO 为止.
            if (Com0.Rd != Com0.Wr) {
                rUTXH0 = Com0.TxBuffer[Com0.Rd++];
                if (Com0.Rd >= UART_TX_MAX_SIZE ) {
                    Com0.Rd = 0x00;
                }
            } else    break;                                // 发送完毕.
        }
        rSUBSRCPND=(BIT_SUB_TXD0);                            // Clear Sub int pending.
    }
    else if( rSUBSRCPND & BIT_SUB_RXD0 )
    {
       cnt = rUFSTAT0 & 0x000F ;
       for( iN = 0 ; iN < cnt ; iN ++ )
       {
          Com0.RxBuffer[Com0.pIn++] = rURXH0 ;
          if( Com0.pIn>=UART_RX_MAX_SIZE )
            Com0.pIn = 0 ;
       }
       rSUBSRCPND = BIT_SUB_RXD0 ;
    
    }
    
   
    OSIntExit();

    ClearPending(BIT_UART0);                                // Clear master pending.
   
}

void  __irq ISR_Uart1 (void)
{
    uchar iN, cnt ;
    
    OSIntEnter();

    if (rSUBSRCPND & BIT_SUB_TXD1) {
        while ((rUFSTAT1 & (1<<9)) == 0) {                  // 塞满 FIFO 为止.
            if (Com1.Rd != Com1.Wr) {
                rUTXH1 = Com1.TxBuffer[Com1.Rd++];
                if (Com1.Rd >= UART_TX_MAX_SIZE ) {
                    Com1.Rd = 0x00;
                }
            } else    break;                                // 发送完毕.
        }
        rSUBSRCPND=(BIT_SUB_TXD1);                            // Clear Sub int pending.
    }
    else if( rSUBSRCPND & BIT_SUB_RXD1 )
    {
       
         
        cnt = rUFSTAT1 & 0x000F ;
        for( iN = 0 ; iN < cnt ; iN ++ )
        {
          Com1.RxBuffer[Com1.pIn++] = rURXH1 ;
          if(Com1.pIn>=UART_RX_MAX_SIZE )
              Com1.pIn = 0 ;
        }
      
        rSUBSRCPND = BIT_SUB_RXD1 ;
    
    }

   
    OSIntExit();
    
    ClearPending(BIT_UART1);                                // Clear master pending
}

void  __irq ISR_Uart2 (void)
{
    uchar iN, cnt ;
    
    OSIntEnter();

    if (rSUBSRCPND & BIT_SUB_TXD2) {
        while ((rUFSTAT2 & (1<<9)) == 0) {                  // 塞满 FIFO 为止.
            if (Com2.Rd != Com2.Wr) {
                rUTXH2 = Com2.TxBuffer[Com2.Rd++];
                if (Com2.Rd >= UART_TX_MAX_SIZE ) {
                    Com2.Rd = 0x00;
                }
            } else    break;                                // 发送完毕.
        }
        rSUBSRCPND=(BIT_SUB_TXD2);                            // Clear Sub int pending.
    }
    else if( rSUBSRCPND & BIT_SUB_RXD2 )
    {
       
         
        cnt = rUFSTAT2 & 0x000F ;
        for( iN = 0 ; iN < cnt ; iN ++ )
        {
           Com2.RxBuffer[Com2.pIn++] = rURXH2 ;
           if( Com2.pIn >= UART_RX_MAX_SIZE )
             Com2.pIn = 0 ;
        }          
        rSUBSRCPND = BIT_SUB_RXD2 ;
    
    }


   
    OSIntExit();

    ClearPending(BIT_UART2);                                // Clear master pending.
}



/*------------------------------------------------------------------------
Function  :  串口初始化
Input     :  ComNum -- 串口号
             baud   -- 串口波特率
Output    :  COM_NUM_ERR   不存在的串口
             COM_BAUD_ERR  非法波特率

-------------------------------------------------------------------------*/

char ComInit( uchar ComNum, uint baud )
{
   if( ComNum>2 )                  // Para Limit
     return  COM_NUM_ERR   ;       // 不存在的串口
   else if( baud > 115200 )     
     return  COM_BAUD_ERR ;        // 非法的波物率
   
    if(ComNum==0 )
    {  
        rULCON0  = ((0<<6)+(0<<3)+(0<<2)+(3<<0));            //stop bit and parity bit
        rUCON0   = ((0<<10)+(0<<9)+(0<<8)+(0<<7)+(0<<6)+(0<<5)+(0<<4)+(1<<2)+(1<<0));  // interrupt mode or poll mode
        rUFCON0  = ((0<<6)+( 2<<4)+(0<<2)+(0<<1)+(1<<0));    // fifo
        rUMCON0  = ((0<<4)+(0<<0));                         //disable the modem mode
        rUBRDIV0 = ((int)(PCLK/(baud*16))-1);               // baud

        Com0.Wr  = 0;                                       // 初始化自定义缓冲区.
        Com0.Rd  = 0;
        Com0.tSem = OSSemCreate (1);                        // 建立一个信号量,避免冲突.
        
        Com0.pIn = 0 ;
        Com0.pOut= 0 ;
        Com0.rSem = OSSemCreate( 1 ) ;
        
        pISR_UART0 =(uint) ISR_Uart0 ;

        rINTMOD    &= ~(BIT_UART0);                         // IRQ mode.
        rINTMSK    &= ~(BIT_UART0);                         // 开 UART0 发送中断屏蔽位.
        rINTSUBMSK &= ~(BIT_SUB_TXD0);
        rINTSUBMSK &= ~(BIT_SUB_RXD0) ;
    }
    else if( ComNum==1 )
    {     
        rULCON1  = ((0<<6)+(0<<3)+(0<<2)+(3<<0));            //stop bit and parity bit
        rUCON1   = ((0<<10)+(0<<9)+(0<<8)+(0<<7)+(0<<6)+(0<<5)+(0<<4)+(1<<2)+(1<<0));  // interrupt mode or poll mode
        rUFCON1  = ((0<<6)+( 2<<4)+(0<<2)+(0<<1)+(1<<0));    // fifo
        rUMCON1  = ((0<<4)+(0<<0));                         //disable the modem mode
        rUBRDIV1 = ((int)(PCLK/(baud*16))-1);               // baud

        Com1.Wr  = 0;                                     // 初始化自定义缓冲区.
        Com1.Rd  = 0;
        Com1.tSem = OSSemCreate (1);                      // 建立一个信号量,避免冲突.
        
        Com1.pIn = 0 ;
        Com1.pOut= 0 ;
        Com1.rSem = OSSemCreate( 1 ) ;
        
        pISR_UART1 =(uint) ISR_Uart1 ;

        rINTMOD    &= ~(BIT_UART1);                       // IRQ mode.
        rINTMSK    &= ~(BIT_UART1);                        // 开 UART0 发送中断屏蔽位.
        rINTSUBMSK &= ~(BIT_SUB_TXD1);
        rINTSUBMSK &= ~(BIT_SUB_RXD1) ;
        
   }else if( ComNum == 2 )
   {
        rULCON2  = ((0<<6)+(0<<3)+(0<<2)+(3<<0));            //stop bit and parity bit
        rUCON2   = ((0<<10)+(0<<9)+(0<<8)+(0<<7)+(0<<6)+(0<<5)+(0<<4)+(1<<2)+(1<<0));  // interrupt mode or poll mode
        rUFCON2  = ((0<<6)+( 2<<4)+(0<<2)+(0<<1)+(1<<0));    // fifo
        rUMCON2  = ((0<<4)+(0<<0));                         //disable the modem mode
        rUBRDIV2 = ((int)(PCLK/(baud*16))-1);               // baud

        Com2.Wr  = 0;                                       // 初始化自定义缓冲区.
        Com2.Rd  = 0;
        Com2.tSem = OSSemCreate (1);                         // 建立一个信号量,避免冲突.
        
        Com2.pIn = 0 ;
        Com2.pOut= 0 ;
        Com2.rSem = OSSemCreate( 1 ) ;
        
        pISR_UART2 =(uint) ISR_Uart2 ;

        rINTMOD    &= ~(BIT_UART2);                         // IRQ mode.
        rINTMSK    &= ~(BIT_UART2);                         // 开 UART0 发送中断屏蔽位.
        rINTSUBMSK &= ~(BIT_SUB_TXD2);
        rINTSUBMSK &= ~(BIT_SUB_RXD2) ;
        
          
    
   }

   return  COM_INIT_OK ;
}




void Wr_Uart_Buf( UART *ch,  uchar data )
{
   UART *p ;
   
   p = ch ;
   
   p->TxBuffer[ p->Wr++] = data ;
   if( p->Wr > UART_TX_MAX_SIZE ) p->Wr = 0x00 ;

}

void StartSend( char ComNum )
{
    UART *ptr ;  
    uint status ;
    uchar *reg ;
    
     
    switch( ComNum )
    {
      case  0 :  ptr = &Com0 ; status = rUTRSTAT0&(1<<2 ) ; reg =(uchar *)&rUTXH0;  break ;
      case  1 :  ptr = &Com1 ; status = rUTRSTAT1&(1<<2 ) ; reg =(uchar *)&rUTXH1;  break ;
      case  2 :  ptr = &Com2 ; status = rUTRSTAT2&(1<<2 ) ; reg =(uchar *)&rUTXH2;  break ;
      default :  ptr = &Com0 ; status = rUTRSTAT0&(1<<2 ) ; reg =(uchar *)&rUTXH0;  break ;
     
    }
   
    if( status )
    {
      if( ptr->Rd != ptr->Wr )
      {
         *reg = ptr->TxBuffer[ptr->Rd++] ;
         if( ptr->Rd >= UART_TX_MAX_SIZE ) ptr->Rd = 0x00 ;
      }
    }
  

}







//Write Data To Uart Buffer (Tx )

void UartPrintf(UART *ch , const char *format, ...)
{

  #define MAX_TBUF    100                             // 注意: 只允许接收改大小的字符串.
  char    tbuf[MAX_TBUF] = {0, };
  va_list v_list;
  char    *ptr;
  uchar    err;
  UART    *p ;
  char    comnum ;
  
  if( ch == &Com0 ) comnum = 0 ;
  else if(ch== &Com1 ) comnum = 1 ;
  else if( ch == &Com2 ) comnum = 2 ;
  else  comnum = 0 ;  
  
   p = ch ;
  
   OSSemPend( p->tSem, 0, &err);

   va_start( v_list, format);                       // Initialize variable arguments.
   vsprintf( tbuf, format, v_list );
   va_end(v_list);
    
   ptr= tbuf;
   
   while(*ptr !=  '\0')
   {
        Wr_Uart_Buf(p, *ptr++ );
   }

   StartSend (comnum);

   OSSemPost (p->tSem);

   #undef MAX_TBUF
}


void  UartSendData( UART *ch, uchar *buf, uint len )
{
   
    
    uchar    err;
    UART    *p ;
    uint     iN ;
    char    comnum ;
  
    if( ch == &Com0 ) comnum = 0 ;
    else if(ch== &Com1 ) comnum = 1 ;
    else if( ch == &Com2 ) comnum = 2 ;
    else  comnum = 0 ;  
  
    
    p = ch ;
    
    OSSemPend( p->tSem, 0,&err ) ;
    
    for( iN = 0 ; iN < len ; iN ++ )
    {
         Wr_Uart_Buf( p, *buf++ ) ;
       
    }
    
    StartSend( comnum ) ;  
    
    OSSemPost( p->tSem ) ;



}




//serial.h

/*----------------------------------------------------------
其中包括 :
        串口的类抽象
        串口属性参数
----------------------------------------------------------*/


#ifdef  SERIAL_G
#define serial_ext
#else
#define serial_ext  extern
#endif



#define   UART_TX_MAX_SIZE    100
#define   UART_RX_MAX_SIZE    8192

#define   COMM0   &Com0
#define   COMM1   &Com1
#define   COMM2   &Com2


typedef struct {
  
   unsigned char    TxBuffer[UART_TX_MAX_SIZE];
   unsigned char    Wr;
   unsigned char    Rd;
   OS_EVENT *tSem;

   unsigned char     RxBuffer[UART_RX_MAX_SIZE] ;
   unsigned int      pIn ;
   unsigned int      pOut;
   OS_EVENT *rSem ;  
    
} UART     ;     // Uart Struct     


serial_ext UART    Com0,Com1,Com2 ;    //  2410 Uart0 - Uart2




//Error List

#define    COM_BAUD_ERR       0
#define    COM_NUM_ERR        1
#define    COM_INIT_OK        2



serial_ext  void  UartPrintf(UART *ch , const char *format, ...) ;
serial_ext  char  ComInit( uchar omNum, uint baud ) ;
serial_ext  void  UartSendData( UART *ch, uchar *buf, uint len )  ;  








共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]