Lee Shen / FTHR_USB_serial_qSPI
Committer:
boonshen
Date:
Tue Mar 13 21:12:00 2018 +0000
Revision:
0:a35c40f49345
MAX32630FTHR QuadSPI test

Who changed what in which revision?

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