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

📄 kitlserial.c

📁 freescale i.mx31 BSP CE5.0全部源码
💻 C
字号:
//-----------------------------------------------------------------------------
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//------------------------------------------------------------------------------
//
//  Copyright (C) 2004, Freescale Semiconductor, Inc. All Rights Reserved
//  THIS SOURCE CODE IS CONFIDENTIAL AND PROPRIETARY AND MAY NOT
//  BE USED OR DISTRIBUTED WITHOUT THE WRITTEN PERMISSION OF
//  FREESCALE SEMICONDUCTOR, INC.
//
//------------------------------------------------------------------------------

//------------------------------------------------------------------------------
//
// FILE:    kitlserial.c
//
// PURPOSE: This module provides the OAL functions for supporting a
//          serial tranport for KITL.
//
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// INCLUDE FILES
//-----------------------------------------------------------------------------
#include <windows.h>
#include <nkintr.h>
#include <oal.h>
#include "mxarm11.h"

//-----------------------------------------------------------------------------
// EXTERNAL FUNCTIONS
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// GLOBAL DEFINITIONS
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// GLOBAL VARIABLES
//-----------------------------------------------------------------------------
static PCSP_UART_REG g_pUART;


//-----------------------------------------------------------------------------
// STATIC FUNCTIONS
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
//
// FUNCTION:    SerialInit
//
// DESCRIPTION:
//      Initializes the interal UART with the specified communication settings.
//
// PARAMETERS:
//      pSerInfo
//          [in/out] Pointer to KITL_SERIAL_INFO structure that contains
//          information about how to initialize the serial KITL transport.
//
// RETURNS:
//      If this function succeeds, it returns TRUE, otherwise
//      it returns FALSE.
//
//-----------------------------------------------------------------------------
BOOL SerialInit(KITL_SERIAL_INFO *pSerInfo)
{
    // Get pointer to peripheral base
    g_pUART = (PCSP_UART_REG) pSerInfo->pAddress;

    if (g_pUART == NULL) return FALSE;

    // Tell KITL upper layers that optimal tranfer size is TXFIFO size
    pSerInfo->bestSize = UART_TXFIFO_DEPTH;

    OUTREG16(&g_pUART->UCR2,
        CSP_BITFVAL(UART_UCR2_SRST, UART_UCR2_SRST_RESET) |
        CSP_BITFVAL(UART_UCR2_RXEN, UART_UCR2_RXEN_ENABLE) |
        CSP_BITFVAL(UART_UCR2_TXEN, UART_UCR2_TXEN_ENABLE) |
        CSP_BITFVAL(UART_UCR2_ATEN, UART_UCR2_ATEN_DISABLE) |
        CSP_BITFVAL(UART_UCR2_RTSEN, UART_UCR2_RTSEN_DISABLE) |
        CSP_BITFVAL(UART_UCR2_WS, UART_UCR2_WS_8BIT) |
        CSP_BITFVAL(UART_UCR2_STPB, UART_UCR2_STPB_1STOP) |
        CSP_BITFVAL(UART_UCR2_PROE, UART_UCR2_PROE_EVEN) |
        CSP_BITFVAL(UART_UCR2_PREN, UART_UCR2_PREN_DISBLE) |
        CSP_BITFVAL(UART_UCR2_RTEC, UART_UCR2_RTEC_RISEDGE) |
        CSP_BITFVAL(UART_UCR2_ESCEN, UART_UCR2_ESCEN_DISABLE) |
        CSP_BITFVAL(UART_UCR2_CTS, UART_UCR2_CTS_HIGH) |
        CSP_BITFVAL(UART_UCR2_CTSC, UART_UCR2_CTSC_RXCTRL) |
        CSP_BITFVAL(UART_UCR2_IRTS, UART_UCR2_IRTS_USERTS) |
        CSP_BITFVAL(UART_UCR2_ESCI, UART_UCR2_ESCI_DISABLE));

    // Software reset clears RX/TX state machines, FIFOs, and all status bits
    // which means all interrupts will be cleared

    // Wait until UART comes out of reset (reset asserted via UCR2 SRST)
    while (!(INREG16(&g_pUART->UCR2) & CSP_BITFMASK(UART_UCR2_SRST)));

    OUTREG16(&g_pUART->UCR1,
        CSP_BITFVAL(UART_UCR1_UARTEN, UART_UCR1_UARTEN_ENABLE) |
        CSP_BITFVAL(UART_UCR1_DOZE, UART_UCR1_DOZE_ENABLE) |
        CSP_BITFVAL(UART_UCR1_TDMAEN, UART_UCR1_TXDMAEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_SNDBRK, UART_UCR1_SNDBRK_NOBREAK) |
        CSP_BITFVAL(UART_UCR1_RTSDEN, UART_UCR1_RTSDEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_TXMPTYEN, UART_UCR1_TXMPTYEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_IREN, UART_UCR1_IREN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_RDMAEN, UART_UCR1_RXDMAEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_RRDYEN, UART_UCR1_RRDYEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_ICD, UART_UCR1_ICD_8FRAMES) |
        CSP_BITFVAL(UART_UCR1_IDEN, UART_UCR1_IDEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_TRDYEN, UART_UCR1_TRDYEN_DISABLE) |
        CSP_BITFVAL(UART_UCR1_ADBR, UART_UCR1_ADBR_DISABLE) |
        CSP_BITFVAL(UART_UCR1_ADEN, UART_UCR1_ADEN_DISABLE));

    OUTREG16(&g_pUART->UCR3,
        CSP_BITFVAL(UART_UCR3_ACIEN, UART_UCR3_ACIEN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_INVT, UART_UCR3_INVT_ACTIVELOW) |
        CSP_BITFVAL(UART_UCR3_RXDMUXSEL, UART_UCR3_RXDMUXSEL_NOTMUX) |
        CSP_BITFVAL(UART_UCR3_DTRDEN, UART_UCR3_DTRDEN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_AWAKEN, UART_UCR3_AWAKEN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_AIRINTEN, UART_UCR3_AIRINTEN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_RXDSEN, UART_UCR3_RXDSEN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_RI, UART_UCR3_RI_LOW) |
        CSP_BITFVAL(UART_UCR3_DCD, UART_UCR3_DCD_LOW) |
        CSP_BITFVAL(UART_UCR3_DSR, UART_UCR3_DSR_LOW) |
        CSP_BITFVAL(UART_UCR3_FRAERREN, UART_UCR3_FRAERREN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_PARERREN, UART_UCR3_PARERREN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_DTREN, UART_UCR3_DTREN_DISABLE) |
        CSP_BITFVAL(UART_UCR3_DPEC, UART_UCR3_DPEC_RISEDGE));

    OUTREG16(&g_pUART->UCR4,
        CSP_BITFVAL(UART_UCR4_DREN, UART_UCR4_DREN_ENABLE) |
        CSP_BITFVAL(UART_UCR4_OREN, UART_UCR4_OREN_DISABLE) |
        CSP_BITFVAL(UART_UCR4_BKEN, UART_UCR4_BKEN_DISABLE) |
        CSP_BITFVAL(UART_UCR4_TCEN, UART_UCR4_TCEN_DISABLE) |
        CSP_BITFVAL(UART_UCR4_LPBYP, UART_UCR4_LPBYP_DISABLE) |
        CSP_BITFVAL(UART_UCR4_IRSC, UART_UCR4_IRSC_SAMPCLK) |
        CSP_BITFVAL(UART_UCR4_WKEN, UART_UCR4_WKEN_DISABLE) |
        CSP_BITFVAL(UART_UCR4_ENIRI, UART_UCR4_ENIRI_DISABLE) |
        CSP_BITFVAL(UART_UCR4_INVR, UART_UCR4_INVR_ACTIVELOW) |
        CSP_BITFVAL(UART_UCR4_CTSTL, 24));

    OUTREG16(&g_pUART->UFCR,
        CSP_BITFVAL(UART_UFCR_RXTL, 16) |
        CSP_BITFVAL(UART_UFCR_DCEDTE, UART_UFCR_DCEDTE_DCE) |
        CSP_BITFVAL(UART_UFCR_RFDIV, UART_UFCR_RFDIV_DIV6) |
        CSP_BITFVAL(UART_UFCR_TXTL, 4));

    OUTREG16(&g_pUART->UTS,
        CSP_BITFVAL(UART_UTS_RXDBG, UART_UTS_RXDBG_NOINCREMENT) |
        CSP_BITFVAL(UART_UTS_LOOPIR, UART_UTS_LOOPIR_NOLOOP) |
        CSP_BITFVAL(UART_UTS_DBGEN, UART_UTS_DBGEN_DEBUG) |
        CSP_BITFVAL(UART_UTS_LOOP, UART_UTS_LOOP_NOLOOP) |
        CSP_BITFVAL(UART_UTS_FRCPERR, UART_UTS_FRCPERR_NOERROR));

    // Set default baud rate
    // TODO:  Need to program baud rate correctly once
    OUTREG16(&g_pUART->UBIR, 0);

    OUTREG16(&g_pUART->UBMR, 0);

    return TRUE;
}


