mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Fri May 26 12:39:01 2017 +0100
Revision:
165:e614a9f1c9e2
Parent:
153:fa9ff456f731
Child:
167:e84263d55307
This updates the lib to the mbed lib v 143

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 153:fa9ff456f731 1 /* mbed Microcontroller Library
<> 153:fa9ff456f731 2 * Copyright (c) 2006-2016 ARM Limited
<> 153:fa9ff456f731 3 *
<> 153:fa9ff456f731 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 153:fa9ff456f731 5 * you may not use this file except in compliance with the License.
<> 153:fa9ff456f731 6 * You may obtain a copy of the License at
<> 153:fa9ff456f731 7 *
<> 153:fa9ff456f731 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 153:fa9ff456f731 9 *
<> 153:fa9ff456f731 10 * Unless required by applicable law or agreed to in writing, software
<> 153:fa9ff456f731 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 153:fa9ff456f731 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 153:fa9ff456f731 13 * See the License for the specific language governing permissions and
<> 153:fa9ff456f731 14 * limitations under the License.
<> 153:fa9ff456f731 15 */
<> 153:fa9ff456f731 16 #include <stddef.h>
<> 153:fa9ff456f731 17 #include "us_ticker_api.h"
<> 153:fa9ff456f731 18 #include "PeripheralNames.h"
<> 153:fa9ff456f731 19 #include "hal_tick.h"
<> 153:fa9ff456f731 20
<> 153:fa9ff456f731 21 // A 16-bit timer is used
<> 153:fa9ff456f731 22 #if TIM_MST_16BIT
<> 153:fa9ff456f731 23
<> 153:fa9ff456f731 24 TIM_HandleTypeDef TimMasterHandle;
<> 153:fa9ff456f731 25
<> 153:fa9ff456f731 26 volatile uint32_t SlaveCounter = 0;
<> 153:fa9ff456f731 27 volatile uint32_t oc_int_part = 0;
<> 153:fa9ff456f731 28 volatile uint16_t oc_rem_part = 0;
<> 153:fa9ff456f731 29 volatile uint8_t tim_it_update; // TIM_IT_UPDATE event flag set in timer_irq_handler()
<> 153:fa9ff456f731 30 volatile uint32_t tim_it_counter = 0; // Time stamp to be updated by timer_irq_handler()
<> 153:fa9ff456f731 31
<> 153:fa9ff456f731 32 static int us_ticker_inited = 0;
<> 153:fa9ff456f731 33
<> 153:fa9ff456f731 34 void set_compare(uint16_t count)
<> 153:fa9ff456f731 35 {
<> 153:fa9ff456f731 36 TimMasterHandle.Instance = TIM_MST;
<> 153:fa9ff456f731 37 // Set new output compare value
AnnaBridge 165:e614a9f1c9e2 38 __HAL_TIM_SET_COMPARE(&TimMasterHandle, TIM_CHANNEL_1, count);
<> 153:fa9ff456f731 39 // Enable IT
<> 153:fa9ff456f731 40 __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC1);
<> 153:fa9ff456f731 41 }
<> 153:fa9ff456f731 42
<> 153:fa9ff456f731 43 void us_ticker_init(void)
<> 153:fa9ff456f731 44 {
<> 153:fa9ff456f731 45 if (us_ticker_inited) return;
<> 153:fa9ff456f731 46 us_ticker_inited = 1;
<> 153:fa9ff456f731 47
<> 153:fa9ff456f731 48 TimMasterHandle.Instance = TIM_MST;
<> 153:fa9ff456f731 49
<> 153:fa9ff456f731 50 HAL_InitTick(0); // The passed value is not used
<> 153:fa9ff456f731 51 }
<> 153:fa9ff456f731 52
<> 153:fa9ff456f731 53 uint32_t us_ticker_read()
<> 153:fa9ff456f731 54 {
<> 153:fa9ff456f731 55 uint32_t counter;
<> 153:fa9ff456f731 56
<> 153:fa9ff456f731 57 TimMasterHandle.Instance = TIM_MST;
<> 153:fa9ff456f731 58
<> 153:fa9ff456f731 59 if (!us_ticker_inited) us_ticker_init();
<> 153:fa9ff456f731 60
<> 153:fa9ff456f731 61 #if defined(TARGET_STM32L0)
<> 153:fa9ff456f731 62 uint16_t cntH_old, cntH, cntL;
<> 153:fa9ff456f731 63 do {
<> 153:fa9ff456f731 64 // For some reason on L0xx series we need to read and clear the
<> 153:fa9ff456f731 65 // overflow flag which give extra time to propelry handle possible
<> 153:fa9ff456f731 66 // hiccup after ~60s
<> 153:fa9ff456f731 67 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF) == SET) {
<> 153:fa9ff456f731 68 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1OF);
<> 153:fa9ff456f731 69 }
<> 153:fa9ff456f731 70 cntH_old = SlaveCounter;
<> 153:fa9ff456f731 71 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
<> 153:fa9ff456f731 72 cntH_old += 1;
<> 153:fa9ff456f731 73 }
<> 153:fa9ff456f731 74 cntL = TIM_MST->CNT;
<> 153:fa9ff456f731 75 cntH = SlaveCounter;
<> 153:fa9ff456f731 76 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_UPDATE) == SET) {
<> 153:fa9ff456f731 77 cntH += 1;
<> 153:fa9ff456f731 78 }
<> 153:fa9ff456f731 79 } while(cntH_old != cntH);
<> 153:fa9ff456f731 80 // Glue the upper and lower part together to get a 32 bit timer
<> 153:fa9ff456f731 81 return (uint32_t)(cntH << 16 | cntL);
<> 153:fa9ff456f731 82 #else
<> 153:fa9ff456f731 83 tim_it_update = 0; // Clear TIM_IT_UPDATE event flag
<> 153:fa9ff456f731 84 counter = TIM_MST->CNT + (uint32_t)(SlaveCounter << 16); // Calculate new time stamp
<> 153:fa9ff456f731 85 if (tim_it_update == 1) {
<> 153:fa9ff456f731 86 return tim_it_counter; // In case of TIM_IT_UPDATE return the time stamp that was calculated in timer_irq_handler()
<> 153:fa9ff456f731 87 }
<> 153:fa9ff456f731 88 else {
<> 153:fa9ff456f731 89 return counter; // Otherwise return the time stamp calculated here
<> 153:fa9ff456f731 90 }
<> 153:fa9ff456f731 91 #endif
<> 153:fa9ff456f731 92 }
<> 153:fa9ff456f731 93
<> 153:fa9ff456f731 94 void us_ticker_set_interrupt(timestamp_t timestamp)
<> 153:fa9ff456f731 95 {
<> 153:fa9ff456f731 96 int delta = (int)((uint32_t)timestamp - us_ticker_read());
<> 153:fa9ff456f731 97
<> 153:fa9ff456f731 98 uint16_t cval = TIM_MST->CNT;
<> 153:fa9ff456f731 99
<> 153:fa9ff456f731 100 if (delta <= 0) { // This event was in the past
<> 153:fa9ff456f731 101 us_ticker_irq_handler();
<> 153:fa9ff456f731 102 } else {
<> 153:fa9ff456f731 103 oc_int_part = (uint32_t)(delta >> 16);
<> 153:fa9ff456f731 104 oc_rem_part = (uint16_t)(delta & 0xFFFF);
<> 153:fa9ff456f731 105 if (oc_rem_part <= (0xFFFF - cval)) {
<> 153:fa9ff456f731 106 set_compare(cval + oc_rem_part);
<> 153:fa9ff456f731 107 oc_rem_part = 0;
<> 153:fa9ff456f731 108 } else {
<> 153:fa9ff456f731 109 set_compare(0xFFFF);
<> 153:fa9ff456f731 110 oc_rem_part = oc_rem_part - (0xFFFF - cval);
<> 153:fa9ff456f731 111 }
<> 153:fa9ff456f731 112 }
<> 153:fa9ff456f731 113 }
<> 153:fa9ff456f731 114
<> 153:fa9ff456f731 115 void us_ticker_disable_interrupt(void)
<> 153:fa9ff456f731 116 {
<> 153:fa9ff456f731 117 TimMasterHandle.Instance = TIM_MST;
<> 153:fa9ff456f731 118 __HAL_TIM_DISABLE_IT(&TimMasterHandle, TIM_IT_CC1);
<> 153:fa9ff456f731 119 }
<> 153:fa9ff456f731 120
<> 153:fa9ff456f731 121 void us_ticker_clear_interrupt(void)
<> 153:fa9ff456f731 122 {
<> 153:fa9ff456f731 123 TimMasterHandle.Instance = TIM_MST;
<> 153:fa9ff456f731 124 if (__HAL_TIM_GET_FLAG(&TimMasterHandle, TIM_FLAG_CC1) == SET) {
<> 153:fa9ff456f731 125 __HAL_TIM_CLEAR_FLAG(&TimMasterHandle, TIM_FLAG_CC1);
<> 153:fa9ff456f731 126 }
<> 153:fa9ff456f731 127 }
<> 153:fa9ff456f731 128
<> 153:fa9ff456f731 129 #endif // TIM_MST_16BIT