123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530 |
- #include <nrfx.h>
- #if NRFX_CHECK(NRFX_PPI_ENABLED)
- #include <nrfx_ppi.h>
- #define NRFX_LOG_MODULE PPI
- #include <nrfx_log.h>
- static uint32_t m_channels_allocated;
- static uint8_t m_groups_allocated;
- __STATIC_INLINE uint32_t group_to_mask(nrf_ppi_channel_group_t group)
- {
- return (1uL << (uint32_t) group);
- }
- __STATIC_INLINE bool is_programmable_app_channel(nrf_ppi_channel_t channel)
- {
- return ((NRFX_PPI_PROG_APP_CHANNELS_MASK & nrfx_ppi_channel_to_mask(channel)) != 0);
- }
- __STATIC_INLINE bool are_app_channels(uint32_t channel_mask)
- {
-
- return ((~(NRFX_PPI_ALL_APP_CHANNELS_MASK) & channel_mask) == 0);
- }
- __STATIC_INLINE bool is_app_channel(nrf_ppi_channel_t channel)
- {
- return are_app_channels(nrfx_ppi_channel_to_mask(channel));
- }
- __STATIC_INLINE bool is_app_group(nrf_ppi_channel_group_t group)
- {
- return ((NRFX_PPI_ALL_APP_GROUPS_MASK & group_to_mask(group)) != 0);
- }
- __STATIC_INLINE bool is_allocated_channel(nrf_ppi_channel_t channel)
- {
- return ((m_channels_allocated & nrfx_ppi_channel_to_mask(channel)) != 0);
- }
- __STATIC_INLINE void channel_allocated_set(nrf_ppi_channel_t channel)
- {
- m_channels_allocated |= nrfx_ppi_channel_to_mask(channel);
- }
- __STATIC_INLINE void channel_allocated_clr(nrf_ppi_channel_t channel)
- {
- m_channels_allocated &= ~nrfx_ppi_channel_to_mask(channel);
- }
- __STATIC_INLINE void channel_allocated_clr_all(void)
- {
- m_channels_allocated &= ~NRFX_PPI_ALL_APP_CHANNELS_MASK;
- }
- __STATIC_INLINE bool is_allocated_group(nrf_ppi_channel_group_t group)
- {
- return ((m_groups_allocated & group_to_mask(group)) != 0);
- }
- __STATIC_INLINE void group_allocated_set(nrf_ppi_channel_group_t group)
- {
- m_groups_allocated |= group_to_mask(group);
- }
- __STATIC_INLINE void group_allocated_clr(nrf_ppi_channel_group_t group)
- {
- m_groups_allocated &= ~group_to_mask(group);
- }
- __STATIC_INLINE void group_allocated_clr_all()
- {
- m_groups_allocated &= ~NRFX_PPI_ALL_APP_GROUPS_MASK;
- }
- void nrfx_ppi_free_all(void)
- {
- uint32_t mask = NRFX_PPI_ALL_APP_GROUPS_MASK;
- nrf_ppi_channel_group_t group;
-
- nrf_ppi_channels_disable(NRFX_PPI_ALL_APP_CHANNELS_MASK);
- for (group = NRF_PPI_CHANNEL_GROUP0; mask != 0; mask &= ~group_to_mask(group), group++)
- {
- if (mask & group_to_mask(group))
- {
- nrf_ppi_channel_group_clear(group);
- }
- }
- channel_allocated_clr_all();
- group_allocated_clr_all();
- }
- nrfx_err_t nrfx_ppi_channel_alloc(nrf_ppi_channel_t * p_channel)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- nrf_ppi_channel_t channel;
- uint32_t mask = 0;
- err_code = NRFX_ERROR_NO_MEM;
- mask = NRFX_PPI_PROG_APP_CHANNELS_MASK;
- for (channel = NRF_PPI_CHANNEL0;
- mask != 0;
- mask &= ~nrfx_ppi_channel_to_mask(channel), channel++)
- {
- NRFX_CRITICAL_SECTION_ENTER();
- if ((mask & nrfx_ppi_channel_to_mask(channel)) && (!is_allocated_channel(channel)))
- {
- channel_allocated_set(channel);
- *p_channel = channel;
- err_code = NRFX_SUCCESS;
- }
- NRFX_CRITICAL_SECTION_EXIT();
- if (err_code == NRFX_SUCCESS)
- {
- NRFX_LOG_INFO("Allocated channel: %d.", channel);
- break;
- }
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_channel_free(nrf_ppi_channel_t channel)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_programmable_app_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else
- {
-
- nrf_ppi_channel_disable(channel);
- NRFX_CRITICAL_SECTION_ENTER();
- channel_allocated_clr(channel);
- NRFX_CRITICAL_SECTION_EXIT();
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_channel_assign(nrf_ppi_channel_t channel, uint32_t eep, uint32_t tep)
- {
- if ((uint32_t *)eep == NULL || (uint32_t *)tep == NULL)
- {
- return NRFX_ERROR_NULL;
- }
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_programmable_app_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else if (!is_allocated_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else
- {
- nrf_ppi_channel_endpoint_setup(channel, eep, tep);
- NRFX_LOG_INFO("Assigned channel: %d, event end point: %x, task end point: %x.",
- channel,
- eep,
- tep);
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_channel_fork_assign(nrf_ppi_channel_t channel, uint32_t fork_tep)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- #ifdef PPI_FEATURE_FORKS_PRESENT
- if (!is_allocated_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else
- {
- nrf_ppi_fork_endpoint_setup(channel, fork_tep);
- NRFX_LOG_INFO("Fork assigned channel: %d, task end point: %d.", channel, fork_tep);
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- #else
- err_code = NRFX_ERROR_NOT_SUPPORTED;
- NRFX_LOG_WARNING("Function: %s, error code: %s.",
- __func__,
- NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- #endif
- }
- nrfx_err_t nrfx_ppi_channel_enable(nrf_ppi_channel_t channel)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else if (is_programmable_app_channel(channel) && !is_allocated_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else
- {
- nrf_ppi_channel_enable(channel);
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_channel_disable(nrf_ppi_channel_t channel)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else if (is_programmable_app_channel(channel) && !is_allocated_channel(channel))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else
- {
- nrf_ppi_channel_disable(channel);
- err_code = NRFX_SUCCESS;
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_group_alloc(nrf_ppi_channel_group_t * p_group)
- {
- nrfx_err_t err_code;
- uint32_t mask = 0;
- nrf_ppi_channel_group_t group;
- err_code = NRFX_ERROR_NO_MEM;
- mask = NRFX_PPI_ALL_APP_GROUPS_MASK;
- for (group = NRF_PPI_CHANNEL_GROUP0; mask != 0; mask &= ~group_to_mask(group), group++)
- {
- NRFX_CRITICAL_SECTION_ENTER();
- if ((mask & group_to_mask(group)) && (!is_allocated_group(group)))
- {
- group_allocated_set(group);
- *p_group = group;
- err_code = NRFX_SUCCESS;
- }
- NRFX_CRITICAL_SECTION_EXIT();
- if (err_code == NRFX_SUCCESS)
- {
- NRFX_LOG_INFO("Allocated group: %d.", group);
- break;
- }
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_group_free(nrf_ppi_channel_group_t group)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_group(group))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- if (!is_allocated_group(group))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else
- {
- nrf_ppi_group_disable(group);
- NRFX_CRITICAL_SECTION_ENTER();
- group_allocated_clr(group);
- NRFX_CRITICAL_SECTION_EXIT();
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_group_enable(nrf_ppi_channel_group_t group)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_group(group))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else if (!is_allocated_group(group))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else
- {
- nrf_ppi_group_enable(group);
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_group_disable(nrf_ppi_channel_group_t group)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_group(group))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else
- {
- nrf_ppi_group_disable(group);
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_channels_remove_from_group(uint32_t channel_mask,
- nrf_ppi_channel_group_t group)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_group(group))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else if (!is_allocated_group(group))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else if (!are_app_channels(channel_mask))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else
- {
- NRFX_CRITICAL_SECTION_ENTER();
- nrf_ppi_channels_remove_from_group(channel_mask, group);
- NRFX_CRITICAL_SECTION_EXIT();
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- nrfx_err_t nrfx_ppi_channels_include_in_group(uint32_t channel_mask,
- nrf_ppi_channel_group_t group)
- {
- nrfx_err_t err_code = NRFX_SUCCESS;
- if (!is_app_group(group))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else if (!is_allocated_group(group))
- {
- err_code = NRFX_ERROR_INVALID_STATE;
- }
- else if (!are_app_channels(channel_mask))
- {
- err_code = NRFX_ERROR_INVALID_PARAM;
- }
- else
- {
- NRFX_CRITICAL_SECTION_ENTER();
- nrf_ppi_channels_include_in_group(channel_mask, group);
- NRFX_CRITICAL_SECTION_EXIT();
- }
- NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
- return err_code;
- }
- #endif
|