#include <stdint.h>

#include "rtl8710bx.h"

extern uint32_t DiagPrintf(const char *fmt, ...)
    __attribute__((format(printf, 1, 2)));
#define printf DiagPrintf

static inline void delay_cycles(unsigned int count) {
  __asm volatile(
      "1: \n"
      "  subs %[count], %[count], #1 \n"
      "  bne 1b \n"
      : [count] "+r"(count)
      :
      : "cc");
}
static inline void delay_ms(unsigned int ms) {
  for (unsigned int i = 0; i < ms; i++) {
    delay_cycles(15625);  // 62500/4 cycles
  }
}

int main(void) {
  printf("hello from main\n");

  PERI_ON->SOC_PERI_FUNC1_EN |= BIT_PERI_GPIO_EN;
  PERI_ON->PESOC_CLK_CTRL |= APBPeriph_GPIO_CLOCK;
  PINMUX_Config(_PA_0, PINMUX_FN_GPIO);
  PINMUX_ConfigPadPull(_PA_0, GPIO_PuPd_DOWN);
  GPIOA->DDR |= (1 << 0);

  while (1) {
    GPIOA->DR |= (1 << 0);
    delay_ms(2000);
    GPIOA->DR &= ~(1 << 0);
    delay_ms(2000);
  }
}
