> For the complete documentation index, see [llms.txt](https://sdk.twelite.info/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://sdk.twelite.info/en/twelite-net-api-ref/twelite-net-guan-shu/toconet_vsleep.md).

# ToCoNet\_vSleep()

## Explanation

Sleep with [WakeTimer](/en/hw-api-ref/perifuraru/waketimer.md).

#### About wake-up interruptions

It is also possible to set an interrupt source other than WakeTimer. It is set beforehand before calling this function.

#### E\_EVENT\_TOCONET\_ON\_SLEEP event

Just before sleep, `E_EVENT_TOCONET_ON_SLEEP` is passed to each [user-defined event processing function.](/en/twelite-net-api-ref/yzaibento.md)

{% hint style="warning" %}
To prevent `ToCoNet_vSleep()` from being called recursively, the following code example handles `E_EVENT_NEW_STATE`.
{% endhint %}

```c
void vMyProcessEv(...) {
    ...
    case E_STATE_SLEEPING:　
        if (eEvent == E_EVENT_NEW_STATE) { 
            ToCoNet_vSleep(…);
        }
        break;
    ...
}
```

#### About sleep time

The WakeTimer works with the accuracy of a 32 kHz oscillator, so the accuracy of the oscillator is very important for the wake-up accuracy. The built-in oscillator must be calibrated before use (unless otherwise specified in `sToCoNet_AppContext`, in which case it will be executed in the TWENET library on cold start).

{% hint style="warning" %}
Extremely short sleep intervals cannot be set. The lower limit is approximately 30ms.

The wake-up time depends on the accuracy of the WakeTimer, but even in the best case there is an error of about ±0.1%.
{% endhint %}

## Arguments

| Type     | Name          | Remark                                                                                                                                                                                                                                                                                                                                                                                                            |
| -------- | ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `uint8`  | `u8Device`    | Wake-up timer to be used.                                                                                                                                                                                                                                                                                                                                                                                         |
| `uint32` | `u32Periodms` | Specifies the sleep period in ms.                                                                                                                                                                                                                                                                                                                                                                                 |
| `bool_t` | `bPeriodic`   | Specifies whether periodic sleep should be performed. If cycle sleep is specified, the time elapsed since the last wake-up time is subtracted from the next wake-up time. If the next cycle has already passed, the sleep is performed for the specified period from the time of the call to this function, otherwise the sleep is performed for the specified period from the time of the call to this function. |
| `bool_t` | `bRamOff`     | If `FALSE`, sleep is performed with RAM retained. If `TRUE`, sleep is performed without retaining RAM.                                                                                                                                                                                                                                                                                                            |

## Returns

None

## Sample code

```c
// set UART Rx port as interrupt source
vAHI_DioSetDirection(u32DioPortWakeUp, 0); // set as input

// set DIO wakeup source
(void)u32AHI_DioInterruptStatus(); // clear interrupt register
vAHI_DioWakeEnable(u32DioPortWakeUp, 0); // enable ports
vAHI_DioWakeEdge(u32DioPortWakeUp, 0); // set edge (rising)

// 10sec
ToCoNet_vSleep(E_AHI_WAKE_TIMER_0, 10000, FALSE, FALSE); 
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://sdk.twelite.info/en/twelite-net-api-ref/twelite-net-guan-shu/toconet_vsleep.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
