flabbergast's ARM/STM32 examples using libopencm3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.2KB

  1. /*
  2. * This file is part of the libopencm3 project.
  3. *
  4. * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>,
  5. * Copyright (C) 2010 Piotr Esden-Tempski <piotr@esden.net>
  6. * Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
  7. *
  8. * This library is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <libopencm3/stm32/rcc.h>
  22. #include <libopencm3/stm32/flash.h>
  23. #include <libopencm3/stm32/gpio.h>
  24. #define LED_GPIO GPIOA
  25. #define LED_PIN GPIO15
  26. #define LED_RCC_GPIO RCC_GPIOA
  27. #define BUTTON_GPIO GPIOA
  28. #define BUTTON_PIN GPIO1
  29. #define BUTTON_RCC_GPIO RCC_GPIOA
  30. /* Set the clock to max speed. */
  31. static void clock_setup(void)
  32. {
  33. #if defined(STM32F0)
  34. rcc_clock_setup_in_hsi_out_48mhz();
  35. #elif defined(STM32L0)
  36. /* After a reset, the system uses MSI@2.1MHz. */
  37. /* end result: 32MHz PLLVCO from HSI16,
  38. * no system/periph clock divide.
  39. */
  40. /* increase the latency to 1 wait state (we'll be speeding up) */
  41. flash_set_ws(1);
  42. /* turn on HSI16 */
  43. rcc_osc_on(RCC_HSI16);
  44. rcc_wait_for_osc_ready(RCC_HSI16);
  45. /* run AHB, APB1, APB2 at full speed */
  46. rcc_set_hpre(RCC_CFGR_HPRE_NODIV);
  47. rcc_set_ppre1(RCC_CFGR_PPRE1_NODIV);
  48. rcc_set_ppre2(RCC_CFGR_PPRE2_NODIV);
  49. /* turn off PLL and wait for it to fully stop */
  50. rcc_osc_off(RCC_PLL);
  51. while (RCC_CR & RCC_CR_PLLRDY);
  52. /* set PLL source to HSI16 */
  53. RCC_CFGR &= ~(1<<16); // RCC_CFGR_PLLSRC
  54. /* set up PLL */
  55. rcc_set_pll_multiplier(RCC_CFGR_PLLMUL_MUL4);
  56. rcc_set_pll_divider(RCC_CFGR_PLLDIV_DIV2);
  57. /* turn on and switch to PLL */
  58. rcc_osc_on(RCC_PLL);
  59. rcc_wait_for_osc_ready(RCC_PLL);
  60. rcc_set_sysclk_source(RCC_PLL);
  61. rcc_ahb_frequency = 32000000;
  62. rcc_apb1_frequency = 32000000;
  63. rcc_apb2_frequency = 32000000;
  64. #else
  65. #error "Implement a clock setup."
  66. #endif
  67. }
  68. static void gpio_setup(void)
  69. {
  70. /* Enable clocks. */
  71. rcc_periph_clock_enable(LED_RCC_GPIO);
  72. rcc_periph_clock_enable(BUTTON_RCC_GPIO);
  73. /* Set the LED pin to 'output push-pull'. */
  74. gpio_mode_setup(LED_GPIO, GPIO_MODE_OUTPUT,
  75. GPIO_PUPD_NONE, LED_PIN);
  76. /* Set the button pin to 'input' (external pull-down). */
  77. gpio_mode_setup(BUTTON_GPIO, GPIO_MODE_INPUT, GPIO_PUPD_NONE, BUTTON_PIN);
  78. }
  79. #define SCB_VTOR MMIO32(0xE000ED08)
  80. int main(void)
  81. {
  82. #if defined(BOOTLOADER8K)
  83. SCB_VTOR = (uint32_t) 0x08002000;
  84. #endif
  85. int i;
  86. clock_setup();
  87. gpio_setup();
  88. /* Blink the LED on the board. */
  89. while (1) {
  90. gpio_toggle(LED_GPIO, LED_PIN);
  91. /* Upon button press, blink more slowly. */
  92. if (gpio_get(BUTTON_GPIO, BUTTON_PIN)) {
  93. for (i = 0; i < 2000000; i++) { /* Wait a bit. */
  94. __asm__("nop");
  95. }
  96. }
  97. for (i = 0; i < 2000000; i++) { /* Wait a bit. */
  98. __asm__("nop");
  99. }
  100. }
  101. return 0;
  102. }
  103. // vim: tabstop=4:shiftwidth=4:noexpandtab