SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: How to start ARM Cortex programming using embedded 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

How to start ARM Cortex programming using embedded C?

Eileen David
Last updated: September 14, 2023 12:26 pm
Eileen David 8 Min Read
Share
SHARE

Getting started with ARM Cortex programming using embedded C is easier than you think. With the right tools and basic knowledge, you can start building your own ARM-based applications in no time. The key is having a development board, compiler toolchain, and IDE. Once set up, you can begin coding in C/C++ and debugging your programs.

Contents
Acquire an ARM Development BoardInstall a Compiler ToolchainChoose an IDE for Programming & DebuggingStart an Example ARM Cortex ProjectLearn the Basics of Embedded CConfigure the ARM ProcessorManipulate Peripheral RegistersManage Timing & DelaysRead & Write GPIO PinsReceive & Transmit Serial DataCommunicate via Buses & NetworksRespond to System Events via InterruptsOptimize Performance with DMA TransfersInterface with Analog Devices & SignalsImplement Digital Filters & Control LoopsBuild Functional Prototypes

Acquire an ARM Development Board

An ARM development board contains a Cortex processor and peripherals to get you up and running quickly. Popular options include:

  • STM32 Discovery – Cortex-M3/M4 processor, budget-friendly
  • NXP LPCXpresso – Cortex-M0+/M3/M4, low-cost and simple
  • Raspberry Pi – Cortex-A53, Linux capable, strong community
  • BeagleBone Black – Cortex-A8, open-source hardware, capes available

Consider the features you need like USB, Ethernet, display out, debug interface, and available pins/peripherals. Most boards range from $10 to $100 USD.

Install a Compiler Toolchain

You’ll need a toolchain to compile C/C++ code into ARM instructions the processor can run. The GNU Arm Embedded Toolchain from Arm is a free, ready-to-use toolchain. Download the binary for your OS and extract it. Add the ‘bin’ folder to your system PATH to access the compiler (arm-none-eabi-gcc) and other tools from any directory.

Choose an IDE for Programming & Debugging

An integrated development environment (IDE) greatly simplifies coding and debugging. Options like Keil MDK, IAR EWARM, and OpenOCD run natively or integrate with Eclipse or Visual Studio. Most provide an editor, project management, compiler/debugger integration, and support offline simulation. Evaluate free trials and community licenses to select an IDE fitting your experience, OS, and budget.

Start an Example ARM Cortex Project

Your development board and IDE likely include example projects to start from. These demonstrate how to properly initialize the hardware, peripherals, clocks, and pins needed for bare metal (no OS) Cortex programming. Review the example code and configuration before modifying it for your own application. Pay attention to the startup sequence and any board-specific settings.

Learn the Basics of Embedded C

While standard C/C++ is used, embedded C has some key differences to understand:

  • Lack of operating system – Bare metal systems require direct hardware manipulation
  • Fixed program memory – Code size optimized for smallest footprint
  • Limited resources – Efficiently use the processor, memory, peripherals
  • Real-time processing – Meet timing deadlines, manage concurrency

Study embedded C techniques like memory management, interrupt handling, and dealing with limited resources before diving deeper.

Configure the ARM Processor

The processor and clock speeds are configured at startup, normally using CMSIS functions. This initializes core peripherals like timers, GPIO, interrupts, and DMA. Settings are applied through System Control Block (SCB) and Nested Vectored Interrupt Controller (NVIC) registers in the Cortex-M cores. Configuration code specific to your ARM chip will be provided by the vendor.

Manipulate Peripheral Registers

To control hardware peripherals like GPIO, UARTs, PWM, etc direct register manipulation is needed. Vendor-provided CMSIS headers define the addresses and bit fields for each peripheral register. Single registers can be volatile variables, but often a group of registers are defined in a struct for that peripheral. Set and clear bits using shifts, masks, and ORs to enable features, trigger actions, and pass data.

Manage Timing & Delays

Precise timing is critical in embedded systems. SysTick timers generate regular interrupts like an RTOS tick. Use cycle-accurate registers like DWT_CYCCNT to benchmark code and ensure execution deadlines are met. Busy spin loops can create small delays, but avoid long active waits which block processing. Peripheral timers and PWM also provide timing signals to pace external devices.

Read & Write GPIO Pins

General purpose I/O pins connect to sensors, buttons, LEDs, motors, and more. Configure pins as inputs with pull-up/down, filtered, or analog detection modes. Monitor input pins in code for external stimulus. Set output pins high and low to drive LEDs and small loads directly. Use latches, buffers, and drivers to connect to external devices needing higher currents.

Receive & Transmit Serial Data

UARTs, USARTs, and other serial interfaces exchange data with sensors, radios, and devices. Enable the peripheral clock and pins, then configure baud rate, data bits, and stop bits to match the external device. Use interrupts or DMA instead of polling status bits for efficient reception. Format and queue transmit data using APIs. Many boards break out serial pins to headers or USB virtual serial.

Communicate via Buses & Networks

Shared buses like I2C, SPI, and CAN minimize wiring in connected systems. Initialize each peripheral as master/slave with correct timing parameters. Frame data with device addresses, command codes, and error checking. Small Ethernet MAC/PHY chips enable networking over LAN or WAN. IP stack libraries implement TCP and UDP messaging in embedded firmware.

Respond to System Events via Interrupts

Interrupts allow immediate response to asynchronous system events like serial RX ready, ADC conversion done, or GPIO state change. Define interrupt priority and handlers using the NVIC. Trigger interrupts on logic state changes, DMA transfer completion, timer match signals, and peripheral actions. Keep handlers brief using flags to defer longer processing to the main loop.

Optimize Performance with DMA Transfers

Direct memory access (DMA) offloads data movement from the CPU to dedicated controllers. DMA improves performance for serial, SPI, ADC, PWM, and other repetitive I/O. Configure DMA channels with source/destination addresses and transfer size. Use interrupts or flags on transfer complete. Coordinate with peripherals capable of DMA like UARTs and SPI.

Interface with Analog Devices & Signals

ADCs convert analog voltages to numeric values for processing. Initialize ADC resolution, source channels, and acquisitions using interrupts or DMA. Match sample rates to the sensor bandwidth. Timer-triggered ADCs pace reading from multiple sources. DACs output analog values generated from firmware to create waveforms, drive motors, etc.

Implement Digital Filters & Control Loops

Reading noisy analog inputs requires filtering. Use exponential, FIR, or IIR filters to smooth ADC readings. Calculate PID control loop coefficients and apply to motor PWM, process variables, or other closed-loop targets. Avoid accumulated error using integral terms and compensation. Profile filter performance and tuning using real sensor data.

Build Functional Prototypes

Construct prototypes demonstrating ARM Cortex capabilities and features. Blink LEDs, read switches and sensors, drive small motors and actuators, display data on LCD screens, transmit sensor data, create audio effects. Expand proof of concept demos into practical devices with robust hardware design and enclosure.

These tips will help you get started programming ARM Cortex chips using the C language in embedded systems. Follow example projects, learn peripheral operation, optimize performance, and build prototypes to grow from Cortex beginner to experienced embedded developer.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article How to get QEMU to run an ARM Thumb binary?
Next Article ARM Cortex-M0 SPI outputting 16bit not 8bit
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

Debugging “Cannot stop Cortex-M device” error when using RTX on Cortex-M1

The "Cannot stop Cortex-M device" error when trying to debug…

11 Min Read

What is DSP in ARM processor?

Digital Signal Processing (DSP) refers to processing digital signals using…

6 Min Read

ARM Cortex M Cache

ARM Cortex-M processors are known for their power efficiency and…

8 Min Read

Debugging issues when porting the Cortex-M1 DesignStart project

Porting the Cortex-M1 DesignStart project to a new hardware platform…

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

Sign in to your account