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

📄 wirelessuart.c

📁 关于zigbee厂家jennic的zigbee通信模块JN5139的一些示例程序。
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *
 * MODULE:             Wireless Uart code
 *
 * COMPONENT:          WirelessUart.c,v
 *
 * VERSION:
 *
 * REVISION:           1.15
 *
 * DATED:              2007/01/03 15:18:25
 *
 * STATUS:             Exp
 *
 * AUTHOR:             GPfef
 *
 * DESCRIPTION:
 *
 *
 * LAST MODIFIED BY:   gpfef
 *                     $Modtime: $
 *
 ****************************************************************************
 *
 * This software is owned by Jennic and/or its supplier and is protected
 * under applicable copyright laws. All rights are reserved. We grant You,
 * and any third parties, a license to use this software solely and
 * exclusively on Jennic products. You, and any third parties must reproduce
 * the copyright and warranty notice and any other legend of ownership on each
 * copy or partial copy of the software.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS". JENNIC MAKES NO WARRANTIES, WHETHER
 * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE,
 * ACCURACY OR LACK OF NEGLIGENCE. JENNIC SHALL NOT, IN ANY CIRCUMSTANCES,
 * BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL,
 * INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER.
 *
 * Copyright Jennic Ltd 2005, 2006. All rights reserved
 *
 ****************************************************************************/

/****************************************************************************/
/***        Include files                                                 ***/
/****************************************************************************/
#include "jendefs.h"
#include <AppHardwareApi.h>
#include <string.h>
#include "Button.h"
#include "LedControl.h"
#include "gdb.h"
#include "JZ_Api.h"
#include "af.h"
#include "aps.h"
#include "nwk.h"
#include "zdp.h"

#include "WirelessUartProfile.h"
#include "serialq.h"
#include "uart.h"
#include "serial.h"

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/
/* number of spaces in hardware event queue */
#define HW_INT_Q_SIZE          		32
#define HW_INT_Q_PTR_MASK      		0x1f

/* Wireless UART device data */
#define MAX_DATA_PER_FRAME        	64

/* number of 10ms intervals which may elapse while waiting for match
descriptor response, before making another request */
#define MAX_BIND_RESPONSE_WAIT		500

/* max number of 10ms intervals waiting for response before re-sending
packet */
#define MAX_KVP_RESPONSE_WAIT 		4
/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/
/* Button values */
typedef enum
{
    E_KEY_0 = BUTTON_0_MASK,
    E_KEY_1 = BUTTON_1_MASK,
} teKeyValues;

/* binding state machine */
typedef enum
{
    E_BIND_NONE,
    E_BIND_BINDING,
    E_BIND_MATCHED
} teBindType;

/* structure used to pass hardware events to application */
typedef struct
{
    uint32 u32Device;
    uint32 u32ItemBitmap;
} tsHwIntData;

/* All application data with scope within the entire file is kept here, */
typedef struct
{
    struct
    {
        uint32  	u32AppApiVersion;
        uint32  	u32HwApiVersion;
        uint32  	u32CalibratedTimeout;
        uint16		u16MatchAddr;
        uint8		u8MatchEndpoint;
        uint8		u8WuartEndpoint;
        teBindType  eBound;
        uint16		u16BindResponseTimeout;
        bool_t		bAwaitingResponse;
        uint8		u8KvpResponseTimeout;
        uint8 		u8TxFrameHandle;
        uint8 		u8RxFrameHandle;
    	bool_t		bStackReady;
    } sSystem;

    /* Queue for hardware interrupts */
    struct
    {
        tsHwIntData    asHwIntData[HW_INT_Q_SIZE];
        volatile uint8 u8ReadPtr;
        volatile uint8 u8WritePtr;
    } sQueue;
} tsWuart;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void vInitDevice(void);
PRIVATE void vInit(void);
PRIVATE void vTxData(void);
PRIVATE void vPerformMatchRequest(void);
PRIVATE void vCheckForBindResponseTimeout(void);
PRIVATE void vCheckForKvpResponseTimeout(void);
PRIVATE void vAddDesc(void);

/****************************************************************************/
/***        Exported Variables                                            ***/
/****************************************************************************/

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/
/* File scope data */
PRIVATE tsWuart sWuart;

/****************************************************************************/
/***        Exported Functions                                            ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 * Entry point for application from boot loader. Initialises system.
 *
 * RETURNS:
 * Never returns.
 *
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    /* Debug hooks: include these regardless of whether debugging or not */
    HAL_GDB_INIT();
    HAL_BREAKPOINT();

    /* General initialisation: reset hardware, set some values in PIB */
    JZS_sConfig.u32Channel = 0x11;
    JZS_sConfig.u16PanId = 0x1aab;
    /* JZS_sConfig.u16AppDataLength = 0; */

    /* set this field for 5139-based platforms. */
    /* JZS_sConfig.bFlashTypeIsSST = FALSE; */

    /* Enable UART 0: 19200-8-N-1 */
    vAHI_UartEnable(E_AHI_UART_0);
    vAHI_UartReset(E_AHI_UART_0, TRUE, TRUE);
    vAHI_UartReset(E_AHI_UART_0, FALSE, FALSE);
    vAHI_UartSetClockDivisor(E_AHI_UART_0, E_AHI_UART_RATE_19200);

    /* General initialisation */
    vInit();

    /* No return from the above function call */
}

