ARM microcontrollers support assembly language and C/C++ as their primary programming languages. Assembly language provides low-level control over the microcontroller, while C/C++ allows for easier and faster application development. The choice depends on factors like performance requirements, development time, code size, and programmer experience.
Introduction to ARM Microcontrollers
ARM microcontrollers are RISC-based embedded processor cores designed and licensed by ARM Holdings. They are found in a wide range of devices from smartphones to appliances due to their power efficiency, performance, and relative simplicity compared to other architectures.
The ARM architecture is 32-bit, meaning its general purpose registers and data bus are 32 bits wide. This provides a good balance of performance, power efficiency, and cost. ARM cores range from the ultra low power Cortex-M series to the high performance Cortex-A series optimized for mobile applications.
ARM microcontrollers combine one or more ARM processor cores along with embedded memory and peripherals like timers, ADCs, serial communication blocks, etc. on a single chip. This provides a flexible and cost-effective solution for embedded products.
Programming Languages Supported
The two primary programming languages used for ARM microcontrollers are:
- Assembly Language
Let’s look at each of these in more detail:
Assembly language is the lowest level programming language available on ARM microcontrollers. It provides a thin layer above the underlying machine code, with a one-to-one correspondence between assembly instructions and machine code instructions.
Coding in assembly language gives the programmer complete control over the microcontroller. All registers, memory, I/O, interrupts etc. can be directly accessed and manipulated through assembly code. This allows for very efficient and compact code to be written.
The downside is that assembly language requires greater programmer effort and expertise. Simple operations like displaying text on a screen may take many lines of assembly code compared to a few lines in a higher level language.
Assembly language is generally used in situations where optimizing performance, code size and precise timing are critical. For example, some key functions in operating systems, cryptographic algorithms, motor controls etc. are often written in assembly language even when the rest of the system uses C.
The C programming language, along with object-oriented extensions like C++, is the most common high level language used for ARM microcontrollers.
C/C++ provides many advantages over assembly language such as:
- Faster and easier development due to higher abstraction from hardware.
- Reusability through functions, libraries and classes.
- Portability across different ARM cores and even other architectures after recompilation.
- Easier debugging and maintenance from improved readability.
The compilation process translates C/C++ code into target-specific assembly or machine code. This allows programmers to take advantage of high level features without sacrificing performance. Compilers also perform optimizations to improve speed and code size.
C/C++ gives access to underlying hardware through libraries and intrinsics. Peripheral registers can be directly accessed using special variables, and functions can invoke specific assembly instructions. This bridges the gap between high level code and low level control.
The C language has been efficiently implemented on ARM cores making it well suited for embedded applications. Most ARM microcontrollers provide an on-chip C library with common functions. C++ support may be more limited but is available on many cores.
Choosing the Right Language
The choice between assembly and C/C++ depends on several factors:
- Performance requirements – For very fast or timing critical code, assembly language may be required to meet specs.
- Code size constraints – Assembly code can be extremely compact which may be necessary in memory limited devices.
- Development time – C/C++ is significantly faster to develop in compared to assembly language.
- Developer experience – Assembly language requires greater experience and expertise.
- Reusability needs – C/C++ code can be modularized and reused easily across projects.
For many applications, a combination approach is used. The bulk of the code is written in C/C++, which calls key assembly language functions for hardware interaction or performance optimization as needed.
Here are some general guidelines on choosing the programming language:
- Use C/C++ as the primary language when rapid development is needed.
- Opt for assembly language when code size and performance are the utmost priority.
- Leverage C/C++ for hardware abstraction and overall structure, utilizing assembly language selectively.
- Start prototyping in C/C++, then optimize critical parts later in assembly language.
Most ARM microcontrollers include toolchains, libraries and code examples for both assembly language and C/C++ support. Developers have the flexibility to use the best language for their application requirements.
There are several programming environments and tools used to develop applications on ARM microcontrollers:
Assemblers convert assembly language source files into machine code. Each ARM core has its own assembly language formatted as per the Unified Assembler Language (UAL). Popular assemblers used are:
- GNU Assembler (GAS) – Open source, included with GCC toolchain.
- ARM Assembler (ARMASM) – Provided by ARM in the ARM Development Studio.
- Keil μVision IDE – Contains assembler for ARM Cortex-M cores.
Compilers convert high level C/C++ code into target assembly or machine code. Some commonly used compilers are:
- GCC (GNU Compiler Collection) – Open source compilers with support for C/C++.
- ARM Compiler – Optimizing C/C++ compilers from ARM.
- IAR C/C++ Compiler – Optimized compilers bundled with IAR’s IDE.
Debuggers are vital tools for testing application code and finding bugs. They allow stepping through code while monitoring registers, memory and peripherals. Some debugging options are:
- GDB – GNU debugger that works with the GCC toolchain.
- ARM Development Studio – Contains debugger from ARM.
- IAR Embedded Workbench – Integrated debugger in IAR’s IDE.
- SEGGER J-Link – Popular JTAG/SWD debug probe with debugging software.
Integrated Development Environments (IDEs)
IDEs provide a unified software environment for managing the entire code development workflow – editing, compiling, debugging etc. Some IDE choices are:
- Eclipse IDE – Open source IDE with ARM plugins available.
- IAR Embedded Workbench – Full featured commercial IDE from IAR Systems.
- Keil μVision IDE – Popular commercial IDE from Arm subsidiary Keil.
ARM microcontrollers provide assembly language and C/C++ support, enabling both low level control and easier high level programming. Assembly language is used when performance and code size are critical, while C/C++ improves developer productivity. The large ecosystem of compilers, debuggers and IDEs available makes ARM microcontrollers flexible and easy to program for a wide range of embedded applications.