📄 wirelessuart.c
字号:
/**************************************************************************** * * 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 matchdescriptor response, before making another request */#define MAX_BIND_RESPONSE_WAIT 500/* max number of 10ms intervals waiting for response before re-sendingpacket */#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(); vAHI_HighPowerModuleEnable(TRUE, TRUE); //高功率模块(M02/M04)使用此句 /* 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; } /* 如果成功运行建立网络(coord),或者加入(Router)网络 Led0熄灭*/ vLedControl(0, FALSE); /* 等待绑定时led1打开*/ if (sWuart.sSystem.eBound != E_BIND_MATCHED) { vLedControl(1, TRUE); } /* 处理中断队列里的各种中断事件 */ while (sWuart.sQueue.u8WritePtr != sWuart.sQueue.u8ReadPtr) { psHwIntData = &sWuart.sQueue.asHwIntData[sWuart.sQueue.u8ReadPtr]; switch (psHwIntData->u32Device) { // 串口0事件 case E_AHI_DEVICE_UART0: /* 如果接收到数据 */ if ((psHwIntData->u32ItemBitmap & 0x000000FF) == E_AHI_UART_INT_RXDATA) { /* 取接收数据 */ cCharIn = ((psHwIntData->u32ItemBitmap & 0x0000FF00) >> 8); /* 加入uart接收队列 */ bBufferFull = bUART_RxCharISR((psHwIntData->u32ItemBitmap & 0x0000FF00) >> 8); if (bBufferFull) { vLedControl(1, TRUE); } } else if (psHwIntData->u32ItemBitmap == E_AHI_UART_INT_TX) { /* 进入uart发送程序 */ vUART_TxCharISR(); } break; /* timer 1 中断事件 (which occurs every 10ms) */ case E_AHI_DEVICE_TIMER1: /* 检测绑定状态 */ if (sWuart.sSystem.eBound == E_BIND_MATCHED) { if (sWuart.sSystem.bAwaitingResponse) { /* 如果绑定成功,zigbee发送数据没有响应,超时重发*/ vCheckForKvpResponseTimeout(); } else { /* 如果没有发送不成功的,就发送zigbee新数据*/ vTxData(); } } else if (sWuart.sSystem.eBound == E_BIND_NONE) { /* 如果没有绑定继续发送绑定命令 */ vPerformMatchRequest(); } else if (sWuart.sSystem.eBound == E_BIND_BINDING) { /* 绑定超时,在发送匹配绑定命令 */ vCheckForBindResponseTimeout(); } break; default: break; } /* 队列加一 */ 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; /* 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 TRUE; } /* 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 TRUE; } /* Check address of source*/ if ( (u16AddrSrc != sWuart.sSystem.u16MatchAddr) || (sWuart.sSystem.eBound != E_BIND_MATCHED)) { puTransactionRsp->uFrame.sKvp.eErrorCode = KVP_SUCCESS; return TRUE; } 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++) { vSerial_TxChar(*pu8Afdu++); } } /* Must have missed a frame */ else if (puTransactionInd->u8SequenceNum > sWuart.sSystem.u8RxFrameHandle)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -