/**
 * Projekt Kap08-SysTick-01
 * ========================
 *
 * Ein Beispiel zum Einsatz des SysTick-Timers (das teilweise aber im Vorgriff
 * auf dieses Kapitel bereits früher verwendet wurde).
 */

#if !defined(__SOFT_FP__) && defined(__ARM_FP)
  #warning "FPU is not initialized, but the project is compiling for an FPU. Please initialize the FPU before use."
#endif

#include <stm32f4xx.h>
#include <system_stm32f4xx.h>

#include <mcalGPIO.h>

void SysTick_Handler(void);

uint16_t tick = 0;

int main(void)
{
	uint8_t pinState;

    // Die beiden folgenden Zeilen werden als systickInit(uint32_t divisor)
    // in MCAL aufgenommen. Fuer den Divisor werden in mcalSysTick.h
    // Konstanten eingefuehrt, die die Zeitbasis festlegen.
    SystemCoreClockUpdate();                  // SysTick: Konfig.
    SysTick_Config(SystemCoreClock / 1000);   // SysTick = 1 ms

    gpioInitPort(GPIOA);
    gpioSelectPinMode(GPIOA, PIN0, OUTPUT);
    gpioSetOutputType(GPIOA, PIN0, PUSHPULL);
    gpioSelectPushPullMode(GPIOA, PIN0, PULLUP);

    while(1)
    {
    	if (tick > 499)
    	{
    		// Testet, ob PA0 auf High- oder auf Low-Level ist
    		pinState = gpioGetPinState(GPIOA, PIN0, &pinState);
    		if (pinState)
    		{
    			gpioResetPin(GPIOA, PIN0);
    		}
    		else
    		{
    			gpioSetPin(GPIOA, PIN0);
    		}

    		tick = 0;
    	}
    }
}

/**
 * Interrupt-Service-Routine des SysTick-Timers
 */
void SysTick_Handler(void)
{
    ++tick;
}
