mbed library sources. Supersedes mbed-src.

Fork of mbed by teralytic

Committer:
mbed_official
Date:
Mon May 23 14:15:16 2016 +0100
Revision:
136:5728e9819171
Parent:
0:9b334a45a8ff
Child:
144:ef7eb2e8f9f7
Synchronized with git revision f74fc12313356f861665b8e8a11afcc972a543fc

Full URL: https://github.com/mbedmicro/mbed/commit/f74fc12313356f861665b8e8a11afcc972a543fc/

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bogdanm 0:9b334a45a8ff 1 /* mbed Microcontroller Library
bogdanm 0:9b334a45a8ff 2 * Copyright (c) 2015, STMicroelectronics
bogdanm 0:9b334a45a8ff 3 * All rights reserved.
bogdanm 0:9b334a45a8ff 4 *
bogdanm 0:9b334a45a8ff 5 * Redistribution and use in source and binary forms, with or without
bogdanm 0:9b334a45a8ff 6 * modification, are permitted provided that the following conditions are met:
bogdanm 0:9b334a45a8ff 7 *
bogdanm 0:9b334a45a8ff 8 * 1. Redistributions of source code must retain the above copyright notice,
bogdanm 0:9b334a45a8ff 9 * this list of conditions and the following disclaimer.
bogdanm 0:9b334a45a8ff 10 * 2. Redistributions in binary form must reproduce the above copyright notice,
bogdanm 0:9b334a45a8ff 11 * this list of conditions and the following disclaimer in the documentation
bogdanm 0:9b334a45a8ff 12 * and/or other materials provided with the distribution.
bogdanm 0:9b334a45a8ff 13 * 3. Neither the name of STMicroelectronics nor the names of its contributors
bogdanm 0:9b334a45a8ff 14 * may be used to endorse or promote products derived from this software
bogdanm 0:9b334a45a8ff 15 * without specific prior written permission.
bogdanm 0:9b334a45a8ff 16 *
bogdanm 0:9b334a45a8ff 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bogdanm 0:9b334a45a8ff 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bogdanm 0:9b334a45a8ff 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bogdanm 0:9b334a45a8ff 20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bogdanm 0:9b334a45a8ff 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bogdanm 0:9b334a45a8ff 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bogdanm 0:9b334a45a8ff 23 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bogdanm 0:9b334a45a8ff 24 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bogdanm 0:9b334a45a8ff 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bogdanm 0:9b334a45a8ff 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bogdanm 0:9b334a45a8ff 27 */
bogdanm 0:9b334a45a8ff 28 #include <stddef.h>
mbed_official 136:5728e9819171 29 #include <stdbool.h>
bogdanm 0:9b334a45a8ff 30 #include "us_ticker_api.h"
bogdanm 0:9b334a45a8ff 31 #include "PeripheralNames.h"
bogdanm 0:9b334a45a8ff 32
bogdanm 0:9b334a45a8ff 33 // Timer selection
bogdanm 0:9b334a45a8ff 34 #define TIM_MST TIM21
bogdanm 0:9b334a45a8ff 35
bogdanm 0:9b334a45a8ff 36 static TIM_HandleTypeDef TimMasterHandle;
bogdanm 0:9b334a45a8ff 37 static int us_ticker_inited = 0;
mbed_official 136:5728e9819171 38 static bool us_ticker_stabilized = false;
bogdanm 0:9b334a45a8ff 39
mbed_official 136:5728e9819171 40 volatile uint16_t SlaveCounter = 0;
bogdanm 0:9b334a45a8ff 41 volatile uint32_t oc_int_part = 0;
bogdanm 0:9b334a45a8ff 42 volatile uint16_t oc_rem_part = 0;
bogdanm 0:9b334a45a8ff 43
bogdanm 0:9b334a45a8ff 44 void set_compare(uint16_t count)
bogdanm 0:9b334a45a8ff 45 {
bogdanm 0:9b334a45a8ff 46 TimMasterHandle.Instance = TIM_MST;
bogdanm 0:9b334a45a8ff 47 // Set new output compare value
bogdanm 0:9b334a45a8ff 48 __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_1, count);
bogdanm 0:9b334a45a8ff 49 // Enable IT
bogdanm 0:9b334a45a8ff 50 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
bogdanm 0:9b334a45a8ff 51 }
bogdanm 0:9b334a45a8ff 52
bogdanm 0:9b334a45a8ff 53 void us_ticker_init(void)
bogdanm 0:9b334a45a8ff 54 {
bogdanm 0:9b334a45a8ff 55 if (us_ticker_inited) return;
bogdanm 0:9b334a45a8ff 56 us_ticker_inited = 1;
bogdanm 0:9b334a45a8ff 57
bogdanm 0:9b334a45a8ff 58 HAL_InitTick(0); // The passed value is not used
bogdanm 0:9b334a45a8ff 59 }
bogdanm 0:9b334a45a8ff 60
bogdanm 0:9b334a45a8ff 61 uint32_t us_ticker_read()
bogdanm 0:9b334a45a8ff 62 {
mbed_official 136:5728e9819171 63 volatile uint16_t cntH_old, cntH, cntL;
mbed_official 136:5728e9819171 64
bogdanm 0:9b334a45a8ff 65 if (!us_ticker_inited) us_ticker_init();
mbed_official 136:5728e9819171 66
mbed_official 136:5728e9819171 67 // There's a situation where the first tick still may overflow and to avoid
mbed_official 136:5728e9819171 68 // it we need to check if our ticker has stabilized and due to that we need
mbed_official 136:5728e9819171 69 // to return only the lower part of your 32 bit software timer.
mbed_official 136:5728e9819171 70 if (us_ticker_stabilized) {
mbed_official 136:5728e9819171 71 do {
mbed_official 136:5728e9819171 72 // For some reason on L0xx series we need to read and clear the
mbed_official 136:5728e9819171 73 // overflow flag which give extra time to propelry handle possible
mbed_official 136:5728e9819171 74 // hiccup after ~60s
mbed_official 136:5728e9819171 75 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF) == SET) {
mbed_official 136:5728e9819171 76 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF);
mbed_official 136:5728e9819171 77 }
mbed_official 136:5728e9819171 78 cntH_old = SlaveCounter;
mbed_official 136:5728e9819171 79 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
mbed_official 136:5728e9819171 80 cntH_old += 1;
mbed_official 136:5728e9819171 81 }
mbed_official 136:5728e9819171 82 cntL = TIM_MST->CNT;
mbed_official 136:5728e9819171 83
mbed_official 136:5728e9819171 84 cntH = SlaveCounter;
mbed_official 136:5728e9819171 85 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
mbed_official 136:5728e9819171 86 cntH += 1;
mbed_official 136:5728e9819171 87 }
mbed_official 136:5728e9819171 88 } while(cntH_old != cntH);
mbed_official 136:5728e9819171 89 } else {
mbed_official 136:5728e9819171 90 us_ticker_stabilized = true;
mbed_official 136:5728e9819171 91 return (uint32_t) TIM_MST->CNT;
mbed_official 136:5728e9819171 92 }
mbed_official 136:5728e9819171 93
mbed_official 136:5728e9819171 94 // Glue the upper and lower part together to get a 32 bit timer
mbed_official 136:5728e9819171 95 return (uint32_t)(cntH << 16 | cntL);
bogdanm 0:9b334a45a8ff 96 }
bogdanm 0:9b334a45a8ff 97
bogdanm 0:9b334a45a8ff 98 void us_ticker_set_interrupt(timestamp_t timestamp)
bogdanm 0:9b334a45a8ff 99 {
bogdanm 0:9b334a45a8ff 100 int delta = (int)((uint32_t)timestamp - us_ticker_read());
bogdanm 0:9b334a45a8ff 101 uint16_t cval = TIM_MST->CNT;
bogdanm 0:9b334a45a8ff 102
bogdanm 0:9b334a45a8ff 103 if (delta <= 0) { // This event was in the past
bogdanm 0:9b334a45a8ff 104 us_ticker_irq_handler();
bogdanm 0:9b334a45a8ff 105 } else {
bogdanm 0:9b334a45a8ff 106 oc_int_part = (uint32_t)(delta >> 16);
bogdanm 0:9b334a45a8ff 107 oc_rem_part = (uint16_t)(delta & 0xFFFF);
bogdanm 0:9b334a45a8ff 108 if (oc_rem_part <= (0xFFFF - cval)) {
bogdanm 0:9b334a45a8ff 109 set_compare(cval + oc_rem_part);
bogdanm 0:9b334a45a8ff 110 oc_rem_part = 0;
bogdanm 0:9b334a45a8ff 111 } else {
bogdanm 0:9b334a45a8ff 112 set_compare(0xFFFF);
bogdanm 0:9b334a45a8ff 113 oc_rem_part = oc_rem_part - (0xFFFF - cval);
bogdanm 0:9b334a45a8ff 114 }
bogdanm 0:9b334a45a8ff 115 }
bogdanm 0:9b334a45a8ff 116 }
bogdanm 0:9b334a45a8ff 117
bogdanm 0:9b334a45a8ff 118 void us_ticker_disable_interrupt(void)
bogdanm 0:9b334a45a8ff 119 {
bogdanm 0:9b334a45a8ff 120 TimMasterHandle.Instance = TIM_MST;
bogdanm 0:9b334a45a8ff 121 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
bogdanm 0:9b334a45a8ff 122 }
bogdanm 0:9b334a45a8ff 123
bogdanm 0:9b334a45a8ff 124 void us_ticker_clear_interrupt(void)
bogdanm 0:9b334a45a8ff 125 {
bogdanm 0:9b334a45a8ff 126 TimMasterHandle.Instance = TIM_MST;
bogdanm 0:9b334a45a8ff 127 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
bogdanm 0:9b334a45a8ff 128 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
bogdanm 0:9b334a45a8ff 129 }
bogdanm 0:9b334a45a8ff 130 }