/****************************************************************************
 *
 * NAME: AppWarmStart
 *
 * DESCRIPTION:
 * Entry point for application from boot loader. Simply jumps to AppColdStart
 * as, in this instance, application will never warm start.
 *
 * RETURNS:
 * Never returns.
 *
 ****************************************************************************/
PUBLIC void AppWarmStart(void)
{
    AppColdStart();
}

/****************************************************************************
 *
 * NAME: JZA_boAppStart
 *
 * DESCRIPTION:
 * Adds a simple descriptor for the UART input and output clusters.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PUBLIC bool_t JZA_boAppStart(void)
{
    /* start as a coordinator/router/end device according to the make file*/
    JZS_vStartStack();
    return TRUE;
}

/****************************************************************************
 *
 * NAME: JZA_vAppEventHandler
 *
 * DESCRIPTION:
 * Main user routine. This is called by the Basic Operating System (BOS)
 * at regular intervals.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PUBLIC void JZA_vAppEventHandler(void)
{
    tsHwIntData *psHwIntData;
    char 		cCharIn;
    bool_t		bBufferFull;

	if (sWuart.sSystem.bStackReady != TRUE)
	{
		return;
	}

    /* indicate that the device has started/joined a network and that the application
    is now running */
	vLedControl(0, FALSE);

	/* ensure that LED 2 is on while waiting to bind */
	if (sWuart.sSystem.eBound != E_BIND_MATCHED)
	{
    	vLedControl(1, TRUE);
	}

    /* Check queue for hardware interrupts, and process. Only process one per call */
    while (sWuart.sQueue.u8WritePtr != sWuart.sQueue.u8ReadPtr)
    {
        psHwIntData = &sWuart.sQueue.asHwIntData[sWuart.sQueue.u8ReadPtr];

        switch (psHwIntData->u32Device)
        {
        case E_AHI_DEVICE_UART0:
            /* If data has been received */
            if ((psHwIntData->u32ItemBitmap & 0x000000FF) == E_AHI_UART_INT_RXDATA)
            {
                /* Process UART0 RX interrupt */
                cCharIn = ((psHwIntData->u32ItemBitmap & 0x0000FF00) >> 8);
	            /* add character to UART Rx buffer; turn LED2 on if buffer full */
	            bBufferFull = bUART_RxCharISR((psHwIntData->u32ItemBitmap & 0x0000FF00) >> 8);
	            if (bBufferFull)
	            {
					vLedControl(1, TRUE);
				}
            }
            else if (psHwIntData->u32ItemBitmap == E_AHI_UART_INT_TX)
            {
                /* Process UART0 TX interrupt */
                vUART_TxCharISR();
            }
            break;


        /* If this is an event from timer 1 (which occurs every 10ms) */
        case E_AHI_DEVICE_TIMER1:
        /* check binding state */
            if (sWuart.sSystem.eBound == E_BIND_MATCHED)
            {
				/* already bound so we can send data received over the UART
				to the matched node */
				if (sWuart.sSystem.bAwaitingResponse)
				{
					/* still waiting for a kvp response from the last packet
					so increment the timeout counter */
					vCheckForKvpResponseTimeout();
				}
				else
				{
					/* not waiting for a response so can send a new packet */
					vTxData();
				}
			}
			else if (sWuart.sSystem.eBound == E_BIND_NONE)
			{
				/* there has not yet been an attempt to find a matching node,
				so try to find one now */
				vPerformMatchRequest();
			}
			else if (sWuart.sSystem.eBound == E_BIND_BINDING)
			{
				/* already tried to find a matching node; check how long ago
				this occurred and re-start the process if necessary */
				vCheckForBindResponseTimeout();
			}

            break;

        default:
            break;
        }

        /* advance hardware queue pointer */
        sWuart.sQueue.u8ReadPtr = (sWuart.sQueue.u8ReadPtr + 1) & HW_INT_Q_PTR_MASK;
    }

    /* check if user is trying to clear the buffer overflow LED */
    if ((u8ButtonReadRfd() == E_KEY_1) && (sWuart.sSystem.eBound == E_BIND_MATCHED))
    {
		vLedControl(1, FALSE);
	}
}

