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

📄 wsn_router.c

📁 ZIGBEE无线组网架构方案说明,主从方式祥述
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
 *
 * MODULE:			   WSN_Router.c
 *
 * COMPONENT:          $RCSfile: WSN_Router.c,v $
 *
 * VERSION:            $Name: JN-AN-1015-1v2 $
 *
 * REVISION:           $Revision: 1.3 $
 *
 * DATED:              $Date: 2006/09/13 14:13:10 $
 *
 * STATUS:             $State: Exp $
 *
 * AUTHOR:             IDM
 *
 * DESCRIPTION:
 *
 * Implements a Wireless Sensor Network Router using the Jennic Zigbee stack.
 * Reads temperature, humidity and battery voltage and transmits these to
 * network coordinator. Assumes code is running on a evaluation kit sensor
 * board.
 *
 * LAST MODIFIED BY:   $Author: imorr $
 *                     $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 "ALSdriver.h"
#include "HTSdriver.h"
#include "AppHardwareApi.h"

#include "JZ_Api.h"
#include "WSN_Profile.h"

/****************************************************************************/
/***        Macro Definitions                                             ***/
/****************************************************************************/

#define LED_OUTPUTS_MASK          0x0000C000UL
#define LED1_MASK                 0x00004000UL
#define LED2_MASK                 0x00008000UL
#define LED_INIT                  (vAHI_DioSetDirection(0, LED_OUTPUTS_MASK))
#define LED_1_OFF                 (vAHI_DioSetOutput(LED1_MASK, 0))
#define LED_1_ON                  (vAHI_DioSetOutput(0, LED1_MASK))
#define LED_2_OFF                 (vAHI_DioSetOutput(LED2_MASK, 0))
#define LED_2_ON                  (vAHI_DioSetOutput(0, LED2_MASK))

/* Timing values */
#define APP_TICK_PERIOD_ms		  100
#define APP_TICK_PERIOD     	  (APP_TICK_PERIOD_ms * 32)

#define APP_DATA_SEND_PERIOD_ms	  1000
#define APP_DATA_SEND_PERIOD	  (APP_DATA_SEND_PERIOD_ms / APP_TICK_PERIOD_ms)

#define HW_INT_Q_SIZE          	  32
#define HW_INT_Q_PTR_MASK      	  0x1f

/****************************************************************************/
/***        Type Definitions                                              ***/
/****************************************************************************/

/* Battery reading state definitions */
typedef enum
{
	E_STATE_READ_BATT_VOLT_IDLE,
	E_STATE_READ_BATT_VOLTS_ADC_CONVERTING,
	E_STATE_READ_BATT_VOLTS_COMPLETE,
    E_STATE_READ_BATT_VOLTS_READY
}teStateReadBattVolt;

/* Temperature/Humidity Sensor - reading state definitions */
typedef enum
{
	E_STATE_READ_TEMP_HUMID_IDLE,
	E_STATE_READ_HUMID_RUNNING,
	E_STATE_READ_TEMP_HUMID_COMPLETE,
	E_STATE_READ_TEMP_START,
	E_STATE_READ_TEMP_HUMID_RUNNING,
	E_STATE_READ_TEMP_COMPLETE,
	E_STATE_READ_TEMP_HUMID_READY
}teStateReadTempHumidity;

/* Battery measurement data */
typedef struct
{
	uint16 					u16Reading;
	teStateReadBattVolt 	eState;
}tsBattSensor;

/* Temp/Humidity measurement data */
typedef struct
{
	uint16 					u16TempReading;
	uint16 					u16HumidReading;
	teStateReadTempHumidity eState;
}tsTempHumiditySensor;

typedef struct
{
    uint32 u32Device;
    uint32 u32ItemBitmap;
} tsHwIntData;

typedef struct
{
    tsHwIntData    asHwIntData[HW_INT_Q_SIZE];
    volatile uint8 u8ReadPtr;
    volatile uint8 u8WritePtr;
} tsHwEventQueue;

/****************************************************************************/
/***        Local Function Prototypes                                     ***/
/****************************************************************************/
PRIVATE void vInit(void);
PRIVATE void vSendData(void);
PRIVATE void vInitSensors(void);
PRIVATE void vAppTick(uint8 u8Param);
PRIVATE void vReadTempHumidity(void);
PRIVATE void vReadBatteryVoltage(void);

/****************************************************************************/
/***        Local Variables                                               ***/
/****************************************************************************/
tsHwEventQueue sHwEventQueue;
uint8 u8AppTicks = 0;
tsBattSensor sBattSensor;
tsTempHumiditySensor sTempHumiditySensor;
bool_t bAppTimerStarted = FALSE;

/****************************************************************************
 *
 * NAME: AppColdStart
 *
 * DESCRIPTION:
 * Entry point for application. Initialises system, starts scan then
 * processes interrupts.
 *
 * RETURNS:
 * void, never returns
 *
 ****************************************************************************/
