Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-cloud-client/update-client-hub/modules/atomic-queue/source/critical-nordic.c@0:276e7a263c35, 2018-07-02 (annotated)
- Committer:
- MACRUM
- Date:
- Mon Jul 02 06:30:39 2018 +0000
- Revision:
- 0:276e7a263c35
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MACRUM | 0:276e7a263c35 | 1 | // ---------------------------------------------------------------------------- |
MACRUM | 0:276e7a263c35 | 2 | // Copyright 2015-2017 ARM Ltd. |
MACRUM | 0:276e7a263c35 | 3 | // |
MACRUM | 0:276e7a263c35 | 4 | // SPDX-License-Identifier: Apache-2.0 |
MACRUM | 0:276e7a263c35 | 5 | // |
MACRUM | 0:276e7a263c35 | 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
MACRUM | 0:276e7a263c35 | 7 | // you may not use this file except in compliance with the License. |
MACRUM | 0:276e7a263c35 | 8 | // You may obtain a copy of the License at |
MACRUM | 0:276e7a263c35 | 9 | // |
MACRUM | 0:276e7a263c35 | 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
MACRUM | 0:276e7a263c35 | 11 | // |
MACRUM | 0:276e7a263c35 | 12 | // Unless required by applicable law or agreed to in writing, software |
MACRUM | 0:276e7a263c35 | 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
MACRUM | 0:276e7a263c35 | 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
MACRUM | 0:276e7a263c35 | 15 | // See the License for the specific language governing permissions and |
MACRUM | 0:276e7a263c35 | 16 | // limitations under the License. |
MACRUM | 0:276e7a263c35 | 17 | // ---------------------------------------------------------------------------- |
MACRUM | 0:276e7a263c35 | 18 | |
MACRUM | 0:276e7a263c35 | 19 | #ifdef TARGET_MCU_NRF51 |
MACRUM | 0:276e7a263c35 | 20 | |
MACRUM | 0:276e7a263c35 | 21 | #include <stdint.h> // uint32_t, UINT32_MAX |
MACRUM | 0:276e7a263c35 | 22 | #include <stddef.h> // NULL |
MACRUM | 0:276e7a263c35 | 23 | #include <cmsis-core/core_generic.h> //__disable_irq, __enable_irq |
MACRUM | 0:276e7a263c35 | 24 | #include <assert.h> |
MACRUM | 0:276e7a263c35 | 25 | #include <stdbool.h> |
MACRUM | 0:276e7a263c35 | 26 | #include <nrf_soc.h> |
MACRUM | 0:276e7a263c35 | 27 | #include <nrf_sdm.h> |
MACRUM | 0:276e7a263c35 | 28 | |
MACRUM | 0:276e7a263c35 | 29 | // Module include |
MACRUM | 0:276e7a263c35 | 30 | #include "aq_critical.h" |
MACRUM | 0:276e7a263c35 | 31 | |
MACRUM | 0:276e7a263c35 | 32 | static volatile union { |
MACRUM | 0:276e7a263c35 | 33 | uint32_t _PRIMASK_state; |
MACRUM | 0:276e7a263c35 | 34 | uint8_t _sd_state; |
MACRUM | 0:276e7a263c35 | 35 | } _state = { 0 } ; |
MACRUM | 0:276e7a263c35 | 36 | static volatile uint32_t _entry_count = 0; |
MACRUM | 0:276e7a263c35 | 37 | static volatile bool _use_softdevice_routine = false; |
MACRUM | 0:276e7a263c35 | 38 | |
MACRUM | 0:276e7a263c35 | 39 | void aq_critical_section_enter() |
MACRUM | 0:276e7a263c35 | 40 | { |
MACRUM | 0:276e7a263c35 | 41 | // if a critical section has already been entered, just update the counter |
MACRUM | 0:276e7a263c35 | 42 | if (_entry_count) { |
MACRUM | 0:276e7a263c35 | 43 | ++_entry_count; |
MACRUM | 0:276e7a263c35 | 44 | return; |
MACRUM | 0:276e7a263c35 | 45 | } |
MACRUM | 0:276e7a263c35 | 46 | |
MACRUM | 0:276e7a263c35 | 47 | // in this path, a critical section has never been entered |
MACRUM | 0:276e7a263c35 | 48 | uint32_t primask = __get_PRIMASK(); |
MACRUM | 0:276e7a263c35 | 49 | |
MACRUM | 0:276e7a263c35 | 50 | // if interrupts are enabled, try to use the soft device |
MACRUM | 0:276e7a263c35 | 51 | uint8_t sd_enabled; |
MACRUM | 0:276e7a263c35 | 52 | if ((primask == 0) && (sd_softdevice_is_enabled(&sd_enabled) == NRF_SUCCESS) && sd_enabled == 1) { |
MACRUM | 0:276e7a263c35 | 53 | // if the soft device can be use, use it |
MACRUM | 0:276e7a263c35 | 54 | sd_nvic_critical_region_enter(&_state._sd_state); |
MACRUM | 0:276e7a263c35 | 55 | _use_softdevice_routine = true; |
MACRUM | 0:276e7a263c35 | 56 | } else { |
MACRUM | 0:276e7a263c35 | 57 | // if interrupts where enabled, disable them |
MACRUM | 0:276e7a263c35 | 58 | if(primask == 0) { |
MACRUM | 0:276e7a263c35 | 59 | __disable_irq(); |
MACRUM | 0:276e7a263c35 | 60 | } |
MACRUM | 0:276e7a263c35 | 61 | |
MACRUM | 0:276e7a263c35 | 62 | // store the PRIMASK state, it will be restored at the end of the critical section |
MACRUM | 0:276e7a263c35 | 63 | _state._PRIMASK_state = primask; |
MACRUM | 0:276e7a263c35 | 64 | _use_softdevice_routine = false; |
MACRUM | 0:276e7a263c35 | 65 | } |
MACRUM | 0:276e7a263c35 | 66 | |
MACRUM | 0:276e7a263c35 | 67 | assert(_entry_count == 0); // entry count should always be equal to 0 at this point |
MACRUM | 0:276e7a263c35 | 68 | ++_entry_count; |
MACRUM | 0:276e7a263c35 | 69 | } |
MACRUM | 0:276e7a263c35 | 70 | |
MACRUM | 0:276e7a263c35 | 71 | void aq_critical_section_exit() |
MACRUM | 0:276e7a263c35 | 72 | { |
MACRUM | 0:276e7a263c35 | 73 | assert(_entry_count > 0); |
MACRUM | 0:276e7a263c35 | 74 | --_entry_count; |
MACRUM | 0:276e7a263c35 | 75 | |
MACRUM | 0:276e7a263c35 | 76 | // If their is other segments which have entered the critical section, just leave |
MACRUM | 0:276e7a263c35 | 77 | if (_entry_count) { |
MACRUM | 0:276e7a263c35 | 78 | return; |
MACRUM | 0:276e7a263c35 | 79 | } |
MACRUM | 0:276e7a263c35 | 80 | |
MACRUM | 0:276e7a263c35 | 81 | // This is the last segment of the critical section, state should be restored as before entering |
MACRUM | 0:276e7a263c35 | 82 | // the critical section |
MACRUM | 0:276e7a263c35 | 83 | if (_use_softdevice_routine) { |
MACRUM | 0:276e7a263c35 | 84 | sd_nvic_critical_region_exit(_state._sd_state); |
MACRUM | 0:276e7a263c35 | 85 | } else { |
MACRUM | 0:276e7a263c35 | 86 | __set_PRIMASK(_state._PRIMASK_state); |
MACRUM | 0:276e7a263c35 | 87 | } |
MACRUM | 0:276e7a263c35 | 88 | } |
MACRUM | 0:276e7a263c35 | 89 | |
MACRUM | 0:276e7a263c35 | 90 | #endif //TARGET_MCU_NRF51 |