HAL library 2- LEDS and BUTTON for STM32 boards
Next library in series in disco library for controlling leds and reading button state on various STM32F0, STM32F4 and STM32F7 boards.
Supported boards:
- STM32F401-Discovery
- STM32F4-Discovery
- STM32F411-Discovery
- STM32F429-Discovery
- STM32F439-EVAL,
- STM32F401-Nucleo
- STM32F411-Nucleo
- STM32F446-Nucleo
- STM32F091-Nucleo
- STM32F7-Discovery
Library
Features
- Set LED state
- Read LED state
- Read button state
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
Library required settings
In order to tell which board you use, you have to set some defines for it. These defines are also used in other libraries to automatically identify used board. For example, my SDRAM library checks for board define to know how to init SDRAM settings for different boards. So, in your defines.h configuration file, set one define according to these available:
1 2 3 4 5 6 7 8 9 10 11 |
/* Use proper define for your board */ #define STM32F429_DISCOVERY #define STM32F401_DISCOVERY #define STM32F411_DISCOVERY #define STM32F4_DISCOVERY #define NUCLEO_F401 #define NUCLEO_F411 #define NUCLEO_F446 #define NUCLEO_F091 #define STM32F439_EVAL #define STM32F7_DISCOVERY |
When using my examples for Keil uVision, you won’t see these defines in defines.h file. They are set in “Options for Target” in IDE!
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
/** * @defgroup TM_DISCO_Functions * @brief Library Functions * @{ */ /** * @brief Configures LED pins as outputs * @param None * @retval None */ void TM_DISCO_LedInit(void); /** * @brief Configures Button pin as input * @param None * @retval None */ void TM_DISCO_ButtonInit(void); /** * @brief Turns on LED on board * @note STM32F4x9-Eval board uses inverse logic for leds * @param led: LED you want to turn on * - LED_RED: Red led * - LED_GREEN: Green led * - LED_BLUE: Blue led * - LED_ORANGE: Orange led * - LED_ALL: All leds * @retval None */ #ifndef TM_DISCO_SWAP_LOGIC #define TM_DISCO_LedOn(led) TM_GPIO_SetPinHigh(TM_DISCO_LED_PORT, (led)) #else #define TM_DISCO_LedOn(led) TM_GPIO_SetPinLow(TM_DISCO_LED_PORT, (led)) #endif /** * @brief Turns off LED on board * @note STM32F4x9-Eval board uses inverse logic for leds * @param led: LED you want to turn off * - LED_RED: Red led * - LED_GREEN: Green led * - LED_BLUE: Blue led * - LED_ORANGE: Orange led * - LED_ALL: All leds * @retval None */ #ifndef TM_DISCO_SWAP_LOGIC #define TM_DISCO_LedOff(led) TM_GPIO_SetPinLow(TM_DISCO_LED_PORT, (uint16_t)(led)) #else #define TM_DISCO_LedOff(led) TM_GPIO_SetPinHigh(TM_DISCO_LED_PORT, (led)) #endif /** * @brief Toggles LED on board * @param led: LED you want to toggle * - LED_RED: Red led * - LED_GREEN: Green led * - LED_BLUE: Blue led * - LED_ORANGE: Orange led * - LED_ALL: All leds * @retval None */ #define TM_DISCO_LedToggle(led) TM_GPIO_TogglePinValue(TM_DISCO_LED_PORT, (led)) /** * @brief Checks if led is on * @note STM32F4x9-Eval board uses inverse logic for leds * @param led: Led you want to checking * - LED_RED: Red led * - LED_GREEN: Green led * - LED_BLUE: Blue led * - LED_ORANGE: Orange led * - LED_ALL: All leds * @retval 1 if led is on or 0 if not */ #ifndef TM_DISCO_SWAP_LOGIC #define TM_DISCO_LedIsOn(led) TM_GPIO_GetOutputPinValue(TM_DISCO_LED_PORT, (led)) #else #define TM_DISCO_LedIsOn(led) !TM_GPIO_GetOutputPinValue(TM_DISCO_LED_PORT, (led)) #endif /** * @brief Sets led value * @param led: LED you want to set value * - LED_RED: Red led * - LED_GREEN: Green led * - LED_BLUE: Blue led * - LED_ORANGE: Orange led * - LED_ALL: All leds * @param state: Set or clear led * - 0: led is off * - > 0: led is on * @retval None */ #define TM_DISCO_SetLed(led, state) ((state) ? TM_DISCO_LedOn(led): TM_DISCO_LedOff(led)) /** * @brief Checks if user button is pressed * @param None * @retval Button status * - 0: Button is not pressed * - > 0: Button is pressed */ #define TM_DISCO_ButtonPressed() ((TM_GPIO_GetInputPinValue(TM_DISCO_BUTTON_PORT, TM_DISCO_BUTTON_PIN) == 0) != TM_DISCO_BUTTON_PRESSED) /** * @brief Checks if button was pressed now, but was not already pressed before * @param None * @retval Button on pressed value * - 0: In case that button has been already pressed on last call or was not pressed at all yet * - > 0: Button was pressed, but state before was released */ uint8_t TM_DISCO_ButtonOnPressed(void); /** * @brief Checks if button was released now, but was already pressed before * @param None * @retval Button on released value * - 0: In case that button has been already released on last call or was not released at all yet * - > 0: Button was released, but state before was pressed */ uint8_t TM_DISCO_ButtonOnReleased(void); /** * @} */ |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
/** * Keil project example for LEDS and buttons on evaluation boards * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button */ TM_DISCO_ButtonInit(); while (1) { /* If button pressed */ if (TM_DISCO_ButtonPressed()) { /* Turn on ALL leds */ TM_DISCO_LedOn(LED_ALL); } else { /* Turn off ALL leds */ TM_DISCO_LedOff(LED_ALL); } } } |
Example is available on Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments