Skip to content

File syn_sleep.h

FileList > src > syntropic > system > syn_sleep.h

Go to the source code of this file

Sleep coordinator — low-power mode management. More...

  • #include "../common/syn_defs.h"
  • #include <stdbool.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_Sleep
Sleep coordinator — tracks wake locks and sleep statistics.

Public Types

Type Name
enum SYN_SleepMode
Sleep depth selector.

Public Functions

Type Name
void syn_port_sleep (SYN_SleepMode mode)
Enter low-power mode. Implement for your platform.

Public Static Functions

Type Name
bool syn_sleep_any_locked (const SYN_Sleep * s)
Check if any lock is held.
void syn_sleep_enable (SYN_Sleep * s, bool en)
Enable/disable sleep globally.
bool syn_sleep_enter (SYN_Sleep * s)
Try to enter sleep mode.
void syn_sleep_init (SYN_Sleep * s, SYN_SleepMode max_depth)
Initialize the sleep coordinator.
bool syn_sleep_is_locked (const SYN_Sleep * s, uint32_t lock_id)
Check if a specific lock is held.
void syn_sleep_lock (SYN_Sleep * s, uint32_t lock_id)
Acquire a wake lock (prevents sleep).
uint32_t syn_sleep_locks (const SYN_Sleep * s)
Get active lock mask (for debug).
void syn_sleep_unlock (SYN_Sleep * s, uint32_t lock_id)
Release a wake lock.

Macros

Type Name
define SYN_SLEEP_LOCK_ADC (1u &lt;&lt; 4)
define SYN_SLEEP_LOCK_APP0 (1u &lt;&lt; 8)
define SYN_SLEEP_LOCK_APP1 (1u &lt;&lt; 9)
define SYN_SLEEP_LOCK_APP2 (1u &lt;&lt; 10)
define SYN_SLEEP_LOCK_APP3 (1u &lt;&lt; 11)
define SYN_SLEEP_LOCK_DMA (1u &lt;&lt; 3)
define SYN_SLEEP_LOCK_I2C (1u &lt;&lt; 2)
define SYN_SLEEP_LOCK_SPI (1u &lt;&lt; 1)
define SYN_SLEEP_LOCK_TIMER (1u &lt;&lt; 5)
define SYN_SLEEP_LOCK_UART (1u &lt;&lt; 0)

Detailed Description

Coordinates sleep entry with the scheduler. When all tasks are idle (or only timer-blocked), the system can enter a low-power mode. Clients can veto sleep by holding a "stay awake" lock.

** **

  • Each subsystem that needs the CPU awake holds a lock.
  • When all locks are released, the scheduler calls syn_sleep_enter() which invokes the port sleep function.
  • Interrupts wake the MCU, and the scheduler resumes.

** **

SYN_Sleep sleep;
syn_sleep_init(&sleep, SYN_SLEEP_LIGHT);

// UART ISR: hold lock while transmitting
syn_sleep_lock(&sleep, SYN_SLEEP_LOCK_UART);
// ... transmit complete ...
syn_sleep_unlock(&sleep, SYN_SLEEP_LOCK_UART);

// In scheduler idle hook:
syn_sleep_enter(&sleep);  // sleeps only if no locks held

Public Types Documentation

enum SYN_SleepMode

Sleep depth selector.

enum SYN_SleepMode {
    SYN_SLEEP_NONE = 0,
    SYN_SLEEP_LIGHT = 1,
    SYN_SLEEP_DEEP = 2
};


Public Functions Documentation

function syn_port_sleep

Enter low-power mode. Implement for your platform.

void syn_port_sleep (
    SYN_SleepMode mode
) 

For Cortex-M: SYN_SLEEP_LIGHT → WFI, SYN_SLEEP_DEEP → STOP mode. Must return when an interrupt wakes the MCU.

Parameters:

  • mode Requested sleep depth.

Public Static Functions Documentation

function syn_sleep_any_locked

Check if any lock is held.

static inline bool syn_sleep_any_locked (
    const SYN_Sleep * s
) 

Parameters:

  • s Sleep instance.

Returns:

true if any lock is active.


function syn_sleep_enable

Enable/disable sleep globally.

static inline void syn_sleep_enable (
    SYN_Sleep * s,
    bool en
) 

Parameters:

  • s Sleep instance.
  • en true to enable, false to disable.

function syn_sleep_enter

Try to enter sleep mode.

static inline bool syn_sleep_enter (
    SYN_Sleep * s
) 

If no locks are held and sleep is enabled, calls syn_port_sleep().

Parameters:

  • s Sleep instance.

Returns:

true if we actually slept, false if vetoed.


function syn_sleep_init

Initialize the sleep coordinator.

static inline void syn_sleep_init (
    SYN_Sleep * s,
    SYN_SleepMode max_depth
) 

Parameters:

  • s Sleep instance.
  • max_depth Deepest sleep mode allowed.

function syn_sleep_is_locked

Check if a specific lock is held.

static inline bool syn_sleep_is_locked (
    const SYN_Sleep * s,
    uint32_t lock_id
) 

Parameters:

  • s Sleep instance.
  • lock_id Lock bitmask to check.

Returns:

true if the lock is active.


function syn_sleep_lock

Acquire a wake lock (prevents sleep).

static inline void syn_sleep_lock (
    SYN_Sleep * s,
    uint32_t lock_id
) 

Parameters:

  • s Sleep instance.
  • lock_id Lock bitmask (e.g. SYN_SLEEP_LOCK_UART).

function syn_sleep_locks

Get active lock mask (for debug).

static inline uint32_t syn_sleep_locks (
    const SYN_Sleep * s
) 

Parameters:

  • s Sleep instance.

Returns:

Active lock bitmask.


function syn_sleep_unlock

Release a wake lock.

static inline void syn_sleep_unlock (
    SYN_Sleep * s,
    uint32_t lock_id
) 

Parameters:

  • s Sleep instance.
  • lock_id Lock bitmask to release.

Macro Definition Documentation

define SYN_SLEEP_LOCK_ADC

#define SYN_SLEEP_LOCK_ADC `(1u << 4)`

ADC conversion in progress


define SYN_SLEEP_LOCK_APP0

#define SYN_SLEEP_LOCK_APP0 `(1u << 8)`

Application-defined lock 0


define SYN_SLEEP_LOCK_APP1

#define SYN_SLEEP_LOCK_APP1 `(1u << 9)`

Application-defined lock 1


define SYN_SLEEP_LOCK_APP2

#define SYN_SLEEP_LOCK_APP2 `(1u << 10)`

Application-defined lock 2


define SYN_SLEEP_LOCK_APP3

#define SYN_SLEEP_LOCK_APP3 `(1u << 11)`

Application-defined lock 3


define SYN_SLEEP_LOCK_DMA

#define SYN_SLEEP_LOCK_DMA `(1u << 3)`

DMA transfer in progress


define SYN_SLEEP_LOCK_I2C

#define SYN_SLEEP_LOCK_I2C `(1u << 2)`

I2C peripheral active


define SYN_SLEEP_LOCK_SPI

#define SYN_SLEEP_LOCK_SPI `(1u << 1)`

SPI peripheral active


define SYN_SLEEP_LOCK_TIMER

#define SYN_SLEEP_LOCK_TIMER `(1u << 5)`

Hardware timer active


define SYN_SLEEP_LOCK_UART

#define SYN_SLEEP_LOCK_UART `(1u << 0)`

UART peripheral active



The documentation for this class was generated from the following file src/syntropic/system/syn_sleep.h