//-----------------------------------------------------------------------------
//
// FUNCTION:    SerialDeinit
//
// DESCRIPTION:
//      Deinitializes the internal UART previously configured with SerialInit.
//
// PARAMETERS:
//      None.
//
// RETURNS:
//      If this function succeeds, it returns TRUE, otherwise
//      it returns FALSE.
//
//-----------------------------------------------------------------------------
VOID SerialDeinit(void)
{
}


//-----------------------------------------------------------------------------
//
// FUNCTION:    SerialRecv
//
// DESCRIPTION:
//      This function is used by KITL protocol to send a packet/frame to the
//      desktop KITL transport.
//
// PARAMETERS:
//      pch
//          [in] Pointer to buffer used store received data.
//
//      pcbRead
//          [in, out] Set to the size of the buffer when called and then
//          reset within this function to the size of data written to the
//          buffer.  
//
// RETURNS:  
//      If no receive errors are encountered and at least one data value is
//      received, then this function returns TRUE; otherwise, FALSE is 
//      returned.
//
//-----------------------------------------------------------------------------
BOOL SerialRecv(UINT8 *pch, USHORT *pcbRead)
{    
    UINT16 urxd;
    USHORT cbRead;

    // read until buffer size is reached or an error occurs
    for(cbRead = 0; cbRead < *pcbRead; cbRead++)
    {           
        if (!(INREG16(&g_pUART->USR2) & CSP_BITFMASK(UART_USR2_RDR)))
            break;
        
        // read char from FIFO
        urxd = INREG16(&g_pUART->URXD);

        // If error detected in current character
        if (urxd & CSP_BITFMASK(UART_URXD_ERR))
        {
            KITLOutputDebugString("KitlSerialRecvRaw:  receive error\n");
            cbRead= 0;
            break;
        }
        
        // Place read char into buffer
        *(pch + cbRead) = (urxd & CSP_BITFMASK(UART_URXD_RX_DATA));
    }

    // Update ouput parameter for chars read
    *pcbRead = cbRead;

    // KITLOutputDebugString("<");

    // If we ready any characters successfully, return TRUE
    if (cbRead) return TRUE;
    
    return FALSE;
}


