USB Serial application

Fork of USBSerial_HelloWorld by Samuel Mokrani

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Zaitsev 10:41552d038a69 1 /* mbed Microcontroller Library
Zaitsev 10:41552d038a69 2 * Copyright (c) 2006-2013 ARM Limited
Zaitsev 10:41552d038a69 3 *
Zaitsev 10:41552d038a69 4 * Licensed under the Apache License, Version 2.0 (the "License");
Zaitsev 10:41552d038a69 5 * you may not use this file except in compliance with the License.
Zaitsev 10:41552d038a69 6 * You may obtain a copy of the License at
Zaitsev 10:41552d038a69 7 *
Zaitsev 10:41552d038a69 8 * http://www.apache.org/licenses/LICENSE-2.0
Zaitsev 10:41552d038a69 9 *
Zaitsev 10:41552d038a69 10 * Unless required by applicable law or agreed to in writing, software
Zaitsev 10:41552d038a69 11 * distributed under the License is distributed on an "AS IS" BASIS,
Zaitsev 10:41552d038a69 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Zaitsev 10:41552d038a69 13 * See the License for the specific language governing permissions and
Zaitsev 10:41552d038a69 14 * limitations under the License.
Zaitsev 10:41552d038a69 15 */
Zaitsev 10:41552d038a69 16 #include "sleep_api.h"
Zaitsev 10:41552d038a69 17 #include "cmsis.h"
Zaitsev 10:41552d038a69 18 #include "mbed_interface.h"
Zaitsev 10:41552d038a69 19 #include "softdevice_handler.h"
Zaitsev 10:41552d038a69 20 #include "nrf_soc.h"
Zaitsev 10:41552d038a69 21
Zaitsev 10:41552d038a69 22 // Mask of reserved bits of the register ICSR in the System Control Block peripheral
Zaitsev 10:41552d038a69 23 // In this case, bits which are equal to 0 are the bits reserved in this register
Zaitsev 10:41552d038a69 24 #define SCB_ICSR_RESERVED_BITS_MASK 0x9E43F03F
Zaitsev 10:41552d038a69 25
Zaitsev 10:41552d038a69 26 #define FPU_EXCEPTION_MASK 0x0000009F
Zaitsev 10:41552d038a69 27
Zaitsev 10:41552d038a69 28 void sleep(void)
Zaitsev 10:41552d038a69 29 {
Zaitsev 10:41552d038a69 30 // ensure debug is disconnected if semihost is enabled....
Zaitsev 10:41552d038a69 31
Zaitsev 10:41552d038a69 32 // Trigger an event when an interrupt is pending. This allows to wake up
Zaitsev 10:41552d038a69 33 // the processor from disabled interrupts.
Zaitsev 10:41552d038a69 34 SCB->SCR |= SCB_SCR_SEVONPEND_Msk;
Zaitsev 10:41552d038a69 35
Zaitsev 10:41552d038a69 36 #ifdef NRF52
Zaitsev 10:41552d038a69 37 /* Clear exceptions and PendingIRQ from the FPU unit */
Zaitsev 10:41552d038a69 38 __set_FPSCR(__get_FPSCR() & ~(FPU_EXCEPTION_MASK));
Zaitsev 10:41552d038a69 39 (void) __get_FPSCR();
Zaitsev 10:41552d038a69 40 NVIC_ClearPendingIRQ(FPU_IRQn);
Zaitsev 10:41552d038a69 41 #endif
Zaitsev 10:41552d038a69 42
Zaitsev 10:41552d038a69 43 // If the SoftDevice is enabled, its API must be used to go to sleep.
Zaitsev 10:41552d038a69 44 if (softdevice_handler_isEnabled()) {
Zaitsev 10:41552d038a69 45 sd_power_mode_set(NRF_POWER_MODE_LOWPWR);
Zaitsev 10:41552d038a69 46 sd_app_evt_wait();
Zaitsev 10:41552d038a69 47 } else {
Zaitsev 10:41552d038a69 48 NRF_POWER->TASKS_LOWPWR = 1;
Zaitsev 10:41552d038a69 49
Zaitsev 10:41552d038a69 50 // Note: it is not sufficient to just use WFE here, since the internal
Zaitsev 10:41552d038a69 51 // event register may be already set from an event that occurred in the
Zaitsev 10:41552d038a69 52 // past (like an SVC call to the SoftDevice) and in such case WFE will
Zaitsev 10:41552d038a69 53 // just clear the register and continue execution.
Zaitsev 10:41552d038a69 54 // Therefore, the strategy here is to first clear the event register
Zaitsev 10:41552d038a69 55 // by using SEV/WFE pair, and then execute WFE again, unless there is
Zaitsev 10:41552d038a69 56 // a pending interrupt.
Zaitsev 10:41552d038a69 57
Zaitsev 10:41552d038a69 58 // Set an event and wake up whatsoever, this will clear the event
Zaitsev 10:41552d038a69 59 // register from all previous events set (SVC call included)
Zaitsev 10:41552d038a69 60 __SEV();
Zaitsev 10:41552d038a69 61 __WFE();
Zaitsev 10:41552d038a69 62
Zaitsev 10:41552d038a69 63 // Test if there is an interrupt pending (mask reserved regions)
Zaitsev 10:41552d038a69 64 if (SCB->ICSR & (SCB_ICSR_RESERVED_BITS_MASK)) {
Zaitsev 10:41552d038a69 65 // Ok, there is an interrut pending, no need to go to sleep
Zaitsev 10:41552d038a69 66 return;
Zaitsev 10:41552d038a69 67 } else {
Zaitsev 10:41552d038a69 68 // next event will wakeup the CPU
Zaitsev 10:41552d038a69 69 // If an interrupt occured between the test of SCB->ICSR and this
Zaitsev 10:41552d038a69 70 // instruction, WFE will just not put the CPU to sleep
Zaitsev 10:41552d038a69 71 __WFE();
Zaitsev 10:41552d038a69 72 }
Zaitsev 10:41552d038a69 73 }
Zaitsev 10:41552d038a69 74 }
Zaitsev 10:41552d038a69 75
Zaitsev 10:41552d038a69 76 void deepsleep(void)
Zaitsev 10:41552d038a69 77 {
Zaitsev 10:41552d038a69 78 sleep();
Zaitsev 10:41552d038a69 79 // NRF_POWER->SYSTEMOFF=1;
Zaitsev 10:41552d038a69 80 }