void init_HW(void)
{
	RCC->CR |= RCC_CR_MSION;    // Turn on MSI

	// Reset SW[1:0], HPRE[3:0], PPRE1[2:0], PPRE2[2:0], MCOSEL[2:0] and MCOPRE[2:0] bits
	RCC->CFGR &= ~(RCC_CFGR_SW | RCC_CFGR_HPRE | RCC_CFGR_PPRE1 | RCC_CFGR_PPRE2 | RCC_CFGR_MCOSEL | RCC_CFGR_MCO_PRE);

	// Reset HSION, HSIDIVEN, HSEON, CSSON and PLLON bits
	RCC->CR &= ~(RCC_CR_HSION | RCC_CR_HSIDIVEN | RCC_CR_HSEON | RCC_CR_CSSHSEON | RCC_CR_PLLON);

	// Reset HSI48ON  bit
	RCC->CRRCR &= ~RCC_CRRCR_HSI48ON;

	// Reset HSEBYP bit
	RCC->CR &= ~RCC_CR_HSEBYP;

	// Reset PLLSRC, PLLMUL[3:0] and PLLDIV[1:0] bits
	RCC->CFGR &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLMUL | RCC_CFGR_PLLDIV);

	// Disable all interrupts
	RCC->CIER = 0;

	InitClocks();

	// enable IOPx peripheral
	RCC->IOPENR |=
			RCC_IOPENR_GPIOAEN |
			RCC_IOPENR_GPIOBEN |
			RCC_IOPENR_GPIOCEN |
			RCC_IOPENR_GPIODEN |
			RCC_IOPENR_GPIOHEN;
}
Beispiel #2
0
void LED_InitGPIO(void)
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
    
    GPIO_InitTypeDef gpioInit = {
        .GPIO_Pin = 0,
        .GPIO_Mode = GPIO_Mode_OUT,
        .GPIO_OType = GPIO_OType_PP,
        .GPIO_PuPd = GPIO_PuPd_UP,
        .GPIO_Speed = GPIO_Speed_50MHz
    };
    for(int j = 0; j < 4; ++j) {
        gpioInit.GPIO_Pin = LED_PIN[j];
        GPIO_Init(LED_PORT[j], &gpioInit);
    }
}

void SetLED(int led, bool state) {
    if(state)
        LED_PORT[led]->BSRRL = LED_PIN[led];
    else
        LED_PORT[led]->BSRRH = LED_PIN[led];
}

void ToggleLED(int led) {
    LED_PORT[led]->ODR ^= LED_PIN[led];
}

static void InitClocks(void);
// static void TIM4_Config(void);

int main(void)
{
    InitClocks();
    
    // Configure for millisecond system ticks
    if(SysTick_Config(SystemCoreClock/1000))
        while(1) {;}
    
    LED_InitGPIO();
    
    SetLED(0, true);
    SetLED(1, true);
    
    int led = 0;
    while(1)
    {
        delay_ms(1);
        ToggleLED(led);
        led = (led + 3)%4;
    }
} // main()