added prescaler for 16 bit pwm in LPC1347 target
Fork of mbed-dev by
common/Stream.cpp@144:ef7eb2e8f9f7, 2016-09-02 (annotated)
- Committer:
- <>
- Date:
- Fri Sep 02 15:07:44 2016 +0100
- Revision:
- 144:ef7eb2e8f9f7
- Parent:
- 28:8e1f0b362990
This updates the lib to the mbed lib v125
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
<> | 144:ef7eb2e8f9f7 | 3 | * |
<> | 144:ef7eb2e8f9f7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
<> | 144:ef7eb2e8f9f7 | 5 | * you may not use this file except in compliance with the License. |
<> | 144:ef7eb2e8f9f7 | 6 | * You may obtain a copy of the License at |
<> | 144:ef7eb2e8f9f7 | 7 | * |
<> | 144:ef7eb2e8f9f7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
<> | 144:ef7eb2e8f9f7 | 9 | * |
<> | 144:ef7eb2e8f9f7 | 10 | * Unless required by applicable law or agreed to in writing, software |
<> | 144:ef7eb2e8f9f7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
<> | 144:ef7eb2e8f9f7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
<> | 144:ef7eb2e8f9f7 | 13 | * See the License for the specific language governing permissions and |
<> | 144:ef7eb2e8f9f7 | 14 | * limitations under the License. |
<> | 144:ef7eb2e8f9f7 | 15 | */ |
<> | 144:ef7eb2e8f9f7 | 16 | #include "Stream.h" |
<> | 144:ef7eb2e8f9f7 | 17 | |
<> | 144:ef7eb2e8f9f7 | 18 | namespace mbed { |
<> | 144:ef7eb2e8f9f7 | 19 | |
<> | 144:ef7eb2e8f9f7 | 20 | Stream::Stream(const char *name) : FileLike(name), _file(NULL) { |
<> | 144:ef7eb2e8f9f7 | 21 | // No lock needed in constructor |
<> | 144:ef7eb2e8f9f7 | 22 | /* open ourselves */ |
<> | 144:ef7eb2e8f9f7 | 23 | char buf[12]; /* :0x12345678 + null byte */ |
<> | 144:ef7eb2e8f9f7 | 24 | std::sprintf(buf, ":%p", this); |
<> | 144:ef7eb2e8f9f7 | 25 | _file = std::fopen(buf, "w+"); |
<> | 144:ef7eb2e8f9f7 | 26 | mbed_set_unbuffered_stream(_file); |
<> | 144:ef7eb2e8f9f7 | 27 | } |
<> | 144:ef7eb2e8f9f7 | 28 | |
<> | 144:ef7eb2e8f9f7 | 29 | Stream::~Stream() { |
<> | 144:ef7eb2e8f9f7 | 30 | // No lock can be used in destructor |
<> | 144:ef7eb2e8f9f7 | 31 | fclose(_file); |
<> | 144:ef7eb2e8f9f7 | 32 | } |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | int Stream::putc(int c) { |
<> | 144:ef7eb2e8f9f7 | 35 | lock(); |
<> | 144:ef7eb2e8f9f7 | 36 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 37 | int ret = std::fputc(c, _file); |
<> | 144:ef7eb2e8f9f7 | 38 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 39 | return ret; |
<> | 144:ef7eb2e8f9f7 | 40 | } |
<> | 144:ef7eb2e8f9f7 | 41 | int Stream::puts(const char *s) { |
<> | 144:ef7eb2e8f9f7 | 42 | lock(); |
<> | 144:ef7eb2e8f9f7 | 43 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 44 | int ret = std::fputs(s, _file); |
<> | 144:ef7eb2e8f9f7 | 45 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 46 | return ret; |
<> | 144:ef7eb2e8f9f7 | 47 | } |
<> | 144:ef7eb2e8f9f7 | 48 | int Stream::getc() { |
<> | 144:ef7eb2e8f9f7 | 49 | lock(); |
<> | 144:ef7eb2e8f9f7 | 50 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 51 | int ret = mbed_getc(_file); |
<> | 144:ef7eb2e8f9f7 | 52 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 53 | return ret; |
<> | 144:ef7eb2e8f9f7 | 54 | } |
<> | 144:ef7eb2e8f9f7 | 55 | char* Stream::gets(char *s, int size) { |
<> | 144:ef7eb2e8f9f7 | 56 | lock(); |
<> | 144:ef7eb2e8f9f7 | 57 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 58 | char *ret = mbed_gets(s,size,_file); |
<> | 144:ef7eb2e8f9f7 | 59 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 60 | return ret; |
<> | 144:ef7eb2e8f9f7 | 61 | } |
<> | 144:ef7eb2e8f9f7 | 62 | |
<> | 144:ef7eb2e8f9f7 | 63 | int Stream::close() { |
<> | 144:ef7eb2e8f9f7 | 64 | return 0; |
<> | 144:ef7eb2e8f9f7 | 65 | } |
<> | 144:ef7eb2e8f9f7 | 66 | |
<> | 144:ef7eb2e8f9f7 | 67 | ssize_t Stream::write(const void* buffer, size_t length) { |
<> | 144:ef7eb2e8f9f7 | 68 | const char* ptr = (const char*)buffer; |
<> | 144:ef7eb2e8f9f7 | 69 | const char* end = ptr + length; |
<> | 144:ef7eb2e8f9f7 | 70 | |
<> | 144:ef7eb2e8f9f7 | 71 | lock(); |
<> | 144:ef7eb2e8f9f7 | 72 | while (ptr != end) { |
<> | 144:ef7eb2e8f9f7 | 73 | if (_putc(*ptr++) == EOF) { |
<> | 144:ef7eb2e8f9f7 | 74 | break; |
<> | 144:ef7eb2e8f9f7 | 75 | } |
<> | 144:ef7eb2e8f9f7 | 76 | } |
<> | 144:ef7eb2e8f9f7 | 77 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 78 | |
<> | 144:ef7eb2e8f9f7 | 79 | return ptr - (const char*)buffer; |
<> | 144:ef7eb2e8f9f7 | 80 | } |
<> | 144:ef7eb2e8f9f7 | 81 | |
<> | 144:ef7eb2e8f9f7 | 82 | ssize_t Stream::read(void* buffer, size_t length) { |
<> | 144:ef7eb2e8f9f7 | 83 | char* ptr = (char*)buffer; |
<> | 144:ef7eb2e8f9f7 | 84 | char* end = ptr + length; |
<> | 144:ef7eb2e8f9f7 | 85 | |
<> | 144:ef7eb2e8f9f7 | 86 | lock(); |
<> | 144:ef7eb2e8f9f7 | 87 | while (ptr != end) { |
<> | 144:ef7eb2e8f9f7 | 88 | int c = _getc(); |
<> | 144:ef7eb2e8f9f7 | 89 | if (c==EOF) break; |
<> | 144:ef7eb2e8f9f7 | 90 | *ptr++ = c; |
<> | 144:ef7eb2e8f9f7 | 91 | } |
<> | 144:ef7eb2e8f9f7 | 92 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 93 | |
<> | 144:ef7eb2e8f9f7 | 94 | return ptr - (const char*)buffer; |
<> | 144:ef7eb2e8f9f7 | 95 | } |
<> | 144:ef7eb2e8f9f7 | 96 | |
<> | 144:ef7eb2e8f9f7 | 97 | off_t Stream::lseek(off_t offset, int whence) { |
<> | 144:ef7eb2e8f9f7 | 98 | return 0; |
<> | 144:ef7eb2e8f9f7 | 99 | } |
<> | 144:ef7eb2e8f9f7 | 100 | |
<> | 144:ef7eb2e8f9f7 | 101 | int Stream::isatty() { |
<> | 144:ef7eb2e8f9f7 | 102 | return 0; |
<> | 144:ef7eb2e8f9f7 | 103 | } |
<> | 144:ef7eb2e8f9f7 | 104 | |
<> | 144:ef7eb2e8f9f7 | 105 | int Stream::fsync() { |
<> | 144:ef7eb2e8f9f7 | 106 | return 0; |
<> | 144:ef7eb2e8f9f7 | 107 | } |
<> | 144:ef7eb2e8f9f7 | 108 | |
<> | 144:ef7eb2e8f9f7 | 109 | off_t Stream::flen() { |
<> | 144:ef7eb2e8f9f7 | 110 | return 0; |
<> | 144:ef7eb2e8f9f7 | 111 | } |
<> | 144:ef7eb2e8f9f7 | 112 | |
<> | 144:ef7eb2e8f9f7 | 113 | int Stream::printf(const char* format, ...) { |
<> | 144:ef7eb2e8f9f7 | 114 | lock(); |
<> | 144:ef7eb2e8f9f7 | 115 | std::va_list arg; |
<> | 144:ef7eb2e8f9f7 | 116 | va_start(arg, format); |
<> | 144:ef7eb2e8f9f7 | 117 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 118 | int r = vfprintf(_file, format, arg); |
<> | 144:ef7eb2e8f9f7 | 119 | va_end(arg); |
<> | 144:ef7eb2e8f9f7 | 120 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 121 | return r; |
<> | 144:ef7eb2e8f9f7 | 122 | } |
<> | 144:ef7eb2e8f9f7 | 123 | |
<> | 144:ef7eb2e8f9f7 | 124 | int Stream::scanf(const char* format, ...) { |
<> | 144:ef7eb2e8f9f7 | 125 | lock(); |
<> | 144:ef7eb2e8f9f7 | 126 | std::va_list arg; |
<> | 144:ef7eb2e8f9f7 | 127 | va_start(arg, format); |
<> | 144:ef7eb2e8f9f7 | 128 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 129 | int r = vfscanf(_file, format, arg); |
<> | 144:ef7eb2e8f9f7 | 130 | va_end(arg); |
<> | 144:ef7eb2e8f9f7 | 131 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 132 | return r; |
<> | 144:ef7eb2e8f9f7 | 133 | } |
<> | 144:ef7eb2e8f9f7 | 134 | |
<> | 144:ef7eb2e8f9f7 | 135 | int Stream::vprintf(const char* format, std::va_list args) { |
<> | 144:ef7eb2e8f9f7 | 136 | lock(); |
<> | 144:ef7eb2e8f9f7 | 137 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 138 | int r = vfprintf(_file, format, args); |
<> | 144:ef7eb2e8f9f7 | 139 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 140 | return r; |
<> | 144:ef7eb2e8f9f7 | 141 | } |
<> | 144:ef7eb2e8f9f7 | 142 | |
<> | 144:ef7eb2e8f9f7 | 143 | int Stream::vscanf(const char* format, std::va_list args) { |
<> | 144:ef7eb2e8f9f7 | 144 | lock(); |
<> | 144:ef7eb2e8f9f7 | 145 | fflush(_file); |
<> | 144:ef7eb2e8f9f7 | 146 | int r = vfscanf(_file, format, args); |
<> | 144:ef7eb2e8f9f7 | 147 | unlock(); |
<> | 144:ef7eb2e8f9f7 | 148 | return r; |
<> | 144:ef7eb2e8f9f7 | 149 | } |
<> | 144:ef7eb2e8f9f7 | 150 | |
<> | 144:ef7eb2e8f9f7 | 151 | } // namespace mbed |