blinky.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #if defined STM32L0
  2. #include <stm32l0xx_hal.h>
  3. // STM32L0538-Discovery green led - PB4
  4. #define LED_PORT GPIOB
  5. #define LED_PIN GPIO_PIN_4
  6. #define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
  7. #elif defined STM32F1
  8. #include <stm32f1xx_hal.h>
  9. // STM32VL-Discovery green led - PC9
  10. #define LED_PORT GPIOC
  11. #define LED_PIN GPIO_PIN_9
  12. #define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOC_CLK_ENABLE
  13. #elif defined STM32F4
  14. #include <stm32f4xx_hal.h>
  15. // STM32F4-Discovery green led - PD12
  16. #define LED_PORT GPIOD
  17. #define LED_PIN GPIO_PIN_12
  18. #define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOD_CLK_ENABLE
  19. #endif
  20. //This prevent name mangling for functions used in C/assembly files.
  21. extern "C"
  22. {
  23. void SysTick_Handler(void)
  24. {
  25. HAL_IncTick();
  26. // 1 Hz blinking
  27. if ((HAL_GetTick() % 500) == 0)
  28. HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
  29. }
  30. }
  31. void initGPIO()
  32. {
  33. GPIO_InitTypeDef GPIO_Config;
  34. GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
  35. GPIO_Config.Pull = GPIO_NOPULL;
  36. GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
  37. GPIO_Config.Pin = LED_PIN;
  38. LED_PORT_CLK_ENABLE();
  39. HAL_GPIO_Init(LED_PORT, &GPIO_Config);
  40. }
  41. int main(void)
  42. {
  43. HAL_Init();
  44. initGPIO();
  45. // 1kHz ticks
  46. HAL_SYSTICK_Config(SystemCoreClock / 1000);
  47. for (;;)
  48. __WFI();
  49. return 0;
  50. }