TITLE: ARM Cortex M4 Blink Example
AUTHOR: Chuck McManis
LAST UPDATE: 25-May-2014

Description

This is the ARM specific version of the blink.c code. A simple example that, when successfully compiled and run, can prove to you that your toolchain is working. Once you have built and run this program then you can use the exact same techniques in building addtional programs that run on ARM chips.

The Source Code

/*
 * Very simple BLINK example for the STM32F4 chip.
 * Minimal setup, just blink.
 * Code: Public Domain
 * Author : Chuck McManis
 */
#include <stdint.h>

#define GPIOD       0x40020C00ul    // from the data sheets
#define GPIOD_MODE  (*(uint32_t *)(GPIOD+0x00ul))
#define GPIOD_TYPE  (*(uint32_t *)(GPIOD+0x04ul))
#define GPIOD_SPEED (*(uint32_t *)(GPIOD+0x08ul))
#define GPIOD_PUPD  (*(uint32_t *)(GPIOD+0x0aul))
#define GPIOD_IN    (*(uint32_t *)(GPIOD+0x10ul))
#define GPIOD_OUT   (*(uint32_t *)(GPIOD+0x14ul))

#define RCC_BASE    0x40023800ul
#define RCC_ENABLE  (*(uint32_t *)(RCC_BASE + 0x30ul))
#define GPIOD_ENA   0x8

// No exit, just sit and wait
void _exit(int r) {
    while (1) {
        asm("nop");
    }
}

int delay_time = 100000;

void
main() {
    int i;

    RCC_ENABLE = GPIOD_ENA; // Enable clocks to GPIOD
    GPIOD_MODE = 0x55000000; // Bits 15 - 12 all output
    while (1) {
        GPIOD_OUT = 0xa000; // two LEDs on
        for (i = 0; i < delay_time; i++) {
            asm("nop");
        }
        GPIOD_OUT = 0x5000; // other two LEDs on
        for (i = 0; i < delay_time; i++) {
            asm("nop");
        }
    }
}

void
SystemInit() {
    main();
}

License

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License. You are free to play around with it and modify it but you are not licensed to use it for commercial purposes. Click the link above for more details on your rights under this license.