kwp2000_interface.c 5.37 KB
Newer Older
hu's avatar
hu committed
1 2 3 4

#include "kwp2000_interface.h"
#include "UART.h"

hu's avatar
hu committed
5
#define BOARD_TMBCLK_HZ 32000000
hu's avatar
hu committed
6 7 8 9 10

/* physical and functional addresses for the ECU */
unsigned char kwp2000_PhysicalAddress;
unsigned char kwp2000_FunctionalAddress;
unsigned char kwp2000_ServerAddress;
hu's avatar
hu committed
11
unsigned int kwp2000_P3_MAX_LIMIT;
hu's avatar
hu committed
12 13 14 15 16 17 18 19 20

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_SetAddress
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
21
void Kwp2000_SetAddress(void)
hu's avatar
hu committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35
{
    kwp2000_PhysicalAddress = KWP2000_ADDR_IC;
    kwp2000_FunctionalAddress = KWP2000_ADDR_OBD;
    kwp2000_ServerAddress = KWP2000_ADDR_SERVER;
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_SetTiming
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
36
void Kwp2000_SetTiming(void)
hu's avatar
hu committed
37 38 39 40 41 42
{
    kwp2000_P3_MAX_LIMIT = KWP2000_P3_MAX;
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscDisableRx
hu's avatar
hu committed
43
* Description    : 禁止收 使能发
hu's avatar
hu committed
44 45 46 47 48
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
49
void Kwp2000_AscDisableRx(void)
hu's avatar
hu committed
50 51 52 53 54 55 56
{
    Uart_DisableRx();
    Uart_EnableTx();
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscEnableRx
hu's avatar
hu committed
57
* Description    : 使能接收功能
hu's avatar
hu committed
58 59 60 61 62
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
63
void Kwp2000_AscEnableRx(void)
hu's avatar
hu committed
64 65 66 67 68 69 70 71
{
    /* enable receiver, clear all error flags */
    Uart_ClearAllErrorFlag();
    Uart_EnableRx();
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscTx
hu's avatar
hu committed
72
* Description    : 发送数据字节
hu's avatar
hu committed
73 74 75 76 77
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
78
void Kwp2000_AscTx(unsigned char *xBuffer_pu8)
hu's avatar
hu committed
79 80 81 82 83 84 85 86 87 88 89 90
{
    Uart_SetData(*xBuffer_pu8);
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscStart
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
91
void Kwp2000_AscStart(void)
hu's avatar
hu committed
92 93 94 95 96 97 98 99 100 101 102
{
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscStop
* Description    :
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
103
void Kwp2000_AscStop(void)
hu's avatar
hu committed
104 105 106 107 108
{
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscInit
hu's avatar
hu committed
109
* Description    : 串口模块初始化
hu's avatar
hu committed
110 111 112 113 114
* Input          :
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
hu's avatar
hu committed
115
void Kwp2000_AscInit(void)
hu's avatar
hu committed
116
{
hu's avatar
hu committed
117
    Kwp2000_AscStop(); // disable ASC_A mode
hu's avatar
hu committed
118 119

    /* disable interrupt service request, clear service request flags */
hu's avatar
hu committed
120
    /*预留设置*/
hu's avatar
hu committed
121 122 123 124 125 126 127

    /* enable receiver and transmitter function*/
    Uart_EnableRx();
    Uart_EnableTx();
    TJA1021_Enable(1);

    /* disable transmitter interrupt */
hu's avatar
hu committed
128
    // Uart_DisableTxInterrupt();  /*----20220309----*/
hu's avatar
hu committed
129 130 131 132 133 134 135 136 137

    ///  -----------------------------------------------------------------------
    ///  Configuration of the ASC0 Operation Mode:
    ///  -----------------------------------------------------------------------
    Uart_ConfigOperationMode();

    ///  -----------------------------------------------------------------------
    ///  Configuration of the used ASC0 Port Pins:
    ///  -----------------------------------------------------------------------
hu's avatar
hu committed
138 139
    Uart_SetTxGpio(); // TX
    Uart_SetRxGpio(); // RX
hu's avatar
hu committed
140 141 142 143

    ///  -----------------------------------------------------------------------
    ///  Configuration of priority of SCI_A interrupt:
    ///  -----------------------------------------------------------------------
hu's avatar
hu committed
144
    /*预留优先级设置*/
hu's avatar
hu committed
145 146 147 148 149 150 151 152 153 154 155 156

    /* Configuration of Baudrate , as 10400bps */
    Kwp2000_AscSwitchBaudrate(KWP2000_DEFAULT_BAUDRATE_KLINE_DU);

    Kwp2000_AscStart();

    /* clear service request flags */
    Uart_ClearRxFullFlag();
}

/*-------------------------------------------------------------------------
* Function Name  : Kwp2000_AscSwitchBaudrate
hu's avatar
hu committed
157 158
* Description    : 设置波特率
* Input          : xBaudrate_u32 波特率数值
hu's avatar
hu committed
159 160 161 162 163 164 165
* Output         : None
* Return         : None
* onther         :
--------------------------------------------------------------------------*/
void Kwp2000_AscSwitchBaudrate(unsigned long xBaudrate_u32)
{
    /* if configuration is valid, ... */
hu's avatar
hu committed
166
    if (xBaudrate_u32 != 0ul)
hu's avatar
hu committed
167 168 169 170 171 172 173 174 175
    {
        Kwp2000_AscStop();

        Uart_SetBaudrate(xBaudrate_u32);

        /* start ASC */
        Kwp2000_AscStart();
    }
}