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

📄 uart.cu

📁 the uart driver based on NEC V850
💻 CU
📖 第 1 页 / 共 3 页
字号:
/* $Header:   O:\pvcs\ibs_software\06_opel\src\uart.cuv   1.5   May 18 2005 16:29:18   Stryj_Michael  $*/
/**********************************************************************
 *
 *   Copyright 2004 Delphi Technologies, Inc., All Rights Reserved.
 *
***********************************************************************
/**********************************************************************
 *               Title:   uart.cu
 *
 *  Module Description:   This file has the applications specific
 *                        code for uart.  This file is included
 *                        directly by uart.c
 *
 *              Author:   Guido Brunken
 *
 *    Configuration ID:   CU_-10014034_02_UART
 *
 *********************************************************************/
/**********************************************************************
 * File level pragmas
 *********************************************************************/
    
/**********************************************************************
 * SCRs Implemented
 *********************************************************************/
    
/**********************************************************************
 * Constant and Macro Definitions using #define
 *********************************************************************/
#define UART_V850ES_SG2  (true)

/* Check supported uP devices: */
#ifndef UART_V850ES_SG2
#error "Module written for NEC V850es SG2."
#else /* #ifndef UART_V850ES_SG2 */
#endif /* #ifndef UART_V850ES_SG2 */


/* Hisip: ( 32 bytes max frame + 1 byte Separator (Sipprot) + 3 byte word-addr alignment )
 *         multiplied with number of frames to buffer in UART module.
 */
#define UART_RX_BUF_SIZE_0    (128)      /* for CGI, not used*/
#define UART_TX_BUF_SIZE_0    (128)      /* for CGI, not used */

#define UART_RX_BUF_SIZE_1    128	/* not used */
#define UART_TX_BUF_SIZE_1    128	/* not used */

/*for TSI. modify buffer Size according TSI Appliation demand*/
#define UART_RX_BUF_SIZE_2     128
#define UART_TX_BUF_SIZE_2     128

/*for Bluetooth, modify buffer size according to application demand*/
//#define UART_RX_BUF_SIZE_3     128	/* no UART3 in 3335 */
//#define UART_TX_BUF_SIZE_3     128	/* no UART3 in 3335 */

#if (XTAL_FREQ_IN_HZ == 16000000UL)
// UART0_BAUD_RATE   UART0_BAUD_MODE0  Nominal Baud  Actual Baud   % Error 
// to be done..
#define UART_CTL1_5        (0x0D)
#define UART_CTL2_5        (0xC3)

#define UART_CTL1_200      (0x08)
#define UART_CTL2_200      (0x9C)

#define UART_CTL1_9600     (0x06)
#define UART_CTL2_9600     (0x0D)

#define UART_CTL1_38400     (0x01)
#define UART_CTL2_38400     (0x68)

#define UART_CTL1_115K2    (0x00)   
#define UART_CTL2_115K2    (0x45)   

#elif (XTAL_FREQ_IN_HZ == 20000000UL) //==================
// UART0_BAUD_RATE   UART0_BAUD_MODE0  Nominal Baud  Actual Baud   % Error 
//       0x00              0x57           115200       114943        0.22%
#define UART_CTL1_5        (0x0D)
#define UART_CTL2_5        (0xF4)

#define UART_CTL1_200      (0x08)
#define UART_CTL2_200      (0xC3)

#define UART_CTL1_9600     (0x04)
#define UART_CTL2_9600     (0x41)

#define UART_CTL1_38400     (0x01)
#define UART_CTL2_38400     (0x82)

#define UART_CTL1_115K2    (0x00)
#define UART_CTL2_115K2    (0x57)

#elif (XTAL_FREQ_IN_HZ == 32000000UL)

#define UART_CTL1_5        (0xFF)  //invalid value,  TBD
#define UART_CTL2_5        (0xFF)  //invalid value, TBD

#define UART_CTL1_200      (0x09)
#define UART_CTL2_200      (0x9C)

#define UART_CTL1_9600     (0x04)
#define UART_CTL2_9600     (0x68)

