SoC
  • Home
  • Arm
  • Arm Cortex M0/M0+
  • Arm Cortex M4
  • Arm Cortex M3
  • Contact
Reading: Basic Flash Programming and the process in integrating Cortex M0
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

Basic Flash Programming and the process in integrating Cortex M0

Neil Salmon
Last updated: October 5, 2023 9:58 am
Neil Salmon 6 Min Read
Share
SHARE

Flash memory programming is essential for working with microcontrollers like the Cortex M0 that utilize on-chip flash memory for storing firmware. Understanding the basic concepts of flash memory and the process of programming it enables developers to efficiently write, deploy and update firmware on Cortex M0 chips.

Contents
Overview of Flash MemoryFlash Memory OrganizationProgramming Flash MemoryIn-System ProgrammingIntegrating Flash Programming in Cortex M0Example Flash RoutineConclusion

Overview of Flash Memory

Flash memory is a type of non-volatile memory that can be electrically erased and reprogrammed. It got its name because data is erased in blocks resembling a flash of light. Key advantages of flash memory include:

  • Data is retained when power is removed
  • High density allowing large amounts of data storage
  • Faster read access compared to other non-volatile memories
  • In-system programmable allowing firmware upgrades

These characteristics make flash memory well-suited for storing firmware in embedded systems. The Cortex M0 processor utilizes flash memory to store its program code and data.

Flash Memory Organization

The flash memory in Cortex M0 is organized into pages and sectors. A page is the smallest unit that can be programmed. Typical page sizes are 512 bytes to 4KB. A sector is the smallest unit that can be erased. A sector contains multiple pages, often 16 to 256 pages. The large erase block sizes are a key limitation of flash memory.

Programming Flash Memory

Programming flash memory involves two steps – erasing old contents and then writing new data. A sector needs to be erased before new data can be written to the pages within that sector. Erasing sets all bits in a sector to 1. Programming pages can only change 1s to 0s. To erase a sector, an erase command is issued along with the sector address. Page programming requires the page address and new data to be written. The process is:

  1. Erase the target sector
  2. Load new data into a page buffer
  3. Program the page by writing the buffered data

Partial page programming is also possible where new data is merged with existing data in the page buffer before programming. Page buffers speed up programming since data can be accumulated before a program operation.

In-System Programming

A key feature of flash memory is in-system programmability. This allows firmware code on microcontrollers to be updated without removing chips from the system. Special program instructions are used to access the flash memory controller and reprogram firmware contents.

Different techniques for in-system programming include:

  • Bootloader: A bootloader is a small program that executes on reset and handles reprogramming flash memory.
  • External Programmer: A dedicated programmer connected to device programming headers programs the flash memory.
  • Software Update: Updates are delivered over a communication interface like USB or UART.
  • Self-Programming: The firmware itself contains routines to reprogram flash memory.

Bootloaders allow new firmware to be installed without needing physical access to the device. Software updates make it easy to deploy firmware fixes and upgrades remotely.

Integrating Flash Programming in Cortex M0

The Cortex M0 processor includes an embedded flash controller that manages flash erase and program operations. Interfacing with the flash memory involves several steps:

  1. Configure Flash Access: Flash memory is accessible in the Cortex M0 memory map. Flash memory access needs to be enabled by configuring the Flash Access Protection Register.
  2. Issue Erase Command: An erase command with a sector address erases the target sector. Status polling confirms completion.
  3. Load Data Buffer: Prior to programming, new data should be loaded into the flash controller’s program buffer.
  4. Issue Program Command: The program command writes the buffered data to the target page. The Flash Status Register can be polled to confirm completion.

Vendor IDEs like Keil MDK take care of these low-level details of flash programming. But understanding the process is useful when developing custom bootloaders or flash routines.

Example Flash Routine

Here is an example demonstrating flash programming on Cortex M0 in C: // Flash memory module base address #define FLASH_BASE 0x40022000 // Function to erase flash sector void flash_erase_sector(uint32_t sector) { // Enable flash controller access FLASH_BASE->KEYR = 0x45670123; // Start sector erase FLASH_BASE->CR |= (1 << 1); // Set ER bit FLASH_BASE->AR = sector; // Write sector address // Wait for operation complete while(FLASH_BASE->SR & (1 << 0)) {} // Poll BSY bit } // Function to program flash page void flash_program_page(uint32_t address, uint32_t data[]) { // Load data into program buffer for(int i = 0; i < 128; i++) { FLASH_BASE->DR[i] = data[i]; } // Trigger page program FLASH_BASE->CR |= (1 << 0); // Set PG bit FLASH_BASE->AR = address; // Set page address // Wait for completion while(FLASH_BASE->SR & (1 << 0)) {} }

This demonstrates the basic steps to integrate flash programming routines for Cortex M0. Vendor libraries and IDEs provide higher level functions, but understanding the core flash interface is key.

Conclusion

Flash memory is indispensable in microcontrollers for storing firmware. The Cortex M0 processor has integrated flash that can be programmed and erased through a flash controller module. Key steps are erasing sectors, loading page buffers and programming pages. With an understanding of the flash memory organization and programming process, developers can write efficient firmware update capabilities taking advantage of in-system programmability.

Newsletter Form (#3)

More ARM insights right in your inbox

 


Share This Article
Facebook Twitter Email Copy Link Print
Previous Article Code and RAM Size Optimization on ARM Cortex-M0
Next Article Off-chip Memory integration with 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

Does Arm Cortex-M4 have FPU?

The short answer is yes, the Arm Cortex-M4 processor core…

8 Min Read

SysTick Interrupt Handler Design Tips for Cortex-M

The SysTick timer is an essential peripheral found in all…

8 Min Read

Why does my Cortex-M4 assembly run slower than predicted?

There are several potential reasons why Cortex-M4 assembly code may…

9 Min Read

M0 SDK Load Program From External SPI Flash

The M0 SDK provides a way to load programs from…

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

Sign in to your account