PUBLIC void AppColdStart(void)
{
    /* General initialisation: reset hardware */
    JZS_sConfig.u32Channel 	= WSN_CHANNEL;
    JZS_sConfig.u16PanId 	= WSN_PAN_ID;

    /* General initialisation: reset hardware */
    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();
}

/****************************************************************************/
/***        Local Functions                                               ***/
/****************************************************************************/
/****************************************************************************
 *
 * NAME: vInit
 *
 * DESCRIPTION:
 * Initialises Zigbee stack and hardware. Final action is to start BOS, from
 * which there is no return. Subsequent application actions occur in the
 * functions defined above.
 *
 * RETURNS:
 * No return from this function
 *
 ****************************************************************************/
PRIVATE void vInit(void)
{
    /* Initialise Zigbee stack */
    (void)JZS_u32InitSystem();

    /* Set DIO for LEDs */
    LED_INIT;
    LED_1_OFF;
    LED_2_OFF;

    /* Set sensors */
    vInitSensors();

    /* Initialise hardware event queue pointers */
    sHwEventQueue.u8ReadPtr  = 0;
    sHwEventQueue.u8WritePtr = 0;

    /* Start BOS */
    (void)BOSRun();

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

/****************************************************************************
 *
 * NAME: vInitSensors
 *
 * DESCRIPTION:
 * Initialise the temperature/humidity sensor and set the ADC to measure the
 * supply voltage.
 *
 ****************************************************************************/
PRIVATE void vInitSensors(void)
{
    /* Initialise temp/humidity sensor interface */
    vHTSreset();
    sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_IDLE;

    /* Initialise ADC for internal battery voltage measurement */
	sBattSensor.eState = E_STATE_READ_BATT_VOLT_IDLE;

	vAHI_ApConfigure(E_AHI_AP_REGULATOR_ENABLE,
	                 E_AHI_AP_INT_DISABLE,
	                 E_AHI_AP_SAMPLE_2,
	                 E_AHI_AP_CLOCKDIV_2MHZ,
	                 E_AHI_AP_INTREF);

    /* Wait until the analogue peripheral regulator has come up before setting
       the ADC. */
    while(!bAHI_APRegulatorEnabled());

    vAHI_AdcEnable(E_AHI_ADC_CONVERT_DISABLE,
                   E_AHI_AP_INPUT_RANGE_2,
                   E_AHI_ADC_SRC_VOLT);
}

/****************************************************************************
 *
 * NAME: vAppTick
 *
 * DESCRIPTION:
 *
 * Called by a BOS timer expiry. Reads sensor data and if complete transmits
 * to coordinator.
 *
 ****************************************************************************/
PRIVATE void vAppTick(uint8 u8Param)
{
    uint8 u8TimerID;
    static bool_t bToggle;

	/* Read sensor data */
	vReadTempHumidity();
	vReadBatteryVoltage();

	if (u8AppTicks++ > APP_DATA_SEND_PERIOD)
	{
	    /* If sensor reads are compete */
	    if ((sBattSensor.eState         == E_STATE_READ_BATT_VOLTS_READY) &&
            (sTempHumiditySensor.eState == E_STATE_READ_TEMP_HUMID_READY))
        {
		    /* Toggle LED1 to show we are alive */
		    if (bToggle)
		    {
		    	LED_1_OFF;
		    }
		    else
		    {
			   	LED_1_ON;
		    }
		    bToggle = !bToggle;

		    u8AppTicks = 0;

            /* Transmit data to coordinator */
		    vSendData();

           	sBattSensor.eState         = E_STATE_READ_BATT_VOLT_IDLE;
            sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_IDLE;
		}
	}
    BOSCreateTimer(vAppTick, 0, (APP_TICK_PERIOD_ms / 10), &u8TimerID);
}

/****************************************************************************
 *
 * NAME: vReadBatteryVoltage
 *
 * DESCRIPTION:
 *
 * Uses ADC to read supply voltage. Measurement is performed using a state
 * machine to ensure that it never blocks.
 *
 ****************************************************************************/
PRIVATE void vReadBatteryVoltage(void)
{
    uint16 u16AdcReading;

	switch(sBattSensor.eState)
	{
		case E_STATE_READ_BATT_VOLT_IDLE:
	    	vAHI_AdcStartSample();
	    	sBattSensor.eState = E_STATE_READ_BATT_VOLTS_ADC_CONVERTING;
			break;

		case E_STATE_READ_BATT_VOLTS_ADC_CONVERTING:
	    	if (!bAHI_AdcPoll())
	    	{
	    	    sBattSensor.eState = E_STATE_READ_BATT_VOLTS_COMPLETE;
	    	}
			break;

		case E_STATE_READ_BATT_VOLTS_COMPLETE:

		    u16AdcReading = u16AHI_AdcRead();

		    /* Input range is 0 to 2.4V. ADC has full scale range of 12 bits.
		       Therefore a 1 bit change represents a voltage of approx 586uV */
		    sBattSensor.u16Reading = ((uint32)((uint32)(u16AdcReading * 586) +
                                     ((uint32)(u16AdcReading * 586) >> 1)))  /

⌨️ 快捷键说明

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