#define UART_CTL1_38400     (0x02)
#define UART_CTL2_38400     (0x68)

#define UART_CTL1_115K2    (0x00)
#define UART_CTL2_115K2    (0x8B)


#else 

#error "Unsupported crystal frequency"

#endif // (XTAL_FREQ_IN_HZ)

#define UART_CLOCK_ENABLE   0x80
#define UART_TX_ENABLE      0x40
#define UART_RX_ENABLE      0x20
#define UART_LSB_FIRST      0x10
#define UART_MSB_FIRST      0x00

#define UART_PARITY_NONE    0x00        // NO parity
#define UART_PARITY_ZERO    0x04        // ZERO parity
#define UART_PARITY_ODD     0x08        // ODD parity
#define UART_PARITY_EVEN    0x0C        // EVEN parity

#define UART_DATA_BIT_7     0x00        // 7 data bits
#define UART_DATA_BIT_8     0x02        // 8 data bits

#define UART_STOP_ONE       0x00        // 1 stop-bit
#define UART_STOP_TWO       0x01        // 2 stop-bit


//#define UART_HISIP_MODE   ( UART_LSB_FIRST | UART_PARITY_NONE | UART_DATA_BIT_8 | UART_STOP_ONE )
//#define UART_DVD_MODE     ( UART_LSB_FIRST | UART_PARITY_NONE | UART_DATA_BIT_8 | UART_STOP_ONE )
//#define UART_DVD_MODE     ( UART_LSB_FIRST | UART_PARITY_ODD | UART_DATA_BIT_8 | UART_STOP_TWO )
#define UART_BT_MODE       ( UART_LSB_FIRST | UART_PARITY_NONE | UART_DATA_BIT_8 | UART_STOP_ONE )
#define UART_TSI_MODE	  (UART_LSB_FIRST | UART_PARITY_NONE | UART_DATA_BIT_8 | UART_STOP_ONE)
#define UART_CGI_MODE	  (UART_LSB_FIRST | UART_PARITY_NONE | UART_DATA_BIT_8 | UART_STOP_ONE)
#define UART_NONE_MODE    (0)

#define UA0CTL0_MODE    (UART_TSI_MODE)	// TSI
#define UA1CTL0_MODE    (0)		// not used


#define uart_hw_start(device) do                                        \
{                                                                   \
   UA##device##CTL0 |= UART_CLOCK_ENABLE;             /* clock enabled */     \
   NOP();                                    /* wait 2 cycles see manual */  \
   NOP();                                                           \
                                                                    \
   UA##device##TXE= 1;                            /* transmit enabled */  \
   UA##device##RXE= 1;                            /* receive enabled */   \
} while (0)



#define uart_hw_stop(device) do                                       \
{                                                                   \
   UA##device##RXE= 0;                           /* receive disabled */  \
   UA##device##TXE= 0;                           /* transmit disabled */ \
   UA##device##PWR= 0;                           /* clock disabled */    \
} while (0)

#define uart_hw_set_mode(device,mode) do                                        \
{                                                                   \
   UA##device##CTL0 = 0x00;                      /* stop before changing mode */     \
   UA##device##CTL0 |= (mode & 0x1F);             /* set mode bits */     \
} while (0)



/**********************************************************************
 * Description: Init Uart0 (v850ES-SG2)
 *  Parameters: None
 *     Returns: None
 *********************************************************************/
#define uart_initialize_port_0() do \
{  \
   PFC3L.0= 0;     /* Alternate Function = UART */ \
   PFC3L.1= 0;     /* Alternate Function = UART */  \
   PMC3L.0= 1;     /* Alternate Function enabled */ \
   PMC3L.1= 1;     /* Alternate Function enabled */ \
/*   PF3L.0=  0;     /* CMOS output */ \
/*   PF3L.1=  0;     /* CMOS output */ \
   PF3L.0=  1;     /* Open Drain output */ \
   PF3L.1=  1;     /* Open Drain output */ \
   P3L.0= 0;       /* Set TxD to high*/ \
   P3L.1= 0;       /* Set RxD to high/needed? */ \
   PM3L.0= IO_OUT; /* set I/O direction     */ \
   PM3L.1= IO_IN;  /* set I/O direction     */ \
   INTF3.1 = 0 ; /*clear INTP7 edge dection*/ \
   INTR3.1 = 0;  /*clear INTP7 edge dection*/ \
} while (0)


