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

📄 uart_interface.h

📁 freescale的基于802.15.4的无线通讯例程
💻 H
字号:
/*****************************************************************************
* This file contains UART driver API prototypes
*
*
* (c) Copyright 2006, Freescale, Inc. All rights reserved.
*
* Freescale Confidential Proprietary
*
* No part of this document must be reproduced in any form - including copied,
* transcribed, printed or by any electronic means - without specific written
* permission from Freescale Semiconductor Danmark A/S.
*****************************************************************************/
#ifndef _UART_INTERFACE_H_
#define _UART_INTERFACE_H_

#ifndef gUART_Enabled_d
/* Typically set in target compile switches, but if not
   we define a default value. Set to 1 enabled or 0 for
   disabled (Boolean) */
#define gUART_Enabled_d   TRUE
#endif /* gUART_Enabled_d */

#include "IrqControlLib.h"
#include "EmbeddedTypes.h"
#include "PortConfig.h"
#include "AppToPlatformConfig.h"
#include "TS_Interface.h"

/*****************************************************************************
******************************************************************************
* Global macros
******************************************************************************
*****************************************************************************/

/* Various constants for UART settings, do not touch */
#define gUART_Setting8DataBits_c  0
#define gUART_Setting9DataBits_c  (1 << 4)
#define gUART_SettingParityOff_c  0
#define gUART_SettingParityOn_c   (1 << 1)
#define gUART_SettingOddParity_c  0
#define gUART_SettingEvenParity_c (1 << 0)

/* The length of FIFO buffer */  
#define gUART_FIFOLen_c           16      
/* Level in FIFO at which to notify the UART task to empty the FIFO
   This value must be less than or equal to gUART_FIFOLen_c */
#define gUART_FIFOWaterMark_c     8                                         
/* If no data is received in gRx_Timeout_c ms call the callback function
   This value must be from 1 to 1048576 */                                    
#define gUART_RxTimeout_c         100     
/* The fixed timer ID for the UART timer. */
#define gUART_TimerID_c           (gApplicationTimerIDOffset_c + 4)
/* Set to 1 to use HW flow control. If you want to
   use no flow control, set to 0 (Boolean) */
#define gUART_UseHWFlowControl_d  1
/* Set to 1 to enable verification of parameters, 0 to disable (Boolean) */
#define gUART_Verification_d      1
/* Set to 1 to enable reinitialization of UART */
#define gUART_ReinitEnabled_d     1
/* UART settings: */
/* 0 = 1 start bit, 8 data bits, 1 stop bit, no parity  */
/*     Set bit 4 to get 9 data bits, else 8 data bits   */
/*     Set bit 1 to enable parity, else disabled        */
/*     Set bit 0 to get odd parity, else even parity    */
/*     See compile options gUART_SettingXYZ_c above     */
#define gUART_Settings_c          0

/* 
  Set up which SCI port to use for UART functionality
  
  For MC1319x EVB target the following applies:
    SCI1 = UART on RS232 connector
    SCI2 = UART on USB
  For MC1319x SARD target the following applies:
    SCI2 = UART on RS232 connector (only choice)
  For MC1321x NCB  target the following applies:
    SCI1 = UART on RS232 connector
    SCI2 = UART on USB
  For MC1321x SRB target the following applies:
    SCI2 = UART on USB (only choice)
  For Axiom GB 60 target the following applies: 
    SCI?? = UART on RS232 connector
*/

/* If no choice has been made regarding which SCI to use */
#ifndef gUART_OnSCI1_d
#ifndef gUART_OnSCI2_d

/* Set default SCI port for UART functionality */
#define gUART_OnSCI1_d     FALSE /* Boolean */
#define gUART_OnSCI2_d     TRUE /* Boolean */

#endif /* gUART_OnSCI1_d */
#endif /* gUART_OnSCI2_d */

#if ( (gUART_OnSCI1_d != 0) && (gUART_OnSCI2_d != 0) )
#error Only one UART supported at a time
#endif

/*****************************************************************************/ 
/* Initial settings for both SCI's                                           */
/*****************************************************************************/

#define gUART_InitSCIxC1_c    gUART_Settings_c
#define gUART_InitSCIxC2_c    0x0C /* Tx & Rx enabled */
#define gUART_InitSCIxC3_c    0x00 /* Disable error interrupts etc. */
#define gUART_TxInterrupt_c   0x80 /* Enable/Disable transmit data  */
#define gUART_DataRegMask_c   0x20 /* Data Rx */
#define gUART_DataRegEmpty_c  0x00

/*****************************************************************************/
/* Request types to the UART driver                                          */
/*****************************************************************************/ 
enum {  
  gInitUart_c = 1,	                        /* Start UART initialization */
  gRxUart_c,								/* Activate serial receive */
  gTxUart_c,								/* Activate serial transmit */
};
 
/*****************************************************************************/
/* UART identifiers. The actual value is unimportant.                        */
/*****************************************************************************/ 
#define gUART_Id1_c  1
#define gUART_Id2_c  2
 
/*****************************************************************************/
/* Return values to be handled by the UART callback function                 */
/*****************************************************************************/
enum {  
  gUartTxSuccess_c   = 0x01, /* Success Tx */
  gUartRxSuccess_c   = 0x02, /* Success Rx */
  gUartRxTimeout_c   = 0x04, /* Timeout occurred */
  gUartReinit_c      = 0x08, /* UART was reinitialized */
  gUartRxTxUnknown_c = 0x10	 /* Unknown error */			
};
  
/*****************************************************************************/ 
/* UART Request return values                                                */											
/*****************************************************************************/
enum {
  gIrpAccepted_c = 0, /* Request will be executed */
  gIrpInvalidReq_c,   /* Invalid and unknown request */
  gIrpInvalidArg_c,   /* Invalid parameters */
  gIrpNotReady_c      /* Not ready, already busy */
};

/*****************************************************************************/ 
/* Baud rates                                                                */
/*****************************************************************************/

#if !((defined gSystemClock8MHz_d) || (defined gSystemClock12MHz_d) || \
      (defined gSystemClock16MHz_d) || (defined gSystemClock16780MHz_d))
#define gSystemClock8MHz_d     0
#define gSystemClock12MHz_d    0
#define gSystemClock16MHz_d    1 /* Default assumed system clock */
#define gSystemClock16780MHz_d 0
#endif 

/* SCI baud rate = BUSCLK/(16?Baud rate) */

/* Note that not all baud rates works for all clock rates, the actual baud rate
   achieved may deviate too much from the RS232 spec. requirements */ 
      
#if (gSystemClock8MHz_d == 1)
#define gUARTBaudRate1200_c   (0x01A1)
#define gUARTBaudRate2400_c   (0x00D0)
#define gUARTBaudRate4800_c   (0x0068)
#define gUARTBaudRate9600_c   (0x0034)
#define gUARTBaudRate19200_c  (0x001A)
#define gUARTBaudRate38400_c  (0x0011)
#define gUARTBaudRate57600_c  (0x0009)
#define gUARTBaudRate115200_c (0x0004) /* May not work for all clients */
#endif /* gSystemClock8MHz_d */

#if (gSystemClock12MHz_d == 1)
#define gUARTBaudRate1200_c   (0x0271)
#define gUARTBaudRate2400_c   (0x0139)
#define gUARTBaudRate4800_c   (0x009C)
#define gUARTBaudRate9600_c   (0x004E)
#define gUARTBaudRate19200_c  (0x0027)
#define gUARTBaudRate38400_c  (0x0014)
#define gUARTBaudRate57600_c  (0x000D)
#define gUARTBaudRate115200_c (0x0007)
#endif /* gSystemClock12MHz_d */

#if (gSystemClock16MHz_d == 1)
#define gUARTBaudRate1200_c   (0x0341)
#define gUARTBaudRate2400_c   (0x01A1)
#define gUARTBaudRate4800_c   (0x00D0)
#define gUARTBaudRate9600_c   (0x0068)
#define gUARTBaudRate19200_c  (0x0034)
#define gUARTBaudRate38400_c  (0x001A)
#define gUARTBaudRate57600_c  (0x0011)
#define gUARTBaudRate115200_c (0x0009) /* May not work for all clients */
#endif /* gSystemClock16MHz_d */

#if (gSystemClock16780MHz_d == 1)
#define gUARTBaudRate1200_c   (0x036A)
#define gUARTBaudRate2400_c   (0x01B5)
#define gUARTBaudRate4800_c   (0x00DA)
#define gUARTBaudRate9600_c   (0x006D)
#define gUARTBaudRate19200_c  (0x0037)
#define gUARTBaudRate38400_c  (0x001B)
#define gUARTBaudRate57600_c  (0x0012)
#define gUARTBaudRate115200_c (0x0009)
#endif /* gSystemClock16780MHz_d */

/*****************************************************************************/ 
/* PIN out for HCS08                                                         */											
/*****************************************************************************/

#if ( ( gUART_OnSCI1_d == 1 ) && ( gUART_OnSCI2_d == 0 ) )

#define SCIxBDH		SCI1BDH
#define SCIxBDL		SCI1BDL
#define SCIxC1		SCI1C1
#define SCIxC2		SCI1C2
#define SCIxC3		SCI1C3
#define SCIxD		  SCI1D
#define SCIxS1		SCI1S1

