/*
This example is designed for the REva board with the STM8 daughter board.
It is the simplest possible example for toggling the REva LEDs.
Use it to get to know the REva, STM8, RLink, RIDE, etc.
*/

#include <STM8\stm8af61xx.h>

#define myGPIO GPIOB

//definition of the port registers
#define PX_ODR ((myGPIO)->ODR)
#define PX_IDR ((myGPIO)->IDR)
#define PX_DDR ((myGPIO)->DDR)
#define PX_CR1 ((myGPIO)->CR1)
#define PX_CR2 ((myGPIO)->CR2)

//empty function to avoid optimization of delay() function
void emptyfunction(){;}

//variable for the delay function
volatile unsigned long dcnt;

//delay function, waiting for a defined period
void delay(unsigned long cnt)
   {
   //initialize counter
   dcnt=cnt;
   //decrement counter until it is zero
   while(dcnt--)
      emptyfunction();
   }

//Program entry point
void main()
   {
   //main variable, copied on the LEDs
   unsigned char i=0x55;

   //configure PB in output push-pull
   PX_DDR = 0xFF;
   PX_CR1 = 0xFF;
   PX_CR2 = 0x00;

   //main loop
   while(1)
      {
      //change main variable
      //this defines the pattern of the toggling
      i = i+1; //LEDs count. they do not all blink at the same speed
      //i = ~i; //all LEDs blink together

      //copy variable to LEDs
      PX_ODR = i; //this actually modifies the LEDs state

      //wait for some time
      //the value of the parameter of this call defines the speed of the toggling
      delay(5000);
      }
   }