/****************************************************************************
 *
 * NAME: JZA_vPeripheralEvent
 *
 * DESCRIPTION:
 * Adds events to the hardware event queue.
 *
 * PARAMETERS:      Name            RW  Usage
 *                  u32Device       R   Peripheral responsible for interrupt e.g DIO
 *					u32ItemBitmap	R   Source of interrupt e.g. DIO bit map
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PUBLIC void JZA_vPeripheralEvent(uint32 u32Device, uint32 u32ItemBitmap)
{
    tsHwIntData *psHwIntData;
    uint8        u8WriteNextPtr;

    /* Queue event for processing during appKeyOperationKey call */

    u8WriteNextPtr = (sWuart.sQueue.u8WritePtr + 1) & HW_INT_Q_PTR_MASK;

    if (u8WriteNextPtr != sWuart.sQueue.u8ReadPtr)
    {
        /* There is space on queue */
        psHwIntData = &sWuart.sQueue.asHwIntData[sWuart.sQueue.u8WritePtr];
        psHwIntData->u32Device = u32Device;
        psHwIntData->u32ItemBitmap = u32ItemBitmap;

        sWuart.sQueue.u8WritePtr = u8WriteNextPtr;
    }

    /* If no space on queue, interrupt is silently discarded */
}

/****************************************************************************
 *
 * NAME: JZA_vAppDefineTasks
 *
 * DESCRIPTION:
 * Can be used to add additional tasks.
 *
 * RETURNS:
 * void
 *
 ****************************************************************************/
PUBLIC void JZA_vAppDefineTasks(void)
{
}

/****************************************************************************
 *
 * NAME: JZA_eAfKvpObject
 *
 * DESCRIPTION:
 * Receives incoming KVP data frames
 *
 * PARAMETERS:      Name            	RW  Usage
 *                  eAddrMode			R   Address mode of incoming frame
 *					u16AddrSrc			R	Network address of source node
 *					u8SrcEP				R 	Endpoint address of source node
 *					u8LQI				R   Link Quality Indication
 *					u8DstEP				R	Destination endpoint address
 *					u8ClusterId			R	Cluster ID of incoming frame
 *					*pu8ClusterIDRsp	R	Pointer to cluster ID of response frame
 *					*puTransactionInd	R	Pointer to incoming frame
 *					*puTransactionRsp	R	Pointer to response frame
 *
 * RETURNS:
 * TRUE for stack to automatically generate KVP response frames when appropriate
 * FALSE otherwise
 *
 ****************************************************************************/
PUBLIC bool_t JZA_bAfKvpObject(	APS_Addrmode_e			eAddrMode,
								uint16					u16AddrSrc,
								uint8					u8SrcEP,
								uint8					u8LQI,
								uint8					u8DstEP,
								uint8					u8ClusterId,
								uint8					*pu8ClusterIDRsp,
								AF_Transaction_s		*puTransactionInd,
								AF_Transaction_s		*puTransactionRsp)
{
    int i;
    uint8	u8Length;
    uint8	*pu8Afdu;

    bool_t bReturnVal = FALSE;

	if (puTransactionInd->uFrame.sKvp.eCommandTypeID == SET_ACKNOWLEDGMENT)
	{
		bReturnVal = TRUE;
	}

    /* check command frame is valid and for correct endpoint*/
    if (	(eAddrMode != APS_ADDRMODE_SHORT)
    	|| 	(u8DstEP != sWuart.sSystem.u8WuartEndpoint))
    {
        puTransactionRsp->uFrame.sKvp.eErrorCode = KVP_INVALID_ENDPOINT;
		return bReturnVal;
    }

    /* only accept SET commands */
    if (	(puTransactionInd->uFrame.sKvp.eCommandTypeID != SET)
    	&& 	(puTransactionInd->uFrame.sKvp.eCommandTypeID != SET_ACKNOWLEDGMENT))
    {
        puTransactionRsp->uFrame.sKvp.eErrorCode = KVP_INVALID_COMMAND_TYPE;
		return bReturnVal;
    }

    /* Check address of source*/
	if (	(u16AddrSrc != sWuart.sSystem.u16MatchAddr)
		|| 	(sWuart.sSystem.eBound != E_BIND_MATCHED))
    {
        puTransactionRsp->uFrame.sKvp.eErrorCode = KVP_SUCCESS;
		return bReturnVal;
    }

	u8Length = puTransactionInd->uFrame.sKvp.uAttributeData.CharacterString.u8CharacterCount;
	pu8Afdu = puTransactionInd->uFrame.sKvp.uAttributeData.CharacterString.au8CharacterData;

	/* Extract data and send to UART */
    if (puTransactionInd->uFrame.sKvp.u16AttributeID == WUP_CID_UART_DATA)
    {
        if (puTransactionInd->u8SequenceNum == sWuart.sSystem.u8RxFrameHandle)
        {
            sWuart.sSystem.u8RxFrameHandle++;

            /* Copy frame data to serial buffer for output on UART */
            for (i = 0; i < u8Length; i++)

⌨️ 快捷键说明

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