8.7. Reed Switch

8.7.1. Introduction

A reed switch is a small electromechanical device having two ferromagnetic reeds that are hermetically sealed in a glass envelope. When brought into a magnetic field the reeds, which are ferromagnetic, will close, creating a switching function. Smaller reed switches have smaller and less rigid contacts producing less inertia and shorter closure times.

8.7.2. Applications

Reed switches as sensors are used for sensing movement and counting. Attached to windows or doors, they can be used for intrusion detection. Attached to refrigerator doors, they can sense if the door has been left open for too long. Along poorly shielded cables, they can detect excessive magnetic field leakages.

8.7.3. Resources

8.7.4. Platform Integration

The sensor can be enabled by defining compile-time flag PLTFRM_REED_SWITCH_ENA. An example of using this sensor is illustrated in App Examples.

8.7.4.1. Functions

PLTFRM_sts_t REED_SWITCH_init(UINT8_t devId)

Initializes the sensor interface by configuring GPIO. Currently, the device is configured for P1.3. The function stores the initial state of the switch in the device context. Callback function is also associated with the device to handle events from the sensor. This function is typically called during platform initialization.

Parameters:
  • devId – Identity of the device.
Returns:

PLTFRM_STS_SUCCESS is returned on success. A non-zero value indicates a failure. One possible error is PLTFRM_STS_INV_DEV_ID.

PLTFRM_sts_t REED_SWITCH_ena(UINT8_t devId)

Read the current state of the switch and store in the device context. After that, the device is enabled, that is, interrupts for the device are enabled. For example, this could be called when RFD has just associated with PAN coordinator. This is different from masking and unmasking interrupts, which happens later on demand.

Parameters:
  • devId – Identity of the device.
Returns:

PLTFRM_STS_SUCCESS is returned on success. A non-zero value indicates a failure. One possible error is PLTFRM_STS_INV_DEV_ID.

PLTFRM_sts_t REED_SWITCH_unMaskDevEvent(UINT8_t devId)

Function clears the interrupt pending flag and enables interrupts. In current implementation, interrupt callback function REED_SWITCH_intCbFunc() starts a debounce timer. When that timer expires, debounce callback function is called: REED_SWITCH_debounceTmoCbFunc(), which saves the new state, sets the event pending flag and then calls the unmask function.

Parameters:
  • devId – Identity of the device.
Returns:

PLTFRM_STS_SUCCESS is returned on success. A non-zero value indicates a failure. One possible error is PLTFRM_STS_INV_DEV_ID.

PLTFRM_sts_t REED_SWITCH_maskDevEvent(UINT8_t devId)

Disable GPIO interrupt. Typically this is called just after an interrupt is received so that it can be processed.

Parameters:
  • devId – Identity of the device.
Returns:

PLTFRM_STS_SUCCESS is returned on success. A non-zero value indicates a failure. One possible error is PLTFRM_STS_INV_DEV_ID.

PLTFRM_sts_t REED_SWITCH_checkPendingEvt(UINT8_t devId, UINT8_t *evtFlag_p)

Function checks if a switch open/close event has occurred and indicates the same. Once a pending event is indicated, this indication is cleared within the device context. However, interrupts remain masked until application calls REED_SWITCH_unMaskDevEvent(). An application that gets event indication can take proper action such as informing the PAN coordinator.

Parameters:
  • devId – Identity of the device.
  • evtFlag_p – Points to location where the event is indicated. Zero indicates no event. One indicates an event.
Returns:

PLTFRM_STS_SUCCESS is returned on success. A non-zero value indicates a failure. One possible error is PLTFRM_STS_INV_DEV_ID.

PLTFRM_sts_t REED_SWITCH_getState(UINT8_t devId, REED_SWITCH_state_t *swState_p)

Reads the current state of the switch. State is also updated into the device context.

Parameters:
  • devId – Identity of the device.
  • swState_p – Points to location where current state is returned.
Returns:

PLTFRM_STS_SUCCESS is returned on success. A non-zero value indicates a failure. One possible error is PLTFRM_STS_INV_DEV_ID.

8.7.4.2. Macros

#define REED_SWITCH_STATE_OUTPUT_SZ  1

#define REED_SWITCH_DFLT_DEBOUNCE_TMO_MSECS  200

8.7.4.3. Type Definitions

typedef enum
{
   REED_SWITCH_STATE_UNKNOWN = 0,
   REED_SWITCH_STATE_OPEN = 1,
   REED_SWITCH_STATE_CLOSED = 2
} REED_SWITCH_state_t;

typedef struct
{
   UINT8_t devId;
   PLTFRM_gpioPortId_t portId;
   PLTFRM_gpioPortPin_t portPinNr;
   REED_SWITCH_state_t state;
   UINT8_t evtPending;
#ifdef REED_SWITCH_STATS_ENA
   UINT16_t evtCnt;
   UINT16_t debounceCnt;
#endif
} REED_SWITCH_cntxt_s;