//-----------------------------------------------------------------------------
//
// FUNCTION:    SerialSend
//
// DESCRIPTION: 
//      This function is used by KITL protocol to send a packet/frame to the 
//      desktop KITL transport.
//
// PARAMETERS:
//      pch 
//          [in] Pointer to buffer holding data to be transmitted.
//
//      cbWrite 
//          [in, out] Set to the size of the buffer when called and then 
//          reset by this function to the size of data written from the 
//          pbBuffer buffer.  
//
// RETURNS:  
//      Returns TRUE.
//
//-----------------------------------------------------------------------------
BOOL SerialSend(UINT8 *pch, USHORT cbWrite)
{
    // Block until send is complete; no timeout
    for( ; cbWrite > 0; --cbWrite)
    {
        // Wait until there is room in the FIFO
        while(INREG16(&g_pUART->UTS) & CSP_BITFMASK(UART_UTS_TXFULL));

        // Write character to port
        OUTREG16(&g_pUART->UTXD, *pch);

        // Point to next character
        ++pch;
    }

    // KITLOutputDebugString(">");
    
    return TRUE;
}


VOID SerialSendComplete(UINT16 size)
{
}



//-----------------------------------------------------------------------------
//
// FUNCTION:    SerialEnableInts
//
// DESCRIPTION: 
//      This function enables serial interrupts used for the KITL transport.
//
// PARAMETERS:
//      None.
//
// RETURNS:  
//      None.
//
//-----------------------------------------------------------------------------
VOID SerialEnableInts(void)
{
}


//-----------------------------------------------------------------------------
//
// FUNCTION:    SerialDisableInts
//
// DESCRIPTION: 
//      This function disables serial interrupts used for the KITL transport.
//
// PARAMETERS:
//      None.
//
// RETURNS:  
//      None.
//
//-----------------------------------------------------------------------------
VOID SerialDisableInts(void)
{
}


//-----------------------------------------------------------------------------
// PRIVATE FUNCTIONS
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// END OF FILE
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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