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

📄 uart.c

📁 the uart driver based on NEC V850
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************************
 *       Title:   uart.c
 *
 *  Description:  This is the standard code file for UART.
 *
 *      Author:   Guido Brunken
 *
 *    Configuration ID:   C_10014034_02_UART
 *
 *********************************************************************/
                                                               
/**********************************************************************
 * Include header files
 *********************************************************************/
#include "uart.inc"  

/*********************************************************************/
/* Dependent "optimize.cmd"                                          */  
/*********************************************************************/


/**********************************************************************
 * File level pragmas
 *********************************************************************/


/**********************************************************************
 * Constant and Macro Definitions using #define
 *********************************************************************/

/**********************************************************************
 * Enumerations and Structures and Typedefs
 *********************************************************************/
/*
// Information for each UART channel is stored in variables of this type:
typedef struct uart_chan_tag
{
   uint16_t rx_in;         // Rx buffer input index 
   uint16_t rx_out;        // Rx buffer output index
   uint16_t rx_count;      // Rx buffer byte count
   uint16_t rx_buf_size;
   UART_Rx_Func_Ptr rx_func;        // Rx callback function pointer
   uint8_t  *rx_buf;       // Rx ring buffer
   uint16_t tx_in;         // Tx buffer input index 
   uint16_t tx_out;        // Tx buffer output index
   uint16_t tx_count;      // Tx buffer byte counter
   uint16_t tx_buf_size;   
   uint8_t  *tx_buf;  // Tx ring buffer
   volatile uart_reg_t* ptr_reg;    // Pointer to hardware registers            
   bool     tx_progress;   // Tx in progress   
} uart_chan_t;


typedef struct uart_chan_init_data_tag 
{
   uint32_t  channel;      // channel index 
   uint8_t  *ptr_rx_buf;  // Rx ring buffer
   uint8_t  *ptr_tx_buf;  // Tx ring buffer
   uint16_t rx_size;      // Rx buffer byte count
   uint16_t tx_size;      // Tx buffer byte counter
   uart_reg_init_data_t reg_init_data;    // init values for hardware registers
   UART_Rx_Func_Ptr rx_func;        // Rx callback function pointer   
} uart_chan_init_data_t;
*/
/**********************************************************************
 * Global and Const Variable Defining Definitions / Initializations
 *********************************************************************/


/**********************************************************************
 * Static Variables and Const Variables With File Level Scope
 *********************************************************************/
static uart_chan_t uart_chan_ram_data[UART_NUM_HW_DEVICES_USED];

#if (BLUETOOTH_IS || (External_Bluetooth_IS && M12_NAV_MODEL_IS))
static uint8_t  uart3_rx_buffer[100];              // Buffer of current receive message
static uint8_t  uart3_tx_buffer[100];              // Buffer of current transmit message
static uint8_t  uart3_tx_retry_count; 	        		          // Count of retries (0..BT_TX_MAX_TRIES)
static bool     uart3_rx_active;                                 // currently receiving data from bluetooth module
#endif
/**********************************************************************
 * ROM Const Variables With File Level Scope
 *********************************************************************/
#if __v850__
#pragma BEGIN_ROM_CONST

#endif // __v850__
   

#if __v850__
#pragma END_ROM_CONST

#endif // __v850__

/**********************************************************************
 * Function Prototypes for Private Functions with File Level Scope
 *********************************************************************/
#if UART_CONF_USE_TX_INTERRUPT_IS
static void uart_do_tx(Uart_Devices_Enum_T device);    // Transmit helper function
#endif //#if UART_CONF_USE_TX_INTERRUPT_IS

#if UART_CONF_USE_RX_INTERRUPT_IS
static void uart_rx_isr_handler ( Uart_Devices_Enum_T device );
#endif // #if UART_CONF_USE_RX_INTERRUPT_IS


/**********************************************************************
 * Add User defined functions
 *********************************************************************/
#include "uart.cu"

/**********************************************************************
 * SCR Checks
 *********************************************************************/

/**********************************************************************
 * Function Definitions
 *********************************************************************/

#if UART_CONF_USE_TX_INTERRUPT_IS
/*******************************************************************************
*    Function: uart_do_tx
*
*  Parameters: Channel
*     Returns: Nothing
* Description: Transmit helper function. Takes one byte from transmit queue
*              and sends it to the hardware. Provides common code for first
*              byte transmission (before transmit interrupt is enabled) and
*              successive byte transmission (from transmit interrupt).
*******************************************************************************/
static void uart_do_tx (Uart_Devices_Enum_T device)    
{
   uart_chan_t *pChan;

   if ( ( device < UART_NUM_HW_DEVICES) && ( NULL != uart_chan[device] ) )
   {
      pChan = uart_chan[device]; /* optimize ROM size of function */
      
      pChan->tx_count--;      // Decrement tx buffer byte count
      pChan->tx_out++;        // Increment index
      if ((pChan->tx_out) >= pChan->tx_buf_size )
      {
         pChan->tx_out = 0; // Wrap index
      }   

      /* UART Transmission Mode shall be enabled previously */

      // Write to hardware transmit register
      pChan->ptr_reg->tx = pChan->tx_buf[pChan->tx_out]; 
   }
   else
   {
      /* invalid device */
   }

}
#endif // #if UART_CONF_USE_TX_INTERRUPT_IS


