⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 the uart driver based on NEC V850
💻 C
📖 第 1 页 / 共 2 页
字号:
 *     Returns: None
 *********************************************************************/
Uart_Ret_T UART_Stop ( Uart_Devices_Enum_T device )
{
   if ( ( device < UART_NUM_HW_DEVICES) && ( NULL != uart_chan[device] ) )
   {
      UART_Set_Isr_State( device, UART_ISR_OFF );
      uart_chan[device]->ptr_reg->ctl0 &= ~( UART_CLOCK_ENABLE | UART_TX_ENABLE | UART_RX_ENABLE);
      return ( E_UART_OK );
   }
   else
   {
      /* invalid device */
      return ( E_UART_NO_DEVICE );      
   }
}


#if UART_CONF_USE_BUFFERED_IO_IS
/*******************************************************************************
* Description: Reads one byte from the UART receive buffer and writes it to a
*              pointer provided by the caller. A value is also returned to
*              indicate whether a byte was read.
*
*  Parameters: Channel, Pointer to variable which can receive one byte of data
*
*     Returns: TRUE if data is available and written to pointer, else FALSE
*******************************************************************************/
bool UART_Get_Char (Uart_Devices_Enum_T device, uint8_t* ptr)                          
{
   bool_fast fResult = false;
   uart_chan_t *pChan;
   
   if (  ( device < UART_NUM_HW_DEVICES)
      && ( NULL != uart_chan[device] )
      && ( NULL != ptr ) )
   {

      pChan = uart_chan[device]; /* optimize ROM size of function */

      UART_ENTER_CRITICAL_SECTION();
        
      if (!pChan->rx_count)      // Rx buffer empty
      {
         /* do nothing */
      }
      else
      { 
         *ptr = pChan->rx_buf[pChan->rx_out]; // Store read data
         pChan->rx_count--;   // Decrement rx buffer byte count
         pChan->rx_out++;     // Increment rx buffe output index
         if ((pChan->rx_out) >= pChan->rx_buf_size )
         {
            pChan->rx_out = 0; // Wrap index
         }   

         fResult = true;
      }

      UART_LEAVE_CRITICAL_SECTION();      
      
   }
   else
   {
      /* invalid device */
   }

   return ( fResult );
   
}


/*******************************************************************************
* Description: Copy one byte to tx buffer
*
*  Parameters: Channel, Data to transmit
*
*     Returns: TRUE on success, FALSE on failure
*******************************************************************************/
bool UART_Put_Char (Uart_Devices_Enum_T device, uint8_t data)           
{
   bool_fast fResult = false;
   uart_chan_t *pChan;

   if ( ( device < UART_NUM_HW_DEVICES) && ( NULL != uart_chan[device] ) )
   {
      pChan = uart_chan[device];
      
      UART_ENTER_CRITICAL_SECTION();


      if (pChan->tx_count != pChan->tx_buf_size )    // Is tx buffer full 
      {
         pChan->tx_count++;   // Increment tx buffer byte count
         pChan->tx_in++;      // Increment tx buffer input index
         if ((pChan->tx_in) >= pChan->tx_buf_size )
         {
            pChan->tx_in = 0; // Wrap index
         }      
         pChan->tx_buf[pChan->tx_in] = data; // Copy byte to tx buffer

         fResult = true;
      }
      else
      {
         /* buffer full, do nothing */
      }

      if (!pChan->tx_progress) // Send first byte. Interrupts do the rest.
      {
         uart_do_tx(device);          // Send to hardware 
         pChan->tx_progress = true;    // Flag tx in progress
      }
      else
      {
         /* do nothing */
      }

      UART_LEAVE_CRITICAL_SECTION();         

      
   }
   else
   {
      /* invalid device */
   }
   
   return ( fResult );
}
#endif // #if UART_CONF_USE_BUFFERED_IO_IS



#if UART_CONF_USE_UNBUFFERED_WRITE_IS
/*******************************************************************************
* Description: Routine to write directly to the Uart (unbuffered)
*
*  Parameters: channel, data
*     Returns: None
*******************************************************************************/
void UART_Put_Char_Unbuf ( Uart_Devices_Enum_T device, uint8_t data  )
{
   // Write to hardware transmit register
   uart_chan[device]->ptr_reg->tx = data;
}
#endif // #if UART_CONF_USE_UNBUFFERED_WRITE_IS


