SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: ARM Cortex M0 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 Cortex M0 Programming in C

Elijah Erickson
Last updated: October 5, 2023 9:58 am
Elijah Erickson 9 Min Read
Share
SHARE

The ARM Cortex-M0 is a 32-bit processor designed for low-cost and low-power embedded applications. With its simple, compact design, the Cortex-M0 is well-suited for basic microcontroller applications that don’t require the performance of more advanced ARM cores. Programming the Cortex-M0 in C provides a good balance of performance, portability, and ease of development.

Contents
Introduction to the ARM Cortex-M0Programming the Cortex-M0 in CCortex-M0 Hardware Abstraction LayerClock ConfigurationGPIO ProgrammingInterrupt HandlingTimers and CountersSerial InterfacesAnalog to Digital ConversionDirect Memory AccessDebugging on Cortex-M0Conclusion

Introduction to the ARM Cortex-M0

The Cortex-M0 is a stripped down version of the popular Cortex-M3 and Cortex-M4 processors. It has a 2-stage pipeline, no floating point unit, and limited DSP instructions. The M0 implements the ARMv6-M architecture which is a subset of the full ARM instruction set optimized for microcontrollers. Key features of the M0 core include:

  • 32-bit RISC architecture with Thumb-2 instruction set
  • Up to 48MHz clock speed
  • 16KB to 64KB of embedded flash memory
  • 4KB to 16KB of SRAM
  • Nested Vectored Interrupt Controller
  • Low power sleep modes

The Cortex-M0 is designed to offer better performance than traditional 8-bit and 16-bit microcontrollers while minimizing cost and power consumption. It is well-suited for basic industrial, home automation, sensor, motor control, and IoT applications.

Programming the Cortex-M0 in C

The C programming language is commonly used for ARM Cortex-M microcontrollers. Here are some of the benefits of using C:

  • C is familiar to most embedded programmers
  • It provides a good balance of high-level control constructs and low-level hardware access
  • C compilers are available from many vendors including GCC, Keil, IAR, and ARM
  • Code can be made portable between different ARM cores
  • C has a smaller memory footprint than languages like C++ and Java

Here is the basic workflow for programming the Cortex-M0 in C:

  1. Obtain a Cortex-M0 development board or microcontroller
  2. Download and install a C compiler and IDE if using an IDE
  3. Configure the compiler and IDE for the target MCU
  4. Write C code and create libraries for your application
  5. Compile the code to generate an executable
  6. Link the compiled output and libraries to generate a final binary
  7. Debug on actual hardware or an emulator

Cortex-M0 Hardware Abstraction Layer

One important aspect of programming the Cortex-M0 is the Hardware Abstraction Layer (HAL). The HAL provides an abstracted interface for dealing with the microcontroller’s hardware modules like GPIO, timers, UARTs, etc. It hides the low-level hardware implementation details behind a consistent API. HAL APIs are typically provided by the microcontroller vendor or real-time operating system. Some benefits of using a HAL include:

  • Portable interfaces that abstract the hardware
  • Simpler and faster development by avoiding low-level hardware
  • Reusable libraries and drivers
  • Easier migration between microcontroller families

For Cortex-M0 devices, the HAL implements functions like:

  • Clock configuration
  • GPIO pin functions
  • Interrupt handling
  • Timers and counters
  • Serial interfaces like UART, I2C
  • Analog to Digital Conversion
  • Direct Memory Access
  • Low power management

By using the HAL, developers can focus on higher-level application logic rather than low-level hardware manipulation.

Clock Configuration

One of the first things to do when programming the Cortex-M0 is to configure the microcontroller’s system clock. This sets up the clock source, multipliers, and dividers to generate the system clock used by the CPU and peripherals. For example, the M0+ might have an internal 8MHz RC oscillator that gets multiplied up to 48MHz. Clock configuration involves:

  • Enabling clock sources like internal, external and PLLs
  • Setting up clock multipliers and dividers
  • Selecting the clock source and frequency for the system clock
  • Enabling clocks for peripherals like UART, timers, GPIO

This is usually done via the microcontroller’s Clock Configuration Unit registers or HAL function calls. Proper clock configuration is critical for ensuring correct operation of both the CPU and any peripherals used by the application.

GPIO Programming

