📄 evboard.c
字号:
/*
* "Copyright (c) 2006 Robert B. Reese ("AUTHOR")"
* All rights reserved.
* (R. Reese, reese@ece.msstate.edu, Mississippi State University)
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice, the following
* two paragraphs and the author appear in all copies of this software.
*
* IN NO EVENT SHALL THE "AUTHOR" BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHOR"
* HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE "AUTHOR" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE "AUTHOR" HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Please maintain this header in its entirety when copying/modifying
* these files.
*
* Files in this software distribution may have different usage
* permissions, see the header in each file. Some files have NO permission
* headers since parts of the original sources in these files
* came from vendor sources with no usage restrictions.
*
*/
/*
V0.1 Initial Release 10/July/2006 RBR
*/
#include "compiler.h"
#include "hal.h"
#include "halstack.h"
#include "debug.h"
#include "evboard.h"
typedef union _EVB_LED_STATE {
BYTE val;
struct _LED_STATE_bits {
unsigned led1_val:1;
unsigned led2_val:1;
}bits;
}EVB_LED_STATE;
EVB_LED_STATE led_state;
extern BOOL block_radio_rx;
#define LED_OFF 0
#define LED_ON 1
#define LED1 led_state.bits.led1_val
#define LED2 led_state.bits.led2_val
#define LED1_OFF() LED1=LED_OFF
#define LED1_ON() LED1=LED_ON
#define LED2_OFF() LED2=LED_OFF
#define LED2_ON() LED2=LED_ON
#define LED1_STATE() (LED1 == LED_ON)
#define LED2_STATE() (LED2 == LED_ON)
static char szName[32]; // name for shared memory
#define SHARED_MEM_SIZE 16 //DO NOT CHANGE THIS WITHOUT CHANGING FORMS CODE
static HANDLE hMapFile = NULL; //shared memory file handle
static BYTE *pSharedMem; //pointer to shared memory
//pointer to the mutex used to coordinate access to shared memory
//even though this is readonly for now and don't need a mutex
//we might want to change this later, so go ahead and make one
static HANDLE hMutex = NULL;
EVB_SW_STATE sw_state;
//shared memory used to emulate radio channel
//the coordinator node creates this memory
void InitEVBSharedMemory(void){
//EVB_ID must be a #define in the project properties
//unique to boards being simulated
sprintf(szName,"EVBSMEM_%d",EVB_ID);
//Create shared memory if not created yet
if (hMapFile == NULL) {
//create the shared memory segment
printf("In InitEVBSharedMemory(), EVB shared mem name: %s\n",szName);
hMapFile = CreateFileMapping(
INVALID_HANDLE_VALUE, // use paging file
NULL, // default security
PAGE_READWRITE, // read/write access
0, // max. object size
SHARED_MEM_SIZE, // buffer size
szName); // name of mapping object
if (hMapFile == NULL || hMapFile == INVALID_HANDLE_VALUE)
{
printf("Could not create file mapping object (%d).\n",
GetLastError());
return;
}
printf("Created shared memory segment\n");
}
pSharedMem = (BYTE *) MapViewOfFile(hMapFile, // handle to map object
FILE_MAP_ALL_ACCESS, // read/write permission
0,
0,
SHARED_MEM_SIZE);
if (pSharedMem == NULL)
{
printf("Could not map view of file (%d).\n",
GetLastError());
return;
}
//now initialize the shared memory
//initialize to zero
memset(pSharedMem,0,SHARED_MEM_SIZE);
//create MUTEX
sprintf(szName,"EVBMUTEX_%d",EVB_ID);
if (hMutex == NULL) {
hMutex = CreateMutex(NULL, FALSE,szName);
if(!hMutex) {
printf("Mutex creation failed, exiting.\n");
GetLastError();
}
printf("Mutex created\n");
}
}
//read the shared memory
#define BTN1_MASK 0x01
#define BTN2_MASK 0x02
#define LED1_MASK 0x80
#define LED2_MASK 0x40
#define BLOCK_RX_MASK 0x10
#define NO_CMD 0
#define WRITE_DBG 1
void DoSharedMemTxRx(BOOL rx_flag, BYTE *pload,BYTE plen);
//Memory format
// 0: switches/leds
// 1: command
// 2: debug byte transfer
// 3-? unused
void evbPoll(void){
BYTE x;
DWORD rc;
//poll of RX here
DoSharedMemTxRx(TRUE, NULL, 0); //do poll of radio here
//access to shared memory
//grab the mutex first.
rc = WaitForSingleObject(hMutex,INFINITE);
switch(rc) {
case WAIT_OBJECT_0:
break;
case WAIT_FAILED:
case WAIT_ABANDONED:
case WAIT_TIMEOUT:
printf("EVB Mutex grab failed!\n");
return;
}
x = *pSharedMem; //only first byte is used;
if (x & BTN1_MASK) sw_state.bits.s1_val = 1; else sw_state.bits.s1_val = 0;
if (x & BTN2_MASK) sw_state.bits.s2_val = 1; else sw_state.bits.s2_val = 0;
if (sw_state.bits.s1_val != sw_state.bits.s1_last_val) sw_state.bits.s1_tgl = 1;
if (sw_state.bits.s2_val != sw_state.bits.s2_last_val) sw_state.bits.s2_tgl = 1;
sw_state.bits.s1_last_val = sw_state.bits.s1_val;
sw_state.bits.s2_last_val = sw_state.bits.s2_val;
if (x & BLOCK_RX_MASK) block_radio_rx = TRUE; else block_radio_rx = FALSE;
//update the LEDs
if (LED1_STATE()) x = x | LED1_MASK; else x = x & ~LED1_MASK;
if (LED2_STATE()) x = x | LED2_MASK; else x = x & ~LED2_MASK;
*pSharedMem = x; //write the LEDs
//do other data exchange
x = *(pSharedMem+1); //get command
switch (x) {
case WRITE_DBG:
debug_level = *(pSharedMem+2);
break;
default: break;
}
*(pSharedMem+1) = 0; //clear command
//do other data transfer
*(pSharedMem+2)= debug_level;
//release the mutex
rc = ReleaseMutex(hMutex);
if (!rc) printf("EVB Mutex release failed!");
//since can't simulate interrupts, do callback here
usrIntCallback();
}
void evbLedSet(BYTE lednum, BOOL state) {
switch(lednum) {
case 1: if (state) LED1_ON(); else LED1_OFF(); break;
case 2: if (state) LED2_ON(); else LED2_OFF(); break;
}
}
BOOL evbLedGet(BYTE lednum){
switch(lednum) {
case 1: return(LED1_STATE());
case 2: return(LED2_STATE());
}
return(FALSE);
}
//init the board
void evbInit(void){
InitEVBSharedMemory();
sw_state.val = 0;
led_state.val = 0;
block_radio_rx = FALSE;
}
#ifdef LRWPAN_COORDINATOR
#ifndef LRWPAN_USE_DEMO_STATIC_BIND
//if not using the default binding resolution functions
//in stack/staticbind.c, then define your own here.
void evbBindTableIterInit(void){
}
BOOL evbResolveBind(BYTE srcEP, BYTE cluster,
BYTE *dstEP, SADDR *dstSADDR){
return(FALSE);
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -