Initial commit

Dependencies:   FastPWM

Committer:
lypinator
Date:
Wed Sep 16 01:11:49 2020 +0000
Revision:
0:bb348c97df44
Added PWM

Who changed what in which revision?

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