General Purpose Input/Output (GPIO) pins provide flexible I/O capability to embedded systems. The Cortex-M0 HAL provides functions to configure GPIO pins and control their behavior. Typical operations include:

  • Setting a pin as input or output
  • Configuring pull-up/pull-down resistors
  • Setting output levels to drive LEDs, motors, etc
  • Reading input pin levels from buttons or sensors
  • Support for external interrupts on GPIO pins

Setting the mode, pull states, and driving output pins is done through the GPIO peripheral registers or HAL functions. Advanced GPIO features like interrupts, de-bouncing, and complex event detection are also possible.

Interrupt Handling

The Cortex-M0 uses interrupts extensively to respond to events and user input. This allows interrupts to trigger specific event handlers rather than constantly polling for input. The Nested Vectored Interrupt Controller (NVIC) enables low latency interrupt handling. Key steps include:

  1. Enable interrupts globally using the PRIMASK register
  2. Configure interrupt priorities in the NVIC
  3. Enable interrupts on specific NVIC channels
  4. Write interrupt handler functions in C
  5. Clear pending interrupts when done

Common sources of interrupts are timers, GPIO pins, serial interfaces, ADC conversion complete, and software generated interrupts. The interrupt vector table maps each interrupt to the C function to handle it.

Timers and Counters

The Cortex-M0 includes a variety of timers, counters, and pulse width modulators for time critical operations:

  • General purpose timer in Timer0 can do PWM, input capture, output compare
  • SysTick timer generates regular interrupts for OS ticks
  • Low power timer continues running in sleep mode
  • Watchdog timer to recover from crashes

Timers are very useful for generating periodic interrupts for timekeeping, delayed execution, PWM for motors or LEDs, and other timing critical tasks. The timers can count internal clock pulses, external signal edges, or act as a PWM.

Serial Interfaces

UART, SPI, and I2C serial interfaces allow the M0 to communicate with other ICs and peripherals. Configuration includes:

  • Enabling module clock
  • Setting up GPIO pins
  • Configuring baud rates and data format
  • Setting up interrupts and DMA

UART is very commonly used for debug output, serial console I/O, and legacy connectivity. SPI allows fast synchronous full duplex transactions for SD cards and sensors. I2C provides a multi-master serial bus with good hardware addressing support.

Analog to Digital Conversion

The Cortex-M0 includes an Analog to Digital Converter (ADC) peripheral to allow sampling of analog voltages for measurement. Key steps are:

  1. Enable ADC module clock
  2. Configure ADC resolution and sampling parameters
  3. Initialize GPIO pins as analog inputs
  4. Trigger ADC conversion by software or hardware
  5. Read conversion result register

The ADC is useful for reading analog sensors like temperature, pressure, position, etc. It can also be used for applications like motor control using current sensors.

Direct Memory Access

Direct Memory Access (DMA) controllers allow data to be transferred between peripherals and memory without CPU involvement. DMA conserves CPU cycles and speeds up bulk transfers like:

  • SPI sensors to RAM
  • ADC conversion results
  • UART or I2C buffered transfers
  • PWM waveform data

DMA configuration requires setting up source and destination addresses, transfer size, triggers, interrupts, etc. DMA reduces load on the CPU and allows it to work on other tasks.

Debugging on Cortex-M0

Debugging helps identify bugs and crashes during code development. Cortex-M0 debugging requires:

  • JTAG or SWD hardware debugger
  • Debug probes like J-Link, ST-Link
  • Debugger software like GDB
  • IDE integration

Key debugging features supported are breakpoints, single stepping, register and memory inspection, and viewing disassembly. An emulator is very useful for fast debugging without needing to reflash hardware.

Conclusion

The Cortex-M0 offers a good balance of low cost, low power, and easy development. Programming it in C allows developers to take advantage of the 32-bit architecture while retaining the flexibility and portability of C. The HAL eases development by abstracting hardware complexity. Overall, the Cortex-M0 and C provide an accessible entry point for getting started with ARM microcontroller development.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article ARM Cortex-M for Beginners
Next Article How to Program ARM Cortex M0
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

What is the Difference Between ARM Cortex-A and Cortex-M?

ARM Cortex processors are designed and licensed by ARM Holdings…

5 Min Read

How to Start with Cortex-M1

The Cortex-M1 processor from ARM is a great choice for…

8 Min Read

Sleep-on-Exit for Automated Low Power in Cortex-M3 (Explained)

Sleep-on-exit is a feature in Cortex-M3 processors that allows the…

23 Min Read

Cortex M0 toggle pins not responding

If you are working with a Cortex M0 microcontroller and…

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

Sign in to your account