Sarah Marsh / Mbed OS EddystoneBeacon
Committer:
sarahmarshy
Date:
Tue Nov 29 06:29:10 2016 +0000
Revision:
0:1c7da5f83647
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sarahmarshy 0:1c7da5f83647 1 /*
sarahmarshy 0:1c7da5f83647 2 * PackageLicenseDeclared: Apache-2.0
sarahmarshy 0:1c7da5f83647 3 * Copyright (c) 2015 ARM Limited
sarahmarshy 0:1c7da5f83647 4 *
sarahmarshy 0:1c7da5f83647 5 * Licensed under the Apache License, Version 2.0 (the "License");
sarahmarshy 0:1c7da5f83647 6 * you may not use this file except in compliance with the License.
sarahmarshy 0:1c7da5f83647 7 * You may obtain a copy of the License at
sarahmarshy 0:1c7da5f83647 8 *
sarahmarshy 0:1c7da5f83647 9 * http://www.apache.org/licenses/LICENSE-2.0
sarahmarshy 0:1c7da5f83647 10 *
sarahmarshy 0:1c7da5f83647 11 * Unless required by applicable law or agreed to in writing, software
sarahmarshy 0:1c7da5f83647 12 * distributed under the License is distributed on an "AS IS" BASIS,
sarahmarshy 0:1c7da5f83647 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sarahmarshy 0:1c7da5f83647 14 * See the License for the specific language governing permissions and
sarahmarshy 0:1c7da5f83647 15 * limitations under the License.
sarahmarshy 0:1c7da5f83647 16 */
sarahmarshy 0:1c7da5f83647 17
sarahmarshy 0:1c7da5f83647 18 #ifndef __MBED_UTIL_CRITICAL_SECTION_LOCK_H__
sarahmarshy 0:1c7da5f83647 19 #define __MBED_UTIL_CRITICAL_SECTION_LOCK_H__
sarahmarshy 0:1c7da5f83647 20
sarahmarshy 0:1c7da5f83647 21 #include <stdint.h>
sarahmarshy 0:1c7da5f83647 22 #include "cmsis.h"
sarahmarshy 0:1c7da5f83647 23
sarahmarshy 0:1c7da5f83647 24 namespace mbed {
sarahmarshy 0:1c7da5f83647 25 namespace util {
sarahmarshy 0:1c7da5f83647 26
sarahmarshy 0:1c7da5f83647 27 /** RAII object for disabling, then restoring, interrupt state
sarahmarshy 0:1c7da5f83647 28 * Usage:
sarahmarshy 0:1c7da5f83647 29 * @code
sarahmarshy 0:1c7da5f83647 30 *
sarahmarshy 0:1c7da5f83647 31 * void f() {
sarahmarshy 0:1c7da5f83647 32 * // some code here
sarahmarshy 0:1c7da5f83647 33 * {
sarahmarshy 0:1c7da5f83647 34 * CriticalSectionLock lock;
sarahmarshy 0:1c7da5f83647 35 * // Code in this block will run with interrupts disabled
sarahmarshy 0:1c7da5f83647 36 * }
sarahmarshy 0:1c7da5f83647 37 * // interrupts will be restored to their previous state
sarahmarshy 0:1c7da5f83647 38 * }
sarahmarshy 0:1c7da5f83647 39 * @endcode
sarahmarshy 0:1c7da5f83647 40 */
sarahmarshy 0:1c7da5f83647 41 class CriticalSectionLock {
sarahmarshy 0:1c7da5f83647 42 public:
sarahmarshy 0:1c7da5f83647 43 CriticalSectionLock() {
sarahmarshy 0:1c7da5f83647 44 _state = __get_PRIMASK();
sarahmarshy 0:1c7da5f83647 45 __disable_irq();
sarahmarshy 0:1c7da5f83647 46 }
sarahmarshy 0:1c7da5f83647 47
sarahmarshy 0:1c7da5f83647 48 ~CriticalSectionLock() {
sarahmarshy 0:1c7da5f83647 49 __set_PRIMASK(_state);
sarahmarshy 0:1c7da5f83647 50 }
sarahmarshy 0:1c7da5f83647 51
sarahmarshy 0:1c7da5f83647 52 private:
sarahmarshy 0:1c7da5f83647 53 uint32_t _state;
sarahmarshy 0:1c7da5f83647 54 };
sarahmarshy 0:1c7da5f83647 55
sarahmarshy 0:1c7da5f83647 56 } // namespace util
sarahmarshy 0:1c7da5f83647 57 } // namespace mbed
sarahmarshy 0:1c7da5f83647 58
sarahmarshy 0:1c7da5f83647 59 #endif // #ifndef __MBED_UTIL_CRITICAL_SECTION_LOCK_H__