mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
2d81738cb9
- Previously the pulse counter used GCC's built-in functions for atomic memory access. This PR changes this to use C11 atomics instead: - Use of C11 atomics instead of atomic build-in functions makes the code more portable and readable. - Previously pulse_counter_reset() did not use an atomic function, so that resets would be racy. (E.g. on the 8-bit AVR platform an 16-bit store is not atomic, unless special care (like using C11 atomics) is taken.)
97 lines
2.2 KiB
C
97 lines
2.2 KiB
C
/*
|
|
* Copyright (C) 2017 UC Berkeley
|
|
*
|
|
* This file is subject to the terms and conditions of the GNU Lesser
|
|
* General Public License v2.1. See the file LICENSE in the top level
|
|
* directory for more details.
|
|
*/
|
|
|
|
/**
|
|
* @defgroup drivers_pulse_counter Pulse counter
|
|
* @ingroup drivers_sensors
|
|
* @ingroup drivers_saul
|
|
*
|
|
* @brief GPIO based pulse counting driver
|
|
*
|
|
* This driver provides @ref drivers_saul capabilities.
|
|
* @{
|
|
*
|
|
* @file
|
|
* @brief Driver for the pulse counter
|
|
*
|
|
* @author Hyung-Sin Kim <hs.kim@cs.berkeley.edu>
|
|
*/
|
|
|
|
#ifndef PULSE_COUNTER_H
|
|
#define PULSE_COUNTER_H
|
|
|
|
#include <stdint.h>
|
|
#ifdef __cplusplus
|
|
#include "c11_atomics_compat.hpp"
|
|
#else
|
|
#include <stdatomic.h>
|
|
#endif
|
|
#include "periph/gpio.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/**
|
|
* @brief Parameters needed for device initialization
|
|
*/
|
|
typedef struct {
|
|
gpio_t gpio; /**< GPIO pin that sensor is connected to */
|
|
gpio_flank_t gpio_flank; /**< GPIO flank option */
|
|
} pulse_counter_params_t;
|
|
|
|
/**
|
|
* @brief Device descriptor for a pulse counter device
|
|
*/
|
|
typedef struct {
|
|
atomic_uint_least16_t pulse_count; /**< pulse counter */
|
|
} pulse_counter_t;
|
|
|
|
/**
|
|
* @brief Initialize a pulse counter device
|
|
*
|
|
* @param[out] dev device descriptor
|
|
* @param[in] params configuration parameters
|
|
*
|
|
* @return 0 on success
|
|
* @return -1 on error
|
|
*/
|
|
int pulse_counter_init(pulse_counter_t *dev, const pulse_counter_params_t *params);
|
|
|
|
/**
|
|
* @brief Read and reset pulse counter value
|
|
*
|
|
* @param[out] dev device descriptor of sensor
|
|
*
|
|
* @return Accumulated pulse counts
|
|
*/
|
|
int16_t pulse_counter_read_with_reset(pulse_counter_t *dev);
|
|
|
|
/**
|
|
* @brief Read pulse counter value
|
|
*
|
|
* @param[in] dev device descriptor of sensor
|
|
*
|
|
* @return Accumulated pulse counts
|
|
*/
|
|
int16_t pulse_counter_read_without_reset(pulse_counter_t *dev);
|
|
|
|
/**
|
|
* @brief Reset pulse counter value
|
|
*
|
|
* @param[out] dev device descriptor of sensor
|
|
*/
|
|
void pulse_counter_reset(pulse_counter_t *dev);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* PULSE_COUNTER_H */
|
|
/** @} */
|