This is a very simple guide, reviewing the steps required to get Blinky working on an Mbed OS platform.

Mbed OS Blinky

This example shows the use of a DigitalOut object to represent an LED and use of the nonblocking Thread::wait() call. Using nonblocking calls is good practice because Mbed OS can schedule and run other threads while the first thread is waiting.

Building this example

Building with Arm Mbed CLI

To use Mbed CLI to build this example, follow the instructions in the documentation. The instructions here relate to using the Arm Online Compiler.

To use the Online Compiler, import this code into the Online Compiler, and select your platform from the top right. Compile the code using the compile button, load it onto your board and press the reset button on the board. The code will run on the board, and you will see the LED blink.

You can find more instructions for using the Mbed Online Compiler in the documentation.

Committer:
mbed_official
Date:
Wed Sep 04 12:00:04 2019 +0100
Revision:
98:f2d7e14c84a1
Parent:
89:448e37ce650a
Merge pull request #186 from hugueskamba/hk-fix-warnings

Clear all warnings generated by the example project files.
.
Commit copied from https://github.com/ARMmbed/mbed-os-example-blinky

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 82:abf1b1785bd7 1 /* mbed Microcontroller Library
mbed_official 82:abf1b1785bd7 2 * Copyright (c) 2018 ARM Limited
mbed_official 82:abf1b1785bd7 3 * SPDX-License-Identifier: Apache-2.0
mbed_official 82:abf1b1785bd7 4 */
mbed_official 82:abf1b1785bd7 5
mbed_official 82:abf1b1785bd7 6 #ifndef STATS_REPORT_H
mbed_official 98:f2d7e14c84a1 7 #define STATS_REPORT_H
mbed_official 82:abf1b1785bd7 8
mbed_official 98:f2d7e14c84a1 9 #include <inttypes.h>
mbed_official 82:abf1b1785bd7 10 #include "mbed.h"
mbed_official 82:abf1b1785bd7 11
mbed_official 82:abf1b1785bd7 12 /**
mbed_official 82:abf1b1785bd7 13 * System Reporting library. Provides runtime information on device:
mbed_official 82:abf1b1785bd7 14 * - CPU sleep, idle, and wake times
mbed_official 82:abf1b1785bd7 15 * - Heap and stack usage
mbed_official 82:abf1b1785bd7 16 * - Thread information
mbed_official 82:abf1b1785bd7 17 * - Static system information
mbed_official 82:abf1b1785bd7 18 */
mbed_official 82:abf1b1785bd7 19 class SystemReport {
mbed_official 82:abf1b1785bd7 20 mbed_stats_heap_t heap_stats;
mbed_official 82:abf1b1785bd7 21 mbed_stats_cpu_t cpu_stats;
mbed_official 82:abf1b1785bd7 22 mbed_stats_sys_t sys_stats;
mbed_official 82:abf1b1785bd7 23
mbed_official 82:abf1b1785bd7 24 mbed_stats_thread_t *thread_stats;
mbed_official 82:abf1b1785bd7 25 uint8_t thread_count;
mbed_official 82:abf1b1785bd7 26 uint8_t max_thread_count;
mbed_official 82:abf1b1785bd7 27 uint32_t sample_time_ms;
mbed_official 82:abf1b1785bd7 28
mbed_official 82:abf1b1785bd7 29 public:
mbed_official 82:abf1b1785bd7 30 /**
mbed_official 82:abf1b1785bd7 31 * SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
mbed_official 82:abf1b1785bd7 32 */
mbed_official 82:abf1b1785bd7 33 SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
mbed_official 82:abf1b1785bd7 34 {
mbed_official 82:abf1b1785bd7 35 thread_stats = new mbed_stats_thread_t[max_thread_count];
mbed_official 82:abf1b1785bd7 36
mbed_official 82:abf1b1785bd7 37 // Collect the static system information
mbed_official 82:abf1b1785bd7 38 mbed_stats_sys_get(&sys_stats);
mbed_official 82:abf1b1785bd7 39
mbed_official 82:abf1b1785bd7 40 printf("=============================== SYSTEM INFO ================================\r\n");
mbed_official 98:f2d7e14c84a1 41 printf("Mbed OS Version: %" PRIu32 " \r\n", sys_stats.os_version);
mbed_official 98:f2d7e14c84a1 42 printf("CPU ID: 0x%" PRIx32 " \r\n", sys_stats.cpu_id);
mbed_official 82:abf1b1785bd7 43 printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
mbed_official 98:f2d7e14c84a1 44 printf("Compiler Version: %" PRIu32 " \r\n", sys_stats.compiler_version);
mbed_official 89:448e37ce650a 45
mbed_official 89:448e37ce650a 46 for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
mbed_official 89:448e37ce650a 47 if (sys_stats.ram_size[i] != 0) {
mbed_official 98:f2d7e14c84a1 48 printf("RAM%d: Start 0x%" PRIx32 " Size: 0x%" PRIx32 " \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]);
mbed_official 89:448e37ce650a 49 }
mbed_official 89:448e37ce650a 50 }
mbed_official 89:448e37ce650a 51 for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
mbed_official 89:448e37ce650a 52 if (sys_stats.rom_size[i] != 0) {
mbed_official 98:f2d7e14c84a1 53 printf("ROM%d: Start 0x%" PRIx32 " Size: 0x%" PRIx32 " \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]);
mbed_official 89:448e37ce650a 54 }
mbed_official 89:448e37ce650a 55 }
mbed_official 82:abf1b1785bd7 56 }
mbed_official 82:abf1b1785bd7 57
mbed_official 82:abf1b1785bd7 58 ~SystemReport(void)
mbed_official 82:abf1b1785bd7 59 {
mbed_official 82:abf1b1785bd7 60 free(thread_stats);
mbed_official 82:abf1b1785bd7 61 }
mbed_official 82:abf1b1785bd7 62
mbed_official 82:abf1b1785bd7 63 /**
mbed_official 82:abf1b1785bd7 64 * Report on each Mbed OS Platform stats API
mbed_official 82:abf1b1785bd7 65 */
mbed_official 82:abf1b1785bd7 66 void report_state(void)
mbed_official 82:abf1b1785bd7 67 {
mbed_official 82:abf1b1785bd7 68 report_cpu_stats();
mbed_official 82:abf1b1785bd7 69 report_heap_stats();
mbed_official 82:abf1b1785bd7 70 report_thread_stats();
mbed_official 82:abf1b1785bd7 71
mbed_official 82:abf1b1785bd7 72 // Clear next line to separate subsequent report logs
mbed_official 82:abf1b1785bd7 73 printf("\r\n");
mbed_official 82:abf1b1785bd7 74 }
mbed_official 82:abf1b1785bd7 75
mbed_official 82:abf1b1785bd7 76 /**
mbed_official 82:abf1b1785bd7 77 * Report CPU idle and awake time in terms of percentage
mbed_official 82:abf1b1785bd7 78 */
mbed_official 82:abf1b1785bd7 79 void report_cpu_stats(void)
mbed_official 82:abf1b1785bd7 80 {
mbed_official 82:abf1b1785bd7 81 static uint64_t prev_idle_time = 0;
mbed_official 82:abf1b1785bd7 82
mbed_official 82:abf1b1785bd7 83 printf("================= CPU STATS =================\r\n");
mbed_official 82:abf1b1785bd7 84
mbed_official 82:abf1b1785bd7 85 // Collect and print cpu stats
mbed_official 82:abf1b1785bd7 86 mbed_stats_cpu_get(&cpu_stats);
mbed_official 82:abf1b1785bd7 87
mbed_official 82:abf1b1785bd7 88 uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
mbed_official 82:abf1b1785bd7 89 uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
mbed_official 82:abf1b1785bd7 90 uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
mbed_official 82:abf1b1785bd7 91 prev_idle_time = cpu_stats.idle_time;
mbed_official 82:abf1b1785bd7 92
mbed_official 82:abf1b1785bd7 93 printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
mbed_official 82:abf1b1785bd7 94 }
mbed_official 82:abf1b1785bd7 95
mbed_official 82:abf1b1785bd7 96 /**
mbed_official 82:abf1b1785bd7 97 * Report current heap stats. Current heap refers to the current amount of
mbed_official 82:abf1b1785bd7 98 * allocated heap. Max heap refers to the highest amount of heap allocated
mbed_official 82:abf1b1785bd7 99 * since reset.
mbed_official 82:abf1b1785bd7 100 */
mbed_official 82:abf1b1785bd7 101 void report_heap_stats(void)
mbed_official 82:abf1b1785bd7 102 {
mbed_official 82:abf1b1785bd7 103 printf("================ HEAP STATS =================\r\n");
mbed_official 82:abf1b1785bd7 104
mbed_official 82:abf1b1785bd7 105 // Collect and print heap stats
mbed_official 82:abf1b1785bd7 106 mbed_stats_heap_get(&heap_stats);
mbed_official 82:abf1b1785bd7 107
mbed_official 98:f2d7e14c84a1 108 printf("Current heap: %" PRIu32 "\r\n", heap_stats.current_size);
mbed_official 98:f2d7e14c84a1 109 printf("Max heap size: %" PRIu32 "\r\n", heap_stats.max_size);
mbed_official 82:abf1b1785bd7 110 }
mbed_official 82:abf1b1785bd7 111
mbed_official 82:abf1b1785bd7 112 /**
mbed_official 82:abf1b1785bd7 113 * Report active thread stats
mbed_official 82:abf1b1785bd7 114 */
mbed_official 82:abf1b1785bd7 115 void report_thread_stats(void)
mbed_official 82:abf1b1785bd7 116 {
mbed_official 82:abf1b1785bd7 117 printf("================ THREAD STATS ===============\r\n");
mbed_official 82:abf1b1785bd7 118
mbed_official 82:abf1b1785bd7 119 // Collect and print running thread stats
mbed_official 82:abf1b1785bd7 120 int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
mbed_official 82:abf1b1785bd7 121
mbed_official 82:abf1b1785bd7 122 for (int i = 0; i < count; i++) {
mbed_official 98:f2d7e14c84a1 123 printf("ID: 0x%" PRIx32 " \r\n", thread_stats[i].id);
mbed_official 98:f2d7e14c84a1 124 printf("Name: %s \r\n", thread_stats[i].name);
mbed_official 98:f2d7e14c84a1 125 printf("State: %" PRIu32 " \r\n", thread_stats[i].state);
mbed_official 98:f2d7e14c84a1 126 printf("Priority: %" PRIu32 " \r\n", thread_stats[i].priority);
mbed_official 98:f2d7e14c84a1 127 printf("Stack Size: %" PRIu32 " \r\n", thread_stats[i].stack_size);
mbed_official 98:f2d7e14c84a1 128 printf("Stack Space: %" PRIu32 " \r\n", thread_stats[i].stack_space);
mbed_official 82:abf1b1785bd7 129 }
mbed_official 82:abf1b1785bd7 130 }
mbed_official 82:abf1b1785bd7 131 };
mbed_official 82:abf1b1785bd7 132
mbed_official 82:abf1b1785bd7 133 #endif // STATS_REPORT_H