sys/util.h

#include <sys/util.h>

This header includes defines for miscellaneous CE-specific things.

API Documentation

Miscellaneous CE define file.

Authors

Matt “MateoConLechuga” Waltz

Jacob “jacobly” Young

Defines

randInt(min, max)

Returns a pseudo-random integer in the range of min to max (inclusive).

Functions

uint32_t random(void)

Returns a pseudo-random 32-bit integer.

Returns

the random integer

void srandom(uint32_t seed)

Seeds the pseudo-random number generator used by random() and rand() with the value seed.

Parameters

seed[in] the seed value

uint32_t atomic_load_32(volatile uint32_t *p)

“Atomically” loads from a volatile 32-bit value.

Remark

The hardware does not provide a mechanism to truly atomically load from a 32-bit value. This “atomic” load is implemented by non-atomically reading the value twice and retrying if the values read differ.

Attention

If the maximum period between two value changes is 1us or less (assuming a CPU clock speed of 48MHz), then this function may never complete. For instance, the counter of a timer ticking at 1MHz or more should not be read using this function. In such a case of a purely increasing or decreasing value, atomic_load_increasing_32() or atomic_load_decreasing_32() may be appropriate instead.

Parameters

p[in] pointer to 32-bit value

uint32_t atomic_load_increasing_32(volatile uint32_t *p)

“Atomically” loads from a volatile, increasing 32-bit value.

Remark

The hardware does not provide a mechanism to truly atomically load from a 32-bit value. This “atomic” load is implemented by temporarily disabling interrupts while non-atomically reading the value twice and then returning the lesser of the two values read.

Attention

If the minimum period between two value changes is 5us or less and the value’s maximum rate of change over a 5us period exceeds 256 (assuming a CPU clock speed of 48MHz), then the value returned may be incorrect. Of relevant note may be the fact that a 48MHz counter does not exceed this limit.

Parameters

p[in] pointer to 32-bit value

uint32_t atomic_load_decreasing_32(volatile uint32_t *p)

“Atomically” loads from a volatile, decreasing 32-bit value.

Remark

The hardware does not provide a mechanism to truly atomically load from a 32-bit value. This “atomic” load is implemented by temporarily disabling interrupts while non-atomically reading the value twice and then returning the greater of the two values read.

Attention

If the minimum period between two value changes is 5us or less and the value’s maximum rate of change over a 5us period exceeds 256 (assuming a CPU clock speed of 48MHz), then the value returned may be incorrect. Of relevant note may be the fact that a 48MHz counter does not exceed this limit.

Parameters

p[in] pointer to 32-bit value