HWA
Bare metal programming with style
hwa/st/stm32/examples/03-2-systick-irq/main.c

This program blinks a LED using the SysTick timer exception.

main.c
/* This file is part of the HWA project.
* Copyright (c) 2017 Christophe Duparquet.
* All rights reserved. Read LICENSE.TXT for details.
*/
#include BOARD_H
#define SYSHZ 36e6 // Desired frequency for the SYSCLK signal
#define AHBHZ 9e6 // Desired frequency for the core (and systick)
#define PERIOD 0.5 // Blinking period
/* The IRQ is used only to wake the core up.
*/
HW_ISR(systick) {}
int main ( )
{
hwa( begin, reset );
/* Configure the PLL source and multiplier (must be done before it is enabled).
* Prepare the connection of the sysclk to the pll. The hardware will wait for
* the PLL to be locked before actually switching.
*/
hwa( configure, pll,
input, hsi/2,
multiplier, SYSHZ/(HW_DEVICE_HSIHZ/2) );
hwa( connect, sysclk, pll );
hwa( commit );
/* Turn the PLL on.
*/
hwa( power, pll, on );
hwa( commit );
/* Wait for the PLL to be locked.
*/
while ( ! hw(stat,pll).ready ) {}
/* Configure the AHB
*/
hwa( clock, ahb, sysclk / (SYSHZ/AHBHZ) );
hwa( commit );
/* Configure the GPIO pin
*/
hwa( power, (LED,port), on );
hwa( commit );
hwa( configure, LED,
mode, digital_output,
frequency, lowest );
hwa( commit );
/* Configure the system tick timer
*/
uint32_t onems = hw(read, (systick,onems));
hwa( configure, systick,
clock, ahb,
reload, ((uint32_t)(PERIOD/2 / 0.001)*onems - 1) & 0xFFFFFF );
hwa( turn, systick, on );
hwa( enable, (systick,irq) );
hwa( commit );
/* Toggle the LED between sleeps
*/
for(;;) {
hw( wait, irq );
hw( toggle, LED );
}
}
hwa
#define hwa(...)
hwa( action, object [,...] ) stores an action for an object into a HWA context.
Definition: hwa_macros.h:552
HW_DEVICE_HSIHZ
#define HW_DEVICE_HSIHZ
Definition: stm32f103.h:82
hw
#define hw(...)
hw( action, object [,...] ) executes an action immediately on an object.
Definition: hwa_macros.h:523
LED
#define LED
X.
Definition: bluepill.h:55
HW_ISR
#define HW_ISR(...)
Definition: hwa_interrupts.h:80