Skip to content
Permalink
Browse files

Merge pull request #1 from particle-iot/fix/nrf-802154-csma-rand-in-isr

[nrf52840] radio: rand() should not be used from an ISR, replaces with rand_r() and externally provided seed for now
  • Loading branch information...
avtolstoy committed May 19, 2019
2 parents ea382ea + 733e474 commit 0eab4ecddd8208741ae5856275e2dc2bee8e0838
@@ -34,6 +34,8 @@
*
*/

#define _POSIX_C_SOURCE // make sure that POSIX rand_r() is declared

#include "nrf_802154_csma_ca.h"

#include <assert.h>
@@ -57,6 +59,8 @@ static const uint8_t * mp_psdu; ///< Pointer to PSDU of the frame being
static nrf_802154_timer_t m_timer; ///< Timer used to back off during CSMA-CA procedure.
static bool m_is_running; ///< Indicates if CSMA-CA procedure is running.

unsigned int nrf_802154_csma_ca_random_seed = 0; ///< Random seed used for backoff timeout calculation. NOTE: needs to be externally initialized!

/**
* @brief Perform appropriate actions for busy channel conditions.
*
@@ -137,7 +141,8 @@ static void frame_transmit(void * p_context)
*/
static void random_backoff_start(void)
{
uint8_t backoff_periods = rand() % (1 << m_be);
assert(nrf_802154_csma_ca_random_seed != 0);
uint8_t backoff_periods = rand_r(&nrf_802154_csma_ca_random_seed) % (1 << m_be);

m_timer.callback = frame_transmit;
m_timer.p_context = NULL;
@@ -44,6 +44,8 @@
* @brief CSMA-CA procedure.
*/

extern unsigned int nrf_802154_csma_ca_random_seed;

/**
* @brief Start CSMA-CA procedure for transmission of given frame.
*

0 comments on commit 0eab4ec

Please sign in to comment.
You can’t perform that action at this time.