# STM32F407
I used the STMF4 'Discovery' dev board to test this out.
The chip runs at 168MHz at the core, with a 42MHz peripheral clock.
To build code for it, I used the [OpenSTM32 Workbench](http://www.openstm32.org/HomePage) - an IDE based on Eclipse. Here's a [small register structure primer](stm32-register-help.pdf). I watched [this video](https://www.youtube.com/watch?v=FPfTxNS9NvQ) and, while the HAL libraries I was using were not identical, poking around in those .h files and listening to this person helped me figure out the clock setup.
The chip runs a 3.1MHz ring.



Here's the code,
```C
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
#include "stm32f4xx_hal_rcc.h"
#include "stm32f4xx_hal_rcc_ex.h"
#include "stm32f4xx_hal_gpio.h"
int main(void)
{
// boilerplate from STM HAL
SystemInit();
// shutdown RCC
HAL_RCC_DeInit();
// enable the external clock
__HAL_RCC_HSE_CONFIG(RCC_HSE_ON);
while(!(RCC_FLAG_HSERDY));
RCC_OscInitTypeDef oscinit;
oscinit.OscillatorType = RCC_OSCILLATORTYPE_HSE;
oscinit.HSEState = RCC_HSE_ON;
RCC_PLLInitTypeDef pllinit;
pllinit.PLLState = RCC_PLL_ON;
pllinit.PLLSource = RCC_PLLSOURCE_HSE;
// div source by 4, crystal is 8M, so input to pll will be 2M
pllinit.PLLM = 8;
// then mult to 386
pllinit.PLLN = 336;
// div into sysclk
pllinit.PLLP = RCC_PLLP_DIV2;
// div into usb clk etc..
pllinit.PLLQ = 7;
oscinit.PLL = pllinit;
HAL_RCC_OscConfig(&oscinit);
while(!(RCC_FLAG_PLLRDY));
RCC_ClkInitTypeDef clkinit;
clkinit.AHBCLKDivider = RCC_SYSCLK_DIV1;
clkinit.APB1CLKDivider = RCC_SYSCLK_DIV4;
clkinit.APB2CLKDivider = RCC_SYSCLK_DIV2;
clkinit.ClockType = RCC_CLOCKTYPE_SYSCLK;
clkinit.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; //RCC_SYSCLKSOURCE_HSE
// 2nd item is 'flash latency' - see AN3988 and pg 9
HAL_RCC_ClockConfig(&clkinit, FLASH_LATENCY_5);
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_13;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
GPIO_InitTypeDef GPIO_InitStructInput;
GPIO_InitStructInput.Pin = GPIO_PIN_14;
GPIO_InitStructInput.Mode = GPIO_MODE_INPUT;
GPIO_InitStructInput.Speed = GPIO_SPEED_MEDIUM;
HAL_GPIO_Init(GPIOE, &GPIO_InitStructInput);
while(1){
(GPIOE -> IDR & GPIO_PIN_14) ? (GPIOE -> ODR &= ~GPIO_PIN_13) : (GPIOE -> ODR |= GPIO_PIN_13);
}
}
```