inline static void gpio_ospeedr(uint8_t port, uint8_t pin, uint8_t speed) { uint32_t reg = *GPIO_OSPEEDR(port); reg &= ~(GPIO_OSPEEDR_M(pin)); reg |= (speed << GPIO_OSPEEDR_PIN(pin)); *GPIO_OSPEEDR(port) = reg; }
void gpio_set_output_options(u32 gpioport, u8 otype, u8 speed, u16 gpios) { u16 i; u32 ospeedr; if (otype == 0x1) GPIO_OTYPER(gpioport) |= gpios; else GPIO_OTYPER(gpioport) &= ~gpios; ospeedr = GPIO_OSPEEDR(gpioport); for (i = 0; i < 16; i++) { if (!((1 << i) & gpios)) continue; ospeedr &= ~GPIO_OSPEED_MASK(i); ospeedr |= GPIO_OSPEED(i, speed); } GPIO_OSPEEDR(gpioport) = ospeedr; }
/** @brief Set GPIO Output Options When the pin is set to output mode, this sets the configuration (analog/digital and open drain/push pull) and speed, for a set of GPIO pins on a given GPIO port. @param[in] gpioport Unsigned int32. Port identifier @ref gpio_port_id @param[in] otype Unsigned int8. Pin output type @ref gpio_output_type @param[in] speed Unsigned int8. Pin speed @ref gpio_speed @param[in] gpios Unsigned int16. Pin identifiers @ref gpio_pin_id If multiple pins are to be set, use bitwise OR '|' to separate them. */ void gpio_set_output_options(uint32_t gpioport, uint8_t otype, uint8_t speed, uint16_t gpios) { uint16_t i; uint32_t ospeedr; if (otype == 0x1) { GPIO_OTYPER(gpioport) |= gpios; } else { GPIO_OTYPER(gpioport) &= ~gpios; } ospeedr = GPIO_OSPEEDR(gpioport); for (i = 0; i < 16; i++) { if (!((1 << i) & gpios)) { continue; } ospeedr &= ~GPIO_OSPEED_MASK(i); ospeedr |= GPIO_OSPEED(i, speed); } GPIO_OSPEEDR(gpioport) = ospeedr; }
int CSysClock::init( TSysClockType source_clock) { switch(source_clock) { case HSE: // Включаем кварцевый генератор if( !( RCC->CR & RCC_CR_HSERDY ) ) { RCC->CR |= RCC_CR_HSEON; while( !( RCC->CR & RCC_CR_HSERDY ) ); } RCC->CFGR |= RCC_CFGR_SW_HSE; // Переход на HSE на случай незапуска PLL RCC->CR &= ~RCC_CR_PLLON; // Останавливаем PLL while ( RCC->CR & RCC_CR_PLLRDY ); // Настраиваем PLL на выходную частоту 48 МГц RCC->CFGR &= ~(RCC_CFGR_PLLMULL | RCC_CFGR_PLLSRC | RCC_CFGR_SW); RCC->CFGR |= RCC_CFGR_PLLMULL4 | RCC_CFGR_SW_PLL | RCC_CFGR_PLLSRC_PREDIV1; RCC->CR |= RCC_CR_PLLON; while (!( RCC->CR & RCC_CR_PLLRDY )); // Ожидает переключения на PLL while ( (RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); // Отключаем внутренний RC-генератор 8 МГц if( RCC->CR & RCC_CR_HSIRDY ) { RCC->CR &= ~RCC_CR_HSION; while( RCC->CR & RCC_CR_HSIRDY ); } break; case HSI: RCC->CR &= ~RCC_CR_HSEON; while ( RCC->CR & RCC_CR_HSERDY ); if( !(RCC->CR & RCC_CR_HSIRDY) ) { RCC->CR |= RCC_CR_HSION; while (!( RCC->CR & RCC_CR_HSIRDY )); } RCC->CFGR |= RCC_CFGR_SW_HSI; // Переход на HSI на случай незапуска PLL RCC->CR &= ~RCC_CR_PLLON; // Останавливаем PLL while ( RCC->CR & RCC_CR_PLLRDY ); // Настраиваем PLL на выходную частоту 48 МГц RCC->CFGR &= ~(RCC_CFGR_PLLMULL | RCC_CFGR_PLLSRC | RCC_CFGR_SW); RCC->CFGR |= RCC_CFGR_PLLMULL12 | RCC_CFGR_SW_PLL | RCC_CFGR_PLLSRC_HSI_Div2; RCC->CR |= RCC_CR_PLLON; while (!( RCC->CR & RCC_CR_PLLRDY )); // Ожидает переключения на PLL while ( (RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL); break; default: for( ;; ); } // Отладочный вывод клока системной шины (SysClk) на порт PA8 #ifdef MCO_ENABLE RCC->AHBENR |= RCC_AHBENR_GPIOAEN; // Подадим тактирование на PORTА GPIO_MODER(GPIOA, 8, ALT_FUNC_MODE); GPIO_OSPEEDR(GPIOA, 8, HIGH_SPEED); GPIO_AFR(GPIOA, 8, ALT_FUNC_0); RCC->CFGR &= ~(RCC_CFGR_MCO | 0x80000000); RCC->CFGR |= RCC_CFGR_MCO_SYSCLK; // System clock selected as MCO source #endif if (SysTick_Config((uint32_t)SYSTEM_CLOCK / 1000)) return 1; return 0; }