/**********************************************************************
 * Description: Init Uart1 (v850ES-SG2)
 *  Parameters: None
 *     Returns: None
 *********************************************************************/
#define uart_initialize_port_1() do \
{ \
} while ( false )


/**********************************************************************
 * Description: Init Uart2 (v850ES-SG2)
 *  Parameters: None
 *     Returns: None
 *********************************************************************/

#define uart_initialize_port_2() do \
{ \
   PFC3H.0= 0;                          /* Alternate Function = UART */ \
   PFC3H.1= 0;                          /* Alternate Function = UART */ \
   PMC3H.0= 1;                          /* Alternate Function enabled */ \
   PMC3H.1= 1;                          /* Alternate Function enabled */ \
   PF3H.0=  1;                          /* Open drain output */ \
   PF3H.1=  1;                          /* Open drain output */ \
   P3H.0 = 0;                           /* Set TxD to high*/ \
   P3H.1 = 0;                           /* Set RxD to high/needed? */ \
   PM3H.0  = IO_OUT;                    /* set I/O direction     */ \
   PM3H.1  = IO_IN;                     /* set I/O direction     */ \
} while ( false )

#if SJ3_IS	// Zhao Zhigang added, no UART3 in 3335
/*added by steven_yi for UART3 port */
#define uart_initialize_port_3() \
{ \
     PM8.0   =  1;   /*setting not required*/ \
     PMC8.0 =  1;  /*alternate function = UART3 RX*/ \
     PM8. 1  = 1;   /*setting not required */ \
     PMC8.1 = 1;   /*alternate function = UART3 TX*/ \
     INTF8.0 = 0 ; /*clear INTP8 edge dection*/ \
     INTR8.0 = 0;  /*clear INTP8 edge dection*/ \
}while(false)
#endif	// Zhao Zhigang added




#define uart0_set_isr_state(state)     do {  UA0RIC = (state & UART_ISR_RX_ON ) ? 0x06 : 0x46; \
                                             UA0TIC = (state & UART_ISR_TX_ON ) ? 0x06 : 0x46; } while (false)


#define uart1_set_isr_state(state)     do {  UA1RIC = (state & UART_ISR_RX_ON ) ? 0x06 : 0x46; \
                                             UA1TIC = (state & UART_ISR_TX_ON ) ? 0x06 : 0x46; } while (false)


#define uart2_set_isr_state(state)     do {  UA2RIC = (state & UART_ISR_RX_ON ) ? 0x06 : 0x46; \
                                             UA2TIC = (state & UART_ISR_TX_ON ) ? 0x06 : 0x46; } while (false)
#if SJ3_IS	// Zhao Zhigang added, no UART3 in 3335
#define uart3_set_isr_state(state)    do{UA3RIC = (state & UART_ISR_RX_ON)? 0x06: 0x46; \
	                                      UA3TIC = (state & UART_ISR_TX_ON) ? 0x06: 0x46;}while(false)
#endif	// Zhao Zhigang added

#define UART_HW_BASE_ADDR     (0xfffffa00)  /* UARTs base register */
#define UART_HW_REGS_SIZE     (0x10)

#define UART0_ADDRESS         ( UART_HW_BASE_ADDR + (0 * UART_HW_REGS_SIZE) )   /* UARTA0 registers */
#define UART1_ADDRESS         ( UART_HW_BASE_ADDR + (1 * UART_HW_REGS_SIZE) )   /* UARTA1 registers */
#define UART2_ADDRESS         ( UART_HW_BASE_ADDR + (2 * UART_HW_REGS_SIZE) )   /* UARTA2 registers */
#define UART3_ADDRESS         (UART_HW_BASE_ADDR  + (3 * UART_HW_REGS_SIZE) )   /* UARTA3 registers */
#define UARTn_ADDRESS(n)      ( UART_HW_BASE_ADDR + (n * UART_HW_REGS_SIZE) )  


#define UART_ENTER_CRITICAL_SECTION()     Disable_Interrupts()
#define UART_LEAVE_CRITICAL_SECTION()     Enable_Interrupts()

#if  0
// This approach does not work with Desip/Sipprot ! */
#define UART_ENTER_CRITICAL_SECTION(device, oldState) do { \
                                                            oldState = UART_Get_Isr_State( device );  \
                                                            UART_Set_Isr_State( device, false );      \
                                                         }while ( 0 )

#define UART_LEAVE_CRITICAL_SECTION(device,oldState)  do {     \
                                                            UART_Set_Isr_State( device, oldState );  \
                                                         }while ( 0 )
#endif                                                         



/**********************************************************************
 * Enumerations and Structures and Typedefs
 *********************************************************************/
typedef struct uart_reg_init_baud_rate_tag
{
   uint8_t ctl1;  // Control register 1
   uint8_t ctl2;  // Control register 2
} uart_reg_init_baud_rate_t;


/**********************************************************************
 * Global and Const Variable Defining Definitions / Initializations
 *********************************************************************/
#if KW82_USED_IS
extern Uart_Rx_Func_Ret_T KW82_Recv_Handler( uint8_t data, uint8_t status );
#endif // KW82_USED_IS

/**********************************************************************
 * Static Variables and Const Variables With File Level Scope
 *********************************************************************/

STATIC_VAR uint8_t uart_rx_buf_channel_0[UART_RX_BUF_SIZE_0];  // Rx ring buffer for TSI
STATIC_VAR uint8_t uart_tx_buf_channel_0[UART_TX_BUF_SIZE_0];  // Tx ring buffer for TSI
 

STATIC_VAR uint8_t uart_rx_buf_channel_2[UART_RX_BUF_SIZE_2];  // Not use
STATIC_VAR uint8_t uart_tx_buf_channel_2[UART_TX_BUF_SIZE_2];  // Not use

// not used
#if (BLUETOOTH_IS || (External_Bluetooth_IS && M12_NAV_MODEL_IS))
STATIC_VAR uint8_t uart_rx_buf_channel_3[UART_RX_BUF_SIZE_3];  // Rx ring buffer
STATIC_VAR uint8_t uart_tx_buf_channel_3[UART_TX_BUF_SIZE_3];  // Tx ring buffer
#endif

static bool New_Msg_From_SBX = 1;

/**********************************************************************
 * ROM Const Variables With File Level Scope
 *********************************************************************/
#if __v850__
#pragma BEGIN_ROM_CONST

#endif // __v850__


/**
 * The following table contains register setting values for the 
 * supported baudrates. (NEC V850 ES-Sx2)
 */
STATIC_VAR const uart_reg_init_baud_rate_t uart_baud_rate_init_table[UART_NUM_SUPPORTED_BAUDRATES] =
{
    { UART_CTL1_5,      UART_CTL2_5 }
   ,{ UART_CTL1_200,    UART_CTL1_200 }
   ,{ UART_CTL1_9600,   UART_CTL1_9600 }
   ,{ UART_CTL1_115K2,  UART_CTL2_115K2 }
};

/**
 * The following table contains the configuration data for the used UART devices. 
 * These structure elements will be referenced in a further table.
 */ 
