@@ -223,63 +223,74 @@ static void ComputePingOffset( uint64_t beaconTime, uint32_t address, uint16_t p
223223 *
224224 * \param [IN] channel The channel according to the channel plan.
225225 *
226+ * \param [IN] isBeacon Set to true, if the function shall
227+ * calculate the frequency for a beacon.
228+ *
226229 * \retval The downlink frequency
227230 */
228- static uint32_t CalcDownlinkFrequency ( uint8_t channel )
231+ static uint32_t CalcDownlinkFrequency ( uint8_t channel , bool isBeacon )
229232{
230233 GetPhyParams_t getPhy ;
231234 PhyParam_t phyParam ;
232- uint32_t frequency = 0 ;
233- uint32_t stepwidth = 0 ;
234235
235- getPhy .Attribute = PHY_BEACON_CHANNEL_FREQ ;
236- phyParam = RegionGetPhyParam ( * Ctx .LoRaMacClassBParams .LoRaMacRegion , & getPhy );
237- frequency = phyParam .Value ;
236+ getPhy .Attribute = PHY_PING_SLOT_CHANNEL_FREQ ;
238237
239- getPhy .Attribute = PHY_BEACON_CHANNEL_STEPWIDTH ;
238+ if ( isBeacon == true )
239+ {
240+ getPhy .Attribute = PHY_BEACON_CHANNEL_FREQ ;
241+ }
242+ getPhy .Channel = channel ;
240243 phyParam = RegionGetPhyParam ( * Ctx .LoRaMacClassBParams .LoRaMacRegion , & getPhy );
241- stepwidth = phyParam .Value ;
242244
243- // Calculate the frequency
244- return frequency + ( channel * stepwidth );
245+ return phyParam .Value ;
245246}
246247
247248/*!
248249 * \brief Calculates the downlink channel for the beacon and for
249250 * ping slot downlinks.
250251 *
251- * \param [IN] devAddr The address of the device
252+ * \param [IN] devAddr The address of the device. Assign 0 if its a beacon.
252253 *
253254 * \param [IN] beaconTime The beacon time of the beacon.
254255 *
255- * \param [IN] beaconInterval The beacon interval
256+ * \param [IN] beaconInterval The beacon interval.
257+ *
258+ * \param [IN] isBeacon Set to true, if the function shall
259+ * calculate the frequency for a beacon.
256260 *
257261 * \retval The downlink channel
258262 */
259- static uint32_t CalcDownlinkChannelAndFrequency ( uint32_t devAddr , TimerTime_t beaconTime , TimerTime_t beaconInterval )
263+ static uint32_t CalcDownlinkChannelAndFrequency ( uint32_t devAddr , TimerTime_t beaconTime ,
264+ TimerTime_t beaconInterval , bool isBeacon )
260265{
261266 GetPhyParams_t getPhy ;
262267 PhyParam_t phyParam ;
263268 uint32_t channel = 0 ;
264269 uint8_t nbChannels = 0 ;
265- uint32_t frequency = 0 ;
266270
267- getPhy .Attribute = PHY_BEACON_NB_CHANNELS ;
271+ // Default initialization - ping slot channels
272+ getPhy .Attribute = PHY_PING_SLOT_NB_CHANNELS ;
273+
274+ if ( isBeacon == true )
275+ {
276+ // Beacon channels
277+ getPhy .Attribute = PHY_BEACON_NB_CHANNELS ;
278+ }
268279 phyParam = RegionGetPhyParam ( * Ctx .LoRaMacClassBParams .LoRaMacRegion , & getPhy );
269- nbChannels = (uint8_t ) phyParam .Value ;
280+ nbChannels = ( uint8_t ) phyParam .Value ;
270281
282+ // nbChannels is > 1, when the channel plan requires more than one possible channel
283+ // defined by the calculation below.
271284 if ( nbChannels > 1 )
272285 {
273286 // Calculate the channel for the next downlink
274287 channel = devAddr + ( beaconTime / ( beaconInterval / 1000 ) );
275288 channel = channel % nbChannels ;
276289 }
277290
278- // Calculate the frequency for the next downlink
279- frequency = CalcDownlinkFrequency ( channel );
280-
281- // Calculate the frequency for the next downlink
282- return frequency ;
291+ // Calculate the frequency for the next downlink. This holds
292+ // for beacons and ping slots.
293+ return CalcDownlinkFrequency ( channel , isBeacon );
283294}
284295
285296/*!
@@ -302,13 +313,13 @@ static void RxBeaconSetup( TimerTime_t rxTime, bool activateDefaultChannel )
302313 {
303314 // This is the default frequency in case we don't know when the next
304315 // beacon will be transmitted. We select channel 0 as default.
305- frequency = CalcDownlinkFrequency ( 0 );
316+ frequency = CalcDownlinkFrequency ( 0 , true );
306317 }
307318 else
308319 {
309320 // This is the frequency according to the channel plan
310321 frequency = CalcDownlinkChannelAndFrequency ( 0 , Ctx .BeaconCtx .BeaconTime .Seconds + ( CLASSB_BEACON_INTERVAL / 1000 ),
311- CLASSB_BEACON_INTERVAL );
322+ CLASSB_BEACON_INTERVAL , true );
312323 }
313324
314325 if ( Ctx .NvmCtx -> BeaconCtx .Ctrl .CustomFreq == 1 )
@@ -321,7 +332,7 @@ static void RxBeaconSetup( TimerTime_t rxTime, bool activateDefaultChannel )
321332 {
322333 // Set the frequency which was provided by BeaconTimingAns MAC command
323334 Ctx .BeaconCtx .Ctrl .BeaconChannelSet = 0 ;
324- frequency = CalcDownlinkFrequency ( Ctx .BeaconCtx .BeaconTimingChannel );
335+ frequency = CalcDownlinkFrequency ( Ctx .BeaconCtx .BeaconTimingChannel , true );
325336 }
326337
327338 if ( ( Ctx .BeaconCtx .Ctrl .BeaconAcquired == 1 ) || ( Ctx .BeaconCtx .Ctrl .AcquisitionPending == 1 ) )
@@ -1021,7 +1032,8 @@ static void LoRaMacClassBProcessPingSlot( void )
10211032 if ( Ctx .NvmCtx -> PingSlotCtx .Ctrl .CustomFreq == 0 )
10221033 {
10231034 // Restore floor plan
1024- frequency = CalcDownlinkChannelAndFrequency ( * Ctx .LoRaMacClassBParams .LoRaMacDevAddr , Ctx .BeaconCtx .BeaconTime .Seconds , CLASSB_BEACON_INTERVAL );
1035+ frequency = CalcDownlinkChannelAndFrequency ( * Ctx .LoRaMacClassBParams .LoRaMacDevAddr , Ctx .BeaconCtx .BeaconTime .Seconds ,
1036+ CLASSB_BEACON_INTERVAL , false );
10251037 }
10261038
10271039 // Open the ping slot window only, if there is no multicast ping slot
@@ -1178,7 +1190,8 @@ static void LoRaMacClassBProcessMulticastSlot( void )
11781190 if ( frequency == 0 )
11791191 {
11801192 // Restore floor plan
1181- frequency = CalcDownlinkChannelAndFrequency ( Ctx .PingSlotCtx .NextMulticastChannel -> ChannelParams .Address , Ctx .BeaconCtx .BeaconTime .Seconds , CLASSB_BEACON_INTERVAL );
1193+ frequency = CalcDownlinkChannelAndFrequency ( Ctx .PingSlotCtx .NextMulticastChannel -> ChannelParams .Address ,
1194+ Ctx .BeaconCtx .BeaconTime .Seconds , CLASSB_BEACON_INTERVAL , false );
11821195 }
11831196
11841197 Ctx .MulticastSlotState = PINGSLOT_STATE_RX ;
0 commit comments