🛠 Réveil sur claquement : interruption click du MEMS, double révision et EXTI sur PE0/PE1

STM32F4DISCOVERY (STM32F407VGT6 Discovery kit) · avance · Schéma : l'accéléromètre MEMS de la STM32F4DISCOVERY en SPI1 (SCK PA5, MISO PA6, MOSI PA7, CS PE3) avec ses deux broches d'interruption INT1/INT2 reliées à PE0/PE1 configurées en EXTI ; un bloc « chip ID » arbitre entre LIS302DL (révision ancienne) et LIS3DSH (révision récente) ; la LED rouge PD14 s'allume sur détection de claquement.

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

Software Strategy

  1. 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.
  2. 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.
  3. 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.
  4. 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

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

Code examples for this board

← Back to the board 🛠 All tutorials