Lancaster University / mbed-src

Fork of mbed-src by mbed official

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:52:54 2016 +0100
Revision:
641:be9b2017785a
Parent:
324:406fd2029f23
Synchronized with git rev 1fb8ab4c
Author: James Devine
mbed-classic: BUGFIX for timer when using wait_ms from interrupt context

Previously if a user used wait[_ms,_us] in interrupt context the device would
hang indefinitely. This was due to incrementing overflowCount from
interrupt context only.

This meant that if a user used wait[_ms,_us] in an ISR with
the same or greater interrupt priority, it would result in an infinite
loop as the overflowCount variable would never be incremented, and
wait[_ms,_us] would never return.

This patch simply applies a better solution for the race condition
mentioned in the previous commit. It instead disables the timer1
interrupt and increments the overflowCount variable, preventing
the race condition whilst supporting wait[_ms,_us] in interrupt
context.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 324:406fd2029f23 1 /* mbed Microcontroller Library
mbed_official 324:406fd2029f23 2 * Copyright (c) 2006-2013 ARM Limited
mbed_official 324:406fd2029f23 3 *
mbed_official 324:406fd2029f23 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 324:406fd2029f23 5 * you may not use this file except in compliance with the License.
mbed_official 324:406fd2029f23 6 * You may obtain a copy of the License at
mbed_official 324:406fd2029f23 7 *
mbed_official 324:406fd2029f23 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 324:406fd2029f23 9 *
mbed_official 324:406fd2029f23 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 324:406fd2029f23 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 324:406fd2029f23 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 324:406fd2029f23 13 * See the License for the specific language governing permissions and
mbed_official 324:406fd2029f23 14 * limitations under the License.
mbed_official 324:406fd2029f23 15 */
mbed_official 324:406fd2029f23 16 #include "sleep_api.h"
mbed_official 324:406fd2029f23 17 #include "cmsis.h"
mbed_official 324:406fd2029f23 18 #include "fsl_mcg_hal.h"
mbed_official 324:406fd2029f23 19 #include "fsl_smc_hal.h"
mbed_official 324:406fd2029f23 20
mbed_official 324:406fd2029f23 21 void sleep(void) {
mbed_official 324:406fd2029f23 22 smc_power_mode_protection_config_t sleep_config = {true};
mbed_official 324:406fd2029f23 23 SMC_HAL_SetProtection(SMC_BASE, &sleep_config);
mbed_official 324:406fd2029f23 24
mbed_official 324:406fd2029f23 25 SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk;
mbed_official 324:406fd2029f23 26 __WFI();
mbed_official 324:406fd2029f23 27 }
mbed_official 324:406fd2029f23 28
mbed_official 324:406fd2029f23 29 void deepsleep(void) {
mbed_official 324:406fd2029f23 30 mcg_clock_select_t mcg_clock = CLOCK_HAL_GetClkSrcMode(MCG_BASE);
mbed_official 324:406fd2029f23 31
mbed_official 324:406fd2029f23 32 smc_power_mode_protection_config_t sleep_config = {true};
mbed_official 324:406fd2029f23 33 SMC_HAL_SetProtection(SMC_BASE, &sleep_config);
mbed_official 324:406fd2029f23 34 SMC->PMCTRL = SMC_PMCTRL_STOPM(2);
mbed_official 324:406fd2029f23 35
mbed_official 324:406fd2029f23 36 //Deep sleep for ARM core:
mbed_official 324:406fd2029f23 37 SCB->SCR = 1 << SCB_SCR_SLEEPDEEP_Pos;
mbed_official 324:406fd2029f23 38
mbed_official 324:406fd2029f23 39 __WFI();
mbed_official 324:406fd2029f23 40
mbed_official 324:406fd2029f23 41 //Switch back to PLL as clock source if needed
mbed_official 324:406fd2029f23 42 //The interrupt that woke up the device will run at reduced speed
mbed_official 324:406fd2029f23 43 if (mcg_clock == kMcgClkSelOut) {
mbed_official 324:406fd2029f23 44 if (CLOCK_HAL_GetPllStatMode(MCG_BASE) == kMcgPllStatPllClkSel) {
mbed_official 324:406fd2029f23 45 while (CLOCK_HAL_GetLock0Mode(MCG_BASE) == kMcgLockUnlocked);
mbed_official 324:406fd2029f23 46 }
mbed_official 324:406fd2029f23 47 CLOCK_HAL_SetClkSrcMode(MCG_BASE, kMcgClkSelOut);
mbed_official 324:406fd2029f23 48 }
mbed_official 324:406fd2029f23 49 }