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.
Revision 0:d7214673ab2d, committed 2019-07-22
- Comitter:
- joshdavy
- Date:
- Mon Jul 22 14:45:38 2019 +0000
- Commit message:
- a
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Mon Jul 22 14:45:38 2019 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CONTRIBUTING.md Mon Jul 22 14:45:38 2019 +0000 @@ -0,0 +1,5 @@ +# Contributing to Mbed OS + +Mbed OS is an open-source, device software platform for the Internet of Things. Contributions are an important part of the platform, and our goal is to make it as simple as possible to become a contributor. + +To encourage productive collaboration, as well as robust, consistent and maintainable code, we have a set of guidelines for [contributing to Mbed OS](https://os.mbed.com/docs/mbed-os/latest/contributing/index.html).
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Music/Music.cpp Mon Jul 22 14:45:38 2019 +0000
@@ -0,0 +1,57 @@
+
+#include "Music.h"
+AnalogOut speaker(DAC0_OUT);
+/**
+* @brief Constructor (no paramateters)
+*/
+Music::Music()
+{
+
+}
+/**
+* @brief Deconstructor
+*/
+Music::~Music()
+{
+
+
+
+}
+
+/**
+* @brief Initialiser. Takes the sound data and initialises the buzzer
+* @param const int* data @details The audio data. A array of the amplitude of
+* the waveform over time
+* @param length @details Length of above sound array
+*/
+void Music::init(const int* data,int length)
+{
+
+ _data = data;
+ _length = length;
+ // Has to << then 1/sample rate
+ // until music played stay silent
+ _index = 0;
+
+}
+/**
+* @brief Plays the next sound sample. Must be called at the sample rate
+*/
+void Music::play_next()
+{
+ double duty_cycle;
+ duty_cycle = _data[_index];
+ //printf("%i = %f\n",_index,duty_cycle);
+
+ if (_index < _length) {
+ speaker = duty_cycle/255;
+ //printf("%f\n",duty_cycle/255);
+
+ _index++;
+ } else {
+ _index = 0;
+ }
+
+}
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Music/Music.h Mon Jul 22 14:45:38 2019 +0000
@@ -0,0 +1,37 @@
+#ifndef MUSIC_H
+#define MUSIC_H
+
+/*Music Class*/
+#include "mbed.h"
+
+
+/** Music Class
+
+@brief The Music Class handles the playing of the background music while playing
+the game. It uses the buzzer with PWM audio to play complex waveforms through
+a standard 5V pin of the board. See presentation for more detail how this works.
+@version 1.0
+
+@author Joshua Davy el17jd
+
+@date April 2019
+
+*/
+
+class Music {
+
+public:
+ Music();
+ ~Music();
+ void init(const int* data,int length);
+ void play_next();
+
+
+private:
+ const int* _data;
+ int _index;
+ int _length;
+
+
+};
+#endif
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Music/SoundData.h Mon Jul 22 14:45:38 2019 +0000
@@ -0,0 +1,147 @@
+/**
+* @brief The Sound Data. Generated using Wav2Code.
+* https://github.com/Sphinkie/Arditime/tree/master/tools/wav2code
+*/
+
+/**********************************************************************
+* Written by WAVToCode
+* Date: Sat Apr 06 05:06:48 PM
+* FileName: 8bit.C
+* Interleaved: Yes
+* Signed: No
+* No. of channels: 1
+* No. of samples: 120000
+* Bits/Sample: 8
+**********************************************************************/
+
+#define NUM_ELEMENTS 1023
+
+const int sound_data[NUM_ELEMENTS] = {
+0x80,0x81,0x82,0x83,0x83,0x84,0x85,
+0x86,0x87,0x87,0x88,0x89,0x8a,0x8a,0x8b,
+0x8c,0x8d,0x8e,0x8e,0x8f,0x90,0x91,0x91,
+0x92,0x93,0x94,0x95,0x95,0x96,0x97,0x98,
+0x98,0x99,0x9a,0x9b,0x9b,0x9c,0x9d,0x9e,
+0x9f,0x9f,0xa0,0xa1,0xa2,0xa2,0xa3,0xa4,
+0xa5,0xa5,0xa6,0xa7,0xa8,0xa8,0xa9,0xaa,
+0xaa,0xab,0xac,0xad,0xad,0xae,0xa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,0xa2,0xa1,0xa0,0xa0,0x9f,
+0x9e,0x9d,0x9d,0x9c,0x9b,0x9a,0x9a,0x99,
+0x98,0x97,0x96,0x96,0x95,0x94,0x93,0x93,
+0x92,0x91,0x90,0x90,0x8f,0x8e,0x8d,0x8c,
+0x8c,0x8b,0x8a,0x89,0x88,0x88,0x87,0x86,
+0x85,0x85,0x84,0x83,0x82,0x81,0x81,0x80,
+0x7f,0x7e,0x7e,0x7d,0x7c,0x7b,0x7a,0x7a,
+0x79,0x78,0x77,0x77,0x76,0x75,0x74,0x73,
+0x73,0x72,0x71,0x70,0x6f,0x6f,0x6e,0x6d,
+0x6c,0x6c,0x6b,0x6a,0x69,0x69,0x68,0x67,
+0x66,0x65,0x65,0x64,0x63,0x62,0x62,0x61,
+0x60,0x5f,0x5f,0x5e,0x5d,0x5c,0x5c,0x5b,
+0x5a,0x59,0x59,0x58,0x57,0x56,0x56,0x55,
+0x54,0x53,0x53,0x52,0x51,0x50,0x50,0x4f,
+0x4e,0x4e,0x4d,0x4c,0x4b,0x4b,0x4a,0x49,
+0x49,0x48,0x47,0x46,0x46,0x45,0x44,0x44,
+0x43,0x42,0x42,0x41,0x40,0x40,0x3f,0x3e,
+0x3e,0x3d,0x3c,0x3c,0x3b,0x3a,0x3a,0x39,
+0x38,0x38,0x37,0x36,0x36,0x35,0x34,0x34,
+0x33,0x33,0x32,0x31,0x31,0x30,0x2f,0x2f,
+0x2e,0x2e,0x2d,0x2c,0x2c,0x2b,0x2b,0x2a,
+0x2a,0x29,0x28,0x28,0x27,0x27,0x26,0x26,
+0x25,0x24,0x24,0x23,0x23,0x22,0x22,0x21,
+0x21,0x20,0x20,0x1f,0x1f,0x1e,0x1e,0x1d,
+0x1d,0x1c,0x1c,0x1b,0x1b,0x1a,0x1a,0x19,
+0x19,0x18,0x18,0x17,0x17,0x17,0x16,0x16,
+0x15,0x15,0x14,0x14,0x14,0x13,0x13,0x12,
+0x12,0x11,0x11,0x11,0x10,0x10,0x10,0xf,
+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0x37,0x37,0x38,0x39,
+0x39,0x3a,0x3b,0x3b,0x3c,0x3d,0x3d,0x3e,
+0x3f,0x3f,0x40,0x41,0x41,0x42,0x43,0x43,
+0x44,0x45,0x45,0x46,0x47,0x48,0x48,0x49,
+0x4a,0x4a,0x4b,0x4c,0x4d,0x4d,0x4e,0x4f,
+0x4f,0x50,0x51,0x52,0x52,0x53,0x54,0x55,
+0x55,0x56,0x57,0x57,0x58,0x59,0x5a,0x5a,
+0x5b,0x5c,0x5d,0x5d,0x5e,0x5f,0x60,0x60,
+0x61,0x62,0x63,0x64,0x64,0x65,0x66,0x67,
+0x67,0x68,0x69,0x6a,0x6a,0x6b,0x6c,0x6d,
+0x6e,0x6e,0x6f,0x70,0x71,0x71,0x72,0x73,
+0x74,0x75,0x75,0x76,0x77,0x78,0x78,0x79,
+0x7a,0x7b,0x7c,0x7c,0x7d,0x7e,0x7f,0x80};
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Mon Jul 22 14:45:38 2019 +0000 @@ -0,0 +1,152 @@ +# Getting started example for Mbed OS + +This guide reviews the steps required to get Blinky with the addition of dynamic OS statistics working on an Mbed OS platform. (Note: To see a rendered example you can import into the Arm Online Compiler, please see our [quick start](https://os.mbed.com/docs/mbed-os/latest/quick-start/online-with-the-online-compiler.html#importing-the-code).) + +Please install [Mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). + +## Import the example application + +From the command-line, import the example: + +``` +mbed import mbed-os-example-blinky +cd mbed-os-example-blinky +``` + +### Now compile + +Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the Arm Compiler: + +``` +mbed compile -m K64F -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end, you see the following result: + +``` +[snip] + +Image: ./BUILD/K64F/GCC_ARM/mbed-os-example-blinky.bin +``` + +### Program your board + +1. Connect your Mbed device to the computer over USB. +1. Copy the binary file to the Mbed device. +1. Press the reset button to start the program. + +The LED on your platform turns on and off. The main thread will additionally take a snapshot of the device's runtime statistics and display it over serial to your PC. The snapshot includes: + +* System Information: + * Mbed OS Version: Will currently default to 999999 + * Compiler ID + * ARM = 1 + * GCC_ARM = 2 + * IAR = 3 + * [CPUID Register Information](#cpuid-register-information) + * [Compiler Version](#compiler-version) +* CPU Statistics + * Percentage of runtime that the device has spent awake versus in sleep +* Heap Statistics + * Current heap size + * Max heap size which refers to the largest the heap has grown to +* Thread Statistics + * Provides information on all running threads in the OS including + * Thread ID + * Thread Name + * Thread State + * Thread Priority + * Thread Stack Size + * Thread Stack Space + +#### Compiler Version + +| Compiler | Version Layout | +| -------- | -------------- | +| ARM | PVVbbbb (P = Major; VV = Minor; bbbb = build number) | +| GCC | VVRRPP (VV = Version; RR = Revision; PP = Patch) | +| IAR | VRRRPPP (V = Version; RRR = Revision; PPP = Patch) | + +#### CPUID Register Information + +| Bit Field | Field Description | Values | +| --------- | ----------------- | ------ | +|[31:24] | Implementer | 0x41 = ARM | +|[23:20] | Variant | Major revision 0x0 = Revision 0 | +|[19:16] | Architecture | 0xC = Baseline Architecture | +| | | 0xF = Constant (Mainline Architecture) | +|[15:4] | Part Number | 0xC20 = Cortex-M0 | +| | | 0xC60 = Cortex-M0+ | +| | | 0xC23 = Cortex-M3 | +| | | 0xC24 = Cortex-M4 | +| | | 0xC27 = Cortex-M7 | +| | | 0xD20 = Cortex-M23 | +| | | 0xD21 = Cortex-M33 | +|[3:0] | Revision | Minor revision: 0x1 = Patch 1 | + + + +You can view individual examples and additional API information of the statistics collection tools at the bottom of the page in the [related links section](#related-links). + + +### Output + +To view the serial output you can use any terminal client of your choosing such as [PuTTY](http://www.putty.org/) or [CoolTerm](http://freeware.the-meiers.org/). Unless otherwise specified, printf defaults to a baud rate of 9600 on Mbed OS. + +You can find more information on the Mbed OS configuration tools and serial communication in Mbed OS in the related [related links section](#related-links). + +The output should contain the following block transmitted at the blinking LED frequency (actual values may vary depending on your target, build profile, and toolchain): + +``` +=============================== SYSTEM INFO ================================ +Mbed OS Version: 999999 +CPU ID: 0x410fc241 +Compiler ID: 2 +Compiler Version: 60300 +RAM0: Start 0x20000000 Size: 0x30000 +RAM1: Start 0x1fff0000 Size: 0x10000 +ROM0: Start 0x0 Size: 0x100000 +================= CPU STATS ================= +Idle: 98% Usage: 2% +================ HEAP STATS ================= +Current heap: 1096 +Max heap size: 1096 +================ THREAD STATS =============== +ID: 0x20001eac +Name: main_thread +State: 2 +Priority: 24 +Stack Size: 4096 +Stack Space: 3296 + +ID: 0x20000f5c +Name: idle_thread +State: 1 +Priority: 1 +Stack Size: 512 +Stack Space: 352 + +ID: 0x20000f18 +Name: timer_thread +State: 3 +Priority: 40 +Stack Size: 768 +Stack Space: 664 + +``` + +## Troubleshooting + +If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it. + +## Related Links + +* [Mbed OS Stats API](https://os.mbed.com/docs/latest/apis/mbed-statistics.html) +* [Mbed OS Configuration](https://os.mbed.com/docs/latest/reference/configuration.html) +* [Mbed OS Serial Communication](https://os.mbed.com/docs/latest/tutorials/serial-communication.html) + +### License and contributions + +The software is provided under Apache-2.0 license. Contributions to this project are accepted under the same license. Please see contributing.md for more info. + +This project contains code from other projects. The original license text is included in those source files. They must comply with our license guide.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Jul 22 14:45:38 2019 +0000
@@ -0,0 +1,29 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2018 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "mbed.h"
+#include "stats_report.h"
+#include "Music.h"
+#include "SoundData.h"
+
+DigitalOut led1(LED1);
+Music music;
+#define SLEEP_TIME 500 // (msec)
+#define PRINT_AFTER_N_LOOPS 20
+
+// main() runs in its own thread in the OS
+int main()
+{
+ SystemReport sys_state( SLEEP_TIME * PRINT_AFTER_N_LOOPS /* Loop delay time in ms */);
+ music.init(sound_data,NUM_ELEMENTS);
+ int count = 0;
+ while (true) {
+ // Blink LED and wait 0.5 seconds
+ music.play_next();
+ //wait_us(1);
+
+ ++count;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Mon Jul 22 14:45:38 2019 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#0063e5de32fc575f061244c96ac60c41c07bd2e6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json Mon Jul 22 14:45:38 2019 +0000
@@ -0,0 +1,11 @@
+{
+ "target_overrides": {
+ "*": {
+ "platform.stack-stats-enabled": true,
+ "platform.heap-stats-enabled": true,
+ "platform.cpu-stats-enabled": true,
+ "platform.thread-stats-enabled": true,
+ "platform.sys-stats-enabled": true
+ }
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/stats_report.h Mon Jul 22 14:45:38 2019 +0000
@@ -0,0 +1,132 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2018 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#ifndef STATS_REPORT_H
+#define STATS_REPORT
+
+#include "mbed.h"
+
+/**
+ * System Reporting library. Provides runtime information on device:
+ * - CPU sleep, idle, and wake times
+ * - Heap and stack usage
+ * - Thread information
+ * - Static system information
+ */
+class SystemReport {
+ mbed_stats_heap_t heap_stats;
+ mbed_stats_cpu_t cpu_stats;
+ mbed_stats_sys_t sys_stats;
+
+ mbed_stats_thread_t *thread_stats;
+ uint8_t thread_count;
+ uint8_t max_thread_count;
+ uint32_t sample_time_ms;
+
+public:
+ /**
+ * SystemReport - Sample rate in ms is required to handle the CPU percent awake logic
+ */
+ SystemReport(uint32_t sample_rate) : max_thread_count(8), sample_time_ms(sample_rate)
+ {
+ thread_stats = new mbed_stats_thread_t[max_thread_count];
+
+ // Collect the static system information
+ mbed_stats_sys_get(&sys_stats);
+
+ printf("=============================== SYSTEM INFO ================================\r\n");
+ printf("Mbed OS Version: %ld \r\n", sys_stats.os_version);
+ printf("CPU ID: 0x%lx \r\n", sys_stats.cpu_id);
+ printf("Compiler ID: %d \r\n", sys_stats.compiler_id);
+ printf("Compiler Version: %ld \r\n", sys_stats.compiler_version);
+
+ for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
+ if (sys_stats.ram_size[i] != 0) {
+ printf("RAM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.ram_start[i], sys_stats.ram_size[i]);
+ }
+ }
+ for (int i = 0; i < MBED_MAX_MEM_REGIONS; i++) {
+ if (sys_stats.rom_size[i] != 0) {
+ printf("ROM%d: Start 0x%lx Size: 0x%lx \r\n", i, sys_stats.rom_start[i], sys_stats.rom_size[i]);
+ }
+ }
+ }
+
+ ~SystemReport(void)
+ {
+ free(thread_stats);
+ }
+
+ /**
+ * Report on each Mbed OS Platform stats API
+ */
+ void report_state(void)
+ {
+ report_cpu_stats();
+ report_heap_stats();
+ report_thread_stats();
+
+ // Clear next line to separate subsequent report logs
+ printf("\r\n");
+ }
+
+ /**
+ * Report CPU idle and awake time in terms of percentage
+ */
+ void report_cpu_stats(void)
+ {
+ static uint64_t prev_idle_time = 0;
+
+ printf("================= CPU STATS =================\r\n");
+
+ // Collect and print cpu stats
+ mbed_stats_cpu_get(&cpu_stats);
+
+ uint64_t diff = (cpu_stats.idle_time - prev_idle_time);
+ uint8_t idle = (diff * 100) / (sample_time_ms * 1000); // usec;
+ uint8_t usage = 100 - ((diff * 100) / (sample_time_ms * 1000)); // usec;;
+ prev_idle_time = cpu_stats.idle_time;
+
+ printf("Idle: %d%% Usage: %d%% \r\n", idle, usage);
+ }
+
+ /**
+ * Report current heap stats. Current heap refers to the current amount of
+ * allocated heap. Max heap refers to the highest amount of heap allocated
+ * since reset.
+ */
+ void report_heap_stats(void)
+ {
+ printf("================ HEAP STATS =================\r\n");
+
+ // Collect and print heap stats
+ mbed_stats_heap_get(&heap_stats);
+
+ printf("Current heap: %lu\r\n", heap_stats.current_size);
+ printf("Max heap size: %lu\r\n", heap_stats.max_size);
+ }
+
+ /**
+ * Report active thread stats
+ */
+ void report_thread_stats(void)
+ {
+ printf("================ THREAD STATS ===============\r\n");
+
+ // Collect and print running thread stats
+ int count = mbed_stats_thread_get_each(thread_stats, max_thread_count);
+
+ for (int i = 0; i < count; i++) {
+ printf("ID: 0x%lx \r\n", thread_stats[i].id);
+ printf("Name: %s \r\n", thread_stats[i].name);
+ printf("State: %ld \r\n", thread_stats[i].state);
+ printf("Priority: %ld \r\n", thread_stats[i].priority);
+ printf("Stack Size: %ld \r\n", thread_stats[i].stack_size);
+ printf("Stack Space: %ld \r\n", thread_stats[i].stack_space);
+ }
+ }
+};
+
+#endif // STATS_REPORT_H