123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- #ifndef APP_PWM_H__
- #define APP_PWM_H__
- #include <stdint.h>
- #include "sdk_errors.h"
- #include "nrf_drv_timer.h"
- #include "nrf_drv_ppi.h"
- #include "nrf_peripherals.h"
- #ifdef __cplusplus
- extern "C" {
- #endif
- #if defined(GPIOTE_FEATURE_SET_PRESENT) && defined(GPIOTE_FEATURE_CLR_PRESENT)
- #define GPIOTE_SET_CLEAR_TASKS
- #endif
- #define APP_PWM_NOPIN 0xFFFFFFFF
- #define APP_PWM_CHANNELS_PER_INSTANCE 2
- #define APP_PWM_INSTANCE(name, num) \
- const nrf_drv_timer_t m_pwm_##name##_timer = NRF_DRV_TIMER_INSTANCE(num); \
- app_pwm_cb_t m_pwm_##name##_cb; \
- \
- const app_pwm_t name = { \
- .p_cb = &m_pwm_##name##_cb, \
- .p_timer = &m_pwm_##name##_timer, \
- }
- #define APP_PWM_DEFAULT_CONFIG_1CH(period_in_us, pin) \
- { \
- .pins = {pin, APP_PWM_NOPIN}, \
- .pin_polarity = {APP_PWM_POLARITY_ACTIVE_LOW, APP_PWM_POLARITY_ACTIVE_LOW}, \
- .num_of_channels = 1, \
- .period_us = period_in_us \
- }
- #define APP_PWM_DEFAULT_CONFIG_2CH(period_in_us, pin0, pin1) \
- { \
- .pins = {pin0, pin1}, \
- .pin_polarity = {APP_PWM_POLARITY_ACTIVE_LOW, APP_PWM_POLARITY_ACTIVE_LOW}, \
- .num_of_channels = 2, \
- .period_us = period_in_us \
- }
- typedef uint16_t app_pwm_duty_t;
- typedef void (* app_pwm_callback_t)(uint32_t);
- typedef enum
- {
- APP_PWM_POLARITY_ACTIVE_LOW = 0,
- APP_PWM_POLARITY_ACTIVE_HIGH = 1
- } app_pwm_polarity_t;
- typedef struct
- {
- uint32_t pins[APP_PWM_CHANNELS_PER_INSTANCE];
- app_pwm_polarity_t pin_polarity[APP_PWM_CHANNELS_PER_INSTANCE];
- uint32_t num_of_channels;
- uint32_t period_us;
- } app_pwm_config_t;
-
- typedef struct
- {
- uint32_t gpio_pin;
- uint32_t pulsewidth;
- nrf_ppi_channel_t ppi_channels[2];
- app_pwm_polarity_t polarity;
- uint8_t initialized;
- } app_pwm_channel_cb_t;
-
- typedef struct
- {
- app_pwm_channel_cb_t channels_cb[APP_PWM_CHANNELS_PER_INSTANCE];
- uint32_t period;
- app_pwm_callback_t p_ready_callback;
- #ifdef GPIOTE_SET_CLEAR_TASKS
- nrf_ppi_channel_t ppi_channel;
- #else
- nrf_ppi_channel_t ppi_channels[2];
- nrf_ppi_channel_group_t ppi_group;
- #endif
- nrfx_drv_state_t state;
- } app_pwm_cb_t;
- typedef struct
- {
- app_pwm_cb_t *p_cb;
- nrf_drv_timer_t const * const p_timer;
- } app_pwm_t;
- bool app_pwm_busy_check(app_pwm_t const * const p_instance);
- ret_code_t app_pwm_init(app_pwm_t const * const p_instance, app_pwm_config_t const * const p_config,
- app_pwm_callback_t p_ready_callback);
- ret_code_t app_pwm_uninit(app_pwm_t const * const p_instance);
- void app_pwm_enable(app_pwm_t const * const p_instance);
- void app_pwm_disable(app_pwm_t const * const p_instance);
- ret_code_t app_pwm_channel_duty_set(app_pwm_t const * const p_instance,
- uint8_t channel, app_pwm_duty_t duty);
- app_pwm_duty_t app_pwm_channel_duty_get(app_pwm_t const * const p_instance, uint8_t channel);
-
- ret_code_t app_pwm_channel_duty_ticks_set(app_pwm_t const * const p_instance,
- uint8_t channel,
- uint16_t ticks);
-
- uint16_t app_pwm_channel_duty_ticks_get(app_pwm_t const * const p_instance, uint8_t channel);
-
- uint16_t app_pwm_cycle_ticks_get(app_pwm_t const * const p_instance);
- #ifdef __cplusplus
- }
- #endif
- #endif
|