mbed library sources with internal temperature sensor for nucleo f401

Committer:
elessair
Date:
Sat Jan 17 18:03:58 2015 +0000
Revision:
0:7e2bd16f80af
nucleo f401re internal temperature added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:7e2bd16f80af 1 /* mbed Microcontroller Library
elessair 0:7e2bd16f80af 2 * Copyright (c) 2006-2013 ARM Limited
elessair 0:7e2bd16f80af 3 *
elessair 0:7e2bd16f80af 4 * Licensed under the Apache License, Version 2.0 (the "License");
elessair 0:7e2bd16f80af 5 * you may not use this file except in compliance with the License.
elessair 0:7e2bd16f80af 6 * You may obtain a copy of the License at
elessair 0:7e2bd16f80af 7 *
elessair 0:7e2bd16f80af 8 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:7e2bd16f80af 9 *
elessair 0:7e2bd16f80af 10 * Unless required by applicable law or agreed to in writing, software
elessair 0:7e2bd16f80af 11 * distributed under the License is distributed on an "AS IS" BASIS,
elessair 0:7e2bd16f80af 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:7e2bd16f80af 13 * See the License for the specific language governing permissions and
elessair 0:7e2bd16f80af 14 * limitations under the License.
elessair 0:7e2bd16f80af 15 */
elessair 0:7e2bd16f80af 16 #include <stddef.h>
elessair 0:7e2bd16f80af 17 #include "us_ticker_api.h"
elessair 0:7e2bd16f80af 18 #include "cmsis.h"
elessair 0:7e2bd16f80af 19
elessair 0:7e2bd16f80af 20 static ticker_event_handler event_handler;
elessair 0:7e2bd16f80af 21 static ticker_event_t *head = NULL;
elessair 0:7e2bd16f80af 22
elessair 0:7e2bd16f80af 23 void us_ticker_set_handler(ticker_event_handler handler) {
elessair 0:7e2bd16f80af 24 us_ticker_init();
elessair 0:7e2bd16f80af 25
elessair 0:7e2bd16f80af 26 event_handler = handler;
elessair 0:7e2bd16f80af 27 }
elessair 0:7e2bd16f80af 28
elessair 0:7e2bd16f80af 29 void us_ticker_irq_handler(void) {
elessair 0:7e2bd16f80af 30 us_ticker_clear_interrupt();
elessair 0:7e2bd16f80af 31
elessair 0:7e2bd16f80af 32 /* Go through all the pending TimerEvents */
elessair 0:7e2bd16f80af 33 while (1) {
elessair 0:7e2bd16f80af 34 if (head == NULL) {
elessair 0:7e2bd16f80af 35 // There are no more TimerEvents left, so disable matches.
elessair 0:7e2bd16f80af 36 us_ticker_disable_interrupt();
elessair 0:7e2bd16f80af 37 return;
elessair 0:7e2bd16f80af 38 }
elessair 0:7e2bd16f80af 39
elessair 0:7e2bd16f80af 40 if ((int)(head->timestamp - us_ticker_read()) <= 0) {
elessair 0:7e2bd16f80af 41 // This event was in the past:
elessair 0:7e2bd16f80af 42 // point to the following one and execute its handler
elessair 0:7e2bd16f80af 43 ticker_event_t *p = head;
elessair 0:7e2bd16f80af 44 head = head->next;
elessair 0:7e2bd16f80af 45 if (event_handler != NULL) {
elessair 0:7e2bd16f80af 46 event_handler(p->id); // NOTE: the handler can set new events
elessair 0:7e2bd16f80af 47 }
elessair 0:7e2bd16f80af 48 /* Note: We continue back to examining the head because calling the
elessair 0:7e2bd16f80af 49 * event handler may have altered the chain of pending events. */
elessair 0:7e2bd16f80af 50 } else {
elessair 0:7e2bd16f80af 51 // This event and the following ones in the list are in the future:
elessair 0:7e2bd16f80af 52 // set it as next interrupt and return
elessair 0:7e2bd16f80af 53 us_ticker_set_interrupt(head->timestamp);
elessair 0:7e2bd16f80af 54 return;
elessair 0:7e2bd16f80af 55 }
elessair 0:7e2bd16f80af 56 }
elessair 0:7e2bd16f80af 57 }
elessair 0:7e2bd16f80af 58
elessair 0:7e2bd16f80af 59 void us_ticker_insert_event(ticker_event_t *obj, timestamp_t timestamp, uint32_t id) {
elessair 0:7e2bd16f80af 60 /* disable interrupts for the duration of the function */
elessair 0:7e2bd16f80af 61 __disable_irq();
elessair 0:7e2bd16f80af 62
elessair 0:7e2bd16f80af 63 // initialise our data
elessair 0:7e2bd16f80af 64 obj->timestamp = timestamp;
elessair 0:7e2bd16f80af 65 obj->id = id;
elessair 0:7e2bd16f80af 66
elessair 0:7e2bd16f80af 67 /* Go through the list until we either reach the end, or find
elessair 0:7e2bd16f80af 68 an element this should come before (which is possibly the
elessair 0:7e2bd16f80af 69 head). */
elessair 0:7e2bd16f80af 70 ticker_event_t *prev = NULL, *p = head;
elessair 0:7e2bd16f80af 71 while (p != NULL) {
elessair 0:7e2bd16f80af 72 /* check if we come before p */
elessair 0:7e2bd16f80af 73 if ((int64_t)(timestamp - p->timestamp) < 0) {
elessair 0:7e2bd16f80af 74 break;
elessair 0:7e2bd16f80af 75 }
elessair 0:7e2bd16f80af 76 /* go to the next element */
elessair 0:7e2bd16f80af 77 prev = p;
elessair 0:7e2bd16f80af 78 p = p->next;
elessair 0:7e2bd16f80af 79 }
elessair 0:7e2bd16f80af 80 /* if prev is NULL we're at the head */
elessair 0:7e2bd16f80af 81 if (prev == NULL) {
elessair 0:7e2bd16f80af 82 head = obj;
elessair 0:7e2bd16f80af 83 us_ticker_set_interrupt(timestamp);
elessair 0:7e2bd16f80af 84 } else {
elessair 0:7e2bd16f80af 85 prev->next = obj;
elessair 0:7e2bd16f80af 86 }
elessair 0:7e2bd16f80af 87 /* if we're at the end p will be NULL, which is correct */
elessair 0:7e2bd16f80af 88 obj->next = p;
elessair 0:7e2bd16f80af 89
elessair 0:7e2bd16f80af 90 __enable_irq();
elessair 0:7e2bd16f80af 91 }
elessair 0:7e2bd16f80af 92
elessair 0:7e2bd16f80af 93 void us_ticker_remove_event(ticker_event_t *obj) {
elessair 0:7e2bd16f80af 94 __disable_irq();
elessair 0:7e2bd16f80af 95
elessair 0:7e2bd16f80af 96 // remove this object from the list
elessair 0:7e2bd16f80af 97 if (head == obj) {
elessair 0:7e2bd16f80af 98 // first in the list, so just drop me
elessair 0:7e2bd16f80af 99 head = obj->next;
elessair 0:7e2bd16f80af 100 if (head == NULL) {
elessair 0:7e2bd16f80af 101 us_ticker_disable_interrupt();
elessair 0:7e2bd16f80af 102 } else {
elessair 0:7e2bd16f80af 103 us_ticker_set_interrupt(head->timestamp);
elessair 0:7e2bd16f80af 104 }
elessair 0:7e2bd16f80af 105 } else {
elessair 0:7e2bd16f80af 106 // find the object before me, then drop me
elessair 0:7e2bd16f80af 107 ticker_event_t* p = head;
elessair 0:7e2bd16f80af 108 while (p != NULL) {
elessair 0:7e2bd16f80af 109 if (p->next == obj) {
elessair 0:7e2bd16f80af 110 p->next = obj->next;
elessair 0:7e2bd16f80af 111 break;
elessair 0:7e2bd16f80af 112 }
elessair 0:7e2bd16f80af 113 p = p->next;
elessair 0:7e2bd16f80af 114 }
elessair 0:7e2bd16f80af 115 }
elessair 0:7e2bd16f80af 116
elessair 0:7e2bd16f80af 117 __enable_irq();
elessair 0:7e2bd16f80af 118 }