🛠 Réveil sur claquement : interruption click du MEMS, double révision et EXTI sur PE0/PE1
Objective
Leverage the most underutilized feature of the MEMS on the STM32F4DISCOVERY: the hardware-detected click/tap, which is routed to the interrupt pins INT PE0/PE1 and wakes up the CPU without polling. This topic is considered "advanced" because it requires handling two different chips (LIS302DL or LIS3DSH depending on the board revision) whose click configuration registers are not compatible.
The Hardware, Exactly
- SPI1 bus: SCK PA5, MISO PA6, MOSI PA7, chip-select PE3.
- MEMS interrupts: PE0 and PE1 (INT1/INT2), to be configured as EXTI inputs on the STM32F407.
- Revisions: early boards = LIS302DL, later boards = LIS3DSH. ST handles this dual support in the CubeF4 BSP (
stm32f4discovery_accelerometer.c) by reading the chip ID duringBSP_ACCELERO_Init— ST's "Accelerometer" demos use the click detection of these chips.
Software Strategy
- Initialize via the BSP (
BSP_ACCELERO_Init) to benefit from auto-detection, then read the detected component to choose the click configuration: the LIS302DL and LIS3DSH do not share the same register addresses nor the same semantics for click thresholds/durations. - Configure the click IT on the INT1 line (routed to PE0) with appropriate threshold and duration settings; test both axes before finalizing the settings — the "perceived" sensitivity depends on the enclosure and the board's mounting.
- EXTI on PE0: rising edge, low priority, short handler — acknowledge the source in the chip (by reading the click response register, which differs between components) otherwise the interrupt will re-arm in a loop.
- In production, replace the LED toggle with a flag + processing in a task, or a wake-up from Stop mode.
Code Skeleton
#include "stm32f4_discovery.h"
#include "stm32f4_discovery_accelerometer.h"
static volatile uint8_t tapFlag = 0;
/* EXTI PE0 : front montant depuis INT1 du MEMS */
void HAL_GPIO_EXTI_Callback(uint16_t pin)
{
if (pin == GPIO_PIN_0) {
tapFlag = 1; /* acquitte la source MEMS via le driver detecte */
}
}
int main(void)
{
HAL_Init;
SystemClock_Config; /* 168 MHz */
BSP_LED_Init(LED5); /* rouge = PD14 */
BSP_ACCELERO_Init; /* chip ID -> LIS302DL ou LIS3DSH */
MemsClickInit_DetectedPart; /* a ecrire : active le click sur */
/* INT1 (vers PE0) avec seuil/duree */
/* GPIO PE0 en EXTI rising via HAL_GPIO_Init */
while (1) {
if (tapFlag) {
tapFlag = 0;
BSP_LED_Toggle(LED5);
}
}
}
Specific Pitfalls
- Forgetting to acknowledge the source in the chip: the IT remains armed, and your EXTI triggers immediately again — the classic symptom is an LED blinking continuously.
- Polluting the ISR: the BSP warns that the audio codec's I2C can be corrupted by long interrupt routines — if your project combines audio and click, keep both ISRs minimal.
- Assuming a single chip: a driver written "for the LIS302DL" will fail silently on a board with a LIS3DSH; the entry test of your code must be the chip ID, never the pin number.
- Confusing with other Discovery boards: the 6-axis IMU LSM330 (F401/F411 kits) has a completely different register tree; this guide only covers the simple 3-axis MEMS on SPI1 of the original F4DISCOVERY.
Expected Verification
Tap a finger on the table next to the board: the red LED (PD14) toggles, once and only once per tap. When gently tilting the board without shocking it, nothing happens — the detection is indeed based on the transient, not on static acceleration. Use Stop mode between taps for battery-powered wake-up.
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
- STM32Cube HAL et BSP officiel : LED, bouton et accéléromètre avec détection automatique
- 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