LCOV - code coverage report
Current view: top level - home/jason/ncs-2.0.0/modules/hal/nordic/nrfx/drivers/src - nrfx_dppi.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 2 80 2.5 %
Date: 2022-08-18 11:36:24 Functions: 1 12 8.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 36 0.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2018 - 2022, Nordic Semiconductor ASA
       3                 :            :  * All rights reserved.
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: BSD-3-Clause
       6                 :            :  *
       7                 :            :  * Redistribution and use in source and binary forms, with or without
       8                 :            :  * modification, are permitted provided that the following conditions are met:
       9                 :            :  *
      10                 :            :  * 1. Redistributions of source code must retain the above copyright notice, this
      11                 :            :  *    list of conditions and the following disclaimer.
      12                 :            :  *
      13                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      14                 :            :  *    notice, this list of conditions and the following disclaimer in the
      15                 :            :  *    documentation and/or other materials provided with the distribution.
      16                 :            :  *
      17                 :            :  * 3. Neither the name of the copyright holder nor the names of its
      18                 :            :  *    contributors may be used to endorse or promote products derived from this
      19                 :            :  *    software without specific prior written permission.
      20                 :            :  *
      21                 :            :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
      22                 :            :  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23                 :            :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24                 :            :  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
      25                 :            :  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
      26                 :            :  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
      27                 :            :  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
      28                 :            :  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
      29                 :            :  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
      30                 :            :  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
      31                 :            :  * POSSIBILITY OF SUCH DAMAGE.
      32                 :            :  */
      33                 :            : 
      34                 :            : #include <nrfx.h>
      35                 :            : 
      36                 :            : #if NRFX_CHECK(NRFX_DPPI_ENABLED)
      37                 :            : 
      38                 :            : #include <nrfx_dppi.h>
      39                 :            : #include <helpers/nrfx_flag32_allocator.h>
      40                 :            : 
      41                 :            : #define NRFX_LOG_MODULE DPPI
      42                 :            : #include <nrfx_log.h>
      43                 :            : 
      44                 :            : #if !defined(NRFX_DPPI_CHANNELS_USED)
      45                 :            : // Default mask of DPPI channels reserved for other modules.
      46                 :            : #define NRFX_DPPI_CHANNELS_USED 0x00000000uL
      47                 :            : #endif
      48                 :            : 
      49                 :            : #if !defined(NRFX_DPPI_GROUPS_USED)
      50                 :            : // Default mask of DPPI groups reserved for other modules.
      51                 :            : #define NRFX_DPPI_GROUPS_USED 0x00000000uL
      52                 :            : #endif
      53                 :            : 
      54                 :            : #define DPPI_AVAILABLE_CHANNELS_MASK \
      55                 :            :     ((uint32_t)(((1ULL << DPPI_CH_NUM) - 1) & (~NRFX_DPPI_CHANNELS_USED)))
      56                 :            : 
      57                 :            : #define DPPI_AVAILABLE_GROUPS_MASK   \
      58                 :            :     (((1UL << DPPI_GROUP_NUM) - 1)   & (~NRFX_DPPI_GROUPS_USED))
      59                 :            : 
      60                 :            : /** @brief Set bit at given position. */
      61                 :            : #define DPPI_BIT_SET(pos) (1uL << (pos))
      62                 :            : 
      63                 :            : /**< Bitmap representing channels availability. */
      64                 :            : static nrfx_atomic_t   m_allocated_channels = DPPI_AVAILABLE_CHANNELS_MASK;
      65                 :            : /**< Bitmap representing groups availability. */
      66                 :            : static nrfx_atomic_t   m_allocated_groups = DPPI_AVAILABLE_GROUPS_MASK;
      67                 :            : 
      68                 :          0 : void nrfx_dppi_free(void)
      69                 :            : {
      70                 :          0 :     uint32_t mask = DPPI_AVAILABLE_GROUPS_MASK & ~m_allocated_groups;
      71                 :          0 :     uint8_t group_idx = NRF_DPPI_CHANNEL_GROUP0;
      72                 :            : 
      73                 :            :     // Disable all channels
      74                 :          0 :     nrf_dppi_channels_disable(NRF_DPPIC,
      75                 :          0 :                               DPPI_AVAILABLE_CHANNELS_MASK & ~m_allocated_channels);
      76                 :            : 
      77                 :            :     // Clear all groups configurations
      78         [ #  # ]:          0 :     while (mask)
      79                 :            :     {
      80                 :          0 :         nrf_dppi_channel_group_t group = (nrf_dppi_channel_group_t)group_idx;
      81         [ #  # ]:          0 :         if (mask & DPPI_BIT_SET(group))
      82                 :            :         {
      83                 :          0 :             nrf_dppi_group_clear(NRF_DPPIC, group);
      84                 :          0 :             mask &= ~DPPI_BIT_SET(group);
      85                 :            :         }
      86                 :          0 :         group_idx++;
      87                 :            :     }
      88                 :            : 
      89                 :            :     // Clear all allocated channels.
      90                 :          0 :     m_allocated_channels = DPPI_AVAILABLE_CHANNELS_MASK;
      91                 :            : 
      92                 :            :     // Clear all allocated groups.
      93                 :          0 :     m_allocated_groups = DPPI_AVAILABLE_GROUPS_MASK;
      94                 :          0 : }
      95                 :            : 
      96                 :          1 : nrfx_err_t nrfx_dppi_channel_alloc(uint8_t * p_channel)
      97                 :            : {
      98                 :          1 :     return nrfx_flag32_alloc(&m_allocated_channels, p_channel);
      99                 :            : }
     100                 :            : 
     101                 :          0 : nrfx_err_t nrfx_dppi_channel_free(uint8_t channel)
     102                 :            : {
     103                 :          0 :     nrf_dppi_channels_disable(NRF_DPPIC, NRFX_BIT(channel));
     104                 :          0 :     return nrfx_flag32_free(&m_allocated_channels, channel);
     105                 :            : }
     106                 :            : 
     107                 :          0 : nrfx_err_t nrfx_dppi_channel_enable(uint8_t channel)
     108                 :            : {
     109                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     110                 :            : 
     111         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_channels, channel))
     112                 :            :     {
     113                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     114                 :            :     }
     115                 :            :     else
     116                 :            :     {
     117                 :          0 :         nrf_dppi_channels_enable(NRF_DPPIC, DPPI_BIT_SET(channel));
     118                 :            :     }
     119         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     120                 :          0 :     return err_code;
     121                 :            : }
     122                 :            : 
     123                 :          0 : nrfx_err_t nrfx_dppi_channel_disable(uint8_t channel)
     124                 :            : {
     125                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     126                 :            : 
     127         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_channels, channel))
     128                 :            :     {
     129                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     130                 :            :     }
     131                 :            :     else
     132                 :            :     {
     133                 :          0 :         nrf_dppi_channels_disable(NRF_DPPIC, DPPI_BIT_SET(channel));
     134                 :          0 :         err_code = NRFX_SUCCESS;
     135                 :            :     }
     136         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     137                 :          0 :     return err_code;
     138                 :            : }
     139                 :            : 
     140                 :          0 : nrfx_err_t nrfx_dppi_group_alloc(nrf_dppi_channel_group_t * p_group)
     141                 :            : {
     142                 :          0 :     return nrfx_flag32_alloc(&m_allocated_groups, (uint8_t *)p_group);
     143                 :            : }
     144                 :            : 
     145                 :          0 : nrfx_err_t nrfx_dppi_group_free(nrf_dppi_channel_group_t group)
     146                 :            : {
     147                 :          0 :     nrf_dppi_group_disable(NRF_DPPIC, group);
     148                 :          0 :     return nrfx_flag32_free(&m_allocated_groups, group);
     149                 :            : }
     150                 :            : 
     151                 :          0 : nrfx_err_t nrfx_dppi_channel_include_in_group(uint8_t                  channel,
     152                 :            :                                               nrf_dppi_channel_group_t group)
     153                 :            : {
     154                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     155                 :            : 
     156         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_groups, group) ||
     157         [ #  # ]:          0 :         !nrfx_flag32_is_allocated(m_allocated_channels, channel))
     158                 :            :     {
     159                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     160                 :            :     }
     161                 :            :     else
     162                 :            :     {
     163                 :          0 :         NRFX_CRITICAL_SECTION_ENTER();
     164                 :          0 :         nrf_dppi_channels_include_in_group(NRF_DPPIC, DPPI_BIT_SET(channel), group);
     165                 :          0 :         NRFX_CRITICAL_SECTION_EXIT();
     166                 :            :     }
     167         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     168                 :          0 :     return err_code;
     169                 :            : }
     170                 :            : 
     171                 :          0 : nrfx_err_t nrfx_dppi_channel_remove_from_group(uint8_t                  channel,
     172                 :            :                                                nrf_dppi_channel_group_t group)
     173                 :            : {
     174                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     175                 :            : 
     176         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_groups, group) ||
     177         [ #  # ]:          0 :         !nrfx_flag32_is_allocated(m_allocated_channels, channel))
     178                 :            :     {
     179                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     180                 :            :     }
     181                 :            :     else
     182                 :            :     {
     183                 :          0 :         NRFX_CRITICAL_SECTION_ENTER();
     184                 :          0 :         nrf_dppi_channels_remove_from_group(NRF_DPPIC, DPPI_BIT_SET(channel), group);
     185                 :          0 :         NRFX_CRITICAL_SECTION_EXIT();
     186                 :            :     }
     187         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     188                 :          0 :     return err_code;
     189                 :            : }
     190                 :            : 
     191                 :          0 : nrfx_err_t nrfx_dppi_group_clear(nrf_dppi_channel_group_t group)
     192                 :            : {
     193                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     194                 :            : 
     195         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_groups, group))
     196                 :            :     {
     197                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     198                 :            :     }
     199                 :            :     else
     200                 :            :     {
     201                 :          0 :         nrf_dppi_channels_remove_from_group(NRF_DPPIC, DPPI_AVAILABLE_CHANNELS_MASK, group);
     202                 :            :     }
     203         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     204                 :          0 :     return err_code;
     205                 :            : }
     206                 :            : 
     207                 :          0 : nrfx_err_t nrfx_dppi_group_enable(nrf_dppi_channel_group_t group)
     208                 :            : {
     209                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     210                 :            : 
     211         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_groups, group))
     212                 :            :     {
     213                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     214                 :            :     }
     215                 :            :     else
     216                 :            :     {
     217                 :          0 :         nrf_dppi_group_enable(NRF_DPPIC, group);
     218                 :            :     }
     219         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     220                 :          0 :     return err_code;
     221                 :            : }
     222                 :            : 
     223                 :          0 : nrfx_err_t nrfx_dppi_group_disable(nrf_dppi_channel_group_t group)
     224                 :            : {
     225                 :          0 :     nrfx_err_t err_code = NRFX_SUCCESS;
     226                 :            : 
     227         [ #  # ]:          0 :     if (!nrfx_flag32_is_allocated(m_allocated_groups, group))
     228                 :            :     {
     229                 :          0 :         err_code = NRFX_ERROR_INVALID_PARAM;
     230                 :            :     }
     231                 :            :     else
     232                 :            :     {
     233                 :          0 :         nrf_dppi_group_disable(NRF_DPPIC, group);
     234                 :            :     }
     235         [ #  # ]:          0 :     NRFX_LOG_INFO("Function: %s, error code: %s.", __func__, NRFX_LOG_ERROR_STRING_GET(err_code));
     236                 :          0 :     return err_code;
     237                 :            : }
     238                 :            : 
     239                 :            : #endif // NRFX_CHECK(NRFX_DPPI_ENABLED)

Generated by: LCOV version 1.14