/**************************************************************************** * * NAME: vInitSystem * * DESCRIPTION: Initialise the radio system * * RETURNS: * void * ****************************************************************************/ PRIVATE void vInitSystem(void) { // Setup interface to MAC (void) u32AHI_Init(); (void) u32AppQApiInit(NULL,mcpsCallback , NULL); loadSettings(); if (useHighPowerModule == TRUE) { //max power for europe including antenna gain is 10dBm // TODO see if we can use more power as rx antenna is lower gain //??? boost is +2.5 ant is 1 and power set to 4 = 7.5 ???? vAHI_HighPowerModuleEnable(TRUE, TRUE); #ifdef JN5168 eAppApiPlmeSet(PHY_PIB_ATTR_TX_POWER, 34+10*2); #else bAHI_PhyRadioSetPower(2); #endif } // Initialise end device state sEndDeviceData.eState = E_STATE_IDLE; sEndDeviceData.u8TxPacketSeqNb = 0; sEndDeviceData.u8RxPacketSeqNb = 0; sEndDeviceData.u8ChannelSeqNo = 0; // Set up the MAC handles. Must be called AFTER u32AppQApiInit() s_pvMac = pvAppApiGetMacHandle(); s_psMacPib = MAC_psPibGetHandle(s_pvMac); // Set Pan ID in PIB (also sets match register in hardware) MAC_vPibSetPanId(s_pvMac, PAN_ID); // Enable receiver to be on when idle MAC_vPibSetRxOnWhenIdle(s_pvMac, TRUE, FALSE); // sometimes useful during development // all messages are passed up from lower levels // MAC_vPibSetPromiscuousMode(s_pvMac, TRUE, FALSE); module_MAC_ExtAddr_s* macptr = (module_MAC_ExtAddr_s*)pvAppApiGetMacAddrLocation(); //moved to after u32AHI_Init() for jn5148 randomizeHopSequence(((uint32) macptr->u32H) ^ ((uint32) macptr->u32L)); #if (defined JN5148 || defined JN5168) /* Enable TOF ranging. */ // vAppApiTofInit(TRUE); #endif }
/**************************************************************************** * * NAME: vModuleSetRadioFrontEnd * * DESCRIPTION: * Set up the module appropriately for a given radio front end * * RETURNS: * void * ****************************************************************************/ PUBLIC void vModuleSetRadioFrontEnd(teRadioFrontEnd eRadioFrontEnd) { if ((u8AHI_PowerStatus() & (1 << 3)) == 0) { /* Power domain is inactive - writing to the register will cause a bus exception */ return; } switch(eRadioFrontEnd) { case (E_FRONTEND_STANDARD_POWER): u32AllocatedPins &= ~(3 << 2); /* DIO2 & 3 Used for Tx/Rx on High power module - mark available */ vLog_Printf(TRACE_BR, LOG_INFO, "\nEnable Std Power"); #if defined JENNIC_CHIP_FAMILY_JN516x vAppApiSetHighPowerMode(APP_API_MODULE_STD, TRUE); #else vAHI_HighPowerModuleEnable(FALSE, FALSE); #endif /* JENNIC_CHIP_FAMILY_JN516x */ break; case (E_FRONTEND_HIGH_POWER): case (E_FRONTEND_ETSI): /* Pin is pulled low - High power mode selected */ u32AllocatedPins |= 3 << 2; /* DIO2 & 3 Used for Tx/Rx on High power module - mark allocated */ if (u32ActivityLEDMask != ACTIVITY_LED_DISABLED) { if (u32ActivityLEDMask & (3 << 2)) { /* The activity LED has already been set up as pin 2/3, disable it! */ u32ActivityLEDMask = ACTIVITY_LED_DISABLED; } } vLog_Printf(TRACE_BR, LOG_INFO, "\nEnable High Power"); #if defined JENNIC_CHIP_FAMILY_JN516x #else vAHI_HighPowerModuleEnable(TRUE, TRUE); #endif /* JENNIC_CHIP_FAMILY_JN516x */ switch(sConfig.u8Region) { case E_REGION_EUROPE: break; case E_REGION_USA: #if defined JENNIC_CHIP_FAMILY_JN516x #else bAHI_PhyRadioSetPower(5); #endif /* JENNIC_CHIP_FAMILY_JN516x */ break; case E_REGION_JAPAN: break; } if (eRadioFrontEnd == E_FRONTEND_HIGH_POWER) { #if defined JENNIC_CHIP_FAMILY_JN516x vAppApiSetHighPowerMode(APP_API_MODULE_HPM06, TRUE); #else vAppApiSetHighPowerMode(APP_API_HPM_MODULE_M06, APP_API_HPM_MODE_FULL); #endif /* JENNIC_CHIP_FAMILY_JN516x */ } else if (eRadioFrontEnd == E_FRONTEND_ETSI) { vLog_Printf(TRACE_BR, LOG_INFO, "\nEnable ETSI"); #if defined JENNIC_CHIP_FAMILY_JN516x vAppApiSetHighPowerMode(APP_API_MODULE_HPM05, TRUE); #else vAppApiSetHighPowerMode(APP_API_HPM_MODULE_M06, APP_API_HPM_MODE_ETSI); #endif /* JENNIC_CHIP_FAMILY_JN516x */ } { uint32 u32TxPower; /* Read then write back TX power level * This forces the MAC to implement the previous change. */ eAppApiPlmeGet(PHY_PIB_ATTR_TX_POWER,&u32TxPower); eAppApiPlmeSet(PHY_PIB_ATTR_TX_POWER, u32TxPower); } break; default: break; } }
/**************************************************************************** * * NAME: frameStartEvent * * DESCRIPTION: Handler for the frame start notification * * PARAMETERS: Name RW Usage * buff ? unused?? * * RETURNS: * * NOTES: ****************************************************************************/ void frameStartEvent(void* context,void* buff) { // Called every frame, currently 20ms frameCounter++; #ifdef JN5168 /* MAC_McpsReqRsp_s sMcpsReqRsp; MAC_McpsSyncCfm_s sMcpsSyncCfm; // Send request to remove a data frame from transaction queue sMcpsReqRsp.u8Type = MAC_MCPS_REQ_PURGE; sMcpsReqRsp.u8ParamLength = sizeof(MAC_McpsReqPurge_s); sMcpsReqRsp.uParam.sReqPurge.u8Handle = 1; vAppApiMcpsRequest(&sMcpsReqRsp, &sMcpsSyncCfm); */ //reset MAC MAC_MlmeReqRsp_s sMlmeReqRsp; MAC_MlmeSyncCfm_s sMlmeSyncCfm; sMlmeReqRsp.u8Type = MAC_MLME_REQ_RESET; sMlmeReqRsp.u8ParamLength = sizeof(MAC_MlmeReqReset_s); sMlmeReqRsp.uParam.sReqReset.u8SetDefaultPib = FALSE; vAppApiMlmeRequest(&sMlmeReqRsp, &sMlmeSyncCfm); uint32 newchannel = getHopChannel(getSeqClock()); eAppApiPlmeSet(PHY_PIB_ATTR_CURRENT_CHANNEL, newchannel); #endif if(frameReceived==FALSE) { RX.missedFrames++; } else { RX.missedFrames=0; } //used instead of sequence number to prevent handling the same frame twice frameReceived=FALSE; // Binding check // Only do automatic bind request after 10secs incase this // was an unintended reboot in flight. // Only do if not already communicating with a tx if ((frameCounter > 500 && frameCounter < 5000) && getHopMode() == hoppingRxStartup) { // TODO - add binding button check and an option to disable auto binding uint32 channel = 0; eAppApiPlmeGet(PHY_PIB_ATTR_CURRENT_CHANNEL, &channel); if (channel == 11) { // TODO - Get rid of magic numbers uint8 packet[14]; packet[0] = 0; //no high priority data packet[1] = 0; //seq no packet[2] = 0; //chunk no packet[3] = 10; //length of message packet[4] = 0; //no route packet[5] = 16; //bind request module_MAC_ExtAddr_s* macptr = (module_MAC_ExtAddr_s*)pvAppApiGetMacAddrLocation(); // TODO - can't we just memcpy the whole thing in one call? memcpy(&packet[6], &macptr->u32L, sizeof(macptr->u32L)); memcpy(&packet[10], &macptr->u32H, sizeof(macptr->u32H)); vTransmitDataPacket(packet, sizeof(packet), TRUE); } // Set the flash rate to fast to indicate binding attempts rxLEDFlashLimit = LED_FLASH_FAST; } // LED Flasher to indicate binding state. if ((getHopMode() == hoppingContinuous) && !rxLEDState) { // We have bound, set the LED on rxLEDState = TRUE; vAHI_DioSetOutput(rxHardware.ledBit, 0); } else { if (frameCounter > 5000) // We have timed out rxLEDFlashLimit = LED_FLASH_SLOW; // If the count exceeds the current limit, toggle the LED state if (++rxLEDFlashCount > rxLEDFlashLimit) { // Toggle the state flag rxLEDState = !rxLEDState; // Set the output accordingly if (rxLEDState) vAHI_DioSetOutput(rxHardware.ledBit, 0); else vAHI_DioSetOutput(0, rxHardware.ledBit); // Reset the counter for the next cycle rxLEDFlashCount = 0; } } }