/* Includes ------------------------------------------------------------------*/
#include "stm8l15x.h"
#include "stm8l15x_conf.h"
#include "stm8l15x_it.h" /* SDCC patch: required by SDCC for interrupts */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define Led_Init GPIO_Init(GPIOC, GPIO_Pin_4, GPIO_Mode_Out_PP_Low_Slow)
#define Led_ON GPIO_SetBits(GPIOC, GPIO_Pin_4)
#define Led_OFF GPIO_ResetBits(GPIOC, GPIO_Pin_4)
#define Led_TOG GPIO_ToggleBits(GPIOC, GPIO_Pin_4)
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void CLK_Config(void);
static void USART_Config(void);
static void putchar(uint8_t Data);
static void print(const char*);
static void println(const char*);
static void blink(uint16_t);
// static ErrorStatus RTC_Config(void);
/* Private functions ---------------------------------------------------------*/

void main(void)
{
  // CLK_Config();
  Led_Init;
  blink(1);
  // USART_Config();
  // println("Hello");
  while (1);
}

static void CLK_Config(void)
{
  /* Select LSE as system clock source */
  CLK_SYSCLKSourceSwitchCmd(ENABLE);
  CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSE);
  /* system clock prescaler: 1*/
  CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1);
  while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_LSE);
}

static void blink(uint16_t repeats) {
  for (uint16_t i = 0; i <= repeats; i++) {
    Led_ON;
    for (uint16_t j = 0; j <= 4000; j++) {nop();}
    Led_OFF;
    for (uint16_t j = 0; j <= 4000; j++) {nop();}
  }
}

// static void putchar(uint8_t Data) {
//   while (!(USART1->SR & USART_FLAG_TXE));
//   USART1->DR = Data;
// }

// static void print(const char* s){
//   while (*s) {
//     putchar(*s++);
//   }
// }

// static void println(const char* s){
//   print(s);
//   putchar('\n');
// }

// static void USART_Config(void)
// {
//   // remap USART1 to pins 5/6 (PA2/3) (TX/RX)
//   SYSCFG->RMPCR1 &= ~(0b11 << 4);
//   SYSCFG->RMPCR1 |= (0b01 << 4);

//   GPIO_Init(GPIOA, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Fast);
//   GPIO_Init(GPIOA, GPIO_Pin_3, GPIO_Mode_In_FL_No_IT);

//   CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);

//   USART_Init(USART1, (uint32_t)9600,
//              USART_WordLength_8b, USART_StopBits_1,
//              USART_Parity_No, USART_Mode_Tx);
//   USART_Cmd(USART1, ENABLE);
// }

/************************ (C) suuppl *****END OF FILE****/
