Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lypinator 0:bb348c97df44 1 /* mbed Microcontroller Library
lypinator 0:bb348c97df44 2 * Copyright (c) 2017 ARM Limited
lypinator 0:bb348c97df44 3 *
lypinator 0:bb348c97df44 4 * Licensed under the Apache License, Version 2.0 (the "License");
lypinator 0:bb348c97df44 5 * you may not use this file except in compliance with the License.
lypinator 0:bb348c97df44 6 * You may obtain a copy of the License at
lypinator 0:bb348c97df44 7 *
lypinator 0:bb348c97df44 8 * http://www.apache.org/licenses/LICENSE-2.0
lypinator 0:bb348c97df44 9 *
lypinator 0:bb348c97df44 10 * Unless required by applicable law or agreed to in writing, software
lypinator 0:bb348c97df44 11 * distributed under the License is distributed on an "AS IS" BASIS,
lypinator 0:bb348c97df44 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lypinator 0:bb348c97df44 13 * See the License for the specific language governing permissions and
lypinator 0:bb348c97df44 14 * limitations under the License.
lypinator 0:bb348c97df44 15 */
lypinator 0:bb348c97df44 16 #include "cmsis.h"
lypinator 0:bb348c97df44 17 #include "hal/critical_section_api.h"
lypinator 0:bb348c97df44 18 #include "platform/mbed_assert.h"
lypinator 0:bb348c97df44 19 #include "platform/mbed_toolchain.h"
lypinator 0:bb348c97df44 20
lypinator 0:bb348c97df44 21 #include <stdbool.h>
lypinator 0:bb348c97df44 22
lypinator 0:bb348c97df44 23 static volatile bool critical_interrupts_enabled = false;
lypinator 0:bb348c97df44 24 static volatile bool state_saved = false;
lypinator 0:bb348c97df44 25
lypinator 0:bb348c97df44 26 static bool are_interrupts_enabled(void)
lypinator 0:bb348c97df44 27 {
lypinator 0:bb348c97df44 28 #if defined(__CORTEX_A9)
lypinator 0:bb348c97df44 29 return ((__get_CPSR() & 0x80) == 0);
lypinator 0:bb348c97df44 30 #else
lypinator 0:bb348c97df44 31 return ((__get_PRIMASK() & 0x1) == 0);
lypinator 0:bb348c97df44 32 #endif
lypinator 0:bb348c97df44 33 }
lypinator 0:bb348c97df44 34
lypinator 0:bb348c97df44 35
lypinator 0:bb348c97df44 36 MBED_WEAK void hal_critical_section_enter(void)
lypinator 0:bb348c97df44 37 {
lypinator 0:bb348c97df44 38 const bool interrupt_state = are_interrupts_enabled();
lypinator 0:bb348c97df44 39
lypinator 0:bb348c97df44 40 __disable_irq();
lypinator 0:bb348c97df44 41
lypinator 0:bb348c97df44 42 if (state_saved == true) {
lypinator 0:bb348c97df44 43 return;
lypinator 0:bb348c97df44 44 }
lypinator 0:bb348c97df44 45
lypinator 0:bb348c97df44 46 critical_interrupts_enabled = interrupt_state;
lypinator 0:bb348c97df44 47 state_saved = true;
lypinator 0:bb348c97df44 48 }
lypinator 0:bb348c97df44 49
lypinator 0:bb348c97df44 50 MBED_WEAK void hal_critical_section_exit(void)
lypinator 0:bb348c97df44 51 {
lypinator 0:bb348c97df44 52 #ifndef FEATURE_UVISOR
lypinator 0:bb348c97df44 53 // Interrupts must be disabled on invoking an exit from a critical section
lypinator 0:bb348c97df44 54 MBED_ASSERT(!are_interrupts_enabled());
lypinator 0:bb348c97df44 55 #endif
lypinator 0:bb348c97df44 56 state_saved = false;
lypinator 0:bb348c97df44 57
lypinator 0:bb348c97df44 58 // Restore the IRQs to their state prior to entering the critical section
lypinator 0:bb348c97df44 59 if (critical_interrupts_enabled == true) {
lypinator 0:bb348c97df44 60 __enable_irq();
lypinator 0:bb348c97df44 61 }
lypinator 0:bb348c97df44 62 }
lypinator 0:bb348c97df44 63
lypinator 0:bb348c97df44 64 MBED_WEAK bool hal_in_critical_section(void)
lypinator 0:bb348c97df44 65 {
lypinator 0:bb348c97df44 66 return (state_saved == true);
lypinator 0:bb348c97df44 67 }