#if UART_CONF_USE_TX_INT_0_IS
/*******************************************************************************
*    Function: UART0_TX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART0 transmit Interrupt Service Routine
*******************************************************************************/
void UART0_TX_ISR (void)                   // SCR 9764
{
   if (uart_chan[UART_DEVICE_0]->tx_count) // Any bytes to send? continue to send bytes
   {                 
      uart_do_tx(UART_DEVICE_0);       // Send to hardware    
   }
   else
   {        
      uart_chan[UART_DEVICE_0]->tx_progress = false;     // Disable transmit
   }
}   
#endif // #if UART_CONF_USE_TX_INT_0_IS



#if UART_CONF_USE_RX_INT_0_IS
/*******************************************************************************
*    Function: UART0_RX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART0 recieve Interrupt Service Routine 
*******************************************************************************/
void UART0_RX_ISR (void)
{
   uart_rx_isr_handler (UART_DEVICE_0);
}
#endif // #if UART_CONF_USE_RX_INT_0_IS

#if UART_CONF_USE_TX_INT_1_IS
/*******************************************************************************
*    Function: UART1_TX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART1 transmit Interrupt Service Routine
*******************************************************************************/
void UART1_TX_ISR (void)                   
{
   if (uart_chan[UART_DEVICE_1]->tx_count) // Any bytes to send?
   {                 
      uart_do_tx(UART_DEVICE_1);       // Send to hardware    
   }
   else
   {        
      uart_chan[UART_DEVICE_1]->tx_progress = false;     // Disable transmit
   }
}   
#endif //#if UART_CONF_USE_TX_INT_1_IS


#if UART_CONF_USE_RX_INT_1_IS
/*******************************************************************************
*    Function: UART1_RX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART1 recieve Interrupt Service Routine 
*******************************************************************************/
void UART1_RX_ISR (void)
{
   uart_rx_isr_handler (UART_DEVICE_1);
}

#endif //#if UART_CONF_USE_RX_INT_1_IS

#if UART_CONF_USE_TX_INT_2_IS
/*******************************************************************************
*    Function: UART2_TX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART2 transmit Interrupt Service Routine
*******************************************************************************/
void UART2_TX_ISR (void)                   // SCR 9764
{
   if (uart_chan[UART_DEVICE_2]->tx_count) // Any bytes to send?
   {                 
      uart_do_tx(UART_DEVICE_2);       // Send to hardware    
   }
   else
   {        
      uart_chan[UART_DEVICE_2]->tx_progress = false;     // Disable transmit
   }
}   
#endif // #if UART_CONF_USE_TX_INT_2_IS

#if UART_CONF_USE_RX_INT_2_IS
/*******************************************************************************
*    Function: UART2_RX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART2 recieve Interrupt Service Routine 
*******************************************************************************/

void UART2_RX_ISR (void)
{
    uart_rx_isr_handler (UART_DEVICE_2);		// store msg, count bytes received
}

#endif // #if UART_CONF_USE_RX_INT_2_IS

#if UART_CONF_USE_TX_INT_3_IS
/*******************************************************************************
*    Function: UART3_TX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART3 transmit Interrupt Service Routine
*******************************************************************************/
void UART3_TX_ISR (void)                   // SCR 9764
{
   if (uart_chan[UART_DEVICE_3]->tx_count) // Any bytes to send?
   {                 
      uart_do_tx(UART_DEVICE_3);       // Send to hardware    
   }
   else
   {        
      uart_chan[UART_DEVICE_3]->tx_progress = false;     // Disable transmit
   }
}   
#endif // #if UART_CONF_USE_TX_INT_2_IS


#if UART_CONF_USE_RX_INT_3_IS
/*******************************************************************************
*    Function: UART3_RX_ISR
*
*  Parameters: None
*     Returns: None
* Description: UART3 recieve Interrupt Service Routine 
*******************************************************************************/
void UART3_RX_ISR (void)
{
     uart_rx_isr_handler (UART_DEVICE_3);
}

#endif // #if UART_CONF_USE_RX_INT_3_IS

/*******************************************************************************
* REVISION RECORDS                                                             *
*******************************************************************************/
/******************************************************************************/
/* $Log:   O:\pvcs\IBS_Software\06_Opel\src\uart.c_v  $
 * 
 *    Rev 1.2   Mar 09 2005 18:49:50   Brunken_Guido
 * + reworked module
 * + added configuration switches for code size optimization
 * 
 *    Rev 1.1   Oct 11 2004 11:05:34   Brunken_Guido
 * + reworked to optimize for code size
 * 
 *    Rev 1.0   Sep 30 2004 21:04:32   Brunken_Guido
 * Initial revision.
 * 
 *    Rev 0.9   23 Sep 2004 12:00:00   Brunken_Guido
 * Copied from O:\Pvcs\IBS_Software\vib\src\uart.c_v Speicher_Jeff
 * 
 *******************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -