Lancaster University's (short term!) clone of mbed-src for micro:bit. This is a copy of the github branch https://github.com/lancaster-university/mbed-classic

Fork of mbed-src by mbed official

Committer:
LancasterUniversity
Date:
Wed Jul 13 12:52:54 2016 +0100
Revision:
641:be9b2017785a
Parent:
457:777fdfed0738
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
bogdanm 13:0645d8841f51 1 /* mbed Microcontroller Library
bogdanm 13:0645d8841f51 2 * Copyright (c) 2006-2013 ARM Limited
bogdanm 13:0645d8841f51 3 *
bogdanm 13:0645d8841f51 4 * Licensed under the Apache License, Version 2.0 (the "License");
bogdanm 13:0645d8841f51 5 * you may not use this file except in compliance with the License.
bogdanm 13:0645d8841f51 6 * You may obtain a copy of the License at
bogdanm 13:0645d8841f51 7 *
bogdanm 13:0645d8841f51 8 * http://www.apache.org/licenses/LICENSE-2.0
bogdanm 13:0645d8841f51 9 *
bogdanm 13:0645d8841f51 10 * Unless required by applicable law or agreed to in writing, software
bogdanm 13:0645d8841f51 11 * distributed under the License is distributed on an "AS IS" BASIS,
bogdanm 13:0645d8841f51 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bogdanm 13:0645d8841f51 13 * See the License for the specific language governing permissions and
bogdanm 13:0645d8841f51 14 * limitations under the License.
bogdanm 13:0645d8841f51 15 */
bogdanm 13:0645d8841f51 16 #include "BusInOut.h"
mbed_official 457:777fdfed0738 17 #include "mbed_assert.h"
bogdanm 13:0645d8841f51 18
bogdanm 13:0645d8841f51 19 namespace mbed {
bogdanm 13:0645d8841f51 20
bogdanm 13:0645d8841f51 21 BusInOut::BusInOut(PinName p0, PinName p1, PinName p2, PinName p3, PinName p4, PinName p5, PinName p6, PinName p7, PinName p8, PinName p9, PinName p10, PinName p11, PinName p12, PinName p13, PinName p14, PinName p15) {
bogdanm 13:0645d8841f51 22 PinName pins[16] = {p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15};
bogdanm 13:0645d8841f51 23
mbed_official 455:8bc3a354916d 24 _nc_mask = 0;
bogdanm 13:0645d8841f51 25 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 26 _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
mbed_official 455:8bc3a354916d 27 if (pins[i] != NC) {
mbed_official 455:8bc3a354916d 28 _nc_mask |= (1 << i);
mbed_official 455:8bc3a354916d 29 }
bogdanm 13:0645d8841f51 30 }
bogdanm 13:0645d8841f51 31 }
bogdanm 13:0645d8841f51 32
bogdanm 13:0645d8841f51 33 BusInOut::BusInOut(PinName pins[16]) {
mbed_official 455:8bc3a354916d 34 _nc_mask = 0;
bogdanm 13:0645d8841f51 35 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 36 _pin[i] = (pins[i] != NC) ? new DigitalInOut(pins[i]) : 0;
mbed_official 455:8bc3a354916d 37 if (pins[i] != NC) {
mbed_official 455:8bc3a354916d 38 _nc_mask |= (1 << i);
mbed_official 455:8bc3a354916d 39 }
bogdanm 13:0645d8841f51 40 }
bogdanm 13:0645d8841f51 41 }
bogdanm 13:0645d8841f51 42
bogdanm 13:0645d8841f51 43 BusInOut::~BusInOut() {
bogdanm 13:0645d8841f51 44 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 45 if (_pin[i] != 0) {
bogdanm 13:0645d8841f51 46 delete _pin[i];
bogdanm 13:0645d8841f51 47 }
bogdanm 13:0645d8841f51 48 }
bogdanm 13:0645d8841f51 49 }
bogdanm 13:0645d8841f51 50
bogdanm 13:0645d8841f51 51 void BusInOut::write(int value) {
bogdanm 13:0645d8841f51 52 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 53 if (_pin[i] != 0) {
bogdanm 13:0645d8841f51 54 _pin[i]->write((value >> i) & 1);
bogdanm 13:0645d8841f51 55 }
bogdanm 13:0645d8841f51 56 }
bogdanm 13:0645d8841f51 57 }
bogdanm 13:0645d8841f51 58
bogdanm 13:0645d8841f51 59 int BusInOut::read() {
bogdanm 13:0645d8841f51 60 int v = 0;
bogdanm 13:0645d8841f51 61 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 62 if (_pin[i] != 0) {
bogdanm 13:0645d8841f51 63 v |= _pin[i]->read() << i;
bogdanm 13:0645d8841f51 64 }
bogdanm 13:0645d8841f51 65 }
bogdanm 13:0645d8841f51 66 return v;
bogdanm 13:0645d8841f51 67 }
bogdanm 13:0645d8841f51 68
bogdanm 13:0645d8841f51 69 void BusInOut::output() {
bogdanm 13:0645d8841f51 70 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 71 if (_pin[i] != 0) {
bogdanm 13:0645d8841f51 72 _pin[i]->output();
bogdanm 13:0645d8841f51 73 }
bogdanm 13:0645d8841f51 74 }
bogdanm 13:0645d8841f51 75 }
bogdanm 13:0645d8841f51 76
bogdanm 13:0645d8841f51 77 void BusInOut::input() {
bogdanm 13:0645d8841f51 78 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 79 if (_pin[i] != 0) {
bogdanm 13:0645d8841f51 80 _pin[i]->input();
bogdanm 13:0645d8841f51 81 }
bogdanm 13:0645d8841f51 82 }
bogdanm 13:0645d8841f51 83 }
bogdanm 13:0645d8841f51 84
bogdanm 13:0645d8841f51 85 void BusInOut::mode(PinMode pull) {
bogdanm 13:0645d8841f51 86 for (int i=0; i<16; i++) {
bogdanm 13:0645d8841f51 87 if (_pin[i] != 0) {
bogdanm 13:0645d8841f51 88 _pin[i]->mode(pull);
bogdanm 13:0645d8841f51 89 }
bogdanm 13:0645d8841f51 90 }
bogdanm 13:0645d8841f51 91 }
bogdanm 13:0645d8841f51 92
bogdanm 13:0645d8841f51 93 #ifdef MBED_OPERATORS
bogdanm 13:0645d8841f51 94 BusInOut& BusInOut::operator= (int v) {
bogdanm 13:0645d8841f51 95 write(v);
bogdanm 13:0645d8841f51 96 return *this;
bogdanm 13:0645d8841f51 97 }
bogdanm 13:0645d8841f51 98
bogdanm 13:0645d8841f51 99 BusInOut& BusInOut::operator= (BusInOut& rhs) {
bogdanm 13:0645d8841f51 100 write(rhs.read());
bogdanm 13:0645d8841f51 101 return *this;
bogdanm 13:0645d8841f51 102 }
bogdanm 13:0645d8841f51 103
mbed_official 455:8bc3a354916d 104 DigitalInOut& BusInOut::operator[] (int index) {
mbed_official 455:8bc3a354916d 105 MBED_ASSERT(index >= 0 && index <= 16);
mbed_official 455:8bc3a354916d 106 MBED_ASSERT(_pin[index]);
mbed_official 455:8bc3a354916d 107 return *_pin[index];
mbed_official 455:8bc3a354916d 108 }
mbed_official 455:8bc3a354916d 109
bogdanm 13:0645d8841f51 110 BusInOut::operator int() {
bogdanm 13:0645d8841f51 111 return read();
bogdanm 13:0645d8841f51 112 }
bogdanm 13:0645d8841f51 113 #endif
bogdanm 13:0645d8841f51 114
bogdanm 13:0645d8841f51 115 } // namespace mbed