SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: ARM Programming in C
SUBSCRIBE
SoCSoC
Font ResizerAa
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Search
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
Have an existing account? Sign In
Follow US
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
© S-O-C.ORG, All Rights Reserved.
Arm

ARM Programming in C

Elijah Erickson
Last updated: September 7, 2023 1:29 am
Elijah Erickson 6 Min Read
Share
SHARE

ARM processors are very popular in embedded systems due to their power efficiency and performance per watt. Programming ARM processors in C is a common task for embedded developers working on products like smartphones, tablets, IoT devices, and more. Here is an overview of how to get started with ARM programming using C and the ARM toolchain.

Contents
Introduction to the ARM ArchitectureGetting Started with ARM C Programming1. Using a vendor SDK2. Installing GNU ARM Embedded Toolchain3. Using ARM Keil MDK4. Using online IDEsARM Compiler OptionsARM C Programming TipsARM C Code ExamplesInitialize Clock and PeripheralsControl GPIO PinsHandle Interrupts

Introduction to the ARM Architecture

ARM processors are based on the ARM architecture, which refers to the ARM instruction set and programming model. Some key features of the ARM architecture include:

  • RISC (Reduced Instruction Set Computer) design for high efficiency
  • Load/store architecture where data is processed in registers, not memory
  • Fixed-length 32-bit instruction set to simplify decoding
  • Conditional execution of most instructions to maximize performance
  • Thumb instruction set, a 16-bit compressed version of the 32-bit ARM instruction set
  • SIMD (Single Instruction Multiple Data) instructions for parallel processing
  • Memory protection unit (MPU) for security

There are many different ARM processor cores that implement the ARM architecture, from Cortex-M microcontrollers to Cortex-A application processors. All ARM cores utilize the same ARM instruction set and programming model, which simplifies software development across the ARM ecosystem.

Getting Started with ARM C Programming

To program an ARM processor in C, you will need:

  • An ARM-based development board or target hardware
  • A cross-compiler toolchain for ARM C programming
  • The ARM C/C++ standard libraries
  • Debugging tools like GDB

Here are some options for setting up an ARM C programming environment:

1. Using a vendor SDK

Many ARM chip vendors like NXP, STMicroelectronics, and Texas Instruments provide free SDKs (Software Development Kits) for their ARM processors. These SDKs include the compiler toolchain, libraries, and debugging tools pre-configured for the vendor’s chips.

2. Installing GNU ARM Embedded Toolchain

The GNU ARM Embedded toolchain is an open source ARM cross-compiler and debugger that works with many ARM processors. It can be downloaded and installed on Linux, Mac and Windows systems.

3. Using ARM Keil MDK

ARM Keil MDK is a commercial IDE for ARM C/C++ development. The MDK kit contains uVision IDE, C compiler, debugger, libraries and code analysis tools. There is a free MDK-Lite version available.

4. Using online IDEs

Some online IDEs like ARMmbed provide browser-based ARM C/C++ development and compilation. This allows coding ARM projects without installing a toolchain locally.

ARM Compiler Options

ARM compilers like armclang have many options to control and optimize the compilation process. Here are some common armclang flags:

  • -mcpu – Select ARM processor core, like Cortex-M4
  • -mfpu – Specify floating point unit, like fpv4-sp-d16 for single precision
  • -mfloat-abi – Choose floating point ABI, like hard or softfp
  • -mthumb – Compile for Thumb 16-bit instruction set
  • -O1 – Basic optimization level
  • -O2 – Medium optimization level
  • -O3 – High optimization level
  • -g – Add debug information
  • -c – Compile only, do not link

Consult your compiler documentation for details on supported options. Optimization flags like -O2 are recommended for production code size and performance.

ARM C Programming Tips

Here are some tips for effective ARM C programming:

  • Use stack variables cautiously since ARM Cortex-M has limited stack space (8-64KB)
  • Use static variables and avoid heap allocation when possible
  • Take advantage of Thumb-2 instruction set optimizations in Cortex-M
  • Enable compiler optimizations like -O2 to minimize code size
  • Use bit banding to access bit variables and registers efficiently
  • Utilize CMSIS functions where possible instead of raw register access
  • Manage idle time and sleep modes to optimize power consumption
  • Handle interrupts quickly and use priority levels appropriately
  • Consider using a RTOS or scheduler for more complex programs

ARM C Code Examples

Here are some example code snippets demonstrating ARM C programming on Cortex-M processors:

Initialize Clock and Peripherals

#include “stm32l4xx.h” int main(void) { // Configure system clock RCC->CR |= RCC_CR_HSION; while(!(RCC->CR & RCC_CR_HSIRDY)); // Enable GPIOC clock RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN; // Enable USART2 clock RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN; // Configure PC10 and PC11 for USART2 pins GPIOC->MODER &= ~(GPIO_MODER_MODER10|GPIO_MODER_MODER11); GPIOC->MODER |= (GPIO_MODER_MODER10_1 | GPIO_MODER_MODER11_1); // Configure USART2 USART2->BRR = 0x1A1; // 115200 baud @ 80MHz USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // … }

Control GPIO Pins

#define LED_PIN 5 // Set GPIOA Pin 5 as output GPIOA->MODER &= ~GPIO_MODER_MODER5; GPIOA->MODER |= GPIO_MODER_MODER5_0; // Toggle LED pin while(1) { GPIOA->ODR ^= (1 << LED_PIN); for(int i=0; i<200000; i++); // Delay }

Handle Interrupts

volatile int ticks = 0; void SysTick_Handler(void) { ticks++; // Increment counter in ISR } int main(void) { // Setup SysTick timer for 1ms interrupts SysTick->LOAD = 8000-1; SysTick->VAL = 0; SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; while(1) { // Do work here } }

With these building blocks, you can start writing C programs for ARM Cortex-M devices to implement real-time control, signal processing, communications protocols, sensor interfaces, and more for your embedded system.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Cortex-R Microcontroller
Next Article Cortex-M Rust
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

2k Followers Like
3k Followers Follow
10.1k Followers Pin
- Sponsored-
Ad image

You Might Also Like

Little Endian Explained

Little endian is a memory architecture where the least significant…

8 Min Read

What is the stack pointer in the ARM Cortex-M4?

The stack pointer in the ARM Cortex-M4 is a register…

11 Min Read

What are Saturated Math Instructions in Arm Cortex-M Series?

Saturated math instructions in Arm Cortex-M series refer to arithmetic…

9 Min Read

Use the same ISR for multiple interrupt sources in Cortex M0+

The Cortex M0+ processor supports handling multiple interrupt sources using…

8 Min Read
SoCSoC
  • Looking for Something?
  • Privacy Policy
  • About Us
  • Sitemap
  • Contact Us
Welcome Back!

Sign in to your account