/**************************************************************************** * * NAME: vJenie_Main * * DESCRIPTION: * Main user routine. This is called by the Basic Operating System (BOS) * at regular intervals. * * RETURNS: * void * ****************************************************************************/ PUBLIC void vJenie_CbMain(void) { static bool phase=0; static uint32 loop_count; /* regular watchdog reset */ #ifdef WATCHDOG_ENABLED vAHI_WatchdogRestart(); #endif if(sHomeData.bStackReady && bTimeOut) // Stack up and running and waiting for us to do something { switch(sHomeData.eAppState) { case E_STATE_REGISTER: if(loop_count % REGISTER_FLASH_RATE == 0) { vTxRegister(); vLedControl(LED1,phase); phase ^= 1; #ifdef NO_SLEEP /* Manually poll parent as not sleeping */ (void)eJenie_PollParent(); #endif } break; case E_STATE_RUNNING: vProcessRead(); if(loop_count % RUNNING_FLASH_RATE == 0) { vLedControl(LED1,phase); phase ^= 1; } if(loop_count % RUNNING_TRANSMIT_RATE == 0) { vProcessTxData(); } break; default: #ifdef DEBUG vUtils_Debug("Unknown State"); #endif break; } loop_count--; #ifdef NO_SLEEP vAHI_WakeTimerStart(E_AHI_WAKE_TIMER_1, DELAY_PERIOD); bTimeOut = FALSE; #else eJenie_SetSleepPeriod(SLEEP_PERIOD * 10); eJenie_Sleep(E_JENIE_SLEEP_OSCON_RAMON); #endif } }
/**************************************************************************** * * 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) { int i; int machh, machl, maclh, macll; uint8 MacAddress[8]; void *pu8ExtAdr; /* Initialise Zigbee stack */ JZS_u32InitSystem(TRUE); /* Set DIO for LEDs */ vLedInitFfd(); vLedControl(0,0); vLedControl(1,0); vLedControl(2,0); vLedControl(3,0); /* Intialise serial comms unless debug mode*/ #ifndef GDB vUART_printInit(); #endif /* Set sensors */ vInitSensors(); singledata.estate.estate = E_FILL_UNCOMPLETE; for(i = 0; i < 6; i++) { sensordata.data.sd.tempdata[i] = 0; sensordata.data.md[i].tempdata = 0; sensordata.data.sd.humidata[i] = 0; sensordata.data.md[i].humidata = 0; sensordata.sensorinfo[i].sensorID = 0; sensordata.sensorinfo[i].isValiable = FALSE; sensordata.sensorCount = 0; sensordata.datatype = UNSTARTED; } //sExtAddr.u32L = *(uint32 *)(0xf0000004); //sExtAddr.u32H = *(uint32 *)(0xf0000000); //vPrintf("%x, %x \n", sExtAddr.u32H, sExtAddr.u32L); /* Set pointer to point to location in internal RAM where extended address is stored */ // pu8ExtAdr = pvAppApiGetMacAddrLocation(); /* Load extended address into frame payload */ // for (i = 0; i < 8; i++) // { // MacAddress[i] = *( (uint8*)pu8ExtAdr + i); // vPrintf(" %x",MacAddress[i] ); // } // vPrintf("\n"); /* Start BOS */ (void)bBosRun(TRUE); /* No return from the above function call */ }
/**************************************************************************** * * NAME: vProcessRead * * DESCRIPTION: * Gets the current readings from each sensor. If the light level causes the * low light alarm to be triggered, an LED is illuminated. * * RETURNS: * void * * NOTES: * This is not an efficient way to read the sensors as much time is wasted * waiting for the humidity and temperature sensor to complete. The sensor * pulls a DIO line low when it is ready, and this could be used to generate * an interrupt to indicate when data is ready to be read. * ****************************************************************************/ PRIVATE void vProcessRead(void) { uint16 u16LightSensor; uint16 u16Diff; /* Read light level, adjust to range 0-6. This sensor automatically starts a new conversion afterwards so there is no need for a 'start read' */ u16LightSensor = u16ALSreadChannelResult(); /* Adjust the high and low values if necessary, and obtain the difference between them */ if (sDemoData.sLightSensor.u16Hi < u16LightSensor) { sDemoData.sLightSensor.u16Hi = u16LightSensor; } if (sDemoData.sLightSensor.u16Lo > u16LightSensor) { sDemoData.sLightSensor.u16Lo = u16LightSensor; } u16Diff = sDemoData.sLightSensor.u16Hi - sDemoData.sLightSensor.u16Lo; /* Work out the current value as a value between 0 and 6 within the range of values that have been seen previously */ if (u16Diff) { sDemoData.sSensors.u8AlsResult = (uint8)(((uint32)(u16LightSensor - sDemoData.sLightSensor.u16Lo) * 6) / (uint32)u16Diff); } else { sDemoData.sSensors.u8AlsResult = 3; } /* Set LED 1 based on light level */ if ((sDemoData.sSensors.u8AlsResult <= sDemoData.sControls.u8LightAlarmLevel) && (sDemoData.sControls.u8LightAlarmLevel < 7)) { vLedControl(LED2, TRUE); } else { vLedControl(LED2, FALSE); } /* Read temperature, 0-52 are acceptable. Polls until result received */ vHTSstartReadTemp(); sDemoData.sSensors.u8TempResult = u8FindMin((uint8)u16HTSreadTempResult(), 52); /* Read humidity, 0-104 are acceptable. Polls until result received */ vHTSstartReadHumidity(); sDemoData.sSensors.u8HtsResult = u8FindMin((uint8)u16HTSreadHumidityResult(), 104); }
void vLEDTask(void* pvParameters) { char LEDTaskNo; LEDTaskNo = *(char *)pvParameters; LEDTaskNo -= '0'; for (;;) { vLedControl(LEDTaskNo); vTaskDelay((rand() & 0x3FF)); } }
PUBLIC void vJenie_CbInit(bool_t bWarmStart) { vUtils_Init(); if(bWarmStart==FALSE) { (void)u32AHI_Init(); sHomeData.bStackReady=FALSE; /* Initialise buttons, LEDs and program variables */ vInitEndpoint(); /* Set DIO for buttons and LEDs */ vLedControl(LED1, FALSE); vLedControl(LED2, FALSE); vLedInitRfd(); vButtonInitRfd(); #ifdef NO_SLEEP vAHI_WakeTimerEnable(E_AHI_WAKE_TIMER_1, TRUE); #endif /* Set SW1(dio9) to input */ vAHI_DioSetDirection(E_AHI_DIO9_INT, 0); /* set interrupt for DIO9 to occur on button release - rising edge */ vAHI_DioInterruptEdge(E_AHI_DIO9_INT, 0); /* enable interrupt for DIO9 */ vAHI_DioInterruptEnable(E_AHI_DIO9_INT, 0); /* Set SW2(dio10) to input */ vAHI_DioSetDirection(E_AHI_DIO10_INT, 0); /* set interrupt for DIO9 to occur on button release - rising edge */ vAHI_DioInterruptEdge(E_AHI_DIO10_INT, 0); /* enable interrupt for DIO9 */ vAHI_DioInterruptEnable(E_AHI_DIO10_INT, 0); /* Set up peripheral hardware */ vALSreset(); vHTSreset(); /* Start ALS now: it automatically keeps re-sampling after this */ vALSstartReadChannel(0); sHomeData.eAppState = E_STATE_REGISTER; switch(eJenie_Start(E_JENIE_END_DEVICE)) /* Start network as end device */ { case E_JENIE_SUCCESS: #ifdef DEBUG vUtils_Debug("Jenie Started"); #endif #ifdef HIGH_POWER /* Set high power mode */ eJenie_RadioPower(18, TRUE); #endif break; case E_JENIE_ERR_UNKNOWN: case E_JENIE_ERR_INVLD_PARAM: case E_JENIE_ERR_STACK_RSRC: case E_JENIE_ERR_STACK_BUSY: default: /* Do something on failure?? */ break; } }else{ /* Set up peripheral hardware */ vALSreset(); vHTSreset(); /* Start ALS now: it automatically keeps re-sampling after this */ vALSstartReadChannel(0); switch(eJenie_Start(E_JENIE_END_DEVICE)) /* Start network as end device */ { case E_JENIE_SUCCESS: #ifdef HIGH_POWER /* Set high power mode */ eJenie_RadioPower(18, TRUE); #endif break; case E_JENIE_ERR_UNKNOWN: case E_JENIE_ERR_INVLD_PARAM: case E_JENIE_ERR_STACK_RSRC: case E_JENIE_ERR_STACK_BUSY: default: /* Do something on failure?? */ break; } } /* set watchdog to long timeout - override setting in JenNet startup */ #ifdef WATCHDOG_ENABLED vAHI_WatchdogStart(254); #endif }
/**************************************************************************** * * NAME: vToggleLed * * DESCRIPTION: * Gets called by a BOS timer. Toggles LED1 to indicate we are alive. * ****************************************************************************/ PRIVATE void vToggleLed(void *pvMsg, uint8 u8Dummy) { uint8 u8Msg; uint8 u8TimerId; static int count = 3; static bool_t bToggle; static int fillcount = 0; MAC_ExtAddr_s sExtAddr, *psExtAddr; int i; AF_Transaction_s asTransaction[1]; asTransaction[0].u8SequenceNum = u8AfGetTransactionSequence(TRUE); asTransaction[0].uFrame.sMsg.u8TransactionDataLen = 1; asTransaction[0].uFrame.sMsg.au8TransactionData[0] = 9; //static int sensorID[3] = {0, }; vReadTempHumidity(); if (bToggle) { vLedControl(0,0); } else { vLedControl(0,1); } bToggle = !bToggle; /* if(afdeDataRequest(APS_ADDRMODE_SHORT, 0x0001, WSN_DATA_SOURCE_ENDPOINT, WSN_DATA_SINK_ENDPOINT, WSN_PROFILE_ID, WSN_CID_SENSOR_READINGS, AF_MSG, 1, asTransaction, APS_TXOPTION_NONE, SUPPRESS_ROUTE_DISCOVERY, 0)){;} armCmd = single;*/ if(commandChanged == TRUE) { //vPrintf("command changed!\n"); commandChanged = FALSE; switch(armCmd) { case reflash: //vPrintf("reflashed\n"); sensordata.datatype = WAITING_MODE; for(i = 0; i < 6; i++) { if(sensordata.sensorinfo[i].isValiable == TRUE) { asTransaction[0].uFrame.sMsg.au8TransactionData[0] = WAITING_MODE; afdeDataRequest(APS_ADDRMODE_SHORT, sensordata.sensorinfo[i].sensorID, WSN_DATA_SOURCE_ENDPOINT, WSN_DATA_SINK_ENDPOINT, WSN_PROFILE_ID, WSN_CID_SENSOR_READINGS, AF_MSG, 1, asTransaction, APS_TXOPTION_NONE, SUPPRESS_ROUTE_DISCOVERY, 0); } } break; case single: //vPrintf("single mode send\n"); sensordata.sensorID = 0x0001; for(i = 0; i < 6; i++) { if(sensordata.sensorID != sensordata.sensorinfo[i].sensorID) { if(sensordata.sensorinfo[i].isValiable == TRUE) { sExtAddr.u32L = sensordata.sensorinfo[i].macl; sExtAddr.u32H = sensordata.sensorinfo[i].mach; JZS_vRemoveNode(&sExtAddr, TRUE); //bNwkRemoveDevice(u16AddrSrc); vPrintf("sensor: %d remove success!x\n", sensordata.sensorinfo[i].sensorID); sensordata.sensorinfo[i].isValiable = FALSE; } } } asTransaction[0].uFrame.sMsg.au8TransactionData[0] = CONTROL_CMD_GET_DATA_S; afdeDataRequest(APS_ADDRMODE_SHORT, sensordata.sensorID, WSN_DATA_SOURCE_ENDPOINT, WSN_DATA_SINK_ENDPOINT, WSN_PROFILE_ID, WSN_CID_SENSOR_READINGS, AF_MSG, 1, asTransaction, APS_TXOPTION_NONE, SUPPRESS_ROUTE_DISCOVERY, 0); /*singledata.estate.num++; if(singledata.estate.num > 5) { singledata.estate.num = 0; singledata.estate.estate = E_FILL_COMPLETE; }else singledata.estate.estate = E_FILL_UNCOMPLETE; singledata.tempdata[singledata.estate.num] = sTempHumiditySensor.u16TempReading; singledata.humidata[singledata.estate.num] = sTempHumiditySensor.u16HumidReading; if(singledata.tempdata[singledata.estate.num] > 100) { singledata.tempdata[singledata.estate.num] = 100; singledata.templength[singledata.estate.num] = 3; }else if(singledata.tempdata[singledata.estate.num] <= 0) { singledata.tempdata[singledata.estate.num] = 0; singledata.templength[singledata.estate.num] = 1; }else { singledata.templength[singledata.estate.num] = 2; } if(singledata.humidata[singledata.estate.num] > 100) { singledata.humidata[singledata.estate.num] = 100; singledata.humilength[singledata.estate.num] = 3; }else if(singledata.humidata[singledata.estate.num] <= 0) { singledata.humidata[singledata.estate.num] = 0; singledata.humilength[singledata.estate.num] = 1; }else { singledata.humilength[singledata.estate.num] = 2; } if(singledata.estate.estate == E_FILL_COMPLETE) vPrintf("S%d,T[%d]%d:%d,H[%d]%d:%d,T[%d]%d:%d,H[%d]%d:%d,T[%d]%d:%d,H[%d]%d:%d,T[%d]%d:%d,H[%d]%d:%d,T[%d]%d:%d,H[%d]%d:%d,T[%d]%d:%d,H[%d]%d:%d\n", singledata.sensorID, 0, singledata.templength[0], singledata.tempdata[0], 0, singledata.humilength[0], singledata.humidata[0], 1, singledata.templength[1], singledata.tempdata[1], 1, singledata.humilength[1], singledata.humidata[1], 2, singledata.templength[2], singledata.tempdata[2], 2, singledata.humilength[2], singledata.humidata[2], 3, singledata.templength[3], singledata.tempdata[3], 3, singledata.humilength[3], singledata.humidata[3], 4, singledata.templength[4], singledata.tempdata[4], 4, singledata.humilength[4], singledata.humidata[4], 5, singledata.templength[5], singledata.tempdata[5], 5, singledata.humilength[5], singledata.humidata[5]); //vTxSerialDataFrame(1, sTempHumiditySensor.u16HumidReading,sTempHumiditySensor.u16TempReading); sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_IDLE;*/ break; case mulit: for(i = 0; i < 6; i++) { asTransaction[0].uFrame.sMsg.au8TransactionData[0] = CONTROL_CMD_GET_DATA_M; if(sensordata.sensorinfo[i].isValiable == TRUE) afdeDataRequest(APS_ADDRMODE_SHORT, sensordata.sensorinfo[i].sensorID, WSN_DATA_SOURCE_ENDPOINT, WSN_DATA_SINK_ENDPOINT, WSN_PROFILE_ID, WSN_CID_SENSOR_READINGS, AF_MSG, 1, asTransaction, APS_TXOPTION_NONE, SUPPRESS_ROUTE_DISCOVERY, 0); /*if(multidata[i].available == NOT_NULL) { multidata[i].tempdata = sTempHumiditySensor.u16TempReading; multidata[i].humidata = sTempHumiditySensor.u16HumidReading; vPrintf("S:%d,T:%d,H:%d\n", multidata[i].sensorID, multidata[i].tempdata, multidata[i].humidata); }*/ } //sTempHumiditySensor.eState = E_STATE_READ_TEMP_HUMID_IDLE; break; case stop: //vPrintf("stopped!\n"); break; default: //vPrintf("wait for start!\n"); break; } } if(sensordata.datatype == WAITING_MODE) { for(i = 0; i < 6; i++) { if(sensordata.sensorinfo[i].isValiable == TRUE) vPrintf("%d is avaliable x\n", sensordata.sensorinfo[i].sensorID); } } else if(sensordata.datatype == SINGLE_MODE) { if(fillcount == 6) fillcount = 0; vPrintf("S%dT%dL%d%dH%dL%d%dx\n", sensordata.data.sd.sensorID, fillcount, sensordata.data.sd.templength[fillcount], sensordata.data.sd.tempdata[fillcount], fillcount, sensordata.data.sd.humilength[fillcount], sensordata.data.sd.humidata[fillcount]); fillcount++; } else if(sensordata.datatype == MULTI_MODE) { for(i = 0; i < 6; i++) { if(sensordata.data.md[i].isValiable == TRUE) vPrintf("S%dT%dL%d%dH%dL%d%dx\n", sensordata.data.md[i].sensorID, i, sensordata.data.md[i].templength, sensordata.data.md[i].tempdata, i, sensordata.data.md[i].humilength, sensordata.data.md[i].humidata); } } else if(sensordata.datatype == UNSTARTED) { //vPrintf("wait for start\n"); } (void)bBosCreateTimer(vToggleLed, &u8Msg, 0, (APP_TICK_PERIOD_ms / 10), &u8TimerId); }
/**************************************************************************** * * NAME: vAppMain * * DESCRIPTION: * Entry point for application from a cold start. * * RETURNS: * Never returns. * ****************************************************************************/ PUBLIC void vAppMain(void) { #ifdef DBG_ENABLE dbg_vPatchInit(); #endif os_vPatchInit(); /* Debug */ DBG_vUartInit(DBG_E_UART_1, DBG_E_UART_BAUD_RATE_115200); vAHI_UartSetRTSCTS(DBG_E_UART_1, FALSE); DBG_vPrintf(RTR_TRACE, "\n%d RTR < vAppMain()", NODE_sData.u32Timer); /* Initialise and turn on LEDs */ vLedInitRfd(); vLedControl(0, TRUE); vLedControl(1, TRUE); /* Initialise buttons */ vButtonInitRfd(); /* Watchdog ? */ #if NODE_FUNC_WATCHDOG { DBG_vPrintf(RTR_TRACE, " Stack low water mark = %08x", &stack_low_water_mark); *(volatile uint32 *)0x020010e0 = ((uint32)&stack_low_water_mark) | 0x8000000; if ((*(volatile uint32 *)0x02000004) & 0x80 ) { DBG_vPrintf(RTR_TRACE, " Watchdog timer has reset device!"); vAHI_WatchdogStop(); while(1); } } #else { /* Don't use watchdog */ vAHI_WatchdogStop(); } #endif /* Initialise application API */ u32AppApiInit(NULL, NULL, NULL, NULL, NULL, NULL); /* Start OS */ OS_vStart(RTR_vInit, RTR_vUncInt); /* Turn off LEDs */ vLedControl(0, FALSE); vLedControl(1, FALSE); /* Idle task commences on exit from OS start call */ while (TRUE) { /* Watchdog ? */ #if NODE_FUNC_WATCHDOG { /* Restart watchdog */ vAHI_WatchdogRestart(); } #endif /* Manage power */ PWRM_vManagePower(); } }