Editor’s Note: In the first of three articles excerpted from their book Fast and effective embedded systems design: Applying the ARM mbed , authors Tim Wilmshurst, and Rob Toulson take you through the basics doing ARM-based application development using the open source mbed integrated development environment. This article and the book uses the version of ARM’s processor provided by NXP Semiconductor,but it is also available for use with Freescale Semiconductor’s ARM implementation.
Microprocessors are everywhere, providing ‘intelligence’ in cars, mobile phones, household and ofﬁce equipment, televisions and entertainment systems, medical products, aircraft: the list seems endless. Those everyday products, where a microprocessor is hidden inside to add intelligence, are called embedded systems.
Not so long ago, designers of embedded systems had to be electronics experts, software experts, or both. Now, with user-friendly and sophisticated building blocks available for our use, both the specialist and the beginner can quickly engage in successful embedded system design.
One such building block is the mbed, launched by ARM Ltd. The mbed takes the form of a 2 inch by 1 inch (53 mm by 26 mm) PCB, with 40 pins arranged in two rows of 20, with 0.1 inch spacing between the pins. This spacing is a standard in many electronic components.
Figure 2.1 shows different mbed views. Looking at the main features, labeled in Figure 2.1b, we see that the mbed discussed here is based around the LPC1768 microcontroller, from NXP semiconductors, and contains an ARM Cortex-M3 core.
Program download to the mbed is achieved through a universal serial bus (USB) connector; this can also power the mbed. Usefully, there are five light-emitting diodes (LEDs) on the board, one for status and four that are connected to four microcontroller digital outputs. These allow a minimum system to be tested with no external component connections needed. A reset switch is included, to force restart of the current program.
The mbed pins are clearly identified in Figure 2.1c , providing a summary of what each pin does. In many instances the pins are shared between several features to allow a number of design options.
Top left we can see the ground and power supply pins. The actual internal circuit runs from 3.3 V. However, the board accepts any supply voltage within the range 4.5 to 9.0 V, while an onboard voltage regulator drops this to the required voltage. A regulated 3.3 V output voltage is available on the top right pin, with a 5 V output on the next pin down.
The remainder of the pins connect to the mbed peripherals. These are almost all the subject of later chapters; we will quickly overview them here, though they may have limited meaning to you now. There are no fewer than five serial interface types on the mbed: I2C, SPI, CAN, USB and Ethernet.
Then there is a set of analog inputs, essential for reading sensor values, and a set of PWM outputs useful for control of external power devices, for example DC motors. While not immediately evident from the figure, pins 5 to 30 can also be configured for general digital input/output.
The mbed is constructed to allow easy prototyping, which is of course its very purpose. While the PCB itself is very high density, interconnection is achieved through the very robust and traditional dual-in-line pin layout.
Background information for the mbed and its support tools can be found at the mbed home page . While this book is intended to give you all information that you need to start work with the mbed, it is inevitable that you will want to keep a close eye on this site, with its cookbook, handbook, blog and forum. Above all else, it provides the entry point to the mbed compiler, through which you will develop all your programs.
The mbed Architecture
A block diagram representation of the mbed architecture is shown in Figure 2.2 . It is possible, and useful, to relate the blocks shown here to the actual mbed. At the heart of the mbed is the LPC1768 microcontroller, clearly seen in Figures 2.1 and 2.2.
The signal pins of the mbed, as seen in Figure 2.1c , connect directly to the microcontroller. Thus, when in the coming chapters we use an mbed digital input or output, or the analog input, or any other of the peripherals, we will be connecting directly to the microcontroller within the mbed, and relying on its features.
An interesting aside to this, however, is that the LPC1768 has 100 pins, but the mbed has only 40. Thus, when we get deeper into understanding the LPC1768, we will find that there are some features that are simply inaccessible to us as mbed users. This is, however, unlikely to be a limiting factor.
There is a second microcontroller on the mbed, which interfaces with the USB. This is called the interface microcontroller in Figure 2.2, and is the largest integrated circuit (IC) on the underside of the mbed PCB.
The cleverness of the mbed hardware design is the way which this device manages the USB link and acts as a USB terminal to the host computer. In most common use it receives program code files through the USB, and transfers those programs to a 16 Mbit memory, which acts as the ‘USB disk’.
When a program ‘binary’ is downloaded to the mbed, it is placed in the USB disk. When the reset button is pressed, the program with the latest timestamp is transferred to the flash memory of the LPC1768, and program execution commences. Data transfer between interface microcontroller and the LPC1768 goes as serial data through the UART (which stands for universal asynchronous receiver/ transmitter e a serial data link, let’s not get into the detail now) port of the LPC1768.
The ‘Power Management’ unit is made up of two voltage regulators, which lie either side of the status LED. There is also a current-limiting IC, which lies at the top left of the mbed. The mbed can be powered from the USB; this is a common way to use it, particularly for simple applications.
For more power-hungry applications, or those that require a higher voltage, it can also be powered from an external 4.5 to 9.0 V input, supplied to pin 2 (labeled VIN). Power can also be sourced from mbed pins 39 and 40 (labeled VU and VOUT, respectively). The VU connection supplies 5 V, taken almost directly from the USB link; hence it is only available if the USB is connected. The VOUT pin supplies a regulated 3.3 V, which is derived either from the USB or from the VIN input. (For those who are inclined, the mbed circuit diagrams are available on the mbed website .The LPC1768 Microcontroller
A block diagram of the LPC1768 microcontroller is shown in Figure 2.3 . Top center in the figure – contained within the dotted line – is the core of this microcontroller, the ARM Cortex-M3.
Tothe left of the core are the memories: the program memory, made withFlash technology, is used for program storage; to the left of that isthe static RAM (random access memory), used for holding temporary data.That leaves most of the rest of the diagram to show the peripherals,which give the microcontroller its embedded capability. These lie in thecenter and lower half of the diagram, and reflect almost exactly whatthe mbed can do. It is interesting to compare the peripherals seen herewith the mbed inputs and outputs seen in Figure 2.1c.
Finally,all these things need to be connected together, a task done by theaddress and data buses. Clever though they are, we have almost nointerest in this side of the microcontroller design, at least not forthis book. It is sufficient to note that the peripherals connect throughsomething called the advanced peripheral bus.
This in turnsconnects back through a bus interconnect called the advancedhigh-performance bus matrix, and from there to the central processingunit (CPU). This interconnection is not completely shown in thisdiagram, and we have neither need nor wish to think about it further.
Getting Started with the mbed
Nowcomes the big moment when you connect the mbed for the first time, andrun a first program. We will follow the procedure given on the mbedwebsite and use the introductory program on the compiler, a simpleflashing LED example. You will need:
- an mbed microcontroller with its USB lead
- a computer running Windows (XP, Vista or 7), Mac OS X or GNU/Linux
- a web browser, for example Internet Explorer or Firefox.
Nowfollow the sequence of instructions below. The purpose of this tutorialis to explain the main steps in getting a program running on the mbed.
Step 1: Connecting the mbed to the PC.Connect the mbed to the PC using the USBlead. The Status light will come on, indicating that the mbed has power.After a few seconds of activity, the PC will recognize the mbed as astandard removable drive, and it will appear on the devices linked tothe computer, as seen in Figure 2.4 .
Step 2: Creating an account. Open the MBED.HTM file found on the mbed in yourweb browser and click the Create a new mbed Account link. Follow theinstructions to create an mbed Account. This will lead you to thewebsite, as seen in Figure 2.5. From here you can link to the compiler,libraries and documentation.
Step 3. Running a program. Open the compiler using the link in the site menu, i.e. at the right of Figure 2.5 .By doing this you enter your allocated personal program workspace. Thecompiler will open in a new tab or window. Follow these steps to create anew program:
1. As seen in Figure 2.6a , right-click (Mac users, Ctrl-click) on ‘My Programs’ and select ‘New Program’.
2.Choose and enter a name for the new program (for example Prog_Ex_2_1)and click ‘OK’. Do not leave spaces in the program name.
3. Your new program folder will be created under ‘My Programs’.
Click on the ‘main.cpp’ file in your new program to open it in the file editor window, as seen in Figure 2.6b .This is the main source code file in your program. Whenever you create anew program it always contains the same simple code. This is shown hereas Program Example 2.1 .
The other item in the programfolder is the ‘mbed’ library. This provides all the functions used tostart up and control the mbed, such as the DigitalOut interface used inexample code below.
Step 4. Compilingthe program. To compile the program, click the ‘Compile’ button in thetoolbar. This will compile all the source code files within the programfolder to create the binary machine code which will be downloaded to thembed. Typically, this is the single program you have written, plus thelibrary calls you have almost certainly made. After a successfulcompile, you will get a ‘Success!’ message in the compiler output, and apopup will prompt you to download the compiled .bin file to the mbed.
Ofcourse, with this given program, it would be most surprising to find ithad an error in it; you will not be so lucky with future programs! Tryinserting a small error into your source code, for example by removingthe semi-colon at the end of a line, and compiling again. Notice how thecompiler gives a useful error message at the bottom of the screen.
Correctthe error, compile again and proceed. The type of error you have justinserted is often called a syntax error. This is an error which relatesto the rules of writing lines of C code. When a syntax error is found,the compiler is unable to proceed with the compilation, as it perceivesthat the program has stepped outside the rules of the language, andhence cannot reliably interpret the code that is written.
Step 5. Downloading the program binary code. After a successful compile, theprogram code, in binary form, can be downloaded to the mbed. Save it tothe location of the mbed drive. You should see the Status LED (as seenin Figure 2.1) flash as the program downloads. Once the Status LED hasstopped flashing, press the reset button on the mbed to start yourprogram running. You should now see LED1 flashing on and off every 0.2seconds.
Step 6. Modifying the program code. In the main.cpp file, simply change the DigitalOut statement to read:
Nowcompile and download your code to the mbed. You should now see thatLED4 flashes instead of LED1. You can also change the pause betweenflashes by modifying the values bracketed in the wait( ) command.
Tim Wilmshurst ,head of Electronics at the University of Derby, led the ElectronicsDevelopment Group in the Engineering Department of Cambridge Universityfor a number of years, before moving to Derby. His design career hasspanned much of the history of microcontrollers and embedded systems.
Rob Toulson is Research Fellow at Anglia Ruskin University in Cambridge. Aftercompleting his PhD, Rob spent a number of years in industry, where heworked on digital signal processing and control systems engineeringprojects, predominantly in audio and automotive ﬁelds. He then moved toan academic career, where his main focus is now in developingcollaborative research between the technical and creative industries.
This article is excerpted from Fast and effective embedded systems design: Applying the ARM mbed byRob Toulsonand Tim Wilmshurst, used with permission from Newnes, adivision of Elsevier. Copyright 2012. All rights reserved. For moreinformation on this title and other similar books, visit www.newnespress.com .