sdc.c

来自「基于nucleus操作系统的GPRS无线数据传输终端全套源文件。包括支持ARM7」· C语言 代码 · 共 1,112 行 · 第 1/4 页

C
1,112
字号
/****************************************************************************/
/*                                                                          */
/*      Copyright (c) 2000 by Accelerated Technology, Inc.                  */
/*                                                                          */
/* PROPRIETARY RIGHTS of Accelerated Technology are involved in the subject */
/* matter of this material.  All manufacturing, reproduction, use and sales */
/* rights pertaining to this subject matter are governed by the license     */
/* agreement.  The recipient of this software implicity accepts the terms   */
/* of the license.                                                          */
/*                                                                          */
/****************************************************************************/
/****************************************************************************/
/*                                                                          */
/* FILENAME                                             VERSION             */
/*                                                                          */
/*  SDC.C                                           PLUS/KS32C41000 1.11.1  */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*  This file contains the Serial Driver specific functions.                */
/*                                                                          */
/* DATA STRUCTURES                                                          */
/*                                                                          */
/*  SD_PORT *       :   An array of pointers to serial port structures.     */
/*                                                                          */
/* FUNCTIONS                                                                */
/*                                                                          */
/*  SDC_Init_Port                                                           */
/*  SDC_Date_Ready                                                          */
/*  SDC_Put_String                                                          */
/*  SDC_LISR                                                                */
/*  SDC_Get_Char                                                            */
/*  SDC_Put_Char                                                            */
/*  SDC_Set_Baud_Rate                                                       */
/*                                                                          */
/* DEPENDENCIES                                                             */
/*                                                                          */
/*  nucleus.h                                                               */
/*  sd_defs.h                                                               */
/*  sd_extr.h                                                               */
/*   target.h                                                               */
/* protocol.h                                                               */
/*  externs.h                                                               */
/*      ppp.h                                                               */
/*                                                                          */
/* HISTORY                                                                  */
/*                                                                          */
/*         NAME         DATE                    REMARKS                     */
/*                                                                          */
/*    Bobby Iden    04-18-2000     Created initial version 1.11.1 for the   */
/*                                 the Samsung KS32C41000.                  */ 
/****************************************************************************/

#include "nucleus.h"
#include "sd_defs.h"
#include "sd_extr.h"

#ifdef PPP

#include "net\target.h"
//#include "net\inc\protocol.h"
#include "net\inc\externs.h"
#include "net\inc\tcp_errs.h"
#include "ppp\inc\ppp.h"

#endif

extern NU_MEMORY_POOL   System_Memory;

extern int INTERRUPTING_DEVICE;   /* Captured in INT_IRQ */

/* Define a small array to hold pointers to the two UART data
   structures. This is used by the LISR to find the correct
   data structure for the interrupt being handled. */
SD_PORT         *SDC_Port_List[SD_MAX_UARTS];

/* Define prototypes for functions local to this module. */

/* NOTE: For the ARM processor there is no Vector passed!  
** static  VOID    SDC_LISR(INT vector);                 */

static  VOID    SDC_Set_Baud_Rate(UNSIGNED, SD_PORT *);

/****************************************************************************/
/* FUNCTION                                                                 */
/*                                                                          */
/*    SDC_Init_Port                                                         */
/*                                                                          */
/* DESCRIPTION                                                              */
/*                                                                          */
/*    This function intializes the COM port that will be used for serial    */
/*    communications.                                                       */
/*                                                                          */
/* CALLED BY                                                                */
/*                                                                          */
/*    Application                                                           */
/*                                                                          */
/* CALLS                                                                    */
/*                                                                          */
/*    NU_Local_Control_Interrupts                                           */
/*    SDC_Set_Baud_Rate                                                     */
/*                                                                          */
/* INPUTS                                                                   */
/*                                                                          */
/*    SD_PORT *     :   device initialization structure.                    */
/*                                                                          */
/* OUTPUTS                                                                  */
/*                                                                          */
/*    STATUS        :   Returns NU_SUCCESS if successful initialization,    */
/*                      else a negative value is returned.                  */
/*                                                                          */
/****************************************************************************/
STATUS  SDC_Init_Port(SD_PORT *uart)
{

UNSIGNED    imr_val;             /* IRQ Mask register value */
STATUS      status = NU_SUCCESS;
CHAR        sem_name[8];
INT         int_level;          /* old interrupt level */
INT         tInt;
static INT  num_ports = 0;


    if (uart->communication_mode == SERIAL_MODE)
    {
		/* Check for max allowed UARTS. */
        if (num_ports >= SD_MAX_UARTS)
        {     
	        /* We have already initialized the max allowed UARTS. */
            status = NU_UART_LIST_FULL;
        }
    }
    
    if (status != NU_SUCCESS)
    {
        return (status);
    }
    /* Check the supplied parity */
    else if ((uart->parity != SD_PARITY_NONE) &&
             (uart->parity != SD_PARITY_EVEN) &&
             (uart->parity != SD_PARITY_ODD))
    {
        /* The supplied parity is not valid */
        status = NU_INVALID_PARITY;
    }
    /* Check the supplied number of data bits */
    else if ((uart->data_bits != SD_DATA_BITS_6) &&
             (uart->data_bits != SD_DATA_BITS_7) &&
             (uart->data_bits != SD_DATA_BITS_8))
    {
        /* The supplied data bits value is not valid */
        status = NU_INVALID_DATA_BITS;
    }
    /* Check the supplied number of stop bits */
    else if ((uart->stop_bits != SD_STOP_BITS_1) &&
             (uart->stop_bits != SD_STOP_BITS_2))
    {
        /* The supplied stop bits value is not valid */
        status = NU_INVALID_STOP_BITS;
    }
    /********************** Begin Port Specific Section *********************
    ** Note: This is port specific since not all UARTS have a mode setting. */

    /* Check the supplied mode */
    else if (uart->data_mode != SD_MODE_NORMAL)   
    {
        /* The supplied mode is not valid */
        status = NU_INVALID_DATA_MODE;
    }
    /**************** End Port Specific Section ****************/

    /* Verify the baud rate is within acceptable range */
    else if ((uart->baud_rate < 300) || (uart->baud_rate > 115200))
    {
        /* The baud rate is out of range */
        status = NU_INVALID_BAUD;
    }
    /************** Begin Port Specific Section ****************/
    /* Validate the com port. */
    else if ((uart->com_port == SD_UART1) ||
             (uart->com_port == SD_UART2))
    {
        /* Handle UART1 */
        if (uart->com_port == SD_UART1)
        {
            /* Set the base address for this UART. */
            uart->base_address = SD_UART1_BASE_OFFSET;
        }
        else    /* Otherwise handle UART2. */
        {
            /* Set the base address for this UART. */
            uart->base_address = SD_UART2_BASE_OFFSET;
        }
    }
    else /************** End Port Specific Section **************/
    {
        /* Not a supported port. */
        status = NU_INVALID_COM_PORT;
    }

    if (uart->communication_mode == SERIAL_MODE)
    {
   	    /* Make sure the port was valid. Then create the semaphore 
           used to make the SD_Put_String service thread safe. */
        if (status == NU_SUCCESS)
        {
		    /* Allocate memory for the semaphore control block. */
		    status = NU_Allocate_Memory ( &System_Memory,
                                          (VOID **) &uart->sd_semaphore,
                                          sizeof(NU_SEMAPHORE),
										  NU_NO_SUSPEND);

            /* Zero the semaphore structure */
            for(tInt=0; tInt < sizeof(NU_SEMAPHORE); tInt++)
            {
                SD_OUTBYTE((unsigned)uart->sd_semaphore + tInt, 0x00);
            }

			if (status == NU_SUCCESS)
			{
	            /* Build the name. */
				sem_name[0] = 's';
				sem_name[1] = 'e';
				sem_name[2] = 'r';
				sem_name[3] = 'i';
				sem_name[4] = 'a';
				sem_name[5] = 'l';
				sem_name[6] = '_';
				sem_name[7] = (CHAR)(0x30 + num_ports);

		        status = NU_Create_Semaphore (uart->sd_semaphore, sem_name,
		                                      1, NU_FIFO);
			}									
        }

        /* Make sure all the above was completed. Then store off this
           UART stucture and initialize the chip. */
        if (status == NU_SUCCESS)
        {
            SDC_Port_List[num_ports++] = uart;

        }
    }

	if (status == NU_SUCCESS)
	{
		/* Allocate memory for the data buffers. PPP only requires a TX
		   buffer so the allocation will be a little different for PPP mode. */
		if (uart->communication_mode == SERIAL_MODE)
		{
			status = NU_Allocate_Memory (&System_Memory,(VOID **)&uart->tx_buffer, 
										 (2 * uart->sd_buffer_size),
										 NU_NO_SUSPEND);
										
			/* Set the RX buffer to just past the TX buffer. */
			uart->rx_buffer = (CHAR *)(uart->tx_buffer + uart->sd_buffer_size);
		}
		else
		{
			status = NU_Allocate_Memory (&System_Memory,(VOID **)&uart->tx_buffer, 
										uart->sd_buffer_size,
										NU_NO_SUSPEND);
		}

		if (status == NU_SUCCESS)
		{
			/* Setup the RX SD buffer */
		    uart->rx_buffer_read = uart->rx_buffer_write = 0;
 	        uart->rx_buffer_status = NU_BUFFER_EMPTY;

			/* Setup the TX SD buffer */
    	    uart->tx_buffer_read = uart->tx_buffer_write = 0;
	        uart->tx_buffer_status = NU_BUFFER_EMPTY;
		}
	}
	
	if (status == NU_SUCCESS)
	{

⌨️ 快捷键说明

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