🛠 STM32Cube HAL et BSP officiel : LED, bouton et accéléromètre avec détection automatique
Objective
Program the STM32F4DISCOVERY "from the source": the official STM32CubeF4 firmware contains a dedicated BSP (Board Support Package) for this board, located in Drivers/BSP/STM32F4-Discovery, with complete examples in Projects/STM32F4-Discovery/. You will use the BSP_LED_*, BSP_PB_*, and BSP_ACCELERO_* APIs — and above all, understand the automatic accelerometer detection, a hardware peculiarity of this board.
Hardware and Project
- 1 STM32F4DISCOVERY board, micro-USB cable on the ST-LINK/V2-A port
- STM32CubeIDE (or Makefile + arm-none-eabi-gcc) and the STM32CubeF4 package (GitHub repository
STMicroelectronics/STM32CubeF4, cloned with--recursive— the board's BSP resides in thestm32f4discovery-bspsubmodule) - Define the
USE_STM32F4_DISCOmacro and addDrivers/BSP/STM32F4-Discovery, as well asDrivers/BSP/Components/lis302dlandlis3dsh, to the include path
The Hardware Trap: Two Accelerometers Depending on the Revision
The board was produced with two different MEMS on the same SPI1 bus: the LIS302DL on early boards, and the LIS3DSH on later ones (SCK PA5, MISO PA6, MOSI PA7, CS PE3, interrupts PE0/PE1). Code that assumes a single component fails silently on the other revision. The BSP solves this problem by reading the chip ID: use BSP_ACCELERO_Init and do not hardcode the component.
Complete Code
#include "stm32f4_discovery.h"
#include "stm32f4_discovery_accelerometer.h"
int main(void)
{
int16_t xyz[3] = {0, 0, 0};
HAL_Init; /* SysTick/NVIC par defaut */
SystemClock_Config; /* 168 MHz depuis le quartz HSE 8 MHz */
BSP_LED_Init(LED3); /* LED3 = PD13 (orange) ; LED4/5/6 = PD12/14/15 */
BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* USER = PA0 */
BSP_ACCELERO_Init; /* auto-detecte LIS302DL vs LIS3DSH via chip ID */
while (1)
{
BSP_LED_Toggle(LED3);
BSP_ACCELERO_GetXYZ(xyz); /* axes normalises entre revisions */
if (BSP_PB_GetState(BUTTON_KEY) == GPIO_PIN_SET)
BSP_LED_On(LED4);
else
BSP_LED_Off(LED4);
HAL_Delay(100);
}
}
What the BSP Hides from You (Useful to Know)
- The actual LED pins are defined in
stm32f4_discovery.h: LED4=PD12 (green), LED3=PD13 (orange), LED5=PD14 (red), LED6=PD15 (blue). - The button is named
BUTTON_KEYin the BSP, although everyone calls it USER (PA0, active high). BSP_ACCELERO_GetXYZreturns axes oriented consistently between the two chips — this is exactly the normalization that your own driver should reimplement otherwise.
Expected Verification
The orange LED blinks at 5 Hz; the green LED stays on while USER is pressed. When tilting the board, the xyz values change (to be observed in the debugger, as the ST-LINK console is not wired on stock boards).
Common Errors
- Cloning STM32CubeF4 without
--recursive: the BSP components (lis302dl/lis3dsh/cs43l22) are missing during compilation. - Forgetting
USE_STM32F4_DISCO: the BSP headers do not select the correct board. - Writing a custom LIS302DL driver and discovering on a second board that the accelerometer is a LIS3DSH: read the chip ID, just like the BSP does.
- Looking for a joystick: unlike other Discovery kits, this one has only a USER button and a RESET button.
Other tutorials for this board
- Premier téléversement sur la STM32F4DISCOVERY avec l'Arduino STM32duino
- Zephyr RTOS sur la STM32F4DISCOVERY : west build et west flash
- MicroPython sur la STM32F4DISCOVERY : REPL interactif et pilotage des LED
- Lecture audio sur le codec CS43L22 : I2S, classe D et les limitations du BSP
- USB OTG FS sur le port micro-AB : se faire passer pour un périphérique classe CDC
- Acquérir le micro PDM MP45DT02 : I2S2, filtre PDM-vers-PCM et contraintes de sortie