int main() { rcc_clock_setup_hsi(&hsi_8mhz[CLOCK_48MHZ]); rcc_usb_prescale_1(); rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_USBRST); rcc_peripheral_reset(&RCC_AHBRSTR, RCC_AHBRSTR_IOPARST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_USBRST); rcc_peripheral_clear_reset(&RCC_AHBRSTR, RCC_AHBRSTR_IOPARST); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_IOPAEN); gpio_mode_setup(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO11 | GPIO12); gpio_clear(GPIOA, GPIO11 | GPIO12); for (int i = 0; i < 0x800000; i++) __asm__ volatile("nop"); gpio_mode_setup(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO11 | GPIO12); gpio_set_af(GPIOA, GPIO_AF14, GPIO11| GPIO12); arm_bootloader::Dest dest = arm_bootloader::get_unique_dest(); char serial[10] = {0}; { uint32_t x = dest; char hex[] = "0123456789abcdef"; for(int i = 0; i < 8; i++) { serial[7-i] = hex[x & 0xF]; x = x >> 4; } } /* Buffer to be used for control requests. */ uint8_t usbd_control_buffer[128]; char const *usb_strings[] = { "uf-mil", "subbus", serial, }; usbd_device * usbd_dev = usbd_init(&stm32f103_usb_driver, &uf_subbus_protocol::usb::dev, &uf_subbus_protocol::usb::config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer)); usbd_register_set_config_callback(usbd_dev, cdcacm_set_config); uf_subbus_protocol::usb::Sink sink(usbd_dev); arm_bootloader::Handler handler( sink, dest, 2048); handlerp = &handler; while (1) usbd_poll(usbd_dev); }
void i2c_reset(uint32_t i2c) { switch (i2c) { case I2C1: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_I2C1RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_I2C1RST); break; case I2C2: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_I2C2RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_I2C2RST); break; } }
void spi_reset(uint32_t spi_peripheral) { switch (spi_peripheral) { case SPI1: rcc_peripheral_reset(&RCC_APB2RSTR, RCC_APB2RSTR_SPI1RST); rcc_peripheral_clear_reset(&RCC_APB2RSTR, RCC_APB2RSTR_SPI1RST); break; case SPI2: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_SPI2RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_SPI2RST); break; #if defined(STM32F1) || defined(STM32F2) || defined(STM32F3) || defined(STM32F4) case SPI3: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_SPI3RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_SPI3RST); break; #endif } }
void disconnect_usb(void) { /* Disconnect USB cable by resetting USB Device and pulling USB_DP low*/ rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); gpio_clear(GPIOA, GPIO12); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12); }
int main(void) { /* Check the force bootloader pin*/ uint16_t pin_b; rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); /* Switch PB5 (SWIM_RST_IN) up */ gpio_set(GPIOB, GPIO5); gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO5); gpio_set(GPIOB, GPIO5); pin_b = gpio_get(GPIOB, GPIO6); /* Check state on PB6 ((SWIM_RST) and release PB5*/ pin_b = gpio_get(GPIOB, GPIO6); gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO5); if(((GPIOA_CRL & 0x40) == 0x40) && pin_b) dfu_jump_app_if_valid(); dfu_protect(DFU_MODE); rcc_clock_setup_in_hse_8mhz_out_72mhz(); systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); systick_set_reload(900000); /* Handle USB disconnect/connect */ /* Just in case: Disconnect USB cable by resetting USB Device * and pulling USB_DP low * Device will reconnect automatically as Pull-Up is hard wired*/ rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN); rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); gpio_clear(GPIOA, GPIO12); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12); /* Handle LED*/ gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO8); systick_interrupt_enable(); systick_counter_enable(); dfu_init(&stm32f103_usb_driver, DFU_MODE); dfu_main(); }
void timer_reset(u32 timer_peripheral) { switch (timer_peripheral) { case TIM1: rcc_peripheral_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM1RST); rcc_peripheral_clear_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM1RST); break; case TIM2: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM2RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM2RST); break; case TIM3: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM3RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM3RST); break; case TIM4: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM4RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM4RST); break; case TIM5: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM5RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM5RST); break; case TIM6: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM6RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM6RST); break; case TIM7: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM7RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM7RST); break; case TIM8: rcc_peripheral_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM8RST); rcc_peripheral_clear_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM8RST); break; /* These timers are not supported in libopencm3 yet */ /* case TIM9: rcc_peripheral_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM9RST); rcc_peripheral_clear_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM9RST); break; case TIM10: rcc_peripheral_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM10RST); rcc_peripheral_clear_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM10RST); break; case TIM11: rcc_peripheral_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM11RST); rcc_peripheral_clear_reset(&RCC_APB2RSTR, RCC_APB2RSTR_TIM11RST); break; case TIM12: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM12RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM12RST); break; case TIM13: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM13RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM13RST); break; case TIM14: rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM14RST); rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1RSTR_TIM14RST); break; */ } }