#if UART_CONF_USE_RX_INTERRUPT_IS
/*******************************************************************************
*    Function: uart_rx_isr_handler
*
*  Parameters: None
*     Returns: None
* Description: called rx ISR
*******************************************************************************/
static void uart_rx_isr_handler ( Uart_Devices_Enum_T device )
{
   uart_chan_t *pChan;
   volatile UINT8 data;
   volatile UINT8 err;

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

   data = pChan->ptr_reg->rx;  // Read data from hardware register

   err = pChan->ptr_reg->str; // Read status from hardware register

#if UART_CONF_USE_RX_CALLBACK_IS
   // Can either read to buffer or use callback function
   if ( NULL != pChan->rx_func )
   {
      // Callback
      if ( UART_RX_DONE == pChan->rx_func(data, err) ) // Pass data and status to callback
      {
         return;
      }
      else
      {
         /* do nothing */
      }
   }
   else
   {
      /* do nothing */
   }
#endif // #if UART_CONF_USE_RX_CALLBACK_IS


   if ( ( 0 == ( ( UART_ERR_FRAME | UART_ERR_PARITY ) & err )  ) && ( NULL != pChan->rx_buf ) )
   {
      // Buffer
      if (pChan->rx_count < pChan->rx_buf_size )   // Check for buffer full
      {
         pChan->rx_buf[pChan->rx_in] = data; // Copy data to receive buffer
         pChan->rx_count++;          // Increment buffered byte count
         pChan->rx_in++;             // Increment rx buffer input index
         if ((pChan->rx_in) >= pChan->rx_buf_size )
         {
            pChan->rx_in = 0; // Wrap index
         }   
      }
      else                                    // Rx buffer full
      {
         NOP(); // Receive buffer overflow. How to handle?
      }
   }
   else
   {
      /* do nothing */
      err = 0; //halt for dbg
   }

   pChan->ptr_reg->str = 0x00; // clear status register
}
#endif // #if UART_CONF_USE_RX_INTERRUPT_IS

/**********************************************************************
 * Description: Initialize UART specified by channel  
 *
 *
 *  Parameters: Channel
 *     Returns: None
 *********************************************************************/
Uart_Devices_Enum_T UART_Initialize ( Uart_Device_Config_T  config_index )
{
   const uart_chan_init_data_t * pInit;
   Uart_Devices_Enum_T  device;   

     
   if (  ( Num_Elems(uart_init_data_table) > config_index )
      && ( NULL != uart_init_data_table[config_index] ) )
   {
      pInit = uart_init_data_table[config_index];
      device = pInit->channel;   /* get HW device index */

      if ( ( UART_NUM_HW_DEVICES > device ) && ( NULL != uart_chan[device] ) )
      {
         uart_chan[device]->rx_count = 0; // Clear rx byte counter
         uart_chan[device]->rx_in = 0;    // Increment before write index
         uart_chan[device]->rx_out = 0;   // Increment before read index
         
         uart_chan[device]->rx_func = pInit->rx_func;  // Initialize callback off
         uart_chan[device]->rx_buf  = pInit->ptr_rx_buf;
         uart_chan[device]->rx_buf_size = pInit->rx_size;
         
         uart_chan[device]->tx_count = 0; // Clear tx byte counter 
         uart_chan[device]->tx_in = 0;    // Clear tx buffer input before write index
         uart_chan[device]->tx_out = 0;   // Clear tx buffer output before read index
         uart_chan[device]->tx_progress = false;   // Clear tx in progress flag
         
         uart_chan[device]->tx_buf = pInit->ptr_tx_buf;
         uart_chan[device]->tx_buf_size = pInit->tx_size;
         
         uart_initialize_hook(device, &pInit->reg_init_data );   // Have to set up pins here
      
      }
      else
      {
         /* no config memory reserved for this HW device or illegal channel */
         device = UART_NUM_HW_DEVICES;    /* return illegal device handle */
      }

   }
   else
   {
      /* illegal channel index */
      device = UART_NUM_HW_DEVICES;
   }

   return ( device );
}


/**********************************************************************
 * Description: Start Uart transmission/reception
 *  Parameters: device
 *     Returns: None
 *********************************************************************/
Uart_Ret_T UART_Start ( Uart_Devices_Enum_T device, Uart_Isr_State_T isr_state )
{
   Uart_Ret_T result = E_UART_NO_DEVICE;
   
   if ( ( device < UART_NUM_HW_DEVICES) && ( NULL != uart_chan[device] ) )
   {
      uart_chan[device]->ptr_reg->ctl0 |= UART_CLOCK_ENABLE;   
      NOP();   /* wait to cycles, ref. to manual */
      NOP();
      uart_chan[device]->ptr_reg->ctl0 |= (UART_TX_ENABLE | UART_RX_ENABLE);

      UART_Set_Isr_State( device, isr_state );
      result = E_UART_OK;
   }
   else
   {
      /* invalid device */
   }

   return ( result );   

}


/**********************************************************************
 * Description: Stop Uart transmission/reception
 *  Parameters: device

⌨️ 快捷键说明

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