STATIC_VAR const uart_chan_init_data_t uart_channel_init_data_table[UART_NUM_DEVICE_CONFIGS] = 
{
   /* CGI interface */
   {
#define INDEX_CONFIG_TSI   0
      UART_DEVICE_TSI,
      &uart_rx_buf_channel_0[0],
      &uart_tx_buf_channel_0[0],
      Num_Elems(uart_rx_buf_channel_0),
      Num_Elems(uart_tx_buf_channel_0),
      { UARTn_ADDRESS(UART_DEVICE_TSI), UART_TSI_MODE, UART_CTL1_115K2, UART_CTL2_115K2, 0 },
      NULL
   }

   /* TSI uart interface*/
   ,{
#define INDEX_CONFIG_NONE  (INDEX_CONFIG_TSI+1)
      UART_DEVICE_NONE,
      &uart_rx_buf_channel_2[0],
      &uart_tx_buf_channel_2[0],
      Num_Elems(uart_rx_buf_channel_2),
      Num_Elems(uart_tx_buf_channel_2),
      { UARTn_ADDRESS(UART_DEVICE_NONE), UART_NONE_MODE, UART_CTL1_115K2, UART_CTL2_115K2, 0 },
      NULL
   }


#if (BLUETOOTH_IS || (External_Bluetooth_IS && M12_NAV_MODEL_IS))
   /* Bluetooth uart interface */
   ,{
#if DVDX_IS
#define INDEX_CONFIG_BT  (INDEX_CONFIG_DVD+1)  //changed for bluetooth
#else
#define INDEX_CONFIG_BT (INDEX_CONFIG_HISIP+1)
#endif
      UART_DEVICE_BT,
      &uart_rx_buf_channel_3[0],
      &uart_tx_buf_channel_3[0],
      Num_Elems(uart_rx_buf_channel_3),
      Num_Elems(uart_tx_buf_channel_3),
      #if BLUETOOTH_IS
      { UARTn_ADDRESS(UART_DEVICE_BT), UART_BT_MODE, UART_CTL1_115K2, UART_CTL2_115K2, 0 },
      #else 
      { UARTn_ADDRESS(UART_DEVICE_BT), UART_BT_MODE, UART_CTL1_38400, UART_CTL1_38400, 0 },
      #endif
      NULL
   }
#endif 
};


/**
 * The following table contains the references to the configuration data
 * for the 'real' UART devices. This level of further indirection is used 
 * to save memory for unused UART devices, e.g. UART 2 is used, UART 0 and 
 * UART 1 initialized only with NULL pointer instead of 'empty' data structure.
 */
STATIC_VAR const uart_chan_init_data_t *const uart_init_data_table[UART_NUM_DEVICE_CONFIGS] = 
{
    &uart_channel_init_data_table[INDEX_CONFIG_TSI]     /* CGI uart*/
    ,&uart_channel_init_data_table[INDEX_CONFIG_NONE]     /* TSI uart*/

#if (BLUETOOTH_IS || (External_Bluetooth_IS && M12_NAV_MODEL_IS))
    ,&uart_channel_init_data_table[INDEX_CONFIG_BT]       /* Bluetooth uart*/
#endif
};


STATIC_VAR uart_chan_t *const uart_chan[UART_NUM_HW_DEVICES] =
{
   &uart_chan_ram_data[0]    /* UART0 = CGI, not used */
   ,NULL                                  /* UART1 =  no use*/ 
   ,&uart_chan_ram_data[1] /* UART2 for TSI */   
#if 0	// Zhao Zhigang add
#if ((BLUETOOTH_IS ||(External_Bluetooth_IS && M12_NAV_MODEL_IS)) && DVDX_IS)               /*UART3 for Bluetooth*/ 
   , &uart_chan_ram_data[2]
#elif (BLUETOOTH_IS ||(External_Bluetooth_IS && M12_NAV_MODEL_IS))
   , &uart_chan_ram_data[1]
#else
   ,NULL
#endif 
#endif	// Zhao Zhigang add
};

#if __v850__
#pragma END_ROM_CONST

#endif // __v850__

   
/******************************************************************************
 * Function Prototypes for Private Functions with File Level Scope
 ******************************************************************************/
#if UART_CONF_USE_GET_ISR_STATE
static Uart_Isr_State_T uart0_get_isr_state( void );
static Uart_Isr_State_T uart1_get_isr_state( void );
static Uart_Isr_State_T uart2_get_isr_state( void );
#if SJ3_IS	// Zhao Zhigang added, no UART3 in 3335
static Uart_Isr_State_T uart3_get_isr_state ( void );

⌨️ 快捷键说明

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