Knight KE
/
Game_Master
游戏王对战板,目前code还是空的
mbed-os/platform/mbed_poll.cpp@0:9b3d4731edbb, 2018-06-21 (annotated)
- Committer:
- WFKnight
- Date:
- Thu Jun 21 13:51:43 2018 +0000
- Revision:
- 0:9b3d4731edbb
UART, RTOS, LED
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WFKnight | 0:9b3d4731edbb | 1 | /* mbed Microcontroller Library |
WFKnight | 0:9b3d4731edbb | 2 | * Copyright (c) 2017 ARM Limited |
WFKnight | 0:9b3d4731edbb | 3 | * |
WFKnight | 0:9b3d4731edbb | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
WFKnight | 0:9b3d4731edbb | 5 | * you may not use this file except in compliance with the License. |
WFKnight | 0:9b3d4731edbb | 6 | * You may obtain a copy of the License at |
WFKnight | 0:9b3d4731edbb | 7 | * |
WFKnight | 0:9b3d4731edbb | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
WFKnight | 0:9b3d4731edbb | 9 | * |
WFKnight | 0:9b3d4731edbb | 10 | * Unless required by applicable law or agreed to in writing, software |
WFKnight | 0:9b3d4731edbb | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
WFKnight | 0:9b3d4731edbb | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
WFKnight | 0:9b3d4731edbb | 13 | * See the License for the specific language governing permissions and |
WFKnight | 0:9b3d4731edbb | 14 | * limitations under the License. |
WFKnight | 0:9b3d4731edbb | 15 | */ |
WFKnight | 0:9b3d4731edbb | 16 | #include "mbed_poll.h" |
WFKnight | 0:9b3d4731edbb | 17 | #include "FileHandle.h" |
WFKnight | 0:9b3d4731edbb | 18 | #if MBED_CONF_RTOS_PRESENT |
WFKnight | 0:9b3d4731edbb | 19 | #include "rtos/Kernel.h" |
WFKnight | 0:9b3d4731edbb | 20 | #include "rtos/Thread.h" |
WFKnight | 0:9b3d4731edbb | 21 | using namespace rtos; |
WFKnight | 0:9b3d4731edbb | 22 | #else |
WFKnight | 0:9b3d4731edbb | 23 | #include "Timer.h" |
WFKnight | 0:9b3d4731edbb | 24 | #include "LowPowerTimer.h" |
WFKnight | 0:9b3d4731edbb | 25 | #endif |
WFKnight | 0:9b3d4731edbb | 26 | |
WFKnight | 0:9b3d4731edbb | 27 | namespace mbed { |
WFKnight | 0:9b3d4731edbb | 28 | |
WFKnight | 0:9b3d4731edbb | 29 | // timeout -1 forever, or milliseconds |
WFKnight | 0:9b3d4731edbb | 30 | int poll(pollfh fhs[], unsigned nfhs, int timeout) |
WFKnight | 0:9b3d4731edbb | 31 | { |
WFKnight | 0:9b3d4731edbb | 32 | /** |
WFKnight | 0:9b3d4731edbb | 33 | * TODO Proper wake-up mechanism. |
WFKnight | 0:9b3d4731edbb | 34 | * In order to correctly detect availability of read/write a FileHandle, we needed |
WFKnight | 0:9b3d4731edbb | 35 | * a select or poll mechanisms. We opted for poll as POSIX defines in |
WFKnight | 0:9b3d4731edbb | 36 | * http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html Currently, |
WFKnight | 0:9b3d4731edbb | 37 | * mbed::poll() just spins and scans filehandles looking for any events we are |
WFKnight | 0:9b3d4731edbb | 38 | * interested in. In future, his spinning behaviour will be replaced with |
WFKnight | 0:9b3d4731edbb | 39 | * condition variables. |
WFKnight | 0:9b3d4731edbb | 40 | */ |
WFKnight | 0:9b3d4731edbb | 41 | #if MBED_CONF_RTOS_PRESENT |
WFKnight | 0:9b3d4731edbb | 42 | uint64_t start_time = 0; |
WFKnight | 0:9b3d4731edbb | 43 | if (timeout > 0) { |
WFKnight | 0:9b3d4731edbb | 44 | start_time = Kernel::get_ms_count(); |
WFKnight | 0:9b3d4731edbb | 45 | } |
WFKnight | 0:9b3d4731edbb | 46 | #define TIME_ELAPSED() int64_t(Kernel::get_ms_count() - start_time) |
WFKnight | 0:9b3d4731edbb | 47 | #else |
WFKnight | 0:9b3d4731edbb | 48 | #if MBED_CONF_PLATFORM_POLL_USE_LOWPOWER_TIMER |
WFKnight | 0:9b3d4731edbb | 49 | LowPowerTimer timer; |
WFKnight | 0:9b3d4731edbb | 50 | #else |
WFKnight | 0:9b3d4731edbb | 51 | Timer timer; |
WFKnight | 0:9b3d4731edbb | 52 | #endif |
WFKnight | 0:9b3d4731edbb | 53 | if (timeout > 0) { |
WFKnight | 0:9b3d4731edbb | 54 | timer.start(); |
WFKnight | 0:9b3d4731edbb | 55 | } |
WFKnight | 0:9b3d4731edbb | 56 | #define TIME_ELAPSED() timer.read_ms() |
WFKnight | 0:9b3d4731edbb | 57 | #endif // MBED_CONF_RTOS_PRESENT |
WFKnight | 0:9b3d4731edbb | 58 | |
WFKnight | 0:9b3d4731edbb | 59 | int count = 0; |
WFKnight | 0:9b3d4731edbb | 60 | for (;;) { |
WFKnight | 0:9b3d4731edbb | 61 | /* Scan the file handles */ |
WFKnight | 0:9b3d4731edbb | 62 | for (unsigned n = 0; n < nfhs; n++) { |
WFKnight | 0:9b3d4731edbb | 63 | FileHandle *fh = fhs[n].fh; |
WFKnight | 0:9b3d4731edbb | 64 | short mask = fhs[n].events | POLLERR | POLLHUP | POLLNVAL; |
WFKnight | 0:9b3d4731edbb | 65 | if (fh) { |
WFKnight | 0:9b3d4731edbb | 66 | fhs[n].revents = fh->poll(mask) & mask; |
WFKnight | 0:9b3d4731edbb | 67 | } else { |
WFKnight | 0:9b3d4731edbb | 68 | fhs[n].revents = POLLNVAL; |
WFKnight | 0:9b3d4731edbb | 69 | } |
WFKnight | 0:9b3d4731edbb | 70 | if (fhs[n].revents) { |
WFKnight | 0:9b3d4731edbb | 71 | count++; |
WFKnight | 0:9b3d4731edbb | 72 | } |
WFKnight | 0:9b3d4731edbb | 73 | } |
WFKnight | 0:9b3d4731edbb | 74 | |
WFKnight | 0:9b3d4731edbb | 75 | if (count) { |
WFKnight | 0:9b3d4731edbb | 76 | break; |
WFKnight | 0:9b3d4731edbb | 77 | } |
WFKnight | 0:9b3d4731edbb | 78 | |
WFKnight | 0:9b3d4731edbb | 79 | /* Nothing selected - this is where timeout handling would be needed */ |
WFKnight | 0:9b3d4731edbb | 80 | if (timeout == 0 || (timeout > 0 && TIME_ELAPSED() > timeout)) { |
WFKnight | 0:9b3d4731edbb | 81 | break; |
WFKnight | 0:9b3d4731edbb | 82 | } |
WFKnight | 0:9b3d4731edbb | 83 | #ifdef MBED_CONF_RTOS_PRESENT |
WFKnight | 0:9b3d4731edbb | 84 | // TODO - proper blocking |
WFKnight | 0:9b3d4731edbb | 85 | // wait for condition variable, wait queue whatever here |
WFKnight | 0:9b3d4731edbb | 86 | rtos::Thread::wait(1); |
WFKnight | 0:9b3d4731edbb | 87 | #endif |
WFKnight | 0:9b3d4731edbb | 88 | } |
WFKnight | 0:9b3d4731edbb | 89 | return count; |
WFKnight | 0:9b3d4731edbb | 90 | } |
WFKnight | 0:9b3d4731edbb | 91 | |
WFKnight | 0:9b3d4731edbb | 92 | } // namespace mbed |