
{"id":733,"date":"2019-09-26T16:44:32","date_gmt":"2019-09-26T16:44:32","guid":{"rendered":"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/?page_id=733"},"modified":"2019-10-10T09:04:20","modified_gmt":"2019-10-10T09:04:20","slug":"review-of-mbed","status":"publish","type":"page","link":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/level-5-embedded-and-real-time-systems\/review-of-mbed\/","title":{"rendered":"Review of Mbed"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/level-5-embedded-and-real-time-systems\/\">Table of Contents<\/a><\/p>\n<hr \/>\n<p>In this section, we review the subject of interfacing with the mbed-os\u00ae\u00a0 framework from ARM. The focus of this module is on software, written to work in modern electronic systems.\u00a0In this section you will review the use \/ consume software components (objects) from the mbed frameworks, and later, you will learn how to create your own.<\/p>\n<h1>Activity 1.1 &#8211; Simple LED Circuit<\/h1>\n<p>In electronics, we mostly work with one (or both) of the following signals types :<\/p>\n<p><strong>Analogue<\/strong> &#8211; where voltages and currents can take any value, typically between and upper and lower limit<\/p>\n<p><strong>Digital<\/strong> &#8211; where signal voltages can only be one of two possible values<\/p>\n<p>For this section, the signals are all going to be digital. Let\u2019s start with a simple circuit schematic as shown.<\/p>\n<p>All components should be mounted on the prototyping board provided, and nothing will work without a power supply!<\/p>\n<p>Is obtained from the Nucleo Board.<\/p>\n<figure id=\"attachment_734\" aria-describedby=\"caption-attachment-734\" style=\"width: 246px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-734 size-medium\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Activity11-246x300.png\" alt=\"\" width=\"246\" height=\"300\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Activity11-246x300.png 246w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Activity11.png 650w\" sizes=\"auto, (max-width: 246px) 100vw, 246px\" \/><figcaption id=\"caption-attachment-734\" class=\"wp-caption-text\">Simple LED Circuit<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<h2>LED Characteristics<\/h2>\n<p>In your kit, you are provided with three LEDs, red, green and amber. The data sheets for these devices are available. However, the most important characteristics for a clear visible brightness are as follows:<\/p>\n<figure id=\"attachment_737\" aria-describedby=\"caption-attachment-737\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-737\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/led-characteristics.png\" alt=\"\" width=\"600\" height=\"168\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/led-characteristics.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/led-characteristics-300x84.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/led-characteristics-768x215.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-737\" class=\"wp-caption-text\">LED Characteristics at 2mA<\/figcaption><\/figure>\n<p>LED Voltage at 2mA<\/p>\n<p>You can drive much higher currents through these LED\u2019s but the additional perceived brightness is not significant. We want to keep current LOW for two reasons:<\/p>\n<ul>\n<li>The source of power for the LED will be our microcontroller. From the data sheet, the maximum output current is limited to 25mA for a single pin, up to a total maximum of 120mA for all pins.<\/li>\n<li>We want to (and should) try and save power<\/li>\n<li>There will be tolerances around these values, and they do not need to be precise.<\/li>\n<\/ul>\n<p>Consider the green LED in the circuit above.<\/p>\n<ul>\n<li>The voltage source\u00a0 Vout=3.3V<\/li>\n<li>We want the current I=2mA<\/li>\n<li>For this current, from the LED data we expect VLED=1.9V<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-738\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-1.png\" alt=\"\" width=\"599\" height=\"342\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-1.png 722w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-1-300x171.png 300w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<p class=\"p1\"><span class=\"s1\">Note that +3V3 and GND are the DIGITAL power sources, not to be confused with the analogue.<\/span><\/p>\n<h2>Activity 1.2 &#8211; Blinky<\/h2>\n<p>When learning to program a new language or framework, there are one or two traditional programs you almost always write &#8211; here is the most popular in the embedded software world, \u201cblinky\u201d.<\/p>\n<p>Before we write this software, first you modify the circuit.<\/p>\n<ul>\n<li>The figure opposite is the schematic for the blinky circuit.<\/li>\n<li>You will find the GPIO D7 labelled on the Nucleo Board.<\/li>\n<\/ul>\n<p>If you we to write some code to flash the LED by referring to the microcontroller data sheet, there would be a number of things you would have to do, including:<\/p>\n<ul>\n<li>Set the mode of the GPIO pin (output, push\/pull)<\/li>\n<li>Turn on the clock<\/li>\n<li>Set a bit somewhere in a register to set the output value<\/li>\n<li>Possibly write a for-loop to create a delay?<\/li>\n<\/ul>\n<p>It\u2019s good to know how to do this, but it\u2019s not overly productive and will result in code written specifically for a small number of devices (or even just one device).<\/p>\n<figure id=\"attachment_739\" aria-describedby=\"caption-attachment-739\" style=\"width: 237px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-739\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Activity1-2-blinky.png\" alt=\"\" width=\"237\" height=\"262\" \/><figcaption id=\"caption-attachment-739\" class=\"wp-caption-text\">Schematic for Blinky with a single external LED<\/figcaption><\/figure>\n<h3>Using mbed Objects<\/h3>\n<p>Below is the source code for blinky written with mbed (task 1.2.1). Let\u2019s break this down line by line.<\/p>\n<pre class=\"lang:c++ decode:true \" title=\"Source code for Blinky\">\/\/This is known as a \u201cheader file\u201d\r\n\/\/In short, this copies and pastes the text file\r\n\/\/mbed.h into this code\r\n#include \"mbed.h\"\r\n\r\n\/\/Create a DigitalOut \u201cobject\u201d called myled\r\n\/\/Pass constant D7 as a \u201cparameter\u201d\r\nDigitalOut myled(D7);\r\n\r\n\/\/The main function - all executable C \/ C++\r\n\/\/applications have a main function. This is\r\n\/\/our entry point in the software\r\nint main() {\r\n\r\n\/\/ ALL the code is contained in a \r\n\/\/ \u201cwhile loop\u201d\r\n    while(1) \r\n\t{\r\n\t\/\/The code between the { curly braces }\r\n\t\/\/is the code that is repeated\t\r\n        myled = 1; \/\/ External LED is ON\r\n        wait(1.0); \/\/ 1 second\r\n        myled = 0; \/\/ LED is OFF\r\n        wait(1.0); \/\/ External 1 second\r\n    }\r\n}<\/pre>\n<p>Firstly, consider a simple variable declaration in C, we might write something like this:<\/p>\n<p><code>int x = 0;<\/code><\/p>\n<p>The variable is called <code>x<\/code>, its data type is <code>int<\/code> (integer) and it is initialised to the value <code>0<\/code>.<\/p>\n<p>Now let\u2019s look at one of the first lines in the Blinky code.<\/p>\n<p class=\"p3\"><span class=\"s1\"><code>DigitalOut myled(D7);<\/code><\/span><\/p>\n<p class=\"p2\"><span class=\"s1\">This is the declaration and definition of the object variable <code>myled<\/code><\/span><span class=\"s1\">, where the data type is <code>DigitalOut<\/code><\/span><span class=\"s1\">.<span class=\"Apple-converted-space\">\u00a0 <\/span>This is a custom data type that is included with mbed.<\/span><\/p>\n<p>Note how a single parameter (the pin name) is passed. This is because when you initialise an object (a C++ concept we will meet later), you have the option to pass parameters that allow the object to initialise it\u2019s internal state.<\/p>\n<p>Note how simple the code is!<\/p>\n<p>The complex details are hidden away inside the software component <code>DigitalOut<\/code>. We are using a <code>DigitalOut<\/code>\u00a0component to set a pin (<code>D7<\/code>) high or low just by equating it to 1 or 0.<\/p>\n<p>All the initialisation code, including write to registers to turn on the clock, set GPIO mode etc. are hidden inside the component <code>DigitalOut<\/code><\/p>\n<p>When we add an instance of this component to our application, note we are also telling it which pin it is assigned to by passing a parameter. You can create more than one instance, but each instance must use a unique pin.<br \/>\nIn our code, we say the the object <code>myled<\/code>\u00a0is an instance of type <code>DigitalOut<\/code>.<\/p>\n<p>One of the great things about components is that they hide the complex details inside, again very much like an electronic component. We just have to work with its external interface. This promotes reuse and shorter \/ easier to write code.<\/p>\n<blockquote><p>Note: Consider the car as a useful analogy. You can learn to operate a car by using its interface (steering wheel, pedals and gear change stick). A car also has properties which can be monitored and sometimes changed, including:<\/p>\n<p>\u2022 fuel level (read\/modify)<\/p>\n<p>\u2022 tyre pressure (read\/modify)<\/p>\n<p>\u2022 engine temperature (read only)<\/p>\n<p>Details workings of a car are hidden from you. You do not need to take the engine apart to be able to drive a car. It\u2019s hard to build a car, but relatively easy to drive one. We call this abstraction. A software component (or object) is very similar in these respects. You don\u2019t need to see the inner workings of an object in order to use it. mbed includes a collection of very useful objects.<\/p><\/blockquote>\n<p>The mbed framework contains many such components, such that if you rebuild you code on a different board, it should just work (you might have to change the pin assignments). We say it is portable.<\/p>\n<p>C++ also supports something known as operator overloading (changing the contextual meaning of +,-,= etc..). This is used extensively on Mbed to make the resulting code read more intuitively.<\/p>\n<blockquote><p>For level 5, we simply need to know how to use a component. Creating a component in C++ is covered in level 6 (final year).<\/p><\/blockquote>\n<p>Consider the following line.<\/p>\n<p><code>mled = 1;<\/code><\/p>\n<p>Here we simply assign an integer value to an instance of <code>DigitalOut<\/code>, and the output pin <code>D7<\/code> changes.<br \/>\nSuch clarity and simplicity is possible because the component understands the = operator to mean \u201cset the pin to this value\u201d.<\/p>\n<p>That is because for <code>DigitalOut<\/code>, mbed redefines the meaning of the <code>=<\/code> operator (under the hood &#8211; more C++ magic). In level 5, we will meet a topic called \u201coperator overloading\u201d where you will learn to do this yourself. Done well, this results in easy to read and debug code. Done badly, and you can end up with ambiguous code!<\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_740\" aria-describedby=\"caption-attachment-740\" style=\"width: 324px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-740\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/assign-instance-value.png\" alt=\"\" width=\"324\" height=\"337\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/assign-instance-value.png 324w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/assign-instance-value-288x300.png 288w\" sizes=\"auto, (max-width: 324px) 100vw, 324px\" \/><figcaption id=\"caption-attachment-740\" class=\"wp-caption-text\">In C++ it is possible to redefine the function of the = assignment operator (and others) using a process known as \u201coperator overloading\u201d. This is a short cut for writing <code>myled.write(1);<\/code><\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p>Now consider the next line:<\/p>\n<p><code>wait(1.0);<\/code><\/p>\n<p><code>wait<\/code> is one of the global utility functions in mbed. In this line of code, we are invoking a function wait. This simply delays execution for a specified time. This function is written for us (it is part of the mbed library).<br \/>\n<code>wait<\/code> has a single parameter. Note this time it is a fractional number. Note that when you invoke <code>wait()<\/code> in this way, the CPU can do nothing else. We say it is a <strong>blocking<\/strong> wait.<\/p>\n<blockquote><p>When your code executes a <strong>blocking<\/strong> operation, such as <code>wait()<\/code>, execution pauses and does not resume until it is complete. Many functions that interact with peripheral hardware are considered to be blocking.This is because peripheral hardware is usually a lot slower than the CPU.<\/p><\/blockquote>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-741\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/wait-blocking.png\" alt=\"\" width=\"235\" height=\"284\" \/><\/p>\n<blockquote><p>Note that <code>wait(float)<\/code> is being deprecated. You should use <code>wait_us<\/code> and specify the delay in microseconds.<\/p><\/blockquote>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-742\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-2-1.png\" alt=\"\" width=\"400\" height=\"204\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-2-1.png 722w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-2-1-300x153.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>What we\u2019ve written is \u201cblinky\u201d &#8211; the classic starter application for embedded programming.<\/p>\n<h2>Activity 1.3 &#8211; Hello World<\/h2>\n<p>The other tradition is Hello World. This is normally the first program you write when learning to program on a desktop computer. However, we\u2019re a generous lot, and wanted you to enjoy both with mbed. See the code below<\/p>\n<pre class=\"lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\n\/\/Create an instance of a Serial \r\n\/\/object called pc.\r\n\/\/Transmit and receive pins have pre-defined\r\n\/\/names USBTX and USBRX\r\nSerial pc(USBTX, USBRX);\r\n\r\nint main() \r\n{\r\n\r\n\/\/Set the baud rate property (bits per second)\r\n    pc.baud(9600);\r\n    \r\n    \/\/Call the printf method on pc\r\n    pc.printf(\"Hello World\\n\");\r\n\r\n    \/\/Run in an infinite loop\r\n    while(1) {\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>Note the object <code>Serial<\/code>.<\/p>\n<p><code>Serial pc(USBTX, USBRX);<\/code><\/p>\n<p>This object has a \u201cmember function\u201d (function that acts on the data inside the object) baud which sets the speed (bits\/s).<\/p>\n<p><code>pc.baud(9600);<\/code><\/p>\n<p>We say <code>pc<\/code>\u00a0is special (C++) variable type, often referred to as an <strong>object<\/strong>. Objects are sometimes said to have \u201cmethods\u201d or \u201cmember functions\u201d (fancy word for functions that belong to objects). These perform specific tasks that relate to the object (they will typically update its internal state). You could compare this to a pin on a chip that performs a specific task when you pull it high or low. On doing so, a sequence of internal electronic states are updated.<\/p>\n<p>Another function that <code>Serial<\/code> provides is <code>printf<\/code>.<\/p>\n<p><code>pc.printf(\"Hello World\\n\");<\/code><\/p>\n<p>The output of <code>printf<\/code> is sent to the <code>Serial<\/code> device represented by pc.<\/p>\n<p><strong>Now watch the following video<\/strong><\/p>\n<p><a href=\"https:\/\/plymouth.cloud.panopto.eu\/Panopto\/Pages\/Viewer.aspx?id=1f5a72c3-9458-4084-96b0-a5c9b973274f\" target=\"_blank\" rel=\"noopener\">https:\/\/plymouth.cloud.panopto.eu\/Panopto\/Pages\/Viewer.aspx?id=1f5a72c3-9458-4084-96b0-a5c9b973274f<\/a><\/p>\n<h3>Debugging with printf?<\/h3>\n<p>It is common to use <code>printf<\/code> function to debug embedded systems, and although a debugger is preferable during the development phase. It can also be invaluable for diagnostics and logging. However, it is also quite a complex function and can consume a significant amount of CPU time and memory. Have a go at the following tasks:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-743\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-3-1.png\" alt=\"\" width=\"599\" height=\"185\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-3-1.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-3-1-300x93.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task1-3-1-768x237.png 768w\" sizes=\"auto, (max-width: 599px) 100vw, 599px\" \/><\/p>\n<h3>Traffic Lights<\/h3>\n<p>Let\u2019s now combine what we\u2019ve learned. In this example we will use printf to debug and trace our code.<\/p>\n<figure id=\"attachment_744\" aria-describedby=\"caption-attachment-744\" style=\"width: 601px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-744\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-2.png\" alt=\"\" width=\"601\" height=\"377\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-2.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-2-300x188.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-2-768x482.png 768w\" sizes=\"auto, (max-width: 601px) 100vw, 601px\" \/><figcaption id=\"caption-attachment-744\" class=\"wp-caption-text\">Task 1.3.2<\/figcaption><\/figure>\n<p><code><\/code><\/p>\n<p style=\"text-align: center\"><strong>TASK 1.3.3 HAS BEEN REMOVED<\/strong><\/p>\n<p>&nbsp;<\/p>\n<figure id=\"attachment_747\" aria-describedby=\"caption-attachment-747\" style=\"width: 602px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-747\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-4.png\" alt=\"\" width=\"602\" height=\"193\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-4.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-4-300x96.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-4-768x246.png 768w\" sizes=\"auto, (max-width: 602px) 100vw, 602px\" \/><figcaption id=\"caption-attachment-747\" class=\"wp-caption-text\">Task 1.3.4<\/figcaption><\/figure>\n<figure id=\"attachment_748\" aria-describedby=\"caption-attachment-748\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-748\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-5.png\" alt=\"\" width=\"600\" height=\"191\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-5.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-5-300x95.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-5-768x244.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-748\" class=\"wp-caption-text\">Task 1.3.5<\/figcaption><\/figure>\n<p>This task requires is a little different as it uses an <strong><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/glossary-2\/interrupt-glossary-entry\/\">interrupt<\/a><\/strong> (a topic we will cover quite soon), and you need to read the documentation and look at the example code. You should get into the habit of reading the documentation on all the mbed Classes.<\/p>\n<h3>Serial Input<\/h3>\n<p>You can also type into the terminal and read the data from the Serial Interface.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-751\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-6.png\" alt=\"\" width=\"600\" height=\"205\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-6.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-6-300x103.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-6-768x263.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<pre class=\"lang:c++ decode:true\">#include \"mbed.h\"\r\n\r\nSerial pc(SERIAL_TX, SERIAL_RX);\r\n\r\nint main()\r\n{\r\n   char nameStr[30]; \/\/ array of chars (string)\r\n   int age;          \/\/ integer to hold your age\r\n   \r\n   pc.scanf(\"%s %d\", nameStr, &amp;age);\r\n   pc.printf(\"Hello %s \\n\\r\", nameStr);\r\n   pc.printf(\"You are %d \\n\", age);\r\n}<\/pre>\n<p><strong>Question<\/strong>: What statements are <strong>blocking<\/strong> in this code? What peripheral is involved and where is it physically located?<\/p>\n<h2>Using Functions<\/h2>\n<p>You may recall C functions from previous years. Here is some revision:<\/p>\n<figure id=\"attachment_752\" aria-describedby=\"caption-attachment-752\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-752\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-7.png\" alt=\"\" width=\"600\" height=\"299\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-7.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-7-300x150.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-7-768x383.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-752\" class=\"wp-caption-text\">Task 1.3.7<\/figcaption><\/figure>\n<pre class=\"lang:c++ decode:true \">#include \"mbed.h\"\r\n\r\nSerial pc(SERIAL_TX, SERIAL_RX);\r\n\r\n\/\/ array of chars (string)\r\nchar nameStr[30]; \r\n\/\/ integer to hold your age\r\nint age;\r\n\r\nvoid getData() \r\n{\r\n   pc.scanf(\"%s %d\", nameStr, &amp;age);\r\n}\r\n\r\nint main()\r\n{\r\n   getData();\r\n   \r\n   pc.printf(\"Hello %s \\n\\r\", nameStr);\r\n   pc.printf(\"You are %d \\n\", age);\r\n}<\/pre>\n<p><strong>Question<\/strong>: what is mean by global scope, and how does it differ from local scope?<\/p>\n<figure id=\"attachment_753\" aria-describedby=\"caption-attachment-753\" style=\"width: 600px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-753\" src=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-9.png\" alt=\"\" width=\"600\" height=\"341\" srcset=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-9.png 935w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-9-300x170.png 300w, https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-content\/uploads\/sites\/94\/2019\/09\/Task-1-3-9-768x436.png 768w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><figcaption id=\"caption-attachment-753\" class=\"wp-caption-text\">Task 1.3.9<\/figcaption><\/figure>\n<h1>Review<\/h1>\n<p>In this section, we wrote some very concise code that has only one task. There was no need for concurrency and assuming the <code>wait()<\/code> function is power-efficient, then nothing we have done is particularly wasteful of power. LEDs were controlled using General Purpose Input \/ Output (GPIO) using the <code>DigitalOutput<\/code>\u00a0type. So we we have only considered a Digital Output.<\/p>\n<p>We also used two more objects:<\/p>\n<p><strong><code>BusOut<\/code><\/strong> &#8211; for writing multiple GPIO pins<\/p>\n<p><strong><code>Serial<\/code><\/strong> &#8211; for communicating with a UART<\/p>\n<p>In the next section, we add digital inputs, which brings new challenges if we still want to be power efficient and responsive.<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"http:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/level-5-embedded-and-real-time-systems\/managing-multiple-inputs-and-outputs\/\">Next &#8211; Managing Multiple Inputs and Ouputs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of Contents In this section, we review the subject of interfacing with the mbed-os\u00ae\u00a0 framework from ARM. The focus of this module is on software, written to work in modern electronic systems.\u00a0In this section you will review the use \/ consume software components (objects) from the mbed frameworks, and later, you will learn how&hellip; <a class=\"more-link\" href=\"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/microcontrollers\/mbed-os-2\/courses\/level-5-embedded-and-real-time-systems\/review-of-mbed\/\">Continue reading <span class=\"screen-reader-text\">Review of Mbed<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":719,"menu_order":1,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-733","page","type-page","status-publish","hentry","entry"],"_links":{"self":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/733","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/comments?post=733"}],"version-history":[{"count":14,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/733\/revisions"}],"predecessor-version":[{"id":826,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/733\/revisions\/826"}],"up":[{"embeddable":true,"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/pages\/719"}],"wp:attachment":[{"href":"https:\/\/blogs.plymouth.ac.uk\/embedded-systems\/wp-json\/wp\/v2\/media?parent=733"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}