Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@13:a37b0e4f51ca, 2018-11-06 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 06 16:30:26 2018 +0000
- Revision:
- 13:a37b0e4f51ca
- Parent:
- 0:3114f82feb68
- Child:
- 15:86c93f5d67c4
Merge pull request #13 from adbridge/master
Updating mbed-os to mbed-os-5.10.3
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-cpu-stats
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mbed_official | 0:3114f82feb68 | 1 | #include "mbed.h" |
| mbed_official | 0:3114f82feb68 | 2 | |
| mbed_official | 0:3114f82feb68 | 3 | #if !defined(MBED_CPU_STATS_ENABLED) || !defined(DEVICE_LPTICKER) || !defined(DEVICE_SLEEP) |
| mbed_official | 0:3114f82feb68 | 4 | #error [NOT_SUPPORTED] test not supported |
| mbed_official | 0:3114f82feb68 | 5 | #endif |
| mbed_official | 0:3114f82feb68 | 6 | |
| mbed_official | 0:3114f82feb68 | 7 | DigitalOut led1(LED1); |
| mbed_official | 0:3114f82feb68 | 8 | |
| mbed_official | 0:3114f82feb68 | 9 | #define MAX_THREAD_STACK 384 |
| mbed_official | 0:3114f82feb68 | 10 | #define SAMPLE_TIME 1000 |
| mbed_official | 0:3114f82feb68 | 11 | #define LOOP_TIME 3000 |
| mbed_official | 0:3114f82feb68 | 12 | |
| mbed_official | 0:3114f82feb68 | 13 | uint64_t prev_idle_time = 0; |
| mbed_official | 0:3114f82feb68 | 14 | int32_t wait_time = 5000; |
| mbed_official | 0:3114f82feb68 | 15 | |
| mbed_official | 0:3114f82feb68 | 16 | void busy_thread() |
| mbed_official | 0:3114f82feb68 | 17 | { |
| mbed_official | 0:3114f82feb68 | 18 | volatile uint64_t i = ~0; |
| mbed_official | 0:3114f82feb68 | 19 | |
| mbed_official | 0:3114f82feb68 | 20 | while(i--) { |
| mbed_official | 0:3114f82feb68 | 21 | led1 = !led1; |
| mbed_official | 0:3114f82feb68 | 22 | wait_us(wait_time); |
| mbed_official | 0:3114f82feb68 | 23 | } |
| mbed_official | 0:3114f82feb68 | 24 | } |
| mbed_official | 0:3114f82feb68 | 25 | |
| mbed_official | 0:3114f82feb68 | 26 | void print_stats() |
| mbed_official | 0:3114f82feb68 | 27 | { |
| mbed_official | 0:3114f82feb68 | 28 | mbed_stats_cpu_t stats; |
| mbed_official | 0:3114f82feb68 | 29 | mbed_stats_cpu_get(&stats); |
| mbed_official | 0:3114f82feb68 | 30 | |
| mbed_official | 0:3114f82feb68 | 31 | printf("%-20lld", stats.uptime); |
| mbed_official | 0:3114f82feb68 | 32 | printf("%-20lld", stats.idle_time); |
| mbed_official | 0:3114f82feb68 | 33 | printf("%-20lld", stats.sleep_time); |
| mbed_official | 0:3114f82feb68 | 34 | printf("%-20lld\n", stats.deep_sleep_time); |
| mbed_official | 0:3114f82feb68 | 35 | } |
| mbed_official | 0:3114f82feb68 | 36 | |
| mbed_official | 0:3114f82feb68 | 37 | int main() |
| mbed_official | 0:3114f82feb68 | 38 | { |
| mbed_official | 0:3114f82feb68 | 39 | // Request the shared queue |
| mbed_official | 0:3114f82feb68 | 40 | EventQueue *stats_queue = mbed_event_queue(); |
| mbed_official | 0:3114f82feb68 | 41 | Thread *thread; |
| mbed_official | 0:3114f82feb68 | 42 | int id; |
| mbed_official | 0:3114f82feb68 | 43 | |
| mbed_official | 0:3114f82feb68 | 44 | id = stats_queue->call_every(SAMPLE_TIME, print_stats); |
| mbed_official | 0:3114f82feb68 | 45 | printf("%-20s%-20s%-20s%-20s\n", "Uptime", "Idle Time", "Sleep time", "DeepSleep time"); |
| mbed_official | 0:3114f82feb68 | 46 | |
| mbed_official | 0:3114f82feb68 | 47 | thread = new Thread(osPriorityNormal, MAX_THREAD_STACK); |
| mbed_official | 0:3114f82feb68 | 48 | thread->start(busy_thread); |
| mbed_official | 0:3114f82feb68 | 49 | |
| mbed_official | 0:3114f82feb68 | 50 | // Steadily increase the system load |
| mbed_official | 0:3114f82feb68 | 51 | for (int count = 1; ; count++) { |
| mbed_official | 0:3114f82feb68 | 52 | Thread::wait(LOOP_TIME); |
| mbed_official | 0:3114f82feb68 | 53 | if (wait_time <= 0) { |
| mbed_official | 0:3114f82feb68 | 54 | break; |
| mbed_official | 0:3114f82feb68 | 55 | } |
| mbed_official | 0:3114f82feb68 | 56 | wait_time -= 1000; |
| mbed_official | 0:3114f82feb68 | 57 | } |
| mbed_official | 0:3114f82feb68 | 58 | thread->terminate(); |
| mbed_official | 0:3114f82feb68 | 59 | stats_queue->cancel(id); |
| mbed_official | 0:3114f82feb68 | 60 | return 0; |
| mbed_official | 0:3114f82feb68 | 61 | } |