void _init_loramac(semtech_loramac_t *mac, LoRaMacPrimitives_t * primitives, LoRaMacCallback_t *callbacks) { mutex_lock(&mac->lock); DEBUG("[semtech-loramac] initializing loramac\n"); primitives->MacMcpsConfirm = mcps_confirm; primitives->MacMcpsIndication = mcps_indication; primitives->MacMlmeConfirm = mlme_confirm; primitives->MacMlmeIndication = mlme_indication; LoRaMacInitialization(&semtech_loramac_radio_events, primitives, callbacks, LORAMAC_ACTIVE_REGION); #ifdef DISABLE_LORAMAC_DUTYCYCLE LoRaMacTestSetDutyCycleOn(false); #endif mutex_unlock(&mac->lock); semtech_loramac_set_dr(mac, LORAMAC_DEFAULT_DR); semtech_loramac_set_adr(mac, LORAMAC_DEFAULT_ADR); semtech_loramac_set_public_network(mac, LORAMAC_DEFAULT_PUBLIC_NETWORK); semtech_loramac_set_class(mac, LORAMAC_DEFAULT_DEVICE_CLASS); semtech_loramac_set_tx_port(mac, LORAMAC_DEFAULT_TX_PORT); semtech_loramac_set_tx_mode(mac, LORAMAC_DEFAULT_TX_MODE); semtech_loramac_set_system_max_rx_error(mac, LORAMAC_DEFAULT_SYSTEM_MAX_RX_ERROR); semtech_loramac_set_min_rx_symbols(mac, LORAMAC_DEFAULT_MIN_RX_SYMBOLS); mac->link_chk.available = false; #ifdef MODULE_PERIPH_EEPROM _read_loramac_config(mac); #endif }
void LoRaMacInit( LoRaMacCallbacks_t *callbacks ) { LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; LoRaMacPrimitives.MacMcpsIndication = McpsIndication; LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacCallbacks.MacEvent = callbacks->MacEvent; LoRaMacCallbacks.GetBatteryLevel = callbacks->GetBatteryLevel; LoRaMacCallback.GetBatteryLevel = callbacks->GetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallback ); }
int main( void ) { HAL_EnableDBGStopMode(); LowPowerConfiguration(); pc.baud(115200); // print banner pc.printf("\r\n============== DEBUG STARTED ==============\r\n"); /** User button triggers the ultrasonic sensor */ //PinDetect button(PC_13,PullUp); //button.attach_asserted(&sensor, &UltraSonic::triggerSample); // callback routine to trigger usonic //button.setSampleFrequency(); LoRaMacPrimitives_t LoRaMacPrimitives; LoRaMacCallback_t LoRaMacCallbacks; MibRequestConfirm_t mibReq; //BoardInitMcu( ); //BoardInitPeriph( ); BoardInit( ); TimerInit( &mainLoopTimeout, onMainLoopTimeoutEvent ); DeviceState = DEVICE_STATE_INIT; while( 1 ) { wait(0.5); // This is a kind of watchdog on the main loop, if the timer isn't cancelled in x seconds then loop will enter INIT state TimerSetValue( &mainLoopTimeout, 3000000 ); TimerStart( &mainLoopTimeout ); switch( DeviceState ) { case DEVICE_STATE_INIT: { pc.printf("DEVICE_STATE_INIT\r\n"); LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; LoRaMacPrimitives.MacMcpsIndication = McpsIndication; LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks ); TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &ultrasonicTimer, OnUltrasonicTimeout ); mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); // MAC information base service to set attributes of LoRaMac layer mibReq.Type = MIB_PUBLIC_NETWORK; mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; LoRaMacMibSetRequestConfirm( &mibReq ); sensor.distanceAvailable = false; // Ensure initialised to false after power up DeviceState = DEVICE_STATE_JOIN; break; } case DEVICE_STATE_JOIN: { pc.printf("DEVICE_STATE_JOIN\r\n"); #if( OVER_THE_AIR_ACTIVATION != 0 ) pc.printf("OTA\r\n"); MlmeReq_t mlmeReq; // Initialize LoRaMac device unique ID //BoardGetUniqueId( DevEui ); mlmeReq.Type = MLME_JOIN; // MAC management service types are MLME_JOIN or MLME_LINK_CHECK mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; if( NextTx == true ) { LoRaMacMlmeRequest( &mlmeReq ); // Sends a join request } // Schedule next packet transmission //TxDutyCycleTime = OVER_THE_AIR_ACTIVATION_DUTYCYCLE; //DeviceState = DEVICE_STATE_CYCLE; DeviceState = DEVICE_STATE_CYCLE; #else pc.printf("ABP\r\n"); // Choose a random device address if not already defined in Comissioning.h if( DevAddr == 0 ) { // Random seed initialization // srand1( BoardGetRandomSeed( ) ); // Choose a random device address DevAddr = randr( 0, 0x01FFFFFF ); } mibReq.Type = MIB_NET_ID; mibReq.Param.NetID = LORAWAN_NETWORK_ID; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_DEV_ADDR; mibReq.Param.DevAddr = DevAddr; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NWK_SKEY; mibReq.Param.NwkSKey = NwkSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_APP_SKEY; mibReq.Param.AppSKey = AppSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NETWORK_JOINED; mibReq.Param.IsNetworkJoined = true; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_SEND; #endif break; } case DEVICE_STATE_SEND: { pc.printf("DEVICE_STATE_SEND\r\n"); if( NextTx == true ) { pc.printf("Sending\r\n"); PrepareTxFrame( AppPort ); NextTx = SendFrame( ); } } case DEVICE_STATE_CYCLE: { pc.printf("DEVICE_STATE_CYCLE\r\n"); pc.printf("SensorState = %d, distanceAvailable = ",SensorState); if (sensor.distanceAvailable) pc.printf("true\r\n"); else pc.printf("false\r\n"); pc.printf("LoRaMacState = %d\r\n",GetMacStatus()); if ((SensorState == SENT) || (SensorState == INIT)) { /** Range and Temperature readings have been sent so we can sleep */ TimerStop( &TxNextPacketTimer ); // Wait for MAC state to become idle before deep sleeping if (GetMacStatus() == 0) { // Ensure MAC state is idle before sleeping pc.printf("DeepSleep ..zzz.\r\n"); TimerStop(&mainLoopTimeout); // cancel main loop guard timeout WakeUp::set(DEEPSLEEP_SECONDS); // Set RTC alarm to wake up from deep sleep LowPowerPrep(); deepsleep(); // Deep sleep until wake up alarm from RTC LowPowerRestore(); SensorState = TRIGGERED; /* Trigger ultrasonic sensor and start sensor read failure timer */ sensor.triggerSample(); // Get Ultrasonic reading TimerSetValue( &ultrasonicTimer, 2000000 ); // 2s timeout TimerStart( &ultrasonicTimer ); DeviceState = DEVICE_STATE_SLEEP; // Cycle in sleep until sensor readings ready } else { // Cycle around until MAC state is idle DeviceState = DEVICE_STATE_CYCLE; } } else { // Error shouldn't get here pc.printf("Error State!!\r\n"); //TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); //TimerStart( &TxNextPacketTimer ); SensorState = INIT; DeviceState = DEVICE_STATE_SLEEP; } break; } case DEVICE_STATE_SLEEP: { pc.printf("DEVICE_STATE_SLEEP\r\n"); // Loop until sensor ready if ((SensorState == TRIGGERED) && (sensor.distanceAvailable)) { pc.printf("Xmit Reading..!\r\n"); TimerStop( &ultrasonicTimer ); // stop the sensor failure timer SensorState = SENDING; TimerSetValue( &TxNextPacketTimer, 10 ); // Schedule immediate transmission TimerStart( &TxNextPacketTimer ); } break; } default: { DeviceState = DEVICE_STATE_INIT; break; } } } }
/** * Main application entry point. */ int main( void ) { LoRaMacPrimitives_t LoRaMacPrimitives; LoRaMacCallback_t LoRaMacCallbacks; MibRequestConfirm_t mibReq; BoardInitMcu( ); BoardInitPeriph( ); DeviceState = DEVICE_STATE_INIT; while( 1 ) { switch( DeviceState ) { case DEVICE_STATE_INIT: { LoRaMacPrimitives.MacMcpsConfirm = McpsConfirm; LoRaMacPrimitives.MacMcpsIndication = McpsIndication; LoRaMacPrimitives.MacMlmeConfirm = MlmeConfirm; LoRaMacPrimitives.MacMlmeIndication = MlmeIndication; LoRaMacCallbacks.GetBatteryLevel = BoardGetBatteryLevel; LoRaMacInitialization( &LoRaMacPrimitives, &LoRaMacCallbacks, ACTIVE_REGION ); TimerInit( &TxNextPacketTimer, OnTxNextPacketTimerEvent ); TimerInit( &Led1Timer, OnLed1TimerEvent ); TimerSetValue( &Led1Timer, 25 ); TimerInit( &Led2Timer, OnLed2TimerEvent ); TimerSetValue( &Led2Timer, 25 ); mibReq.Type = MIB_ADR; mibReq.Param.AdrEnable = LORAWAN_ADR_ON; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_PUBLIC_NETWORK; mibReq.Param.EnablePublicNetwork = LORAWAN_PUBLIC_NETWORK; LoRaMacMibSetRequestConfirm( &mibReq ); #if defined( REGION_EU868 ) LoRaMacTestSetDutyCycleOn( LORAWAN_DUTYCYCLE_ON ); #endif mibReq.Type = MIB_DEVICE_CLASS; mibReq.Param.Class = CLASS_C; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_JOIN; break; } case DEVICE_STATE_JOIN: { #if( OVER_THE_AIR_ACTIVATION != 0 ) MlmeReq_t mlmeReq; // Initialize LoRaMac device unique ID BoardGetUniqueId( DevEui ); mlmeReq.Type = MLME_JOIN; mlmeReq.Req.Join.DevEui = DevEui; mlmeReq.Req.Join.AppEui = AppEui; mlmeReq.Req.Join.AppKey = AppKey; mlmeReq.Req.Join.Datarate = LORAWAN_DEFAULT_DATARATE; if( LoRaMacMlmeRequest( &mlmeReq ) == LORAMAC_STATUS_OK ) { DeviceState = DEVICE_STATE_SLEEP; } else { DeviceState = DEVICE_STATE_CYCLE; } #else // Choose a random device address if not already defined in Commissioning.h if( DevAddr == 0 ) { // Random seed initialization srand1( BoardGetRandomSeed( ) ); // Choose a random device address DevAddr = randr( 0, 0x01FFFFFF ); } mibReq.Type = MIB_NET_ID; mibReq.Param.NetID = LORAWAN_NETWORK_ID; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_DEV_ADDR; mibReq.Param.DevAddr = DevAddr; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NWK_SKEY; mibReq.Param.NwkSKey = NwkSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_APP_SKEY; mibReq.Param.AppSKey = AppSKey; LoRaMacMibSetRequestConfirm( &mibReq ); mibReq.Type = MIB_NETWORK_JOINED; mibReq.Param.IsNetworkJoined = true; LoRaMacMibSetRequestConfirm( &mibReq ); DeviceState = DEVICE_STATE_SEND; #endif break; } case DEVICE_STATE_SEND: { if( NextTx == true ) { PrepareTxFrame( AppPort ); NextTx = SendFrame( ); } if( ComplianceTest.Running == true ) { // Schedule next packet transmission TxDutyCycleTime = 5000; // 5000 ms } else { // Schedule next packet transmission TxDutyCycleTime = APP_TX_DUTYCYCLE + randr( -APP_TX_DUTYCYCLE_RND, APP_TX_DUTYCYCLE_RND ); } DeviceState = DEVICE_STATE_CYCLE; break; } case DEVICE_STATE_CYCLE: { DeviceState = DEVICE_STATE_SLEEP; // Schedule next packet transmission TimerSetValue( &TxNextPacketTimer, TxDutyCycleTime ); TimerStart( &TxNextPacketTimer ); break; } case DEVICE_STATE_SLEEP: { // Wake up through events TimerLowPowerHandler( ); // Process Radio IRQ Radio.IrqProcess( ); break; } default: { DeviceState = DEVICE_STATE_INIT; break; } } } }