#if (gUART_UseHWFlowControl_d == 1)
#if (gTarget_UserDefined_d == 1)
#define EnableFlow()  { gSCI1RTSPort_c &= ~gSCI1RTSPin_c; }
#define DisableFlow() { gSCI1RTSPort_c |= gSCI1RTSPin_c; } 
#else
#define EnableFlow()  { PTAD &= 0xFD; }
#define DisableFlow() { PTAD |= 0x02; }
#endif /* gTarget_UserDefined_d */
#else
#define EnableFlow()
#define DisableFlow()
#endif /* gUART_UseHWFlowControl_d */

#elif ( ( gUART_OnSCI2_d == 1 ) && ( gUART_OnSCI1_d == 0 ) )

#define SCIxBDH		SCI2BDH
#define SCIxBDL		SCI2BDL
#define SCIxC1		SCI2C1
#define SCIxC2		SCI2C2
#define SCIxC3		SCI2C3
#define SCIxD     SCI2D
#define SCIxS1		SCI2S1

#if (gUART_UseHWFlowControl_d == 1)

#if (gTarget_UserDefined_d == 1)
#define EnableFlow()  { gSCI2RTSPort_c &= ~gSCI2RTSPin_c; }
#define DisableFlow() { gSCI2RTSPort_c |= gSCI2RTSPin_c; }
#else
#define EnableFlow()  { PTAD &= 0xBF; }
#define DisableFlow() { PTAD |= 0x40; }
#endif /* gTarget_UserDefined_d */
#else
#define EnableFlow()
#define DisableFlow()
#endif /* gUART_UseHWFlowControl_d */

#endif

/*****************************************************************************/ 
/* Irp structure for sending requests                                        */											
/*****************************************************************************/  
typedef struct irp_tag {
  union {
    struct {      
      uint8_t uartId;    /* Set to gUART_Id1_c or gUART_Id2_c */
      uint8_t *pBuffer;  /* Pointer to the buffer to use */
      uint8_t len;       /* The number of characters in above buffer */
      /* Pointer to the procedure to call when finished
         (Use pointer to the same IRP in call) */
      void (*pfCallback)(struct irp_tag *); 
    } rxtxParams;
    struct {
      uint8_t   uartId;  /* Set to gUART_Id1_c or gUART_Id2_c */
      uint16_t  baudRate;
    } initParams;
  } reqData;
  uint8_t   returnValue; /* The returned status */
  uint8_t   requestType; /* Function type called */
} irp_t;
  
/*****************************************************************************
******************************************************************************
* Public prototypes
******************************************************************************
*****************************************************************************/

/*****************************************************************************
* Handle the app requests. 
*
* Interface assumptions:
*
* Arguments:
*   pIrp - pointer to IRP structure. The data pointed to by this data
*          structure MUST be placed on the heap and NOT on the stack.
*
* Return value:
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
uint8_t UART_Request(irp_t *pIrp);

/*****************************************************************************
* The UART task that drives copying of data, manages timeouts etc. 
*
* Interface assumptions:
*
* Arguments:
*   events - The event mask sent to the UART task. 
*
* Return value:
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
void UART_Main(event_t events);

/*****************************************************************************
* When transmit interrupt is enabled this ISR will execute
* and pumps data out through SCIx Tx pin. 
*
* Interface assumptions:
* 
* Return value:
* None
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
INTERRUPT_KEYWORD void UART_SCITxIsr(void);

/*****************************************************************************
* When data is received in SCIxD ISR will be called
* this routine puts SCIxD into Rx FIFO.
*
* Interface assumptions:
* 
* Return value:
* None
* 
* Revision history:
* date   Author   Comments
* ------ ------   --------
* 240406 AL,FSL  Updated
*****************************************************************************/
INTERRUPT_KEYWORD void UART_SCIRxIsr(void);

#if (gUART_Enabled_d == 1)
/* Set up the ISR routines as used in IsrVectors.c */
#if (gUART_OnSCI1_d == 0)
#define UART_SCI1RxIsr   Default_Dummy_ISR
#define UART_SCI1TxIsr   Default_Dummy_ISR
#else
#define UART_SCI1RxIsr   UART_SCIRxIsr
#define UART_SCI1TxIsr   UART_SCITxIsr
#endif /* gUART_OnSCI1_d */

#if (gUART_OnSCI2_d == 0)
#define UART_SCI2RxIsr   Default_Dummy_ISR
#define UART_SCI2TxIsr   Default_Dummy_ISR
#else
#define UART_SCI2RxIsr   UART_SCIRxIsr
#define UART_SCI2TxIsr   UART_SCITxIsr
#endif /* gUART_OnSCI2_d */  

#else /* gUART_Enabled_d */

/* Put in dummy ISR as the UART module is not used at all */
#define UART_SCI1RxIsr   Default_Dummy_ISR
#define UART_SCI1TxIsr   Default_Dummy_ISR
#define UART_SCI2RxIsr   Default_Dummy_ISR
#define UART_SCI2TxIsr   Default_Dummy_ISR

#endif /* gUART_Enabled_d */

#endif _UART_INTERFACE_H_

⌨️ 快捷键说明

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