#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/usb/usbd.h>

#include <SEGGER_RTT.h>
#include "cdcacm.h"
#include "rndis.h"

int main(void) {
    usbd_device *usbd_dev;


    SEGGER_RTT_printf(0, "\nHello World\n");

    rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);

    rcc_periph_clock_enable(RCC_GPIOB);


    /* Setup GPIOB Pin 9 to pull up the D+ high, so autodect works
     * with the bootloader.  The circuit is active high. */
    gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
                  GPIO_CNF_OUTPUT_OPENDRAIN, GPIO9);

    /* Setup GPIOB Pin 1 for the LED */
    gpio_set(GPIOB, GPIO1);
    gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
                  GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);

    SEGGER_RTT_printf(0, "Starting USB\n");

    rcc_periph_clock_enable(RCC_AFIO);
    rcc_periph_clock_enable(RCC_OTGFS);


    usbd_dev = start_rdnis();


    SEGGER_RTT_printf(0, "Main loop\n");

    while (1) {
        usbd_poll(usbd_dev);
        gpio_toggle(GPIOB, GPIO1);
    }


    return 0;
}
