ruuvi.drivers.c
${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
|
Functions for digitally reading and actuating GPIO pins. More...
Data Structures | |
struct | rd_test_gpio_cfg_t |
structure to configure GPIO test with input and output. These GPIOs must be physically connected on board. More... | |
struct | ri_gpio_evt_t |
Event from GPIO. More... | |
Typedefs | |
typedef void(* | ri_gpio_interrupt_fp_t) (const ri_gpio_evt_t) |
Enumerations | |
enum | ri_gpio_slope_t { RI_GPIO_SLOPE_HITOLO , RI_GPIO_SLOPE_LOTOHI , RI_GPIO_SLOPE_TOGGLE , RI_GPIO_SLOPE_UNKNOWN } |
Enable implementation selected by application. More... | |
enum | ri_gpio_base_freq { RI_GPIO_PWM_BASE_NUM_FREQ_16MHZ = 0 , RI_GPIO_PWM_BASE_NUM_FREQ_8MHZ = 1 , RI_GPIO_PWM_BASE_NUM_FREQ_4MHZ = 2 , RI_GPIO_PWM_BASE_NUM_FREQ_2MHZ = 3 , RI_GPIO_PWM_BASE_NUM_FREQ_1MHZ = 4 , RI_GPIO_PWM_BASE_NUM_FREQ_500KHZ = 5 , RI_GPIO_PWM_BASE_NUM_FREQ_250KHZ = 6 , RI_GPIO_PWM_BASE_NUM_FREQ_125KHZ = 7 } |
Functions | |
rd_status_t | ri_gpio_interrupt_test_init (const rd_test_gpio_cfg_t cfg) |
Test GPIO interrupt initialization. More... | |
rd_status_t | ri_gpio_interrupt_test_enable (const rd_test_gpio_cfg_t cfg) |
Test enabling interrupt on a pin. More... | |
bool | ri_gpio_interrupt_run_integration_test (const rd_test_print_fp printfp, const ri_gpio_id_t input, const ri_gpio_id_t output) |
Run all GPIO interrupt integration tests. More... | |
rd_status_t | ri_gpio_pwm_test (const rd_test_gpio_cfg_t cfg) |
Test running PWM on a pin. More... | |
bool | ri_gpio_pwm_run_integration_test (const rd_test_print_fp printfp, const ri_gpio_id_t input, const ri_gpio_id_t output) |
Run all GPIO interrupt integration tests. More... | |
rd_status_t | ri_gpio_test_init (void) |
Test GPIO module initialization. More... | |
rd_status_t | ri_gpio_test_configure (const ri_gpio_id_t input, const ri_gpio_id_t output) |
Test configuring a pin of a port into a mode. More... | |
rd_status_t | ri_gpio_test_toggle (const ri_gpio_id_t input, const ri_gpio_id_t output) |
Test toggling the state of a pin of a port. More... | |
bool | ri_gpio_run_integration_test (const rd_test_print_fp printfp, const ri_gpio_id_t input, const ri_gpio_id_t output) |
Run all GPIO integration tests. More... | |
rd_status_t | ri_gpio_interrupt_init (ri_gpio_interrupt_fp_t *const interrupt_table, const uint16_t max_interrupts) |
Initialize interrupt functionality to GPIO. Takes address of interrupt table as a pointer to avoid tying driver into a specific board with a specific number of GPIO pins and to avoid including boards repository within the driver. The interrupt table must be retained in the RAM. More... | |
rd_status_t | ri_gpio_interrupt_uninit (void) |
Uninitialize interrupt functionality of GPIO. More... | |
bool | ri_gpio_interrupt_is_init (void) |
Check if interrupt module is initialized. More... | |
rd_status_t | ri_gpio_interrupt_enable (const ri_gpio_id_t pin, const ri_gpio_slope_t slope, const ri_gpio_mode_t mode, const ri_gpio_interrupt_fp_t handler) |
Enable interrupt on a pin. More... | |
rd_status_t | ri_gpio_interrupt_disable (const ri_gpio_id_t pin) |
Disable interrupt on a pin. More... | |
rd_status_t | ri_gpio_pwm_init (void) |
Run any necessary initialization for PWM. More... | |
rd_status_t | ri_gpio_pwm_uninit (void) |
Uninitialize PWM. More... | |
rd_status_t | ri_gpio_pwm_start (const ri_gpio_id_t pin, const ri_gpio_mode_t mode, float *const frequency, float *const duty_cycle) |
Start PWM on given pin at given frequency and duty cycle. More... | |
rd_status_t | ri_gpio_pwm_stop (const ri_gpio_id_t pin) |
Stop PWM on given pin. More... | |
bool | ri_gpio_pwm_is_init (void) |
Check if PWM is initialized. More... | |
rd_status_t | ri_gpio_init (void) |
Initializes GPIO module. Call this before other GPIO functions. After initialization all GPIO pins shall be in High-Z mode. More... | |
rd_status_t | ri_gpio_uninit (void) |
Uninitializes GPIO module. Call this to reset GPIO to High-Z mode. After uninitialization all GPIO pins shall be in High-Z mode. Uninitialization can be called at any time, but behaviour is not defined if some other peripheral (i.e. SPI) is using GPIO pins. More... | |
bool | ri_gpio_is_init (void) |
return true if GPIO is init, false otherwise. More... | |
rd_status_t | ri_gpio_configure (const ri_gpio_id_t pin, const ri_gpio_mode_t mode) |
Configure a pin of a port into a mode. If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t. More... | |
rd_status_t | ri_gpio_toggle (const ri_gpio_id_t pin) |
Toggle the state of a pin of a port. If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t. More... | |
rd_status_t | ri_gpio_write (const ri_gpio_id_t pin, const ri_gpio_state_t state) |
Write a pin of a port into given state If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t. More... | |
rd_status_t | ri_gpio_read (const ri_gpio_id_t pin, ri_gpio_state_t *const state) |
Read state of a pin of a port into bool high If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t. More... | |
Functions for digitally reading and actuating GPIO pins.
Enable implementation selected by application.
The GPIO functions do include interrupts, but they do not include PWM, ADC or DAC functions.
Pulse width modulation of GPIO.
#define RI_GPIO_INTERRUPT_TEST_TABLE_SIZE 64 |
Fixed 64 interrupt table size, adjust this if some device has more than 2 ports with 32 gpios each.
Definition at line 21 of file ruuvi_interface_gpio_interrupt_test.h.
#define RI_GPIO_PWM_BASE_FREQ_125KHZ (125000.00f) |
Definition at line 50 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_16MHZ (RI_GPIO_PWM_MAX_FREQ) |
Definition at line 43 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_1MHZ (1000000.00f) |
Definition at line 47 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_250KHZ (250000.00f) |
Definition at line 49 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_2MHZ (2000000.00f) |
Definition at line 46 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_4MHZ (4000000.00f) |
Definition at line 45 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_500KHZ (500000.00f) |
Definition at line 48 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_BASE_FREQ_8MHZ (8000000.00f) |
Definition at line 44 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_CHANNEL_UNUSED (0) |
Definition at line 36 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_DRIVER 0 |
Definition at line 23 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_DRIVER_PIN_POINT (0) |
Definition at line 26 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_EXPECT_TRIGS (RI_GPIO_PWM_TEST_TIME_MS / RI_GPIO_PWM_TEST_PERIOD_MS) |
Definition at line 29 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_FREQ_COUNT (8) |
Definition at line 41 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_MAX_DUTY (1.00f) |
Definition at line 33 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_MAX_FREQ (16000000.00f) |
Definition at line 30 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_MIN_DUTY (0.00f) |
Definition at line 34 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_MIN_FREQ (2.00f) |
Definition at line 31 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_MIN_REST_FOR_TOP (0.999999f) |
Definition at line 39 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_MIN_TOP_VALUE (1U) |
Definition at line 38 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_PLAYBACK_COUNT (1) |
Definition at line 25 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_PLAYBACK_OK (0) |
Definition at line 24 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_SEQ_DELAY (0) |
Definition at line 27 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_SEQ_REPEATS (1) |
Definition at line 28 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
#define RI_GPIO_PWM_TEST_DC (0.5F) |
Duty cycle in test.
Definition at line 27 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_DUTY_INVALID_MAX_HZ (1.1F) |
Invalid duty cycle.
Definition at line 24 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_DUTY_INVALID_MIN_HZ (-0.1F) |
Invalid duty cycle.
Definition at line 23 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_FREQ_HZ (100U) |
Frequency of test.
Definition at line 20 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_FREQ_INVALID_MAX_HZ (17000000U) |
Invalid frequency.
Definition at line 21 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_FREQ_INVALID_MIN_HZ (1U) |
Invalid frequency.
Definition at line 22 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_PERIOD_MS (1000U / RI_GPIO_PWM_TEST_FREQ_HZ) |
Period of one PWM cycle in ms.
Definition at line 26 of file ruuvi_interface_gpio_pwm_test.h.
#define RI_GPIO_PWM_TEST_TIME_MS (100U) |
Milliseconds to test.
Definition at line 28 of file ruuvi_interface_gpio_pwm_test.h.
typedef void(* ri_gpio_interrupt_fp_t) (const ri_gpio_evt_t) |
Definition at line 45 of file ruuvi_interface_gpio_interrupt.h.
enum ri_gpio_base_freq |
Definition at line 69 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
enum ri_gpio_slope_t |
Enable implementation selected by application.
Enumeration for GPIO slopes
Enumerator | |
---|---|
RI_GPIO_SLOPE_HITOLO | High to low transition |
RI_GPIO_SLOPE_LOTOHI | Low to high transition |
RI_GPIO_SLOPE_TOGGLE | Any transition |
RI_GPIO_SLOPE_UNKNOWN | Error or unknown value. |
Definition at line 26 of file ruuvi_interface_gpio_interrupt.h.
rd_status_t ri_gpio_configure | ( | const ri_gpio_id_t | pin, |
const ri_gpio_mode_t | mode | ||
) |
Configure a pin of a port into a mode. If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t.
[in] | pin | Pin number. |
[in] | mode | Mode to set the pin to. See ri_gpio_mode_t for possible values. |
Definition at line 77 of file ruuvi_nrf5_sdk15_gpio.c.
rd_status_t ri_gpio_init | ( | void | ) |
Initializes GPIO module. Call this before other GPIO functions. After initialization all GPIO pins shall be in High-Z mode.
Definition at line 44 of file ruuvi_nrf5_sdk15_gpio.c.
rd_status_t ri_gpio_interrupt_disable | ( | const ri_gpio_id_t | pin | ) |
Disable interrupt on a pin.
Pin will be left as RI_GPIO_MODE_HIGH_Z.
[in] | pin | pin to disable as interrupt source. |
RD_SUCCESS | on success. |
rd_status_t ri_gpio_interrupt_enable | ( | const ri_gpio_id_t | pin, |
const ri_gpio_slope_t | slope, | ||
const ri_gpio_mode_t | mode, | ||
const ri_gpio_interrupt_fp_t | handler | ||
) |
Enable interrupt on a pin.
Underlying implementation is allowed to use same interrupt channel for all pin interrupts, i.e. simultaneous interrupts might get detected as one and the priority of interrupts is undefined.
[in] | pin | pin to use as interrupt source |
[in] | slope | slope to interrupt on |
[in] | mode | GPIO input mode. Must be (RI_GPIO_)INPUT_PULLUP, INPUT_PULLDOWN or INPUT_NOPULL |
[in] | handler | function pointer which will be called with ri_gpio_evt_t as a parameter on interrupt. |
rd_status_t ri_gpio_interrupt_init | ( | ri_gpio_interrupt_fp_t *const | interrupt_table, |
const uint16_t | max_interrupts | ||
) |
Initialize interrupt functionality to GPIO. Takes address of interrupt table as a pointer to avoid tying driver into a specific board with a specific number of GPIO pins and to avoid including boards repository within the driver. The interrupt table must be retained in the RAM.
RD_ERROR_INVALID_STATE
if GPIO is uninitializedRD_SUCCESS
on first call.RD_ERROR_INVALID_STATE
on second call.RD_SUCCESS
after uninitializtion.RD_ERROR_NULL
if interrupt handler table is NULL
.[in] | interrupt_table | Array of function pointers, initialized to all nulls. Size should be the number of GPIO+1, i.e. RUUVI_BOARD_GPIO_NUMBER + 1. |
[in] | max_interrupts | Size of interrupt table. |
bool ri_gpio_interrupt_is_init | ( | void | ) |
Check if interrupt module is initialized.
true
if module is initialized, false otherwise bool ri_gpio_interrupt_run_integration_test | ( | const rd_test_print_fp | printfp, |
const ri_gpio_id_t | input, | ||
const ri_gpio_id_t | output | ||
) |
Run all GPIO interrupt integration tests.
[in] | printfp | Function pointer to which test result strings are sent. |
[in] | input | Pin used to check the state of output pin. |
[in] | output | Pin being toggled. |
rd_status_t ri_gpio_interrupt_test_enable | ( | const rd_test_gpio_cfg_t | cfg | ) |
Test enabling interrupt on a pin.
Requires basic gpio functionality to work, run gpio tests first. Behaviour is undefined if GPIO is uninitialized while GPIO interrupts are initialized.
[in] | cfg | pins to use for testing interrupts |
rd_status_t ri_gpio_interrupt_test_init | ( | const rd_test_gpio_cfg_t | cfg | ) |
Test GPIO interrupt initialization.
Test GPIO PWM initialization.
RD_ERROR_INVALID_STATE
if GPIO is uninitializedRD_SUCCESS
on first call.RD_ERROR_INVALID_STATE
on second call.RD_SUCCESS
after uninitializtion.[in] | cfg | configuration of GPIO pins to test. |
rd_status_t ri_gpio_interrupt_uninit | ( | void | ) |
Uninitialize interrupt functionality of GPIO.
bool ri_gpio_is_init | ( | void | ) |
return true if GPIO is init, false otherwise.
true
if GPIO module is init false
if GPIO module is not init Definition at line 72 of file ruuvi_nrf5_sdk15_gpio.c.
rd_status_t ri_gpio_pwm_init | ( | void | ) |
Run any necessary initialization for PWM.
After calling this function PWM peripheral may consume power even if no PWM functionality is used.
RD_SUCCESS | Initialization was successful. |
RD_ERROR_INVALID_STATE | If PWM was already initialized. |
TODO | Error code on other error. |
Definition at line 82 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
bool ri_gpio_pwm_is_init | ( | void | ) |
Check if PWM is initialized.
true | PWM is initialized. |
false | PWM is not initialized. |
Definition at line 112 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
bool ri_gpio_pwm_run_integration_test | ( | const rd_test_print_fp | printfp, |
const ri_gpio_id_t | input, | ||
const ri_gpio_id_t | output | ||
) |
Run all GPIO interrupt integration tests.
[in] | printfp | Function pointer to which test result strings are sent. |
[in] | input | Pin used to check the state of output pin. |
[in] | output | Pin being toggled. |
rd_status_t ri_gpio_pwm_start | ( | const ri_gpio_id_t | pin, |
const ri_gpio_mode_t | mode, | ||
float *const | frequency, | ||
float *const | duty_cycle | ||
) |
Start PWM on given pin at given frequency and duty cycle.
Polarity of PWM is active-high and phase is undefined, implementation is allowed to use centered or edge-triggered PWM.
[in] | pin | Pin to start PWM on. |
[in] | mode | Output mode of GPIO pin, input modes or Hi-Z not supported. |
[in,out] | frequency | Input: Target frequency, "at least this much". Output: Configured frequency, equal or greater than input. |
[in,out] | duty_cycle | Input: Target duty cycle of PWM, 0.0 ... 1.0. Output: Configured duty cycle, equal or creater than input. |
RD_SUCCESS | PWM was started. |
RD_ERROR_NULL | one or both of pointers was NULL. |
RD_ERROR_INVALID_STATE | PWM was not initialized. |
RD_ERROR_INVALID_PARAM | Pin, mode, frequency or duty cycle were somehow invalid. |
Definition at line 165 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
rd_status_t ri_gpio_pwm_stop | ( | const ri_gpio_id_t | pin | ) |
Stop PWM on given pin.
After calling this function PWM is stopped and given pin is configured as Hi-Z regardless of it's previous state, this can be called on non-PWM pin too.
[in] | pin | Pin to stop PWM on. |
RD_SUCCESS | If pin was configured as Hi-Z. |
RD_ERROR_INVALID_STATE | currently not stopped. |
Definition at line 238 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
rd_status_t ri_gpio_pwm_test | ( | const rd_test_gpio_cfg_t | cfg | ) |
Test running PWM on a pin.
Requires gpio interrupt functionality to work, run gpio interrupt tests first. Behaviour is undefined if GPIO interrupts are uninitialized.
[in] | cfg | pins to use for testing interrupts |
rd_status_t ri_gpio_pwm_uninit | ( | void | ) |
Uninitialize PWM.
After calling this function PWM peripheral may no longer consume power. GPIO pins may be configured as Hi-Z or they may leave in previous state.
RD_SUCCESS | Initialization was successful. |
RD_ERROR_INVALID_STATE | If PWM was already initialized. |
Definition at line 95 of file ruuvi_nrf5_sdk15_gpio_pwm.c.
rd_status_t ri_gpio_read | ( | const ri_gpio_id_t | pin, |
ri_gpio_state_t *const | p_state | ||
) |
Read state of a pin of a port into bool high If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t.
[in] | pin | Pin number. |
[out] | p_state | Pointer to a ri_gpio_state_t which will be set to the state of the pin. |
Definition at line 198 of file ruuvi_nrf5_sdk15_gpio.c.
bool ri_gpio_run_integration_test | ( | const rd_test_print_fp | printfp, |
const ri_gpio_id_t | input, | ||
const ri_gpio_id_t | output | ||
) |
Run all GPIO integration tests.
[in] | printfp | Function pointer to which test result strings are sent. |
[in] | input | Pin used to check the state of output pin. |
[in] | output | Pin being toggled. |
rd_status_t ri_gpio_test_configure | ( | const ri_gpio_id_t | input, |
const ri_gpio_id_t | output | ||
) |
Test configuring a pin of a port into a mode.
[in] | input | Pin used to check the state of output pin |
[in] | output | Pin being configured into various modes. |
RD_SUCCESS
if all tests pass, error code on failure rd_status_t ri_gpio_test_init | ( | void | ) |
Test GPIO module initialization.
RD_SUCCESS
if all tests pass, error code on failure rd_status_t ri_gpio_test_toggle | ( | const ri_gpio_id_t | input, |
const ri_gpio_id_t | output | ||
) |
Test toggling the state of a pin of a port.
Input is in High-Z mode. Value read by it must toggle after output pin is toggled.
[in] | input | Pin used to check the state of output pin. |
[in] | output | Pin being toggled. |
RD_SUCCESS
if all tests pass, error code on failure rd_status_t ri_gpio_toggle | ( | const ri_gpio_id_t | pin | ) |
Toggle the state of a pin of a port. If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t.
[in] | pin | Pin number. |
Definition at line 161 of file ruuvi_nrf5_sdk15_gpio.c.
rd_status_t ri_gpio_uninit | ( | void | ) |
Uninitializes GPIO module. Call this to reset GPIO to High-Z mode. After uninitialization all GPIO pins shall be in High-Z mode. Uninitialization can be called at any time, but behaviour is not defined if some other peripheral (i.e. SPI) is using GPIO pins.
Definition at line 52 of file ruuvi_nrf5_sdk15_gpio.c.
rd_status_t ri_gpio_write | ( | const ri_gpio_id_t | pin, |
const ri_gpio_state_t | state | ||
) |
Write a pin of a port into given state If there are several ports the platform driver must implement a conversion function from port + pin to uint8_t.
[in] | pin | Pin number. |
[in] | state | State to which the pin should be set to. See ri_gpio_state_t for possible values |
Definition at line 172 of file ruuvi_nrf5_sdk15_gpio.c.