Skip to content

void systemInit(void)

nasagaru edited this page Mar 3, 2015 · 6 revisions
void systemInit(void)
{
    struct {
        GPIO_TypeDef *gpio;
        gpio_config_t cfg;

GPIO_TypeDef

gpio_config_t


    } gpio_setup[] = {
        {
            .gpio = LED0_GPIO,
            .cfg = { LED0_PIN, Mode_Out_PP, Speed_2MHz }
        },
        {
            .gpio = LED1_GPIO,
            .cfg = { LED1_PIN, Mode_Out_PP, Speed_2MHz }
        },
#ifdef BUZZER
        {
            .gpio = BEEP_GPIO,
            .cfg = { BEEP_PIN, Mode_Out_OD, Speed_2MHz }
        },
#endif

#define LED0_GPIO   GPIOB
#define LED0_PIN    Pin_9 // PB4 (LED)
#define LED1_GPIO   GPIOA
#define LED1_PIN    Pin_15 // PB5 (LED)
#define BEEP_GPIO   GPIOB
#define BEEP_PIN    Pin_12 // PA12 (Buzzer)

    };
    gpio_config_t gpio;
    uint32_t i;
    uint8_t gpio_count = sizeof(gpio_setup) / sizeof(gpio_setup[0]);


    // Configure the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers
    // Configure the Flash Latency cycles and enable prefetch buffer
    SetSysClock();

SetSysClock()


    // Turn on clocks for stuff we use
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C2, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM1 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
    RCC_ClearFlag();

    // Make all GPIO in by default to save power and reduce noise
    gpio.pin = Pin_All;
    gpio.mode = Mode_AIN;
    gpioInit(GPIOA, &gpio);
    gpioInit(GPIOB, &gpio);
    gpioInit(GPIOC, &gpio);

    // Turn off JTAG port 'cause we're using the GPIO for leds
#define AFIO_MAPR_SWJ_CFG_NO_JTAG_SW            (0x2 << 24)
    AFIO->MAPR |= AFIO_MAPR_SWJ_CFG_NO_JTAG_SW;

#ifdef BUZZER
    // Configure gpio
    // rev5 needs inverted beeper. oops.
    if (hse_value == 12000000)
        systemBeepPtr = beepRev5;
    else
        systemBeepPtr = beepRev4;
    BEEP_OFF;
#endif
    LED0_OFF;
    LED1_OFF;

    for (i = 0; i < gpio_count; i++) {
        if (hse_value == 12000000 && gpio_setup[i].cfg.mode == Mode_Out_OD)
            gpio_setup[i].cfg.mode = Mode_Out_PP;
        gpioInit(gpio_setup[i].gpio, &gpio_setup[i].cfg);
    }

    // Init cycle counter
    cycleCounterInit();

    // SysTick
    // 일단 임
    SysTick_Config(SystemCoreClock / 1000);

    // Configure the rest of the stuff
#ifndef FY90Q
    i2cInit(I2C2);
#endif
    spiInit();

    // sleep for 100ms
    //일단 임시
    delay(100);
}