mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
mbed_official
Date:
Tue Mar 03 07:15:07 2015 +0000
Revision:
484:b5ae48b573d3
Parent:
481:ca51ab3eed5a
Child:
495:01cb89f68337
Synchronized with git revision 38514db08e270ed489f24230e15a248e23ca5a95

Full URL: https://github.com/mbedmicro/mbed/commit/38514db08e270ed489f24230e15a248e23ca5a95/

Fix issue #934: Handled condition when head is NULL.

Who changed what in which revision?

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