Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
common/Stream.cpp@641:be9b2017785a, 2016-07-13 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Wed Jul 13 12:52:54 2016 +0100
- Revision:
- 641:be9b2017785a
- Parent:
- 431:255afbe6270c
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?
| User | Revision | Line number | New 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 "Stream.h" |
| bogdanm | 13:0645d8841f51 | 17 | |
| bogdanm | 13:0645d8841f51 | 18 | #include <cstdarg> |
| bogdanm | 13:0645d8841f51 | 19 | |
| bogdanm | 13:0645d8841f51 | 20 | namespace mbed { |
| bogdanm | 13:0645d8841f51 | 21 | |
| mbed_official | 212:34d62c0b2af6 | 22 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
| bogdanm | 13:0645d8841f51 | 23 | /* open ourselves */ |
| bogdanm | 13:0645d8841f51 | 24 | char buf[12]; /* :0x12345678 + null byte */ |
| bogdanm | 13:0645d8841f51 | 25 | std::sprintf(buf, ":%p", this); |
| bogdanm | 13:0645d8841f51 | 26 | _file = std::fopen(buf, "w+"); |
| mbed_official | 431:255afbe6270c | 27 | mbed_set_unbuffered_stream(_file); |
| bogdanm | 13:0645d8841f51 | 28 | } |
| bogdanm | 13:0645d8841f51 | 29 | |
| bogdanm | 13:0645d8841f51 | 30 | Stream::~Stream() { |
| bogdanm | 13:0645d8841f51 | 31 | fclose(_file); |
| bogdanm | 13:0645d8841f51 | 32 | } |
| bogdanm | 13:0645d8841f51 | 33 | |
| bogdanm | 13:0645d8841f51 | 34 | int Stream::putc(int c) { |
| bogdanm | 13:0645d8841f51 | 35 | fflush(_file); |
| bogdanm | 13:0645d8841f51 | 36 | return std::fputc(c, _file); |
| bogdanm | 13:0645d8841f51 | 37 | } |
| bogdanm | 13:0645d8841f51 | 38 | int Stream::puts(const char *s) { |
| bogdanm | 13:0645d8841f51 | 39 | fflush(_file); |
| bogdanm | 13:0645d8841f51 | 40 | return std::fputs(s, _file); |
| bogdanm | 13:0645d8841f51 | 41 | } |
| bogdanm | 13:0645d8841f51 | 42 | int Stream::getc() { |
| bogdanm | 13:0645d8841f51 | 43 | fflush(_file); |
| mbed_official | 431:255afbe6270c | 44 | return mbed_getc(_file); |
| bogdanm | 13:0645d8841f51 | 45 | } |
| bogdanm | 13:0645d8841f51 | 46 | char* Stream::gets(char *s, int size) { |
| bogdanm | 13:0645d8841f51 | 47 | fflush(_file); |
| mbed_official | 431:255afbe6270c | 48 | return mbed_gets(s,size,_file); |
| bogdanm | 13:0645d8841f51 | 49 | } |
| bogdanm | 13:0645d8841f51 | 50 | |
| bogdanm | 13:0645d8841f51 | 51 | int Stream::close() { |
| bogdanm | 13:0645d8841f51 | 52 | return 0; |
| bogdanm | 13:0645d8841f51 | 53 | } |
| bogdanm | 13:0645d8841f51 | 54 | |
| bogdanm | 13:0645d8841f51 | 55 | ssize_t Stream::write(const void* buffer, size_t length) { |
| bogdanm | 13:0645d8841f51 | 56 | const char* ptr = (const char*)buffer; |
| bogdanm | 13:0645d8841f51 | 57 | const char* end = ptr + length; |
| bogdanm | 13:0645d8841f51 | 58 | while (ptr != end) { |
| bogdanm | 13:0645d8841f51 | 59 | if (_putc(*ptr++) == EOF) { |
| bogdanm | 13:0645d8841f51 | 60 | break; |
| bogdanm | 13:0645d8841f51 | 61 | } |
| bogdanm | 13:0645d8841f51 | 62 | } |
| bogdanm | 13:0645d8841f51 | 63 | return ptr - (const char*)buffer; |
| bogdanm | 13:0645d8841f51 | 64 | } |
| bogdanm | 13:0645d8841f51 | 65 | |
| bogdanm | 13:0645d8841f51 | 66 | ssize_t Stream::read(void* buffer, size_t length) { |
| bogdanm | 13:0645d8841f51 | 67 | char* ptr = (char*)buffer; |
| bogdanm | 13:0645d8841f51 | 68 | char* end = ptr + length; |
| bogdanm | 13:0645d8841f51 | 69 | while (ptr != end) { |
| bogdanm | 13:0645d8841f51 | 70 | int c = _getc(); |
| bogdanm | 13:0645d8841f51 | 71 | if (c==EOF) break; |
| bogdanm | 13:0645d8841f51 | 72 | *ptr++ = c; |
| bogdanm | 13:0645d8841f51 | 73 | } |
| bogdanm | 13:0645d8841f51 | 74 | return ptr - (const char*)buffer; |
| bogdanm | 13:0645d8841f51 | 75 | } |
| bogdanm | 13:0645d8841f51 | 76 | |
| bogdanm | 13:0645d8841f51 | 77 | off_t Stream::lseek(off_t offset, int whence) { |
| bogdanm | 13:0645d8841f51 | 78 | return 0; |
| bogdanm | 13:0645d8841f51 | 79 | } |
| bogdanm | 13:0645d8841f51 | 80 | |
| bogdanm | 13:0645d8841f51 | 81 | int Stream::isatty() { |
| bogdanm | 13:0645d8841f51 | 82 | return 0; |
| bogdanm | 13:0645d8841f51 | 83 | } |
| bogdanm | 13:0645d8841f51 | 84 | |
| bogdanm | 13:0645d8841f51 | 85 | int Stream::fsync() { |
| bogdanm | 13:0645d8841f51 | 86 | return 0; |
| bogdanm | 13:0645d8841f51 | 87 | } |
| bogdanm | 13:0645d8841f51 | 88 | |
| bogdanm | 13:0645d8841f51 | 89 | off_t Stream::flen() { |
| bogdanm | 13:0645d8841f51 | 90 | return 0; |
| bogdanm | 13:0645d8841f51 | 91 | } |
| bogdanm | 13:0645d8841f51 | 92 | |
| bogdanm | 13:0645d8841f51 | 93 | int Stream::printf(const char* format, ...) { |
| bogdanm | 13:0645d8841f51 | 94 | std::va_list arg; |
| bogdanm | 13:0645d8841f51 | 95 | va_start(arg, format); |
| bogdanm | 13:0645d8841f51 | 96 | fflush(_file); |
| bogdanm | 13:0645d8841f51 | 97 | int r = vfprintf(_file, format, arg); |
| bogdanm | 13:0645d8841f51 | 98 | va_end(arg); |
| bogdanm | 13:0645d8841f51 | 99 | return r; |
| bogdanm | 13:0645d8841f51 | 100 | } |
| bogdanm | 13:0645d8841f51 | 101 | |
| bogdanm | 13:0645d8841f51 | 102 | int Stream::scanf(const char* format, ...) { |
| bogdanm | 13:0645d8841f51 | 103 | std::va_list arg; |
| bogdanm | 13:0645d8841f51 | 104 | va_start(arg, format); |
| bogdanm | 13:0645d8841f51 | 105 | fflush(_file); |
| bogdanm | 13:0645d8841f51 | 106 | int r = vfscanf(_file, format, arg); |
| bogdanm | 13:0645d8841f51 | 107 | va_end(arg); |
| bogdanm | 13:0645d8841f51 | 108 | return r; |
| bogdanm | 13:0645d8841f51 | 109 | } |
| bogdanm | 13:0645d8841f51 | 110 | |
| bogdanm | 13:0645d8841f51 | 111 | } // namespace mbed |
