blinky.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. void SysTick_Handler(void)
  21. {
  22. HAL_IncTick();
  23. // 1 Hz blinking
  24. if ((HAL_GetTick() % 500) == 0)
  25. {
  26. HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
  27. }
  28. }
  29. void initGPIO()
  30. {
  31. GPIO_InitTypeDef GPIO_Config;
  32. GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
  33. GPIO_Config.Pull = GPIO_NOPULL;
  34. GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
  35. GPIO_Config.Pin = LED_PIN;
  36. LED_PORT_CLK_ENABLE();
  37. HAL_GPIO_Init(LED_PORT, &GPIO_Config);
  38. }
  39. int main(void)
  40. {
  41. HAL_Init();
  42. initGPIO();
  43. // 1kHz ticks
  44. HAL_SYSTICK_Config(SystemCoreClock / 1000);
  45. while(1);
  46. return 0;
  47. }