Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
switches 0:5c4d7b2438d3 1 /*
switches 0:5c4d7b2438d3 2 * Implementation for Posix compliant platforms
switches 0:5c4d7b2438d3 3 *
switches 0:5c4d7b2438d3 4 * Copyright (c) 2016 Christopher Haster
switches 0:5c4d7b2438d3 5 *
switches 0:5c4d7b2438d3 6 * Licensed under the Apache License, Version 2.0 (the "License");
switches 0:5c4d7b2438d3 7 * you may not use this file except in compliance with the License.
switches 0:5c4d7b2438d3 8 * You may obtain a copy of the License at
switches 0:5c4d7b2438d3 9 *
switches 0:5c4d7b2438d3 10 * http://www.apache.org/licenses/LICENSE-2.0
switches 0:5c4d7b2438d3 11 *
switches 0:5c4d7b2438d3 12 * Unless required by applicable law or agreed to in writing, software
switches 0:5c4d7b2438d3 13 * distributed under the License is distributed on an "AS IS" BASIS,
switches 0:5c4d7b2438d3 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
switches 0:5c4d7b2438d3 15 * See the License for the specific language governing permissions and
switches 0:5c4d7b2438d3 16 * limitations under the License.
switches 0:5c4d7b2438d3 17 */
switches 0:5c4d7b2438d3 18 #include "equeue/equeue_platform.h"
switches 0:5c4d7b2438d3 19
switches 0:5c4d7b2438d3 20 #if defined(EQUEUE_PLATFORM_POSIX)
switches 0:5c4d7b2438d3 21
switches 0:5c4d7b2438d3 22 #include <time.h>
switches 0:5c4d7b2438d3 23 #include <sys/time.h>
switches 0:5c4d7b2438d3 24 #include <errno.h>
switches 0:5c4d7b2438d3 25
switches 0:5c4d7b2438d3 26
switches 0:5c4d7b2438d3 27 // Tick operations
switches 0:5c4d7b2438d3 28 unsigned equeue_tick(void) {
switches 0:5c4d7b2438d3 29 struct timeval tv;
switches 0:5c4d7b2438d3 30 gettimeofday(&tv, 0);
switches 0:5c4d7b2438d3 31 return (unsigned)(tv.tv_sec*1000 + tv.tv_usec/1000);
switches 0:5c4d7b2438d3 32 }
switches 0:5c4d7b2438d3 33
switches 0:5c4d7b2438d3 34
switches 0:5c4d7b2438d3 35 // Mutex operations
switches 0:5c4d7b2438d3 36 int equeue_mutex_create(equeue_mutex_t *m) {
switches 0:5c4d7b2438d3 37 return pthread_mutex_init(m, 0);
switches 0:5c4d7b2438d3 38 }
switches 0:5c4d7b2438d3 39
switches 0:5c4d7b2438d3 40 void equeue_mutex_destroy(equeue_mutex_t *m) {
switches 0:5c4d7b2438d3 41 pthread_mutex_destroy(m);
switches 0:5c4d7b2438d3 42 }
switches 0:5c4d7b2438d3 43
switches 0:5c4d7b2438d3 44 void equeue_mutex_lock(equeue_mutex_t *m) {
switches 0:5c4d7b2438d3 45 pthread_mutex_lock(m);
switches 0:5c4d7b2438d3 46 }
switches 0:5c4d7b2438d3 47
switches 0:5c4d7b2438d3 48 void equeue_mutex_unlock(equeue_mutex_t *m) {
switches 0:5c4d7b2438d3 49 pthread_mutex_unlock(m);
switches 0:5c4d7b2438d3 50 }
switches 0:5c4d7b2438d3 51
switches 0:5c4d7b2438d3 52
switches 0:5c4d7b2438d3 53 // Semaphore operations
switches 0:5c4d7b2438d3 54 int equeue_sema_create(equeue_sema_t *s) {
switches 0:5c4d7b2438d3 55 int err = pthread_mutex_init(&s->mutex, 0);
switches 0:5c4d7b2438d3 56 if (err) {
switches 0:5c4d7b2438d3 57 return err;
switches 0:5c4d7b2438d3 58 }
switches 0:5c4d7b2438d3 59
switches 0:5c4d7b2438d3 60 err = pthread_cond_init(&s->cond, 0);
switches 0:5c4d7b2438d3 61 if (err) {
switches 0:5c4d7b2438d3 62 return err;
switches 0:5c4d7b2438d3 63 }
switches 0:5c4d7b2438d3 64
switches 0:5c4d7b2438d3 65 s->signal = false;
switches 0:5c4d7b2438d3 66 return 0;
switches 0:5c4d7b2438d3 67 }
switches 0:5c4d7b2438d3 68
switches 0:5c4d7b2438d3 69 void equeue_sema_destroy(equeue_sema_t *s) {
switches 0:5c4d7b2438d3 70 pthread_cond_destroy(&s->cond);
switches 0:5c4d7b2438d3 71 pthread_mutex_destroy(&s->mutex);
switches 0:5c4d7b2438d3 72 }
switches 0:5c4d7b2438d3 73
switches 0:5c4d7b2438d3 74 void equeue_sema_signal(equeue_sema_t *s) {
switches 0:5c4d7b2438d3 75 pthread_mutex_lock(&s->mutex);
switches 0:5c4d7b2438d3 76 s->signal = true;
switches 0:5c4d7b2438d3 77 pthread_cond_signal(&s->cond);
switches 0:5c4d7b2438d3 78 pthread_mutex_unlock(&s->mutex);
switches 0:5c4d7b2438d3 79 }
switches 0:5c4d7b2438d3 80
switches 0:5c4d7b2438d3 81 bool equeue_sema_wait(equeue_sema_t *s, int ms) {
switches 0:5c4d7b2438d3 82 pthread_mutex_lock(&s->mutex);
switches 0:5c4d7b2438d3 83 if (!s->signal) {
switches 0:5c4d7b2438d3 84 if (ms < 0) {
switches 0:5c4d7b2438d3 85 pthread_cond_wait(&s->cond, &s->mutex);
switches 0:5c4d7b2438d3 86 } else {
switches 0:5c4d7b2438d3 87 struct timeval tv;
switches 0:5c4d7b2438d3 88 gettimeofday(&tv, 0);
switches 0:5c4d7b2438d3 89
switches 0:5c4d7b2438d3 90 struct timespec ts = {
switches 0:5c4d7b2438d3 91 .tv_sec = ms/1000 + tv.tv_sec,
switches 0:5c4d7b2438d3 92 .tv_nsec = ms*1000000 + tv.tv_usec*1000,
switches 0:5c4d7b2438d3 93 };
switches 0:5c4d7b2438d3 94
switches 0:5c4d7b2438d3 95 pthread_cond_timedwait(&s->cond, &s->mutex, &ts);
switches 0:5c4d7b2438d3 96 }
switches 0:5c4d7b2438d3 97 }
switches 0:5c4d7b2438d3 98
switches 0:5c4d7b2438d3 99 bool signal = s->signal;
switches 0:5c4d7b2438d3 100 s->signal = false;
switches 0:5c4d7b2438d3 101 pthread_mutex_unlock(&s->mutex);
switches 0:5c4d7b2438d3 102
switches 0:5c4d7b2438d3 103 return signal;
switches 0:5c4d7b2438d3 104 }
switches 0:5c4d7b2438d3 105
switches 0:5c4d7b2438d3 106 #endif